public void UsesCustomIdInUrls()
        {
            var person = Get.Person(id: "abc");
            var target = new ResourceSerializer(person, DefaultResource,
                GetUri(id: "abc"), DefaultPathBuilder, null);

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var job = result["data"]["relationships"]["job"]["links"];
            var friends = result["data"]["relationships"]["friends"]["links"];

            var self = result["links"]["self"].Value<Uri>().AbsolutePath;
            var jobSelf = job["self"].Value<Uri>().AbsolutePath;
            var jobRelated = job["related"].Value<Uri>().AbsolutePath;
            var friendsSelf = friends["self"].Value<Uri>().AbsolutePath;
            var friendsRelated = friends["related"].Value<Uri>().AbsolutePath;
            var included = result["included"][0]["links"]["self"].Value<Uri>().AbsolutePath;

            Assert.Equal("/api/people/abc", self);
            Assert.Equal("/api/people/abc/relationships/employer/", jobSelf);
            Assert.Equal("/api/people/abc/employer/", jobRelated);
            Assert.Equal("/api/people/abc/relationships/friends/", friendsSelf);
            Assert.Equal("/api/people/abc/friends/", friendsRelated);
            Assert.Equal("/api/corporations/456/", included);
        }
Ejemplo n.º 2
0
        public void AttributesSufficient()
        {
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                GetUri(id: "123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var attributes = result["data"]["attributes"];
            Assert.True(attributes["numberOfLegs"] == null);
            Assert.Equal(3, attributes.Count());
        }
        public void SelfLink()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var selfLink = result["links"].Value<Uri>("self").AbsolutePath;

            Assert.Equal("/api/people/123", selfLink);
        }
        public void NoSelfLinksInObject()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var links = result["data"]?["links"];

            Assert.Null(links);
        }
Ejemplo n.º 5
0
        public void AttributesComplete()
        {
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                GetUri(id: "123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var attributes = result["data"]["attributes"];
            Assert.Equal(DefaultObject.FirstName, attributes.Value<string>("first-name"));
            Assert.Equal(DefaultObject.LastName, attributes.Value<string>("last-name"));
            Assert.Equal(DefaultObject.Age, attributes.Value<int>("age"));
        }
Ejemplo n.º 6
0
        public PreprocessResult PreprocessContent(object @object, ApiResource resource, Uri requestUri)
        {
            var result = new PreprocessResult
            {
                JsonConverters = JsonConverters
            };
            if (requestUri == null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }

            try
            {
                var error = GetAsError(@object);
                if (error != null)
                {
                    result.ErrorContent = error;
                    return result;
                }

                var dataObject = @object;

                if (QueryContext?.Filtering != null)
                {
                    dataObject = Query.ApplyFiltering(dataObject, QueryContext.Filtering, resource);
                }

                if (QueryContext?.Sorting != null)
                {
                    dataObject = Query.ApplySorting(dataObject, QueryContext.Sorting, resource);
                }

                if (QueryContext?.Pagination != null)
                {
                    dataObject = Query.ApplyPagination(dataObject, QueryContext.Pagination, resource);
                }

                var serializer = new ResourceSerializer(
                    value: dataObject,
                    type: resource,
                    baseUrl: requestUri,
                    urlBuilder: UrlPathBuilder,
                    paginationContext: QueryContext?.Pagination);

                result.ResourceSerializer = serializer;
            }
            catch (Exception ex)
            {
                result.ErrorContent = GetAsError(ex);
            }

            return result;
        }
        public void UsesSpecifiedPropertyId()
        {
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                GetUri(id: "abc"), DefaultPathBuilder, null);

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(DefaultObject.Identifier, id);
        }
        public void UsesDefaultPropertyId()
        {
            var data = new PersonWithNoJob();
            var target = new ResourceSerializer(data, new PersonWithDefaultIdResource(), 
                GetUri(id: "123"), DefaultPathBuilder, null);

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(data.Id, id);
        }
Ejemplo n.º 9
0
        public void FirstLink()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
               GetUri(), DefaultPathBuilder,
                new PaginationContext(Enumerable.Empty<KeyValuePair<string, string>>(), perPage: 4));

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var nextLink = Uri.UnescapeDataString(result["links"].Value<Uri>("first").Query);
            Assert.Equal("?page[number]=0", nextLink);
        }
