コード例 #1
0
        public void NoIncludedRelationshipData()
        {
            var includes = new IncludingContext();

            includes.DisableDefaultIncluded = true;
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, includes);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            var included = result["included"] as JArray;

            Assert.Null(included);
        }
コード例 #2
0
        public void AttributesComplete()
        {
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, 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"));
            Assert.Equal(DefaultObject.Address.StreetName, attributes["address"].Value <string>("street-name"));
            Assert.Equal(DefaultObject.Address.ZipCode, attributes["address"].Value <string>("zip-code"));
        }
コード例 #3
0
        public void SerializesRelationshipData()
        {
            var person = new PersonWithNoJob();
            var target = new ResourceSerializer(person, new PersonWithDefaultIdResource(),
                                                GetUri(id: "123"), DefaultPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            Assert.Null(job["data"]);

            Assert.NotNull(friends);
        }
コード例 #4
0
        public void UsesTitle()
        {
            var company = Get.Company();
            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("/corporations", "456").ToString()));

            _output.WriteLine(result.ToString());

            Assert.Equal("corporation", result["data"]["type"]);
        }
コード例 #5
0
        public void IncludesRelationshipData()
        {
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            var included = result["included"] as JArray;
            var job      = included?[0];

            Assert.Equal(2, included?.Count);
            Assert.Equal("http://example.com/api/corporations/456/", included?[0]?["links"].Value <Uri>("self").ToString());

            Assert.Equal(DefaultObject.Job.Id, job?["id"]);
            Assert.NotNull(job?["attributes"]);
        }
コード例 #6
0
        public void IncludedRelationshipIdentifierObjects()
        {
            var includes = new IncludeContext();

            includes.DisableDefaultIncluded = true;
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, includes, null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            Assert.NotNull(job["data"]);
        }
コード例 #7
0
        public void OmitRelationshipIdentifierObjectsWithoutProperty()
        {
            var includes = new IncludeContext();

            includes.DisableDefaultIncluded = true;
            var target = new ResourceSerializer(DefaultObject, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, includes, null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            var relationships = result["data"]["relationships"];
            var secretFriends = relationships["secret-friends"];

            Assert.Null(secretFriends["data"]);
        }
コード例 #8
0
        public void IncludedResourceRelationshipURLsAreCorrect()
        {
            var person = new Person(true)
            {
                Job = new CompanyWithCustomers(true)
            };

            var target = new ResourceSerializer(person, new PersonWithCompanyWithCustomersResource(),
                                                GetUri(id: "123"), DefaultPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            var included = result["included"] as JArray;

            Assert.Equal("http://example.com/api/corporations/456/relationships/customers/", included?[0]?["relationships"]?["customers"]?["links"]?.Value <Uri>("self")?.ToString());
        }
コード例 #9
0
        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"]);
        }
コード例 #10
0
        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"]);
        }
コード例 #11
0
        public void HandlesQueryParamsCollection()
        {
            var target = new ResourceSerializer(Get.People(1), new PersonResource(),
                                                GetUri(null, "a=b&c=d"), DefaultPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

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

            Assert.EndsWith("/api/people?a=b&c=d", selfLink);
            Assert.Equal("/api/people/0/relationships/employer/", jobSelfLink);
            Assert.Equal("/api/people/0/employer/", jobRelationLink);
        }
コード例 #12
0
        public void SerializeTest()
        {
            using (var s = GetType().Assembly.GetManifestResourceStream(GetType(), "version"))
            {
                var m = new MemoryStream();
                s.CopyTo(m);
                m.Seek(0, SeekOrigin.Begin);

                var i = ResourceSerializer.Deserialize <VS_VERSIONINFO>(m);

                var m2 = new MemoryStream();
                ResourceSerializer.Serialize(i, m2);

                var b  = m.ToArray();
                var b2 = m2.ToArray();
                Assert.IsTrue(b.SequenceEqual(b2));
            }
        }
コード例 #13
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"]);
        }
コード例 #14
0
        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);
        }
コード例 #15
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);
        }
コード例 #16
0
        public void HandlesRecursiveProperties()
        {
            var firstModel  = new Recursion.FirstModel();
            var secondModel = new Recursion.SecondModel();
            var thirdModel  = new Recursion.ThirdModel();
            var fourthModel = new Recursion.FourthModel();

            firstModel.Child   = secondModel;
            secondModel.Parent = firstModel;
            secondModel.Child  = thirdModel;
            thirdModel.Parent  = secondModel;
            thirdModel.Child   = fourthModel;
            fourthModel.Parent = thirdModel;

            var target = new ResourceSerializer(firstModel, new Recursion.FirstModelResource(),
                                                GetUri(id: firstModel.Id), DefaultPathBuilder, null, null, null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());


            var included = result["included"] as JArray;

            Assert.NotNull(included);


            var secondOutput = included
                               .Where(t => t["type"].Value <string>() == "second-model").FirstOrDefault();

            Assert.NotNull(secondOutput);


            var parentReference = secondOutput["relationships"]?["parent"]?["data"]?["type"];

            Assert.NotNull(parentReference);
            Assert.Equal(parentReference.Value <string>(), "first-model");


            var childReference = secondOutput["relationships"]?["child"]?["data"]?["type"];

            Assert.NotNull(childReference);
            Assert.Equal(childReference.Value <string>(), "third-model");
        }
