private async ValueTask <Comment> TryCatch(
            ReturningCommentFunction returningCommentFunction)
        {
            try
            {
                return(await returningCommentFunction());
            }
            catch (NullCommentException nullCommentException)
            {
                throw CreateAndLogValidationException(nullCommentException);
            }
            catch (InvalidCommentException invalidCommentException)
            {
                throw CreateAndLogValidationException(invalidCommentException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCommentException = new LockedCommentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCommentException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCommentException =
                    new AlreadyExistsCommentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCommentException);
            }
            catch (NotFoundCommentException notFoundCommentException)
            {
                throw CreateAndLogValidationException(notFoundCommentException);
            }
            catch (InvalidCommentInputException invalidCommentInputException)
            {
                throw CreateAndLogValidationException(invalidCommentInputException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Comment        randomComment        = CreateRandomComment(randomDateTime);
            Comment        someComment          = randomComment;

            someComment.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedCommentException             = new LockedCommentException(databaseUpdateConcurrencyException);

            var expectedCommentDependencyException =
                new CommentDependencyException(lockedCommentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(someComment.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCommentId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedCommentException =
                new LockedCommentException(databaseUpdateConcurrencyException);

            var expectedCommentDependencyException =
                new CommentDependencyException(lockedCommentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // then
            await Assert.ThrowsAsync <CommentDependencyException>(() =>
                                                                  retrieveByIdCommentTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCommentId = Guid.NewGuid();
            Guid inputCommentId  = randomCommentId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedCommentException =
                new LockedCommentException(databaseUpdateConcurrencyException);

            var expectedStudentCommentException =
                new CommentDependencyException(lockedCommentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCommentByIdAsync(inputCommentId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // then
            await Assert.ThrowsAsync <CommentDependencyException>(() => deleteStudentCommentTask.AsTask());

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

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

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