コード例 #1
0
        public HttpResponseMessage CreateThread(ThreadModel threadModel, string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
            {
                var user = this.userRepository.GetAll()
                    .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();

                if (user == null)
                {
                    throw new InvalidOperationException("User is not logged in or invalid session key.");
                }

                if (threadModel.Title == null)
                {
                    throw new ArgumentException("The title can't be null.");
                }

                if (threadModel.Content == null)
                {
                    throw new ArgumentException("The content can't be null.");
                }

                var newThread = new Thread()
                {
                    Title = threadModel.Title,
                    Content = threadModel.Content,
                    Creator = user,
                    DateCreated = threadModel.DateCreated,
                };

                foreach (var category in threadModel.Categories)
                {
                    var categoryEntity = this.categoryRepository.GetAll()
                        .Where(cat => cat.Name.ToLower() == category.ToLower()).FirstOrDefault();

                    if (categoryEntity == null)
                    {
                        categoryEntity = new Category();
                        categoryEntity.Name = category;
                        this.categoryRepository.Add(categoryEntity);
                    }

                    categoryEntity.Threads.Add(newThread);
                    newThread.Categories.Add(categoryEntity);
                }

                this.threadRepository.Add(newThread);

                var responseMessage = this.Request.CreateResponse(HttpStatusCode.Created,
                    new
                    {
                        id = newThread.Id,
                        title = newThread.Title
                    });

                return responseMessage;
            });

            return response;
        }
コード例 #2
0
        public void GetByIdTest_ValidId_ShouldReturnValidEntity()
        {
            var entity = new Category()
            {
                Name = "Test name"
            };

            this.repository.Add(entity);
            var foundEntity = this.entities.Find(entity.Id);

            Assert.AreSame(entity, foundEntity);
        }
コード例 #3
0
        public void AddTest_EntityIsValid_ShouldBeAddedSuccessfuly()
        {
            var entity = new Category()
            {
                Name = "Valid test category"
            };

            this.repository.Add(entity);
            var foundEntity = this.entities.Find(entity.Id);

            Assert.IsTrue(entity.Id != 0);
            Assert.IsNotNull(foundEntity);
            Assert.AreSame(entity, foundEntity);
        }
コード例 #4
0
        public HttpResponseMessage CreateCategory(CategoryModel model, string sessionKey)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
                {
                    var user = this.userRepository.GetAll()
                    .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new InvalidOperationException("User is not logged in or invalid session key.");
                    }

                    if (model == null)
                    {
                        throw new ArgumentNullException("The CategoryModel cannot be null.");
                    }

                    if (model.Name == null)
                    {
                        throw new ArgumentNullException("The category name cannot be null.");
                    }

                    Category category = new Category();
                    category.Name = model.Name;

                    var threadTitles = model.threadTitles;
                    var allThreadEntities = this.threadRepository.GetAll();

                    if (threadTitles != null)
                    {
                        foreach (var title in threadTitles)
                        {
                            var threadEntity = allThreadEntities.FirstOrDefault(
                                entity => entity.Title == title);

                            category.Threads.Add(threadEntity);
                        }
                    }

                    this.categoryRepository.Add(category);

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, new { id = category.Id });
                    return response;
                });

            return responseMessage;
        }
コード例 #5
0
        public void GetRequest_GetAllTest_InvalidSessionKey_NoUserWithSuchKey()
        {
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();

            string sessionKey = "01234567890123456789012345678901234567890123456789";

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

            categoryFakeRepository.entities.Add(category);

            CategoriesController categoriesController = new CategoriesController(
                categoryFakeRepository, userFakeRepository, threadFakeRepository);

            SetupController(categoriesController);

            var allCategories = categoriesController.GetAll(sessionKey).ToList<string>();
        }
コード例 #6
0
        public void GetRequest_GetAllTest_OneCategoryOnly()
        {
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();

            string sessionKey = "01234567890123456789012345678901234567890123456789";

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

            categoryFakeRepository.entities.Add(category);

            User user = new User()
            {
                SessionKey = sessionKey
            };

            userFakeRepository.Add(user);

            CategoriesController categoriesController = new CategoriesController(
                categoryFakeRepository, userFakeRepository, threadFakeRepository);

            SetupController(categoriesController);

            var allCategories = categoriesController.GetAll(sessionKey).ToList<string>();

            int expectedCategoryCount = 1;
            Assert.AreEqual(expectedCategoryCount, allCategories.Count);
        }
コード例 #7
0
        public void UpdateTest_InvalidEntityId()
        {
            int invalidId = -1;
            Category entity = new Category()
            {
                Name = "Test category"
            };

            this.categoryRepository.Add(entity);

            Category categoryToUpgradeWith = new Category()
            {
                Name = "Updated category"
            };

            Category upgradedCategory = this.categoryRepository.Update(invalidId, categoryToUpgradeWith);

            Assert.IsNull(upgradedCategory);
        }
コード例 #8
0
        public void UpdateTest_ValidUpdate()
        {
            Category entity = new Category()
            {
                Name = "Test category"
            };

            this.categoryRepository.Add(entity);

            Category categoryToUpgradeWith = new Category()
            {
                Name = "Updated category"
            };

            Category upgradedCategory = this.categoryRepository.Update(entity.Id, categoryToUpgradeWith);

            Assert.AreSame(entity, upgradedCategory);
            Assert.AreEqual(upgradedCategory.Name, categoryToUpgradeWith.Name);
        }
コード例 #9
0
        public void UpdateTest_InvalidUpdateEntity_ShouldThrowArgumentNullException()
        {
            Category entity = new Category()
            {
                Name = "Test category"
            };

            this.categoryRepository.Add(entity);

            Category categoryToUpgradeWith = null;

            Category upgradedCategory = this.categoryRepository.Update(entity.Id, categoryToUpgradeWith);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }