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); }
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")); }
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); }
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"]); }
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"]); }
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"]); }
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"]); }
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()); }
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"]); }
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 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); }
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)); } }
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"]); }
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 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); }
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"); }
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()); }
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); }
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())); }
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"]); }
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 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); }); }
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 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"); }
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 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")); }
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); }
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); }
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); }
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); }
public override bool SerializeChildren(ResourceSerializer serializer) => serializer.Serialize(this, ref Children, ref wLength, typeof(VarFileInfo), typeof(StringFileInfo));
public override bool SerializeChildren(ResourceSerializer serializer) { return serializer.Serialize(this, ref Children, ref wLength, typeof(StringTable)); }