Exemplo n.º 1
0
        public void AddVote_CommentIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int userId = 1;
            bool voteIsPositive = true;

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.AddVote(-1, userId, voteIsPositive));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.AddVote(0, userId, voteIsPositive));
        }
Exemplo n.º 2
0
        public void AddVote_NonexistentCommentId_ThrowsCommentNotFoundException()
        {
            // Arrange
            int commentId = 1;
            int userId = 1;
            bool voteIsPositive = true;

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(commentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns((Comment)null);

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

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

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

            // Act and Assert
            Assert.Throws<CommentNotFoundException>(() => target.AddVote(commentId, userId, voteIsPositive));

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

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 3
0
        public void Delete_PermittedPeriodForDeletingExpired_ThrowsPermittedPeriodForDeletingExpiredException()
        {
            // Arrange
            Comment comment = new Comment
            {
                CommentId = 1, UserId = 2,
                Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForDeleting + 1))
            };

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

            commentRepositoryMock.Setup(r => r.GetById(comment.CommentId))
            .Returns(comment);

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

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

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

            // Act and Assert
            Assert.Throws<PermittedPeriodForDeletingExpiredException>(() => target.Delete(comment.CommentId, comment.UserId));

            commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once);
            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 4
0
        public void DeleteVote_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int commentId = 1;

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.DeleteVote(commentId, -1));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.DeleteVote(commentId, 0));
        }
Exemplo n.º 5
0
        public void Create_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int articleId = 1;
            string commentText = "comment_text";

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Create(-1, articleId, commentText));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Create(0, articleId, commentText));
        }
Exemplo n.º 6
0
        public void Create_CommentTextIsNull_ThrowsArgumentNullException()
        {
            // Arrange
            int userId = 1;
            int articleId = 2;

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentNullException>(() => target.Create(userId, articleId, null));
        }
Exemplo n.º 7
0
        public void CanHaveReply_CommentTreeLevelIsLessThanMaxCommentTreeLevel_ReturnsTrue()
        {
            // Arrange
            Comment comment = new Comment { TreeLevel = (byte)(this._serviceSettings.MaxCommentTreeLevel - 1) };

            // Arrange - create target
            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanHaveReply(comment);

            // Assert
            Assert.IsTrue(result);
        }
Exemplo n.º 8
0
        public void CanHaveReply_CommentIsNull_ThrowsArgumentNullException()
        {
            // Arrange
            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentNullException>(() => target.CanHaveReply(null));
        }
Exemplo n.º 9
0
        public void CanEdit_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            Comment comment = new Comment { CommentId = 1 };

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.CanEdit(-1, comment));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.CanEdit(0, comment));
        }
Exemplo n.º 10
0
        public void Reply_CommentTextIsEmpty_ThrowsArgumentException()
        {
            // Arrange
            int commentId = 1;
            int userId = 2;

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentException>(() => target.Reply(commentId, userId, ""));
        }
Exemplo n.º 11
0
        public void Reply_AllCredentialsAreValid_CreatesAndReturnsReply()
        {
            // Arrange
            User user = new User { UserId = 1 };
            string commentText = "comment_text";
            string validatedCommentText = "validated_comment_text";
            Comment[] commentReplies = { new Comment { CommentId = 2 }, new Comment { CommentId = 3 } };

            Comment comment = new Comment { CommentId = 4, ArticleId = 5, Comment1 = commentReplies.ToList() };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

            IEnumerable<Comment> newCommentReplies = null;

            commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>()))
            .Callback((Comment c) => newCommentReplies = c.Comment1);

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

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

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

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

            unitOfWorkMock.SetupGet(u => u.UserRepository)
            .Returns(userRepositoryMock.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 reply = target.Reply(comment.CommentId, user.UserId, commentText);

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

            Assert.IsNotNull(newCommentReplies);
            Assert.AreEqual(commentReplies.Count() + 1, newCommentReplies.Count());
            Assert.IsTrue(newCommentReplies.Contains(reply));

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Once);

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

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

            this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once);
        }
Exemplo n.º 12
0
        public void Edit_UserIsNotAuthorOfTheComment_ThrowsEditingCommentIsForbiddenException()
        {
            // Arrange
            int userId = 1;
            string newCommentText = "new_comment's_text";
            Comment comment = new Comment { CommentId = 2, UserId = userId + 1 };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

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

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

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

            // Act and Assert
            Assert.Throws<EditingCommentIsForbiddenException>(() => target.Edit(comment.CommentId, userId, newCommentText));

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 13
0
        public void Edit_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int commentId = 1;
            string newCommentText = "new_comment's_text";

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(commentId, -1, newCommentText));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(commentId, 0, newCommentText));
        }
