public void TestWithIdSelector()
 {
     //Arrange
     var resourceConfigurationForAuthor = configurationBuilder.Resource<Author>();
     const int authorId = 5;
     var author = new Author() { Id = authorId };
     //Act
     resourceConfigurationForAuthor.WithIdSelector(a => a.Id);
     //Assert
     var result = (int)resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author);
     result.ShouldEqual(authorId);
 }
 public void TestWithIdSelectorForMultipleTypes()
 {
     //Arrange
     var resourceConfigurationForAuthor = configurationBuilder.Resource<Author>();
     var resourceConfigurationForPost = configurationBuilder.Resource<Post>();
     const int authorId = 5;
     const int postId = 6;
     var author = new Author() { Id = authorId };
     var post = new Post() { Id = postId };
     //Act
     resourceConfigurationForAuthor.WithIdSelector(a => a.Id);
     resourceConfigurationForPost.WithIdSelector(p => p.Id);
     //Assert
     var authorResult = (int)resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author);
     authorResult.ShouldEqual(authorId);
     var postResult = (int)resourceConfigurationForPost.ConstructedMetadata.IdGetter.Invoke(post);
     postResult.ShouldEqual(postId);
 }
 public void IgnorePropertyTest()
 {
     //Arrange
     const int authorId = 5;
     var author = new Author() { Id = authorId };
     var resourceConfigurationForAuthor = configurationBuilder.Resource<Author>();
     resourceConfigurationForAuthor.WithSimpleProperty(a => a.Name);
     resourceConfigurationForAuthor.ConstructedMetadata.PropertyGetters.Count.ShouldEqual(1);
     resourceConfigurationForAuthor.ConstructedMetadata.PropertySetters.Count.ShouldEqual(1);
     resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.ShouldBeNull();
     resourceConfigurationForAuthor.ConstructedMetadata.ResourceType.ShouldContain(typeof(Author).Name.ToLower());
     //Act
     resourceConfigurationForAuthor.IgnoreProperty(a => a.Name);
     //Assert
     resourceConfigurationForAuthor.ConstructedMetadata.PropertyGetters.Count.ShouldEqual(0);
     resourceConfigurationForAuthor.ConstructedMetadata.PropertySetters.Count.ShouldEqual(0);
     resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.ShouldBeNull();
     resourceConfigurationForAuthor.ConstructedMetadata.ResourceType.ShouldContain(typeof(Author).Name.ToLower());
 }
        public void WithLinkedResource_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new CamelCaseLinkNameConvention())
                .WithConvention(new PluralizedCamelCaseTypeConvention())
                .WithConvention(new SimpleLinkedIdConvention());

            var post = new Post();
            var author = new Author
            {
                Posts = new List<Post> { post }
            };

            post.Author = author;
            post.AuthorId = 4;

            //Act
            builder
                .Resource<Post>()
                .WithLinkedResource(p => p.Author);

            builder
                .Resource<Author>()
                .WithLinkedResource(a => a.Posts);

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));
            var authorMapping = configuration.GetMapping(typeof(Author));

            //Assert
            postMapping.Relationships.Count.ShouldEqual(1);

            var linkToAuthor = postMapping.Relationships.Single();

            linkToAuthor.IsCollection.ShouldBeFalse();
            linkToAuthor.RelationshipName.ShouldEqual("author");
            linkToAuthor.ParentType.ShouldEqual(typeof(Post));
            linkToAuthor.RelatedBaseType.ShouldEqual(typeof(Author));
            linkToAuthor.RelatedResource(post).ShouldBeSameAs(author);
            linkToAuthor.RelatedResourceId(post).ShouldEqual(4);
            linkToAuthor.ResourceMapping.ShouldBeSameAs(authorMapping);

            authorMapping.Relationships.Count.ShouldEqual(1);
            var linkToPosts = authorMapping.Relationships.Single();

            linkToPosts.IsCollection.ShouldBeTrue();
            linkToPosts.RelationshipName.ShouldEqual("posts");
            linkToPosts.RelatedBaseType.ShouldEqual(typeof(Post));
            linkToPosts.ParentType.ShouldEqual(typeof(Author));
            linkToPosts.RelatedResource(author).ShouldBeSameAs(author.Posts);
            linkToPosts.RelatedResourceId.ShouldBeNull();
            linkToPosts.ResourceMapping.ShouldBeSameAs(postMapping);
        }
        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 void TestWithSimplePropertyWithIdentyAndAscessor()
        {
            //Arrange
            const int authorId = 5;
            const string authorName = "Valentin";
            var author = new Author() { Id = authorId, Name = authorName };

            //Act
            var resourceConfigurationForAuthor = configurationBuilder
                .Resource<Author>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id);

            var resultForName = resourceConfigurationForAuthor.ConstructedMetadata.PropertyGetters["name"].Invoke(author);
            var resultForId = resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author);

            //Assert
            resultForName.ShouldEqual(authorName);
            resultForId.ShouldEqual(authorId);
        }
        public void TestWithSimplePropertyMultipleTypes()
        {
            //Arrange
            const int authorId = 5;
            const string authorName = "Valentin";
            const int postId = 6;
            const string postTitle = "The measure of a man";
            const string postTitleModifed = "Modified";
            var author = new Author() { Id = authorId, Name = authorName };
            var post = new Post() { Id = postId, Title = postTitle };

            //Act
            var resourceConfigurationForPost = configurationBuilder
                .Resource<Post>()
                .WithSimpleProperty(p => p.Title)
                .WithIdSelector(p => p.Id);
            var resourceConfigurationForAuthor = configurationBuilder
                .Resource<Author>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id);

            //Assert
            AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForAuthor);
            resourceConfigurationForAuthor.ConstructedMetadata.ResourceType.ShouldContain(typeof(Author).Name.ToLower());
            resourceConfigurationForAuthor.ConstructedMetadata.PropertyGetters["name"].Invoke(author).ShouldEqual(authorName);
            resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author).ShouldEqual(authorId);

            AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForPost);
            resourceConfigurationForPost.ConstructedMetadata.ResourceType.ShouldContain(typeof(Post).Name.ToLower());
            resourceConfigurationForPost.ConstructedMetadata.PropertyGetters["title"].Invoke(post).ShouldEqual(postTitle);
            resourceConfigurationForPost.ConstructedMetadata.PropertySetters["title"].Invoke(post, postTitleModifed);
            post.Title.ShouldEqual(postTitleModifed);
            resourceConfigurationForPost.ConstructedMetadata.PropertyGetters["title"].Invoke(post).ShouldEqual(postTitleModifed);
        }