Ejemplo n.º 10
0
        public void BuildsRightLinks()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var job = result["data"]["relationships"]["job"];

            Assert.Equal("http://example.com/api/people/123/employer/",
                job["links"].Value<Uri>("related").ToString());
            Assert.Equal("http://example.com/api/people/123/relationships/employer/",
                job["links"].Value<Uri>("self").ToString());
        }
        public void SelfLinksInCollection()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                GetUri(), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            foreach (var elem in result["data"])
            {
                var links = elem["links"];
                Assert.NotNull(links);
                Assert.Equal("/api/people/" + elem.Value<string>("id") + "/", links.Value<Uri>("self").AbsolutePath);
            }
        }
        public ResourceDeserializerTests()
        {
            _person = Get.Person(id: "hello");
            _person.Friends = Get.People(1);

            _people = Get.People(5).ToArray();
            var singleSerializer = new ResourceSerializer(
            _person, new PersonResource(), new Uri("http://example.com/people/1"),
            new DefaultUrlPathBuilder(), null);
            var multiSerializer = new ResourceSerializer(
                _people, new PersonResource(), new Uri("http://example.com/people/"),
                new DefaultUrlPathBuilder(), null);

            _singleJson = JToken.Parse(singleSerializer.Serialize().ToString());
            _collectionJson = JToken.Parse(multiSerializer.Serialize().ToString());
        }
Ejemplo n.º 13
0
        public void HandlesQueryParams()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123", "a=b&c=d"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var jobLinks = result["data"]?["relationships"]?["job"]?["links"];

            var selfLink = result["links"].Value<Uri>("self")?.PathAndQuery;
            var jobSelfLink = jobLinks?.Value<Uri>("self")?.PathAndQuery;
            var jobRelationLink = jobLinks?.Value<Uri>("related")?.PathAndQuery;

            Assert.Equal("/api/people/123?a=b&c=d", selfLink);
            Assert.Equal("/api/people/123/relationships/employer/", jobSelfLink);
            Assert.Equal("/api/people/123/employer/", jobRelationLink);
        }
        public void HandlesRecursiveProperties()
        {
            var firstModel = new Recursion.FirstModel();
            var secondModel = new Recursion.SecondModel();
            firstModel.Model = secondModel;
            secondModel.Model = firstModel;

            var target = new ResourceSerializer(firstModel, new Recursion.Resource(), 
                GetUri(id: "123"), DefaultPathBuilder, null);

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(firstModel.Id, id);
        }
        public void UsesSpecifiedPropertyId()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = DefaultObject
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(DefaultObject.Identifier, id);
        }
        public void UsesDefaultPropertyId()
        {
            var data = new PersonWithNoJob();
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new PersonWithDefaultIdResource(),
                Result = data
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(data.Id, id);
        }
        public void HandlesRecursiveProperties()
        {
            var firstModel = new Recursion.FirstModel();
            var secondModel = new Recursion.SecondModel();
            firstModel.Model = secondModel;
            secondModel.Model = firstModel;

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new Recursion.Resource(),
                Result = firstModel
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(firstModel.Id, id);
        }
Ejemplo n.º 18
0
        public void UrlBuilder()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), new EmptyUrlBuilder(), null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var job = result["data"]["relationships"]["job"];

            Assert.Equal(null,
                job["links"]["related"]);
            Assert.Equal(null,
                job["links"]["self"]);
        }
Ejemplo n.º 19
0
        public void SerializesRelationshipLinks()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var relationships = result["data"]["relationships"];
            var job = relationships["job"];
            var friends = relationships["friends"];

            Assert.Equal("/api/people/123/employer/", job["links"].Value<Uri>("related").AbsolutePath);
            Assert.Equal("/api/people/123/relationships/employer/", job["links"].Value<Uri>("self").AbsolutePath);

            Assert.Equal("/api/people/123/friends/", friends["links"].Value<Uri>("related").AbsolutePath);
            Assert.Equal("/api/people/123/relationships/friends/", friends["links"].Value<Uri>("self").AbsolutePath);
        }