Exemplo n.º 14
0
        public void Edit_PermittedPeriodForEditingExpired_ThrowsPermittedPeriodForEditingExpiredException()
        {
            // Arrange
            string newCommentText = "new_comment's_text";
            Comment comment = new Comment
            {
                CommentId = 1, UserId = 2,
                Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForEditing + 1))
            };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

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

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

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

            // Act and Assert
            Assert.Throws<PermittedPeriodForEditingExpiredException>(
            () => target.Edit(comment.CommentId, comment.UserId, newCommentText));

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 15
0
        public void Edit_CommentHasNotBeenEditedYet_EditsTheComment()
        {
            // Arrange
            string newCommentText = "new_comment_text";
            string newValidatedCommentText = "new_validated_comment_text";
            Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

            CommentEdit commentEdit = null;

            commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>()))
            .Callback((Comment c) => commentEdit = c.CommentEdit);

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

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

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

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

            // Act
            target.Edit(comment.CommentId, comment.UserId, newCommentText);

            // Assert
            Assert.IsNotNull(commentEdit);
            Assert.IsTrue(new DateTime() != commentEdit.EditDate);

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(
            r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId && c.Text == newValidatedCommentText)),
            Times.Once);

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

            this._commentValidationMock.Verify(v => v.ValidateCommentText(newCommentText), Times.Once);
        }
Exemplo n.º 16
0
        public void Delete_UserIsNotAuthorOfTheComment_ThrowsDeletingCommentIsForbiddenException()
        {
            // Arrange
            int userId = 1;
            Comment comment = new Comment { CommentId = 2, UserId = userId + 1, Date = DateTime.Now };

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

            commentRepositoryMock.Setup(r => r.GetById(comment.CommentId))
            .Returns(comment);

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

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

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

            // Act and Assert
            Assert.Throws<DeletingCommentIsForbiddenException>(() => target.Delete(comment.CommentId, userId));

            commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once);
            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 17
0
        public void CanEdit_UserCanEditTheComment_ReturnsTrue()
        {
            // Arrange
            Comment comment = new Comment { UserId = 1, Date = DateTime.Now };

            // Arrange - create target
            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanEdit(comment.UserId, comment);

            //Assert
            Assert.IsTrue(result);
        }
