public object GetService(Type serviceType)
        {
            var dbContext = new ForumDbContext();

            if (serviceType == typeof(UsersController))
            {
                var repository = new EfRepository<User>(dbContext);
                var userController = new UsersController(repository);

                return userController;
            }
            else if (serviceType == typeof(ThreadsController))
            {
                var categoryRepository = new EfRepository<Category>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);

                var threadController = new ThreadsController(
                    categoryRepository, threadRepository, userRepository);

                return threadController;
            }
            else if (serviceType == typeof(PostsController))
            {
                var postRepository = new EfRepository<Post>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);
                var voteRepository = new EfRepository<Vote>(dbContext);
                var commentRepository = new EfRepository<Comment>(dbContext);

                var postsController = new PostsController(
                    postRepository, userRepository, threadRepository, voteRepository, commentRepository);

                return postsController;
            }
            else if (serviceType == typeof(CategoriesController))
            {
                var categoryRepository = new EfRepository<Category>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);

                var categoriesController = new CategoriesController(categoryRepository, userRepository, threadRepository);

                return categoriesController;
            }
            else
            {
                return null;
            }
        }
        public void Get_GetAllTest_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"
            };

            for (int i = 1; i <= 5; i++)
            {
                Thread thread = new Thread()
                {
                    Title = string.Format("Test title {0}", i),
                    Creator = user,
                    Content = string.Format("Test content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i)
                };

                user.Threads.Add(thread);
            }

            userFakeRepository.entities.Add(user);

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

            SetupController(threadsController);

            var allThreads = threadsController.GetAll("0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ").ToArray<ThreadModel>();

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

                Assert.IsTrue(result > 0);
            }
        }
        public void Get_GetAllTest_InvalidSessionKey_NoUserWithSuchSessionKey()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

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

            SetupController(threadsController);

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

            userFakeRepository.entities.Add(user);

            threadsController.GetAll("1InvalidSessionKeyvHlVfHGotklitbwHdYFkgwIRcIQjRAPQ");
        }
        public void Get_GetAllTest_ValidExecutionOfTheMethod()
        {
            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
            };

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

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

            SetupController(threadsController);

            var allThreads = threadsController.GetAll("0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ").ToList<ThreadModel>();
            int expectedThreadCount = 1;

            Assert.AreEqual(expectedThreadCount, allThreads.Count);
        }
        public void Post_CreateThreadTest_ValidCreation()
        {
            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"
            };

            userFakeRepository.entities.Add(user);

            Category category1 = new Category()
            {
                Name = "Test category 1"
            };

            categoryFakeRepository.entities.Add(category1);

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

            SetupController(threadsController);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = "Test title",
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now,
                Categories = new List<string>()
                {
                    "Test category 1",
                    "Test category 2"
                }
            };

            threadsController.CreateThread(threadModel, user.SessionKey);

            int expectedThreadCount = 1;
            Thread addedThread = threadFakeRepository.entities.FirstOrDefault();
            List<Category> addedThreadCategories = addedThread.Categories.ToList<Category>();

            Assert.AreEqual(expectedThreadCount, threadFakeRepository.entities.Count);
            Assert.AreEqual("Test category 1", addedThreadCategories[0].Name);
            Assert.AreEqual("Test category 2", addedThreadCategories[1].Name);
        }
        public void Post_CreateThreadTest_InvalidCreation_ThreadTitleIsNull()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

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

            SetupController(threadsController);

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

            userFakeRepository.entities.Add(user);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = null,
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now
            };

            threadsController.CreateThread(threadModel, user.SessionKey);
        }
        public void Post_CreateThreadTest_InvalidCreation_SessionKeyIsNull()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

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

            SetupController(threadsController);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = "Test title",
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now
            };

            string userSessionKey = null;
            threadsController.CreateThread(threadModel, userSessionKey);
        }
        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);
            }
        }
        public void Get_GetPageTest_GettingLast5ThreadsFromTotalOf30()
        {
            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"
            };

            for (int i = 1; i <= 30; i++)
            {
                Thread thread = new Thread()
                {
                    Title = string.Format("Test title {0}", i),
                    Content = string.Format("Test content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    Creator = user
                };

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

            userFakeRepository.entities.Add(user);

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

            SetupController(threadsController);

            var requestedThreads = threadsController.GetPage(5, 5, user.SessionKey).ToArray<ThreadModel>();
            int expectedThreadCount = 5;

            Assert.AreEqual(expectedThreadCount, requestedThreads.Length);

            // Checking the last five threads.
            for (int i = 0; i < requestedThreads.Length; i++)
            {
                string expectedTitle = string.Format("Test title {0}", requestedThreads.Length - i);
                string expectedContent = string.Format("Test content {0}", requestedThreads.Length - i);

                Assert.AreEqual(expectedTitle, requestedThreads[i].Title);
                Assert.AreEqual(expectedContent, requestedThreads[i].Content);
            }
        }
        public void Get_GetByCategoryTest_OnlyOneThreadWithThatCategory_ShouldBeReturned()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            string testCategory = "Test category";

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

            Category category = new Category()
            {
                Name = testCategory
            };

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

            thread.Categories.Add(category);
            category.Threads.Add(thread);
            user.Threads.Add(thread);

            userFakeRepository.entities.Add(user);
            threadFakeRepository.Add(thread);
            categoryFakeRepository.Add(category);

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

            SetupController(threadsController);

            var threads = threadsController.GetByCategory(testCategory, user.SessionKey).ToList<ThreadModel>();
            int expectedThreadCount = 1;

            Assert.AreEqual(expectedThreadCount, threads.Count);
        }