public void ResourceWithRelationshipsToResourceObject_WithIncludedRelationshipsAttributes_CanBuild() { // Arrange var resource = new MultipleRelationshipsPrincipalPart { PopulatedToOne = new OneToOneDependent { Id = 10 }, PopulatedToManies = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; var relationships = _resourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>(tr => new { tr.PopulatedToManies, tr.PopulatedToOne, tr.EmptyToOne, tr.EmptyToManies }); // Act var resourceObject = _builder.Build(resource, relationships: relationships); // Assert Assert.Equal(4, resourceObject.Relationships.Count); Assert.Null(resourceObject.Relationships["emptyToOne"].Data); Assert.Empty((IList)resourceObject.Relationships["emptyToManies"].Data); var populatedToOneData = (ResourceIdentifierObject)resourceObject.Relationships["populatedToOne"].Data; Assert.NotNull(populatedToOneData); Assert.Equal("10", populatedToOneData.Id); Assert.Equal("oneToOneDependents", populatedToOneData.Type); var populatedToManiesData = (List <ResourceIdentifierObject>)resourceObject.Relationships["populatedToManies"].Data; Assert.Single(populatedToManiesData); Assert.Equal("20", populatedToManiesData.First().Id); Assert.Equal("oneToManyDependents", populatedToManiesData.First().Type); }
public void SerializeSingle_ResourceWithTargetedRelationships_CanBuild() { // Arrange var entityWithRelationships = new MultipleRelationshipsPrincipalPart { PopulatedToOne = new OneToOneDependent { Id = 10 }, PopulatedToManies = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; _serializer.RelationshipsToSerialize = _resourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>(tr => new { tr.EmptyToOne, tr.EmptyToManies, tr.PopulatedToOne, tr.PopulatedToManies }); // Act string serialized = _serializer.Serialize(entityWithRelationships); // Assert var expectedFormatted = @"{ ""data"":{ ""type"":""multiPrincipals"", ""attributes"":{ ""attributeMember"":null }, ""relationships"":{ ""emptyToOne"":{ ""data"":null }, ""emptyToManies"":{ ""data"":[ ] }, ""populatedToOne"":{ ""data"":{ ""type"":""oneToOneDependents"", ""id"":""10"" } }, ""populatedToManies"":{ ""data"":[ { ""type"":""oneToManyDependents"", ""id"":""20"" } ] } } } }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
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 ResourceWithRelationshipsToResourceObject_EmptyResource_CanBuild() { // Arrange var resource = new MultipleRelationshipsPrincipalPart(); // Act var resourceObject = _builder.Build(resource); // Assert Assert.Null(resourceObject.Attributes); Assert.Null(resourceObject.Relationships); Assert.Null(resourceObject.Id); Assert.Equal("multiPrincipals", resourceObject.Type); }
public void ResourceWithRelationshipsToResourceObject_ResourceWithId_CanBuild() { // Arrange var resource = new MultipleRelationshipsPrincipalPart { PopulatedToOne = new OneToOneDependent { Id = 10 }, }; // Act var resourceObject = _builder.Build(resource); // Assert Assert.Null(resourceObject.Attributes); Assert.Null(resourceObject.Relationships); Assert.Null(resourceObject.Id); Assert.Equal("multiPrincipals", resourceObject.Type); }
public void SerializeSingle_ResourceWithIncludedRelationships_CanSerialize() { // Arrange var resource = new MultipleRelationshipsPrincipalPart { Id = 1, PopulatedToOne = new OneToOneDependent { Id = 10 }, PopulatedToManies = new HashSet <OneToManyDependent> { new OneToManyDependent { Id = 20 } } }; var chain = _resourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>().Select(r => new List <RelationshipAttribute> { r }).ToList(); var serializer = GetResponseSerializer <MultipleRelationshipsPrincipalPart>(inclusionChains: chain); // Act string serialized = serializer.SerializeSingle(resource); // Assert var expectedFormatted = @"{ ""data"":{ ""type"":""multiPrincipals"", ""id"":""1"", ""attributes"":{ ""attributeMember"":null }, ""relationships"":{ ""populatedToOne"":{ ""data"":{ ""type"":""oneToOneDependents"", ""id"":""10"" } }, ""emptyToOne"": { ""data"":null }, ""populatedToManies"":{ ""data"":[ { ""type"":""oneToManyDependents"", ""id"":""20"" } ] }, ""emptyToManies"": { ""data"":[ ] }, ""multi"":{ ""data"":null } } }, ""included"":[ { ""type"":""oneToOneDependents"", ""id"":""10"", ""attributes"":{ ""attributeMember"":null } }, { ""type"":""oneToManyDependents"", ""id"":""20"", ""attributes"":{ ""attributeMember"":null } } ] }"; var expected = Regex.Replace(expectedFormatted, @"\s+", ""); Assert.Equal(expected, serialized); }
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); }