public void WithComplexObjectTest() { //Arrange const int authorId = 5; const string authorName = "Valentin"; const int postId = 6; const string postTitle = "The measure of a man"; const string commentBody = "Comment body"; const int commentId = 7; var author = new Author() { Id = authorId, Name = authorName }; var post = new Post() { Id = postId, Title = postTitle, Author = author }; var comment = new Comment() { Id = commentId, Body = commentBody, Post = post }; post.Replies = new List<Comment>() { comment }; author.Posts = new List<Post>() { post }; var configurationBuilder = new ConfigurationBuilder(); //Act var resourceConfigurationForPost = configurationBuilder .Resource<Post>() .WithSimpleProperty(p => p.Title) .WithIdSelector(p => p.Id) .WithLinkedResource(p => p.Replies); var resourceConfigurationForAuthor = configurationBuilder .Resource<Author>() .WithSimpleProperty(a => a.Name) .WithIdSelector(a => a.Id) .WithLinkedResource(a => a.Posts); var resourceConfigurationForComment = configurationBuilder .Resource<Comment>() .WithIdSelector(c => c.Id) .WithSimpleProperty(c => c.Body); var result = configurationBuilder.Build(); //Assert resourceConfigurationForPost.ConstructedMetadata.Relationships.Count.ShouldEqual(1); resourceConfigurationForAuthor.ConstructedMetadata.Relationships.Count.ShouldEqual(1); configurationBuilder.ResourceConfigurationsByType.All( r => r.Value.ConstructedMetadata.Relationships.All(l => l.ResourceMapping != null)); var authorLinks = configurationBuilder.ResourceConfigurationsByType[ resourceConfigurationForAuthor.ConstructedMetadata.ResourceRepresentationType].ConstructedMetadata.Relationships; authorLinks.ShouldNotBeNull().Count.ShouldEqual(1); authorLinks[0].RelationshipName.ShouldEqual("posts"); authorLinks[0].ResourceMapping.PropertyGetters.ShouldNotBeNull().Count.ShouldEqual(1); authorLinks[0].ResourceMapping.Relationships .ForEach(p => p.ResourceMapping.Relationships .ForEach(c => c .RelationshipName .ShouldEqual(resourceConfigurationForComment.ConstructedMetadata.ResourceType))); }
public Author() { Posts = new List<Post>(); Comments = new Comment[1]; }