public static PostModel CreateFromPostEntity(Post entity)
        {
            PostModel postModel = new PostModel()
            {
                Content = entity.Content,
                PostDate = entity.DateCreated,
                PostedBy = entity.User.Nickname,
                Rating = ((entity.Votes.Count == 0) ? 0 : entity.Votes.Select(val => val.Value).Average()) + "/5"
            };

            return postModel;
        }
        public void GetByIdTest_ValidId_ShouldReturnValidEntity()
        {
            var entity = new Post()
            {
                Content = "Test content",
                DateCreated = DateTime.Now
            };

            this.postRepository.Add(entity);
            var foundEntity = this.postEntities.Find(entity.Id);

            Assert.AreSame(entity, foundEntity);
        }
        public void AddTest_EntityIsValid_ShouldBeAddedSuccessfuly()
        {
            Post entity = new Post()
            {
                Content = "Test content",
                DateCreated = DateTime.Now
            };

            this.postRepository.Add(entity);
            var foundEntity = this.postEntities.Find(entity.Id);

            Assert.IsTrue(entity.Id != 0);
            Assert.IsNotNull(foundEntity);
            Assert.AreSame(entity, foundEntity);
        }
        public void Get_GetAllTest_ValidExecution_CheckingForCorrectOrder()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var posts = postsController.GetAll(user.SessionKey).ToArray<PostModel>();

            for (int i = 0; i < posts.Length - 1; i++)
            {
                int result = posts[i].PostDate.CompareTo(posts[i + 1].PostDate);
                Assert.IsTrue(result > 0);
            }
        }
        public void Get_GetAllTest_ValidExecution_ReturningSomePosts()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var posts = postsController.GetAll(user.SessionKey).ToList<PostModel>();
            int expectedPostCount = 20;

            Assert.AreEqual(expectedPostCount, posts.Count);
        }
        public void Post_VoteForPostTest_ValidExecutionOfTheMethod()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            VoteModel voteModel = new VoteModel()
            {
                Value = 5
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var response = postsController.VoteForPost(voteModel, post.Id, user.SessionKey);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
        public void Post_VoteForPostTest_InvalidVoteValue_ValueTooLarge()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            VoteModel voteModel = new VoteModel()
            {
                Value = 20 // Invalid vote value, too large.
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            int invalidPostId = 100;
            var response = postsController.VoteForPost(voteModel, invalidPostId, user.SessionKey);
        }
        public void Post_CommentForPostTest_InvalidSessionKey_NoUserWithSuchSessionKey()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            CommentModel commentModel = new CommentModel()
            {
                Content = "Just a test comment.",
                CommentDate = DateTime.Now
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var response = postsController.CommentForPost(commentModel, post.Id, "1InvalidSessionKeyvHlVfHGotklitbwHdYFkgwIRcIQjRAPQ");
        }
        public void Get_GetPageTest_GettingLast5PostsFromTotalOf30()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 30; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var lastFivePosts = postsController.GetPage(5, 5, user.SessionKey).ToArray<PostModel>();
            int expectedPostCount = 5;

            Assert.AreEqual(expectedPostCount, lastFivePosts.Length);

            // Checking if the last 5 posts are the ones they should be.
            for (int i = 0; i < lastFivePosts.Length; i++)
            {
                string expectedPostContent = string.Format("Test post content {0}", lastFivePosts.Length - i);
                Assert.AreEqual(expectedPostContent, lastFivePosts[i].Content);
            }
        }
        public void UpdateTest_InvalidEntityId()
        {
            int invalidId = -1;
            var entity = new Post()
            {
                Content = "Test content",
                DateCreated = DateTime.Now
            };

            this.postRepository.Add(entity);

            Post postToUpgradeWith = new Post()
            {
                Content = "Updated test content",
                DateCreated = DateTime.Now.AddDays(1)
            };

            Post upgradedPost = this.postRepository.Update(invalidId, postToUpgradeWith);

            Assert.IsNull(upgradedPost);
        }
        public void UpdateTest_ValidUpdate()
        {
            Post entity = new Post()
            {
                Content = "Test content",
                DateCreated = DateTime.Now
            };

            this.postRepository.Add(entity);

            Post postToUpgradeWith = new Post()
            {
                Content = "Updated test content",
                DateCreated = DateTime.Now.AddDays(1)
            };

            Post upgradedPost = this.postRepository.Update(entity.Id, postToUpgradeWith);

            Assert.AreSame(entity, upgradedPost);
            Assert.AreEqual(upgradedPost.Content, postToUpgradeWith.Content);
        }
        public void UpdateTest_InvalidUpdateEntity_ShouldThrowArgumentNullException()
        {
            Post entity = new Post()
            {
                Content = "Test content",
                DateCreated = DateTime.Now
            };

            this.postRepository.Add(entity);

            Post postToUpgradeWith = null;

            Post upgradedPost = this.postRepository.Update(entity.Id, postToUpgradeWith);
        }
        public HttpResponseMessage CreatePost([FromBody]PostCreateModel model, string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
                {
                    var user = this.userRepository.GetAll()
                    .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new InvalidOperationException("No logged user or invalid sessionKey");
                    }

                    var threadEntity = this.threadRepository.GetAll()
                        .Where(thr => thr.Id == model.ThreadId).FirstOrDefault();

                    if (threadEntity == null)
                    {
                        throw new InvalidOperationException("No thread with such id.");
                    }

                    var post = new Post()
                    {
                        Content = model.Content,
                        DateCreated = model.PostDate,
                        User = user,
                        Thread = threadEntity
                    };

                    this.postRepository.Add(post);

                    var responseMessage = this.Request.CreateResponse(
                        HttpStatusCode.Created,
                        new
                        {
                            id = post.Id,
                            postedBy = user.Nickname
                        });

                    return responseMessage;
                });

            return response;
        }
        public void Get_GetPostsTest_GetsThePostsWithGivenThreadId_ShouldReturnThemAll()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                post.Thread = thread;
                thread.Posts.Add(post);
            }

            user.Threads.Add(thread);
            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            var posts = threadsController.GetPosts(thread.Id, user.SessionKey).ToList<PostModel>();

            int expectedCount = 20;
            Assert.AreEqual(expectedCount, posts.Count);
        }
        public void Get_GetPostsTest_GetsThePostsWithGivenThreadId_InvalidThreadId_ShouldReturnNull()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = "Test post content",
                    DateCreated = DateTime.Now.AddDays(i)
                };

                thread.Posts.Add(post);
            }

            user.Threads.Add(thread);
            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            int unexistingThreadId = 100;
            var posts = threadsController.GetPosts(unexistingThreadId, user.SessionKey);

            Assert.IsNull(posts);
        }
        public void Get_GetPostsTest_CheckForCorrectDescendingOrder()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            for (int i = 1; i <= 5; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                post.Thread = thread;
                thread.Posts.Add(post);
            }

            user.Threads.Add(thread);
            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            var posts = threadsController.GetPosts(thread.Id, user.SessionKey).ToArray<PostModel>();

            for (int i = 0; i < posts.Length - 1; i++)
            {
                int result = posts[i].PostDate.CompareTo(posts[i + 1].PostDate);

                Assert.IsTrue(result > 0);
            }
        }