Ejemplo n.º 20
0
        public void SerializeDifferentBuilder()
        {
            var target = new ResourceSerializer(Get.Person(), new PersonResource(),
                GetUri("123"), new CanonicalUrlPathBuilder(), null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var relationships = result["data"]["relationships"];
            var job = relationships["job"];
            var friends = relationships["friends"];

            Assert.Equal("/corporations/456/", job["links"].Value<Uri>("related").AbsolutePath);
            Assert.Equal("/people/123/relationships/employer/", job["links"].Value<Uri>("self").AbsolutePath);

            Assert.Equal("/people/123/relationships/friends/", friends["links"].Value<Uri>("self").AbsolutePath);
            Assert.Null(friends["links"]["relationships"]);
        }
Ejemplo n.º 21
0
        public void PaginationQueryParams()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                GetUri(query: "q=a"), DefaultPathBuilder,
                new PaginationContext(GetQuery("q", "a"), perPage: 4));

            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            var nextLink = Uri.UnescapeDataString(result["links"].Value<Uri>("next").Query);
            Assert.Equal("?q=a&page[number]=1", nextLink);
        }
        public void SerializeOnlyWhatYouHave()
        {
            var company = new GuidAsId();

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new CompanyResource(),
                Result = company
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            Assert.Null(result["data"]["attributes"]["name"]);
            Assert.Null(result["data"]["attributes"]["location"]);
            Assert.Null(result["data"]["attributes"]["number-of-employees"]);
        }
        public void DocumentMustContainAtLeastOneDataOrErrorOrMeta()
        {
            var people = new Person[] { };

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = people
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.NotNull(result["data"]);
        }
        public void DocumentMustNotContainIncludedForEmptySet()
        {
            var people = new Person[0];

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = people
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.Null(result["included"]);
        }
Ejemplo n.º 25
0
        public void DocumentMustNotContainIncludedForEmptySet()
        {
            var people = new Person[0];
            var target = new ResourceSerializer(people, DefaultResource,
                GetUri(), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            Assert.Null(result["included"]);
        }
        public void HandlesNullResources()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = null
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.Equal(JTokenType.Null, result["data"].Type);
        }
Ejemplo n.º 27
0
        public void DocumentMustContainAtLeastOneDataOrErrorOrMeta()
        {
            var people = new Person[] { };
            var target = new ResourceSerializer(people, DefaultResource,
                GetUri(), DefaultPathBuilder, null);
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            Assert.NotNull(result["data"]);
        }
        public void SupportsGuidIds()
        {
            var guid = new GuidAsId();

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new PersonWithDefaultIdResource(),
                Result = guid
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            Assert.NotNull(result["data"]["id"]);
            Assert.Equal(guid.Id, Guid.Parse(result["data"]["id"].ToString()));
        }
Ejemplo n.º 29
0
        public void NextLink()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                GetUri(), DefaultPathBuilder,
                new PaginationContext(GetQuery(Constants.QueryNames.PageNumber, "2"), perPage: 10));
            var result = target.Serialize();
            _output.WriteLine(result.ToString());

            Assert.Equal(null, result["links"]["next"]);

            target = new ResourceSerializer(people, new PersonResource(),
                GetUri(), DefaultPathBuilder,
                new PaginationContext(GetQuery(Constants.QueryNames.PageNumber, "2"), perPage: 4));
            result = target.Serialize();

            var nextLink = Uri.UnescapeDataString(result["links"].Value<Uri>("next").Query);
            Assert.Equal("?page[number]=3", nextLink);
        }
        public void UsesCustomIdInUrls()
        {
            var person = Get.Person(id: "abc");
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var job = result["data"]["relationships"]["job"]["links"];
            var friends = result["data"]["relationships"]["friends"]["links"];

            var self = new Uri(result["links"]["self"].ToString()).AbsolutePath;
            var jobSelf = new Uri(job["self"].ToString()).AbsolutePath;
            var jobRelated = new Uri(job["related"].ToString()).AbsolutePath;
            var friendsSelf = new Uri(friends["self"].ToString()).AbsolutePath;
            var friendsRelated = new Uri(friends["related"].ToString()).AbsolutePath;
            var included = new Uri(result["included"][0]["links"]["self"].ToString()).AbsolutePath;

            Assert.Equal("/api/people/abc", self);
            Assert.Equal("/api/people/abc/relationships/employer/", jobSelf);
            Assert.Equal("/api/people/abc/employer/", jobRelated);
            Assert.Equal("/api/people/abc/relationships/friends/", friendsSelf);
            Assert.Equal("/api/people/abc/friends/", friendsRelated);
            Assert.Equal("/api/corporations/456/", included);
        }