コード例 #17
0
        public void AttributesSufficient()
        {
            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: "123").ToString()));

            _output.WriteLine(result.ToString());

            var attributes = result["data"]["attributes"];

            Assert.True(attributes["numberOfLegs"] == null);
            Assert.Equal(3, attributes.Count());
        }
コード例 #18
0
        public void SerializesRelationshipDataAsNull()
        {
            var person = Get.Person(id: "123");

            person.Job = null;
            var target = new ResourceSerializer(person, DefaultResource,
                                                GetUri(id: "123"), DefaultPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            Assert.Equal(JTokenType.Null, job["data"].Type);

            Assert.NotNull(friends);
        }
コード例 #19
0
        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()));
        }
コード例 #20
0
        public void HandlesNullValues()
        {
            var person = new Person(id: "45");
            var target = new ResourceSerializer(person, DefaultResource,
                                                GetUri(id: "45"), DefaultPathBuilder, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            Assert.NotNull(attributes["first-name"]);
            Assert.NotNull(attributes["last-name"]);
            Assert.NotNull(attributes["age"]);

            Assert.Null(relationships["job"]["data"]);
            Assert.Null(relationships["friends"]["data"]);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        public void OnlySerializesAttributesInTheResource()
        {
            var personMock = new Mock <LazyPerson>();

            personMock.SetupGet(p => p.Identifier).Returns("123");

            var target = new ResourceSerializer(personMock.Object, DefaultResource,
                                                GetUri(id: "1"), DefaultPathBuilder, null, null, null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            personMock.VerifyGet(p => p.Identifier);
            Assert.Throws <MockException>(() =>
            {
                personMock.VerifyGet(p => p.NumberOfLegs);
            });
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        public void EmptyForNull()
        {
            var company = Get.Company();

            var target = new ResourceSerializer(
                company,
                new CompanyResource(),
                new Uri("http://localhost/people/123"),
                new DefaultUrlPathBuilder(),
                null,
                null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            JToken meta;

            Assert.False(result.TryGetValue("meta", out meta), "Meta hash is null");
        }
コード例 #25
0
        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"]);
        }
コード例 #26
0
        public void AttributesComplete()
        {
            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: "123").ToString()));

            _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"));
        }
コード例 #27
0
        public void SelfLinksInCollectionCustomRoute()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                                                new Uri("http://localhost:80/api/location/123/people"), CustomPathBuilder, null, null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            foreach (var elem in result["data"])
            {
                var links = elem["links"];
                Assert.NotNull(links);
                Assert.Equal("/api/location/123/people/" + elem.Value <string>("id") + "/", links.Value <Uri>("self").AbsolutePath);
            }

            Assert.EndsWith("/api/location/123/people", selfLink);
        }
コード例 #28
0
        public void NextLink()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                                                GetUri(), DefaultPathBuilder,
                                                new PaginationContext(GetQuery(Constants.PageNumberQueryName, "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.PageNumberQueryName, "2"), perPage: 4));
            result = target.Serialize();

            var nextLink = Uri.UnescapeDataString(result["links"].Value <Uri>("next").Query);

            Assert.Equal("?page[number]=3", nextLink);
        }
コード例 #29
0
        public void PreviousLinkWithShiftedFirstPage()
        {
            var people = Get.People(5);
            var target = new ResourceSerializer(people, new PersonResource(),
                                                GetUri(), DefaultPathBuilder,
                                                new PaginationContext(GetQuery(Constants.QueryNames.PageNumber, "1"), pageSizeDefault: 10, pageSizeLimit: null, firstPageNumber: 1), null, null);
            var result = target.Serialize();

            _output.WriteLine(result.ToString());

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

            target = new ResourceSerializer(people, new PersonResource(),
                                            GetUri(), DefaultPathBuilder,
                                            new PaginationContext(GetQuery(Constants.QueryNames.PageNumber, "2"), pageSizeDefault: 10, pageSizeLimit: null, firstPageNumber: 1), null, null);
            result = target.Serialize();

            var nextLink = Uri.UnescapeDataString(result["links"].Value <Uri>("prev").Query);

            Assert.Equal("?page[number]=1", nextLink);
        }
コード例 #30
0
        public void SerializesDynamics()
        {
            dynamic person = new ExpandoObject();

            person.Identifier   = 1;
            person.FirstName    = "John";
            person.LastName     = "Smith";
            person.Age          = 34;
            person.NumberOfLegs = 4;

            var target = new ResourceSerializer(person, DefaultResource,
                                                GetUri(id: "1"), DefaultPathBuilder, null, null, null);

            var result = target.Serialize();

            _output.WriteLine(result.ToString());

            Assert.Equal(result["data"]["id"], "1");
            Assert.Equal(result["data"]["attributes"]["first-name"], "John");
            Assert.Equal(result["data"]["attributes"]["age"], 34);
        }
コード例 #31
0
 public override bool SerializeChildren(ResourceSerializer serializer) => serializer.Serialize(this, ref Children, ref wLength, typeof(VarFileInfo), typeof(StringFileInfo));
コード例 #32
0
 public override bool SerializeChildren(ResourceSerializer serializer)
 {
     return serializer.Serialize(this, ref Children, ref wLength, typeof(StringTable));
 }