Ejemplo n.º 1
0
        public Article Create(int userId, string title, string text, int topicId, int[] tagIds)
        {
            Guard.IntMoreThanZero(userId, "userId");
            Guard.NotNullOrEmpty(title, "title");
            Guard.NotNullOrEmpty(text, "text");
            Guard.IntMoreThanZero(topicId, "topicId");
            Guard.NotNull(tagIds, "tagIds");

            if (tagIds.Any(t => t <= 0))
            {
                throw new ArgumentOutOfRangeException(String.Format("Argument '{0}' must contain only positive numbers.",
                "tagIds"));
            }

            User user = this._unitOfWork.UserRepository.GetById(userId);

            if (user == null)
            {
                throw new UserNotFoundException(String.Format("User with ID '{0}' was not found.", userId));
            }

            Topic topic = this._unitOfWork.TopicRepository.GetById(topicId);

            if (topic == null)
            {
                throw new TopicNotFoundException(String.Format("Topic with ID '{0}' was not found.", topicId));
            }

            string validatedTitle = this._articleValidation.ValidateTitle(title);
            string validatedText = this._articleValidation.ValidateArticleText(text);

            Article article = new Article
            {
                UserId = user.UserId, TopicId = topic.TopicId, Title = validatedTitle, Text = validatedText,
                PublicationDate = DateTime.Now
            };

            if (tagIds.Any())
            {
                IEnumerable<Tag> tags = this._unitOfWork.TagRepository.Get(t => tagIds.Contains(t.TagId));

                foreach (Tag tag in tags)
                {
                    article.Tags.Add(tag);
                }
            }

            this._unitOfWork.ArticleRepository.Insert(article);
            this._unitOfWork.Save();

            return article;
        }
Ejemplo n.º 2
0
        public void Create_AllCredentialsAreValid_CreatesAndReturnsComment()
        {
            // Arrange
            User user = new User { UserId = 1 };
            Article article = new Article { ArticleId = 2 };
            string commentText = "comment_text";
            string validatedCommentText = "validated_comment_text";

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

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

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

            articleRepositoryMock.Setup(r => r.GetById(article.ArticleId))
            .Returns(article);

            // Arrange - mock commentRepository
            Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>();

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

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

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

            unitOfWorkMock.SetupGet(u => u.CommentRepository)
            .Returns(commentRepositoryMock.Object);

            // Arrange - mock commentValidation
            this._commentValidationMock.Setup(v => v.ValidateCommentText(commentText))
            .Returns(validatedCommentText);

            // Arrange - create target
            ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            Comment comment = target.Create(user.UserId, article.ArticleId, commentText);

            // Assert
            Assert.IsNotNull(comment);
            Assert.AreEqual(user.UserId, comment.UserId);
            Assert.AreEqual(article.ArticleId, comment.ArticleId);
            Assert.AreEqual(validatedCommentText, comment.Text);
            Assert.IsTrue(new DateTime() != comment.Date);

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

            articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once);

            commentRepositoryMock.Verify(
            r =>
            r.Insert(
            It.Is<Comment>(c => c.UserId == user.UserId && c.ArticleId == article.ArticleId && c.Text == validatedCommentText)),
            Times.Once);

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

            this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once);
        }
Ejemplo n.º 3
0
        public void GetById_ArticleIdIsValid_ReturnsArticle()
        {
            // Arrange
            Article testArticle = new Article { ArticleId = 1 };

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

            articleRepositoryMock.Setup(r => r.GetById(testArticle.ArticleId))
            .Returns(testArticle);

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

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

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

            // Act
            Article article = target.GetById(testArticle.ArticleId);

            // Assert
            Assert.AreSame(testArticle, article);

            articleRepositoryMock.Verify(r => r.GetById(testArticle.ArticleId), Times.Once);
        }
Ejemplo n.º 4
0
        public void Delete_UserIsNotTheAuthorOfArticle_ThrowsDeletingArticleIsForbiddenException()
        {
            // Arrange
            int userId = 1;
            Article article = new Article { ArticleId = 2, UserId = userId + 1 };

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

            articleRepositoryMock.Setup(r => r.GetById(article.ArticleId))
            .Returns(article);

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

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

            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act and Assert
            Assert.Throws<DeletingArticleIsForbiddenException>(() => target.Delete(article.ArticleId, userId));

            // Assert
            articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once);
            articleRepositoryMock.Verify(r => r.Delete(It.Is<Article>(t => t.ArticleId == article.ArticleId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Ejemplo n.º 5
0
        public void Delete_ArticleIdIsValid_DeletesArticle()
        {
            // Arrange
            Article article = new Article { ArticleId = 1, UserId = 2 };

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

            articleRepositoryMock.Setup(r => r.GetById(article.ArticleId))
            .Returns(article);

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

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

            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            target.Delete(article.ArticleId, article.UserId);

            // Assert
            articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once);
            articleRepositoryMock.Verify(r => r.Delete(It.Is<Article>(t => t.ArticleId == article.ArticleId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
Ejemplo n.º 6
0
        public void ViewArticle_AllCredentialsAreValid_MarksArticleAsViewedByVisitor()
        {
            // Arrange
            int visitorId = 1;
            int articleId = 2;
            ArticleVisitor[] articleVisitors =
            {
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 1 },
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 2 }
            };

            Visitor visitor = new Visitor { VisitorId = visitorId, ArticleVisitors = articleVisitors.ToList() };
            Article article = new Article { ArticleId = articleId };

            Expression<Func<Visitor, object>>[] propertiesToInclude = { v => v.ArticleVisitors };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(visitor);

            IEnumerable<ArticleVisitor> newArticleVisitors = null;

            visitorRepositoryMock.Setup(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)))
            .Callback((Visitor v) => newArticleVisitors = v.ArticleVisitors);

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

            articleRepositoryMock.Setup(r => r.GetById(articleId))
            .Returns(article);

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

            unitOfWorkMock.SetupGet(u => u.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

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

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            target.ViewArticle(visitorId, articleId);

            // Assert
            Assert.IsNotNull(newArticleVisitors);
            Assert.AreEqual(articleVisitors.Count() + 1, newArticleVisitors.Count());

            ArticleVisitor articleVisitor = newArticleVisitors.FirstOrDefault(v => v.ArticleId == articleId);

            Assert.IsNotNull(articleVisitor);
            Assert.IsTrue(new DateTime() != articleVisitor.LastViewDate);

            visitorRepositoryMock.Verify(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            visitorRepositoryMock.Verify(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)), Times.Once);

            articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once);

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