コード例 #1
0
ファイル: CommentService.cs プロジェクト: Yura95st/Doctrine
        public bool CanDelete(int userId, Comment comment)
        {
            Guard.IntMoreThanZero(userId, "userId");
            Guard.NotNull(comment, "comment");

            return (!comment.IsDeleted && comment.UserId == userId
                    && !CommentService.IsPermittedPeriodExpired(comment.Date,
                    this._serviceSettings.PermittedPeriodForDeleting));
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: CommentService.cs プロジェクト: Yura95st/Doctrine
        public Comment Create(int userId, int articleId, string commentText)
        {
            Guard.IntMoreThanZero(userId, "userId");
            Guard.IntMoreThanZero(articleId, "articleId");
            Guard.NotNullOrEmpty(commentText, "commentText");

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

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

            Article article = this._unitOfWork.ArticleRepository.GetById(articleId);

            if (article == null)
            {
                throw new ArticleNotFoundException(String.Format("Article with ID '{0}' was not found.", articleId));
            }

            // Validate comment's text
            string validatedCommentText = this._commentValidation.ValidateCommentText(commentText);

            Comment comment = new Comment
            { ArticleId = article.ArticleId, UserId = user.UserId, Text = validatedCommentText, Date = DateTime.Now };

            this._unitOfWork.CommentRepository.Insert(comment);
            this._unitOfWork.Save();

            return comment;
        }
コード例 #5
0
        public void AddVote_NonexistentUserId_ThrowsUserNotFoundException()
        {
            // Arrange
            int commentId = 1;
            int userId = 1;
            bool voteIsPositive = true;

            Comment comment = new Comment { CommentId = commentId, CommentVotes = new List<CommentVote>() };

            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 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 - create target
            ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object,
            this._serviceSettings);

            // Act and Assert
            Assert.Throws<UserNotFoundException>(() => 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);

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

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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));
        }
コード例 #9
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);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
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);
        }
コード例 #13
0
ファイル: CommentService.cs プロジェクト: Yura95st/Doctrine
        public Comment Reply(int commentId, int userId, string commentText)
        {
            Guard.IntMoreThanZero(commentId, "commentId");
            Guard.IntMoreThanZero(userId, "userId");
            Guard.NotNullOrEmpty(commentText, "commentText");

            Comment comment = this._unitOfWork.CommentRepository.GetById(commentId, c => c.Comment1);

            if (comment == null)
            {
                throw new CommentNotFoundException(String.Format("Comment with ID '{0}' was not found.", commentId));
            }

            if (comment.TreeLevel >= this._serviceSettings.MaxCommentTreeLevel)
            {
                throw new MaxCommentTreeLevelReachedException(
                String.Format(
                "Tree level value of the comment with ID '{0}' is equal to the maximum comments' tree level value ('{1}').",
                commentId, this._serviceSettings.MaxCommentTreeLevel));
            }

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

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

            // Validate comment's text
            string validatedCommentText = this._commentValidation.ValidateCommentText(commentText);

            Comment replyComment = new Comment
            { ArticleId = comment.ArticleId, UserId = user.UserId, Text = validatedCommentText, Date = DateTime.Now };

            comment.Comment1.Add(replyComment);

            this._unitOfWork.CommentRepository.Update(comment);
            this._unitOfWork.Save();

            return replyComment;
        }
コード例 #14
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);
        }
コード例 #15
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);
        }
コード例 #16
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);
        }
コード例 #17
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);
        }
コード例 #18
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);
        }
コード例 #19
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);
        }
コード例 #20
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);
        }
コード例 #21
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);
        }
コード例 #22
0
        public void DeleteVote_NonexistentCommentVote_ThrowsCommentVoteNotFoundException()
        {
            // Arrange
            int userId = 1;
            int commentId = 2;

            Comment comment = new Comment
            {
                CommentId = commentId,
                CommentVotes =
                {
                    new CommentVote { CommentId = commentId, UserId = userId + 1 },
                    new CommentVote { CommentId = commentId, UserId = userId + 2 }
                }
            };

            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 and Assert
            Assert.Throws<CommentVoteNotFoundException>(() => target.DeleteVote(commentId, 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 == commentId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
コード例 #23
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);
        }
コード例 #24
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);
        }
コード例 #25
0
ファイル: CommentService.cs プロジェクト: Yura95st/Doctrine
        public bool CanHaveReply(Comment comment)
        {
            Guard.NotNull(comment, "comment");

            return comment.TreeLevel < this._serviceSettings.MaxCommentTreeLevel;
        }