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);
        }
Exemple #2
0
        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);
        }