private async ValueTask <ExamFee> TryCatch(
            ReturningExamFeeFunction returningExamFeeFunction)
        {
            try
            {
                return(await returningExamFeeFunction());
            }

            catch (NullExamFeeException nullExamFeeException)
            {
                throw CreateAndLogValidationException(nullExamFeeException);
            }
            catch (InvalidExamFeeException invalidExamFeeInputException)
            {
                throw CreateAndLogValidationException(invalidExamFeeInputException);
            }
            catch (NotFoundExamFeeException nullExamFeeException)
            {
                throw CreateAndLogValidationException(nullExamFeeException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsExamFeeException =
                    new AlreadyExistsExamFeeException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsExamFeeException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidExamFeeReferenceException =
                    new InvalidExamFeeReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidExamFeeReferenceException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedExamFeeException = new LockedExamFeeException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedExamFeeException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedExamFeeServiceException =
                    new FailedExamFeeServiceException(exception);

                throw CreateAndLogServiceException(failedExamFeeServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            ExamFee        someExamFee          = CreateRandomExamFee(randomDateTime);

            someExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedExamFeeServiceException =
                new FailedExamFeeServiceException(serviceException);

            var expectedExamFeeServiceException =
                new ExamFeeServiceException(failedExamFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamFeeByIdAsync(someExamFee.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <ExamFee> modifyExamFeeTask =
                this.examFeeService.ModifyExamFeeAsync(someExamFee);

            // then
            await Assert.ThrowsAsync <ExamFeeServiceException>(() =>
                                                               modifyExamFeeTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamFeeByIdAsync(someExamFee.Id),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime    = GetRandomDateTime();
            ExamFee        someExamFee = CreateRandomExamFee(dateTime);

            someExamFee.UpdatedBy   = someExamFee.CreatedBy;
            someExamFee.UpdatedDate = someExamFee.CreatedDate;
            var serviceException = new Exception();

            var failedExamFeeServiceException =
                new FailedExamFeeServiceException(serviceException);

            var expectedExamFeeServiceException =
                new ExamFeeServiceException(failedExamFeeServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamFeeAsync(It.IsAny <ExamFee>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <ExamFee> createExamFeeTask =
                this.examFeeService.AddExamFeeAsync(someExamFee);

            // then
            await Assert.ThrowsAsync <ExamFeeServiceException>(() =>
                                                               createExamFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <ExamFee> TryCatch(
            ReturningExamFeesFunction returningExamFeesFunction)
        {
            try
            {
                return(returningExamFeesFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedExamFeeServiceException =
                    new FailedExamFeeServiceException(exception);

                throw CreateAndLogServiceException(failedExamFeeServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someExamFeeId    = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedExamFeeServiceException =
                new FailedExamFeeServiceException(serviceException);

            var expectedExamFeeServiceException =
                new ExamFeeServiceException(failedExamFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamFeeByIdAsync(someExamFeeId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <ExamFee> retrieveExamFeeByIdTask =
                this.examFeeService.RetrieveExamFeeByIdAsync(someExamFeeId);

            // then
            await Assert.ThrowsAsync <ExamFeeServiceException>(() =>
                                                               retrieveExamFeeByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamFeeByIdAsync(someExamFeeId),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllExamFeesWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedExamFeeServiceException =
                new FailedExamFeeServiceException(serviceException);

            var expectedExamFeeServiceException =
                new ExamFeeServiceException(failedExamFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllExamFees())
            .Throws(serviceException);

            // when
            Action retrieveAllExamFeesAction = () =>
                                               this.examFeeService.RetrieveAllExamFees();

            // then
            Assert.Throws <ExamFeeServiceException>(
                retrieveAllExamFeesAction);

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

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

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