Exemplo n.º 18
0
        public void Reply_CommentTreeLevelIsEqualToMaxCommentTreeLevel_ThrowsMaxCommentTreeLevelReachedException()
        {
            // Arrange
            int userId = 1;
            string commentText = "comment_text";
            Comment comment = new Comment { CommentId = 2, TreeLevel = (byte)this._serviceSettings.MaxCommentTreeLevel };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

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

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

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

            // Act and Assert
            Assert.Throws<MaxCommentTreeLevelReachedException>(() => target.Reply(comment.CommentId, userId, commentText));

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 19
0
        public void CanEdit_UserIsNotAuthorOfTheComment_ReturnsFalse()
        {
            // Arrange
            int userId = 1;
            Comment comment = new Comment { UserId = userId + 1 };

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanEdit(userId, comment);

            //Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 20
0
        public void Reply_NonexistentUserId_ThrowsUserNotFoundException()
        {
            // Arrange
            Comment comment = new Comment { CommentId = 1 };
            int userId = 2;
            string commentText = "comment_text";

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

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

            userRepositoryMock.Setup(r => r.GetById(userId))
            .Returns((User)null);

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

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

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

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

            // Act and Assert
            Assert.Throws<UserNotFoundException>(() => target.Reply(comment.CommentId, userId, commentText));

            commentRepositoryMock.Verify(
            r =>
            r.GetById(comment.CommentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never);

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

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 21
0
        public void CanHaveReply_CommentTreeLevelIsEqualToMaxCommentTreeLevel_ReturnsFalse()
        {
            // Arrange
            Comment comment = new Comment { TreeLevel = (byte)this._serviceSettings.MaxCommentTreeLevel };

            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanHaveReply(comment);

            // Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 22
0
        public void AddVote_UserHasVotedWithAnotherVoteType_EditsVote()
        {
            // Arrange
            int commentId = 1;
            int userId = 1;
            bool voteIsPositive = true;
            CommentVote[] commentVotes =
            {
                new CommentVote { CommentId = commentId, UserId = userId, IsPositive = !voteIsPositive },
                new CommentVote { CommentId = commentId, UserId = userId + 1 },
                new CommentVote { CommentId = commentId, UserId = userId + 2 }
            };

            Comment comment = new Comment { CommentId = commentId, CommentVotes = commentVotes.ToList() };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(commentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

            IEnumerable<CommentVote> newCommentVotes = null;

            commentRepositoryMock.Setup(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)))
            .Callback((Comment c) => newCommentVotes = c.CommentVotes);

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

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

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

            // Act
            target.AddVote(commentId, userId, voteIsPositive);

            // Assert
            Assert.IsNotNull(newCommentVotes);
            Assert.AreEqual(commentVotes.Count(), newCommentVotes.Count());

            CommentVote commentVote =
            newCommentVotes.FirstOrDefault(
            v => v.CommentId == commentId && v.UserId == userId && v.IsPositive == voteIsPositive);

            Assert.IsNotNull(commentVote);

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

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
0
        public void AddVote_UserHasVotedWithTheSameVoteType_DoNotUpdateTheVote()
        {
            // Arrange
            int commentId = 1;
            int userId = 1;
            bool voteIsPositive = true;

            Comment comment = new Comment
            {
                CommentId = commentId,
                CommentVotes = { new CommentVote { CommentId = commentId, UserId = userId, IsPositive = voteIsPositive } }
            };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(commentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

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

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

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

            // Act
            target.AddVote(commentId, userId, voteIsPositive);

            // Assert
            commentRepositoryMock.Verify(
            r =>
            r.GetById(commentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 25
0
        public void Create_NonexistentUserId_ThrowsUserNotFoundException()
        {
            // Arrange
            int userId = 1;
            int articleId = 2;
            string commentText = "comment_text";

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

            userRepositoryMock.Setup(r => r.GetById(userId))
            .Returns((User)null);

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

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

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

            // Act and Assert
            Assert.Throws<UserNotFoundException>(() => target.Create(userId, articleId, commentText));

            userRepositoryMock.Verify(r => r.GetById(userId), Times.Once);
        }
Exemplo n.º 26
0
        public void CanDelete_CommentIsDeleted_ReturnsFalse()
        {
            // Arrange
            Comment comment = new Comment { UserId = 1, Date = DateTime.Now, IsDeleted = true };

            // Arrange - create target
            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanDelete(comment.UserId, comment);

            //Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 27
0
        public void DeleteVote_AllCredentialsAreValid_DeletesCommentVote()
        {
            // Arrange
            int commentId = 1;
            int userId = 2;
            CommentVote[] commentVotes =
            {
                new CommentVote { CommentId = commentId, UserId = userId },
                new CommentVote { CommentId = commentId, UserId = userId + 1 },
                new CommentVote { CommentId = commentId, UserId = userId + 2 }
            };

            Comment comment = new Comment { CommentId = commentId, CommentVotes = commentVotes.ToList() };

            Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes };

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

            commentRepositoryMock.Setup(
            r =>
            r.GetById(commentId,
            It.Is<Expression<Func<Comment, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(comment);

            IEnumerable<CommentVote> newCommentVotes = null;

            commentRepositoryMock.Setup(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)))
            .Callback((Comment c) => newCommentVotes = c.CommentVotes);

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

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

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

            // Act
            target.DeleteVote(comment.CommentId, userId);

            // Assert
            Assert.AreEqual(commentVotes.Count() - 1, newCommentVotes.Count());
            Assert.AreEqual(0, newCommentVotes.Count(v => v.CommentId == commentId && v.UserId == userId));

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

            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
Exemplo n.º 28
0
        public void CanEdit_PermittedPeriodForEditingExpired_ReturnsFalse()
        {
            // Arrange
            Comment comment = new Comment
            { UserId = 1, Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForEditing + 1)) };

            // Arrange - create target
            ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act
            bool result = target.CanEdit(comment.UserId, comment);

            //Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 29
0
        public void Delete_AllCredentialsAreValid_DeletesTheComment()
        {
            // Arrange
            Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now };

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

            commentRepositoryMock.Setup(r => r.GetById(comment.CommentId))
            .Returns(comment);

            Comment deletedComment = null;

            commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>()))
            .Callback((Comment c) => deletedComment = c);

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

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

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

            // Act
            target.Delete(comment.CommentId, comment.UserId);

            // Assert
            Assert.IsNotNull(deletedComment);
            Assert.IsTrue(deletedComment.IsDeleted);

            commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once);
            commentRepositoryMock.Verify(
            r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId && c.UserId == comment.UserId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }
Exemplo n.º 30
0
        public void Delete_NonexistentCommentId_ThrowsCommentNotFoundException()
        {
            // Arrange
            int commentId = 1;
            int userId = 1;

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

            commentRepositoryMock.Setup(r => r.GetById(commentId))
            .Returns((Comment)null);

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

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

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

            // Act and Assert
            Assert.Throws<CommentNotFoundException>(() => target.Delete(commentId, userId));

            commentRepositoryMock.Verify(r => r.GetById(commentId), Times.Once);
            commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }