public void Create_TopicNameAlreadyExists_ThrowsTopicNameAlreadyExistsException()
        {
            // Arrange
            string topicName = "topic_name";

            Topic topic = new Topic { TopicId = 1, Name = topicName };

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetByName(topicName))
            .Returns(topic);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            // Arrange - create target
            ITopicService target = new TopicService(unitOfWorkMock.Object, this._topicValidationMock.Object);

            // Act and Assert
            Assert.Throws<TopicNameAlreadyExistsException>(() => target.Create(topicName));

            topicRepositoryMock.Verify(r => r.GetByName(topicName), Times.Once);
            topicRepositoryMock.Verify(r => r.Insert(It.Is<Topic>(t => t.Name == topicName)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemple #2
0
        public Topic Create(string topicName)
        {
            Guard.NotNullOrEmpty(topicName, "topicName");

            if (!this._topicValidation.IsValidName(topicName))
            {
                throw new InvalidTopicNameFormatException(String.Format("Topic's name '{0}' has invalid format.", topicName));
            }

            Topic topic = this._unitOfWork.TopicRepository.GetByName(topicName);

            if (topic != null)
            {
                throw new TopicNameAlreadyExistsException(String.Format("Topic with name '{0}' already exists.", topicName));
            }

            topic = new Topic { Name = topicName };

            this._unitOfWork.TopicRepository.Insert(topic);

            this._unitOfWork.Save();

            return topic;
        }
        public void GetByName_TopicNameIsValid_ReturnsTopic()
        {
            // Arrange
            Topic testTopic = new Topic { TopicId = 1, Name = "topic_name" };

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetByName(testTopic.Name))
            .Returns(testTopic);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            // Arrange - create target
            ITopicService target = new TopicService(unitOfWorkMock.Object, this._topicValidationMock.Object);

            // Act
            Topic topic = target.GetByName(testTopic.Name);

            // Assert
            Assert.AreSame(testTopic, topic);
        }
        public void Edit_AllCredentialsAreValid_EditsTopic()
        {
            // Arrange
            Topic topic = new Topic { TopicId = 1 };
            string newTopicName = "new_topic_name";

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetById(topic.TopicId))
            .Returns(topic);

            topicRepositoryMock.Setup(r => r.GetByName(newTopicName))
            .Returns((Topic)null);

            Topic editedTopic = null;

            topicRepositoryMock.Setup(r => r.Update(It.Is<Topic>(t => t.TopicId == topic.TopicId)))
            .Callback((Topic t) => editedTopic = t);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            // Arrange - create target
            ITopicService target = new TopicService(unitOfWorkMock.Object, this._topicValidationMock.Object);

            // Act
            target.Edit(topic.TopicId, newTopicName);

            // Assert
            Assert.IsNotNull(editedTopic);
            Assert.AreEqual(newTopicName, editedTopic.Name);

            topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once);
            topicRepositoryMock.Verify(r => r.GetByName(newTopicName), Times.Once);
            topicRepositoryMock.Verify(r => r.Update(It.Is<Topic>(t => t.TopicId == topic.TopicId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
        public void Create_TagIdsArrayIsEmpty_DoNotGetTagsFromRepository()
        {
            // Arrange
            User user = new User { UserId = 1 };

            string articleTitle = "article_title";
            string articleText = "article_text";
            string validatedArticleTitle = "validated_article_title";
            string validatedArticleText = "validated_article_text";

            Topic topic = new Topic { TopicId = 2 };
            int[] tagIds = { };

            // Arrange - mock userRepository
            Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>();

            userRepositoryMock.Setup(r => r.GetById(user.UserId))
            .Returns(user);

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetById(topic.TopicId))
            .Returns(topic);

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.UserRepository)
            .Returns(userRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            // Arrange - mock articleValidation
            this._articleValidationMock.Setup(v => v.ValidateTitle(articleTitle))
            .Returns(validatedArticleTitle);

            this._articleValidationMock.Setup(v => v.ValidateArticleText(articleText))
            .Returns(validatedArticleText);

            // Arrange - create target
            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            Article article = target.Create(user.UserId, articleTitle, articleText, topic.TopicId, tagIds);

            // Assert
            Assert.IsNotNull(article);
            Assert.AreEqual(user.UserId, article.UserId);
            Assert.AreEqual(topic.TopicId, article.TopicId);
            Assert.AreEqual(validatedArticleTitle, article.Title);
            Assert.AreEqual(validatedArticleText, article.Text);
            Assert.IsFalse(article.Tags.Any());
            Assert.IsTrue(new DateTime() != article.PublicationDate);

            userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once);

            topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once);

            tagRepositoryMock.Verify(
            r =>
            r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), It.IsAny<Func<IQueryable<Tag>, IOrderedQueryable<Tag>>>(),
            It.IsAny<Expression<Func<Tag, object>>[]>()), Times.Never);

            articleRepositoryMock.Verify(
            r =>
            r.Insert(
            It.Is<Article>(a => a.UserId == user.UserId && a.Title == validatedArticleTitle && a.Text == validatedArticleText)),
            Times.Once);

            unitOfWorkMock.Verify(u => u.Save(), Times.Once);

            this._articleValidationMock.Verify(v => v.ValidateTitle(articleTitle), Times.Once);
            this._articleValidationMock.Verify(v => v.ValidateArticleText(articleText), Times.Once);
        }
        public void Create_AllCredentialsAreValid_CreatesArticle()
        {
            // Arrange
            User user = new User { UserId = 1 };

            string articleTitle = "article_title";
            string articleText = "article_text";
            string validatedArticleTitle = "validated_article_title";
            string validatedArticleText = "validated_article_text";

            Topic topic = new Topic { TopicId = 2 };
            int[] tagIds = { 1, 2, 3, 4 };

            IList<Tag> tags = new List<Tag>();

            foreach (int tagId in tagIds)
            {
                tags.Add(new Tag { TagId = tagId });
            }

            // Arrange - mock userRepository
            Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>();

            userRepositoryMock.Setup(r => r.GetById(user.UserId))
            .Returns(user);

            // Arrange - mock topicRepository
            Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>();

            topicRepositoryMock.Setup(r => r.GetById(topic.TopicId))
            .Returns(topic);

            // Arrange - mock tagRepository
            Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>();

            tagRepositoryMock.Setup(
            r => r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), null, It.Is<Expression<Func<Tag, object>>[]>(e => !e.Any())))
            .Returns(
            (Expression<Func<Tag, bool>> filter, Func<IQueryable<Tag>, IOrderedQueryable<Tag>> orderBy,
             Expression<Func<Tag, object>>[] selector) => CollectionHelper.FilterCollection(tags, filter, orderBy));

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.UserRepository)
            .Returns(userRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.TopicRepository)
            .Returns(topicRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.TagRepository)
            .Returns(tagRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            // Arrange - mock articleValidation
            this._articleValidationMock.Setup(v => v.ValidateTitle(articleTitle))
            .Returns(validatedArticleTitle);

            this._articleValidationMock.Setup(v => v.ValidateArticleText(articleText))
            .Returns(validatedArticleText);

            // Arrange - create target
            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            Article article = target.Create(user.UserId, articleTitle, articleText, topic.TopicId, tagIds);

            // Assert
            Assert.IsNotNull(article);
            Assert.AreEqual(user.UserId, article.UserId);
            Assert.AreEqual(topic.TopicId, article.TopicId);
            Assert.AreEqual(validatedArticleTitle, article.Title);
            Assert.AreEqual(validatedArticleText, article.Text);
            Assert.AreEqual(tags, article.Tags);
            Assert.IsTrue(new DateTime() != article.PublicationDate);

            userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once);

            topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once);

            tagRepositoryMock.Verify(
            r => r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), null, It.Is<Expression<Func<Tag, object>>[]>(e => !e.Any())),
            Times.Once);

            articleRepositoryMock.Verify(
            r =>
            r.Insert(
            It.Is<Article>(a => a.UserId == user.UserId && a.Title == validatedArticleTitle && a.Text == validatedArticleText)),
            Times.Once);

            unitOfWorkMock.Verify(u => u.Save(), Times.Once);

            this._articleValidationMock.Verify(v => v.ValidateTitle(articleTitle), Times.Once);
            this._articleValidationMock.Verify(v => v.ValidateArticleText(articleText), Times.Once);
        }