Exemple #1
0
        public async void ShouldThrowValidationExceptionOnModifyWhenCommentIsNullAndLogItAsync()
        {
            // given
            Comment randomComment        = null;
            Comment nullComment          = randomComment;
            var     nullCommentException = new NullCommentException();

            var expectedCommentValidationException =
                new CommentValidationException(nullCommentException);

            // when
            ValueTask <Comment> modifyCommentTask =
                this.commentService.ModifyCommentAsync(nullComment);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  modifyCommentTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCommentByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        public async Task ShouldThrowValidationExceptionOnModifyWhenCommentBodyIsInvalidAndLogItAsync(
            string invalidCommentBody)
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Comment        randomComment  = CreateRandomComment(dateTime);
            Comment        invalidComment = randomComment;

            invalidComment.Body = invalidCommentBody;

            var invalidCommentException = new InvalidCommentException(
                parameterName: nameof(Comment.Body),
                parameterValue: invalidComment.Body);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentException);

            // when
            ValueTask <Comment> modifyCommentTask =
                this.commentService.ModifyCommentAsync(invalidComment);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  modifyCommentTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private CommentValidationException CreateAndLogValidationException(Exception exception)
        {
            var CommentValidationException = new CommentValidationException(exception);

            this.loggingBroker.LogError((Exception)CommentValidationException);

            return(CommentValidationException);
        }
Exemple #4
0
        public async void ShouldThrowValidationExceptionOnAddWhenCommentAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            Comment        randomComment        = CreateRandomComment(dateTime);
            Comment        alreadyExistsComment = randomComment;

            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsCommentException =
                new AlreadyExistsCommentException(duplicateKeyException);

            var expectedCommentValidationException =
                new CommentValidationException(alreadyExistsCommentException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCommentAsync(alreadyExistsComment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Comment> createCommentTask =
                this.commentService.AddCommentAsync(alreadyExistsComment);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  createCommentTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCommentAsync(alreadyExistsComment),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #5
0
        public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(int minutes)
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Comment        randomComment = CreateRandomComment(dateTime);
            Comment        inputComment  = randomComment;

            inputComment.CreatedDate = dateTime.AddMinutes(minutes);
            inputComment.UpdatedDate = inputComment.CreatedDate;

            var invalidCommentValidationException = new InvalidCommentException(
                parameterName: nameof(Comment.CreatedDate),
                parameterValue: inputComment.CreatedDate);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentValidationException);

            // when
            ValueTask <Comment> createCommentTask =
                this.commentService.AddCommentAsync(inputComment);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  createCommentTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCommentAsync(It.IsAny <Comment>()),
                                          Times.Never);


            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenStorageCommentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            Comment        randomComment      = CreateRandomComment(dateTime);
            Guid           inputCommentId     = randomComment.Id;
            Comment        inputComment       = randomComment;
            Comment        nullStorageComment = null;

            var notFoundCommentException = new NotFoundCommentException(inputCommentId);

            var expectedCommentValidationException =
                new CommentValidationException(notFoundCommentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(inputCommentId))
            .ReturnsAsync(nullStorageComment);

            // when
            ValueTask <Comment> actualCommentTask =
                this.commentService.RemoveCommentByIdAsync(inputCommentId);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() => actualCommentTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCommentByIdAsync(inputCommentId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteCommentAsync(It.IsAny <Comment>()),
                                          Times.Never);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #7
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenStorageCommentIsNullAndLogItAsync()
        {
            // given
            Guid    randomCommentId          = Guid.NewGuid();
            Guid    someCommentId            = randomCommentId;
            Comment invalidStorageComment    = null;
            var     notFoundCommentException = new NotFoundCommentException(someCommentId);

            var exceptionCommentValidationException =
                new CommentValidationException(notFoundCommentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(invalidStorageComment);

            // when
            ValueTask <Comment> retrieveCommentByIdTask =
                this.commentService.RetrieveCommentByIdAsync(someCommentId);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  retrieveCommentByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(exceptionCommentValidationException))),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Never);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCommentByIdAsync(It.IsAny <Guid>()),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #8
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomCommentId = default;
            Guid inputCommentId  = randomCommentId;

            var invalidCommentInputException = new InvalidCommentInputException(
                parameterName: nameof(Comment.Id),
                parameterValue: inputCommentId);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentInputException);

            // when
            ValueTask <Comment> retrieveCommentByIdTask =
                this.commentService.RetrieveCommentByIdAsync(inputCommentId);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  retrieveCommentByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Never);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCommentByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #9
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCommentIdIsInvalidAndLogItAsync()
        {
            //given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Comment        randomComment = CreateRandomComment(dateTime);
            Comment        inputComment  = randomComment;

            inputComment.Id = default;

            var invalidCommentInputException = new InvalidCommentException(
                parameterName: nameof(Comment.Id),
                parameterValue: inputComment.Id);

            var expectedCommentValidationException =
                new CommentValidationException(invalidCommentInputException);

            // when
            ValueTask <Comment> registerCommentTask =
                this.commentService.AddCommentAsync(inputComment);

            // then
            await Assert.ThrowsAsync <CommentValidationException>(() =>
                                                                  registerCommentTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCommentAsync(It.IsAny <Comment>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }