private async ValueTask <Exam> TryCatch(ReturningExamFunction returningExamFunction)
        {
            try
            {
                return(await returningExamFunction());
            }
            catch (NullExamException nullExamException)
            {
                throw CreateAndLogValidationException(nullExamException);
            }
            catch (InvalidExamException invalidExamInputException)
            {
                throw CreateAndLogValidationException(invalidExamInputException);
            }
            catch (NotFoundExamException nullExamException)
            {
                throw CreateAndLogValidationException(nullExamException);
            }
            catch (SqlException sqlException)
            {
                var failedExamStorageException =
                    new FailedExamStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedExamStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsExamException =
                    new AlreadyExistsExamException(duplicateKeyException);

                throw CreateAndLogDependencyValidationException(alreadyExistsExamException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedExamException = new LockedExamException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedExamException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedExamStorageException =
                    new FailedExamStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedExamStorageException);
            }
            catch (Exception exception)
            {
                var failedExamServiceException =
                    new FailedExamServiceException(exception);

                throw CreateAndLogServiceException(failedExamServiceException);
            }
        }
Exemplo n.º 2
0
        public async void ShouldThrowValidationExceptionOnAddWhenExamAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime          = GetRandomDateTime();
            Exam           randomExam        = CreateRandomExam(dateTime);
            Exam           alreadyExistsExam = randomExam;

            alreadyExistsExam.UpdatedBy = alreadyExistsExam.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsExamException =
                new AlreadyExistsExamException(duplicateKeyException);

            var expectedExamValidationException =
                new ExamValidationException(alreadyExistsExamException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamAsync(alreadyExistsExam))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Exam> createExamTask =
                this.examService.AddExamAsync(alreadyExistsExam);

            // then
            await Assert.ThrowsAsync <ExamValidationException>(() =>
                                                               createExamTask.AsTask());

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertExamAsync(alreadyExistsExam),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowDependencyValidationExceptionOnAddIfExamAlreadyExistsAndLogItAsync()
        {
            // given
            Exam   someExam         = CreateRandomExam();
            string exceptionMessage = GetRandomMessage();

            var duplicateKeyException =
                new DuplicateKeyException(exceptionMessage);

            var alreadyExistsExamException =
                new AlreadyExistsExamException(duplicateKeyException);

            var expectedExamDependencyValidationException =
                new ExamDependencyValidationException(alreadyExistsExamException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(duplicateKeyException);

            // when
            ValueTask <Exam> addExamTask =
                this.examService.AddExamAsync(someExam);

            // then
            await Assert.ThrowsAsync <ExamDependencyValidationException>(() =>
                                                                         addExamTask.AsTask());

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

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

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

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