public void SerializeSingle_NullWithLinksAndMeta_StillShowsLinksAndMeta() { // Arrange var meta = new Dictionary <string, object> { { "test", "meta" } }; OneToManyPrincipal entity = null; var serializer = GetResponseSerializer <OneToManyPrincipal>(metaDict: meta, topLinks: _dummyToplevelLinks, relationshipLinks: _dummyRelationshipLinks, resourceLinks: _dummyResourceLinks); // Act string serialized = serializer.SerializeSingle(entity); // Assert var expectedFormatted = @"{ ""meta"":{ ""test"": ""meta"" }, ""links"":{ ""self"":""http://www.dummy.com/dummy-self-link"", ""next"":""http://www.dummy.com/dummy-next-link"", ""prev"":""http://www.dummy.com/dummy-prev-link"", ""first"":""http://www.dummy.com/dummy-first-link"", ""last"":""http://www.dummy.com/dummy-last-link"" }, ""data"": null }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void SerializeSingle_ResourceWithMeta_IncludesMetaInResult() { // Arrange var meta = new Dictionary <string, object> { { "test", "meta" } }; var resource = new OneToManyPrincipal { Id = 10 }; var serializer = GetResponseSerializer <OneToManyPrincipal>(metaDict: meta); // Act string serialized = serializer.SerializeSingle(resource); // Assert var expectedFormatted = @"{ ""meta"":{ ""test"": ""meta"" }, ""data"":{ ""type"":""oneToManyPrincipals"", ""id"":""10"", ""attributes"":{ ""attributeMember"":null } } }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void SerializeSingleWithRequestRelationship_PopulatedToManyRelationship_CanSerialize() { // Arrange var resource = new OneToManyPrincipal { Id = 2, Dependents = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 1 } } }; var serializer = GetResponseSerializer <OneToManyPrincipal>(); var requestRelationship = _resourceGraph.GetRelationships((OneToManyPrincipal t) => t.Dependents).First(); serializer.RequestRelationship = requestRelationship; // Act string serialized = serializer.SerializeSingle(resource); // Assert var expectedFormatted = @"{ ""data"":[{ ""type"":""oneToManyDependents"", ""id"":""1"" }] }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void Build_RelationshipIncludedAndLinksEnabled_RelationshipEntryWithDataAndLinks() { // Arrange var resource = new OneToManyPrincipal { Id = 10, Dependents = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; ResponseResourceObjectBuilder builder = GetResponseResourceObjectBuilder(_relationshipsForBuild.AsEnumerable(), relationshipLinks: DummyRelationshipLinks); // Act ResourceObject resourceObject = builder.Build(resource, relationships: _relationshipsForBuild); // Assert Assert.True(resourceObject.Relationships.TryGetValue(RelationshipName, out RelationshipEntry entry)); Assert.Equal("http://www.dummy.com/dummy-relationship-self-link", entry.Links.Self); Assert.Equal("http://www.dummy.com/dummy-relationship-related-link", entry.Links.Related); Assert.True(entry.IsPopulated); Assert.Equal("20", entry.ManyData.Single().Id); }
public void DeserializeSingle_NestedIncluded_CanDeserialize() { // Arrange Document content = CreateDocumentWithRelationships("multiPrincipals"); content.SingleData.Relationships.Add("populatedToManies", CreateRelationshipData("oneToManyDependents", true)); const string toManyAttributeValue = "populatedToManies member content"; const string nestedIncludeAttributeValue = "nested include member content"; content.Included = new List <ResourceObject> { new ResourceObject { Type = "oneToManyDependents", Id = "10", Attributes = new Dictionary <string, object> { ["attributeMember"] = toManyAttributeValue }, Relationships = new Dictionary <string, RelationshipEntry> { ["principal"] = CreateRelationshipData("oneToManyPrincipals") } }, new ResourceObject { Type = "oneToManyPrincipals", Id = "10", Attributes = new Dictionary <string, object> { ["attributeMember"] = nestedIncludeAttributeValue } } }; string body = JsonConvert.SerializeObject(content); // Act SingleResponse <MultipleRelationshipsPrincipalPart> result = _deserializer.DeserializeSingle <MultipleRelationshipsPrincipalPart>(body); MultipleRelationshipsPrincipalPart resource = result.Data; // Assert Assert.Equal(1, resource.Id); Assert.Null(resource.PopulatedToOne); Assert.Null(resource.EmptyToManies); Assert.Null(resource.EmptyToOne); Assert.NotNull(resource.PopulatedToManies); OneToManyDependent includedResource = resource.PopulatedToManies.First(); Assert.Equal(toManyAttributeValue, includedResource.AttributeMember); OneToManyPrincipal nestedIncludedResource = includedResource.Principal; Assert.Equal(nestedIncludeAttributeValue, nestedIncludedResource.AttributeMember); }
public void Build_RelationshipNotIncludedAndLinksDisabled_NoRelationshipObject() { // Arrange var entity = new OneToManyPrincipal { Id = 10 }; var builder = GetResponseResourceObjectBuilder(); // Act var resourceObject = builder.Build(entity, relationships: _relationshipsForBuild); // Assert Assert.Null(resourceObject.Relationships); }
public void SerializeSingle_ResourceWithLinksEnabled_CanSerialize() { // Arrange var resource = new OneToManyPrincipal { Id = 10 }; ResponseSerializer <OneToManyPrincipal> serializer = GetResponseSerializer <OneToManyPrincipal>(topLinks: DummyTopLevelLinks, relationshipLinks: DummyRelationshipLinks, resourceLinks: DummyResourceLinks); // Act string serialized = serializer.SerializeSingle(resource); // Assert const string expectedFormatted = @"{ ""links"":{ ""self"":""http://www.dummy.com/dummy-self-link"", ""first"":""http://www.dummy.com/dummy-first-link"", ""last"":""http://www.dummy.com/dummy-last-link"", ""prev"":""http://www.dummy.com/dummy-prev-link"", ""next"":""http://www.dummy.com/dummy-next-link"" }, ""data"":{ ""type"":""oneToManyPrincipals"", ""id"":""10"", ""attributes"":{ ""attributeMember"":null }, ""relationships"":{ ""dependents"":{ ""links"":{ ""self"":""http://www.dummy.com/dummy-relationship-self-link"", ""related"":""http://www.dummy.com/dummy-relationship-related-link"" } } }, ""links"":{ ""self"":""http://www.dummy.com/dummy-resource-self-link"" } } }"; string expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void Build_RelationshipNotIncludedAndLinksEnabled_RelationshipEntryWithLinks() { // Arrange var entity = new OneToManyPrincipal { Id = 10 }; var builder = GetResponseResourceObjectBuilder(relationshipLinks: _dummyRelationshipLinks); // Act var resourceObject = builder.Build(entity, relationships: _relationshipsForBuild); // Assert Assert.True(resourceObject.Relationships.TryGetValue(_relationshipName, out var entry)); Assert.Equal("http://www.dummy.com/dummy-relationship-self-link", entry.Links.Self); Assert.Equal("http://www.dummy.com/dummy-relationship-related-link", entry.Links.Related); Assert.False(entry.IsPopulated); }
public void SerializeSingleWithRequestRelationship_EmptyToManyRelationship_CanSerialize() { // Arrange var entity = new OneToManyPrincipal { Id = 2, Dependents = new List <OneToManyDependent>() }; var serializer = GetResponseSerializer <OneToManyPrincipal>(); var requestRelationship = _resourceGraph.GetRelationships((OneToManyPrincipal t) => t.Dependents).First(); serializer.RequestRelationship = requestRelationship; // Act string serialized = serializer.SerializeSingle(entity); // Assert var expectedFormatted = @"{ ""data"": [] }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void Build_RelationshipIncludedAndLinksDisabled_RelationshipEntryWithData() { // Arrange var resource = new OneToManyPrincipal { Id = 10, Dependents = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; var builder = GetResponseResourceObjectBuilder(inclusionChains: _relationshipsForBuild.AsEnumerable()); // Act var resourceObject = builder.Build(resource, relationships: _relationshipsForBuild); // Assert Assert.True(resourceObject.Relationships.TryGetValue(RelationshipName, out var entry)); Assert.Null(entry.Links); Assert.True(entry.IsPopulated); Assert.Equal("20", entry.ManyData.Single().Id); }
public void Build_RelationshipIncludedAndLinksEnabled_RelationshipEntryWithDataAndLinks() { // Arrange var entity = new OneToManyPrincipal { Id = 10, Dependents = new List <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; var builder = GetResponseResourceObjectBuilder(inclusionChains: new List <List <RelationshipAttribute> > { _relationshipsForBuild }, relationshipLinks: _dummyRelationshipLinks); // Act var resourceObject = builder.Build(entity, relationships: _relationshipsForBuild); // Assert Assert.True(resourceObject.Relationships.TryGetValue(_relationshipName, out var entry)); Assert.Equal("http://www.dummy.com/dummy-relationship-self-link", entry.Links.Self); Assert.Equal("http://www.dummy.com/dummy-relationship-related-link", entry.Links.Related); Assert.True(entry.IsPopulated); Assert.Equal("20", entry.ManyData.Single().Id); }
public void SerializeSingle_ResourceWithDeeplyIncludedRelationships_CanSerialize() { // Arrange var deeplyIncludedResource = new OneToManyPrincipal { Id = 30, AttributeMember = "deep" }; var includedResource = new OneToManyDependent { Id = 20, Principal = deeplyIncludedResource }; var resource = new MultipleRelationshipsPrincipalPart { Id = 10, PopulatedToManies = new HashSet <OneToManyDependent> { includedResource } }; var chains = _resourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>() .Select(r => { var chain = new List <RelationshipAttribute> { r }; if (r.PublicName != "populatedToManies") { return new List <RelationshipAttribute> { r } } ; chain.AddRange(_resourceGraph.GetRelationships <OneToManyDependent>()); return(chain); }).ToList(); var serializer = GetResponseSerializer <MultipleRelationshipsPrincipalPart>(inclusionChains: chains); // Act string serialized = serializer.SerializeSingle(resource); // Assert var expectedFormatted = @"{ ""data"":{ ""type"":""multiPrincipals"", ""id"":""10"", ""attributes"":{ ""attributeMember"":null }, ""relationships"":{ ""populatedToOne"":{ ""data"":null }, ""emptyToOne"":{ ""data"":null }, ""populatedToManies"":{ ""data"":[ { ""type"":""oneToManyDependents"", ""id"":""20"" } ] }, ""emptyToManies"":{ ""data"":[] }, ""multi"":{ ""data"":null } } }, ""included"":[ { ""type"":""oneToManyDependents"", ""id"":""20"", ""attributes"":{ ""attributeMember"":null }, ""relationships"":{ ""principal"":{ ""data"":{ ""type"":""oneToManyPrincipals"", ""id"":""30"" } } } }, { ""type"":""oneToManyPrincipals"", ""id"":""30"", ""attributes"":{ ""attributeMember"":""deep"" } } ] }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
public void DeserializeList_DeeplyNestedIncluded_CanDeserialize() { // Arrange var content = new Document { Data = new List <ResourceObject> { CreateDocumentWithRelationships("multiPrincipals").SingleData } }; content.ManyData[0].Relationships.Add("multi", CreateRelationshipData("multiPrincipals")); const string includedAttributeValue = "multi member content"; const string nestedIncludedAttributeValue = "nested include member content"; const string deeplyNestedIncludedAttributeValue = "deeply nested member content"; content.Included = new List <ResourceObject> { new ResourceObject { Type = "multiPrincipals", Id = "10", Attributes = new Dictionary <string, object> { ["attributeMember"] = includedAttributeValue }, Relationships = new Dictionary <string, RelationshipEntry> { ["populatedToManies"] = CreateRelationshipData("oneToManyDependents", true) } }, new ResourceObject { Type = "oneToManyDependents", Id = "10", Attributes = new Dictionary <string, object> { ["attributeMember"] = nestedIncludedAttributeValue }, Relationships = new Dictionary <string, RelationshipEntry> { ["principal"] = CreateRelationshipData("oneToManyPrincipals") } }, new ResourceObject { Type = "oneToManyPrincipals", Id = "10", Attributes = new Dictionary <string, object> { ["attributeMember"] = deeplyNestedIncludedAttributeValue } } }; string body = JsonConvert.SerializeObject(content); // Act ManyResponse <MultipleRelationshipsPrincipalPart> result = _deserializer.DeserializeMany <MultipleRelationshipsPrincipalPart>(body); MultipleRelationshipsPrincipalPart resource = result.Data.First(); // Assert Assert.Equal(1, resource.Id); MultipleRelationshipsPrincipalPart included = resource.Multi; Assert.Equal(10, included.Id); Assert.Equal(includedAttributeValue, included.AttributeMember); OneToManyDependent nestedIncluded = included.PopulatedToManies.First(); Assert.Equal(10, nestedIncluded.Id); Assert.Equal(nestedIncludedAttributeValue, nestedIncluded.AttributeMember); OneToManyPrincipal deeplyNestedIncluded = nestedIncluded.Principal; Assert.Equal(10, deeplyNestedIncluded.Id); Assert.Equal(deeplyNestedIncludedAttributeValue, deeplyNestedIncluded.AttributeMember); }