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 TestWithIdSelector()
        {
            //Arrange
            var resourceConfigurationForAuthor = configurationBuilder.Resource<Author, AuthorsController>();
            const int authorId = 5;
            var author = new Author() { Id = authorId };

            //Act
            resourceConfigurationForAuthor.WithIdSelector(a => a.Id);

            //Assert
            var result = (int)resourceConfigurationForAuthor.BuiltResourceMapping.IdGetter.Invoke(author);
            Assert.Equal(result, 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 TestWithIdSelectorForMultipleTypes()
        {
            //Arrange
            var resourceConfigurationForAuthor = configurationBuilder.Resource<Author, AuthorsController>();
            var resourceConfigurationForPost = configurationBuilder.Resource<Post, PostsController>();
            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.BuiltResourceMapping.IdGetter.Invoke(author);
            Assert.Equal(authorResult, authorId);
            var postResult = (int)resourceConfigurationForPost.BuiltResourceMapping.IdGetter.Invoke(post);
            Assert.Equal(postResult, postId);
        }
Example #6
0
 public PostBuilder WithAuthor(Author author)
 {
     post.Author = author;
     return this;
 }
        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, PostsController>()
                .WithLinkedResource(p => p.Author);

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

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

            //Assert
            Assert.Equal(postMapping.Relationships.Count, 1);

            var linkToAuthor = postMapping.Relationships.Single();

            Assert.False(linkToAuthor.IsCollection);
            Assert.Equal(linkToAuthor.RelationshipName, "author");
            Assert.Equal(linkToAuthor.ParentType, typeof(Post));
            Assert.Equal(linkToAuthor.RelatedBaseType, typeof(Author));
            Assert.Same(linkToAuthor.RelatedResource(post), author);
            Assert.Equal(linkToAuthor.RelatedResourceId(post), 4);
            Assert.Same(linkToAuthor.ResourceMapping, authorMapping);
            Assert.Equal(authorMapping.Relationships.Count, 1);
            
            var linkToPosts = authorMapping.Relationships.Single();

            Assert.True(linkToPosts.IsCollection);
            Assert.Equal(linkToPosts.RelationshipName, "posts");
            Assert.Equal(linkToPosts.ParentType, typeof(Author));
            Assert.Equal(linkToPosts.RelatedBaseType, typeof(Post));
            Assert.Same(linkToPosts.RelatedResource(author), author.Posts);
            Assert.Null(linkToPosts.RelatedResourceId);
            Assert.Same(linkToPosts.ResourceMapping, 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, PostsController>()
                .WithSimpleProperty(p => p.Title)
                .WithIdSelector(p => p.Id)
                .WithLinkedResource(p => p.Replies);
            var resourceConfigurationForAuthor = configurationBuilder
                .Resource<Author, AuthorsController>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id)
                .WithLinkedResource(a => a.Posts);
            var resourceConfigurationForComment = configurationBuilder
                .Resource<Comment, CommentsController>()
                .WithIdSelector(c => c.Id)
                .WithSimpleProperty(c => c.Body);
            var result = configurationBuilder.Build();

            //Assert
            Assert.Equal(resourceConfigurationForPost.BuiltResourceMapping.Relationships.Count, 1);
            Assert.Equal(resourceConfigurationForAuthor.BuiltResourceMapping.Relationships.Count, 1);
            configurationBuilder.ResourceConfigurationsByType.All(
                r => r.Value.BuiltResourceMapping.Relationships.All(l => l.ResourceMapping != null));
            var authorLinks =
                 configurationBuilder.ResourceConfigurationsByType[
                     resourceConfigurationForAuthor.BuiltResourceMapping.ResourceRepresentationType].BuiltResourceMapping.Relationships;
            Assert.Equal(authorLinks.Count, 1);
            Assert.Equal(authorLinks[0].RelationshipName, "posts");
            Assert.Equal(authorLinks[0].ResourceMapping.PropertyGetters.Count, 1);
        }
        public void TestWithSimpleProperty()
        {
            //Arrange
            const int authorId = 5;
            var author = new Author() { Id = authorId };
            var resourceConfigurationForAuthor = configurationBuilder.Resource<Author, AuthorsController>();
            
            //Act
            resourceConfigurationForAuthor.WithSimpleProperty(a => a.Name);

            //Assert
            Assert.Equal(resourceConfigurationForAuthor.BuiltResourceMapping.PropertyGetters.Count, 1);
            Assert.Equal(resourceConfigurationForAuthor.BuiltResourceMapping.PropertySetters.Count, 1);
            Assert.Null(resourceConfigurationForAuthor.BuiltResourceMapping.IdGetter);
            Assert.Contains("author", resourceConfigurationForAuthor.BuiltResourceMapping.ResourceType);
        }
        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, PostsController>()
                .WithSimpleProperty(p => p.Title)
                .WithIdSelector(p => p.Id);
            var resourceConfigurationForAuthor = configurationBuilder
                .Resource<Author, AuthorsController>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id);

            //Assert
            var result = resourceConfigurationForAuthor.BuiltResourceMapping;
            AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForAuthor);

            Assert.Contains("author", result.ResourceType);
            Assert.Equal(result.PropertyGetters["name"].Invoke(author), authorName);
            Assert.Equal(result.IdGetter.Invoke(author), authorId);

            AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForPost);

            result = resourceConfigurationForPost.BuiltResourceMapping;
            Assert.Contains("post", result.ResourceType);
            Assert.Equal(result.PropertyGetters["title"].Invoke(post), postTitle);

            resourceConfigurationForPost.BuiltResourceMapping.PropertySetters["title"].Invoke(post, postTitleModifed);
            Assert.Equal(post.Title,postTitleModifed);
            Assert.Equal(result.PropertyGetters["title"].Invoke(post), postTitleModifed);
        }
        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, AuthorsController>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id);

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

            //Assert
            Assert.Equal(resultForName, authorName);
            Assert.Equal(resultForId, authorId);
        }
        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);
        }