public void For_existing_id_properties_returns_proper_expression()
        {
            // Arrange
            var convention = new SimpleLinkedIdConvention();
            var post = new Post {AuthorId = 4};

            // Act
            var expression = convention.GetIdExpression((Post p) => p.Author);

            // Assert
            expression.ShouldNotBeNull();
            var compiled = expression.Compile();
            compiled(post).ShouldEqual(4);
        }
 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 WithSimpleProperty_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            var post = new Post() { Title = "test" };

            //Act
            builder
                .Resource<Post>()
                .WithSimpleProperty(p => p.Title);

            var configuration = builder.Build();
            var mapping = configuration.GetMapping(typeof(Post));

            //Assert
            mapping.PropertyGetters.Count.ShouldEqual(1);
            mapping.PropertySetters.Count.ShouldEqual(1);

            var getter = mapping.PropertyGetters.Single().Value;
            var setter = mapping.PropertySetters.Single().Value;

            ((string)getter(post)).ShouldEqual("test");

            setter(post, "works");
            post.Title.ShouldEqual("works");
        }
        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 WithIdSelector_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            var post = new Post { Id = 4 };

            //Act
            builder
                .Resource<Post>()
                .WithIdSelector(p => p.Id);

            var configuration = builder.Build();
            var mapping = configuration.GetMapping(typeof(Post));

            //Assert
            mapping.IdGetter.ShouldNotBeNull();
            mapping.IdGetter(post).ShouldEqual(4);
        }
        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 WithAllSimpleProperties_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new DefaultPropertyScanningConvention());

            const string testTitle = "test";
            var post = new Post
            {
                Id = 4,
                Title = testTitle
            };

            //Act
            builder
                .Resource<Post>()
                .WithAllSimpleProperties();

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

            //Assert
            postMapping.IdGetter.ShouldNotBeNull();
            postMapping.IdGetter(post).ShouldEqual(4);
            postMapping.PropertyGetters.Count.ShouldEqual(2);
            postMapping.PropertySetters.Count.ShouldEqual(2);
            postMapping.PropertyGetters["title"](post).ShouldEqual(testTitle);
            postMapping.PropertyGetters.ContainsKey("authorId").ShouldBeTrue();
        }
        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);
        }