public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomExamFeeId = Guid.NewGuid();
            Guid inputExamFeeId  = randomExamFeeId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedExamFeeException = new LockedExamFeeException(databaseUpdateConcurrencyException);

            var expectedExamFeeException = new ExamFeeDependencyException(lockedExamFeeException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamFeeByIdAsync(inputExamFeeId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <ExamFee> deleteExamFeeTask =
                this.examFeeService.RemoveExamFeeByIdAsync(inputExamFeeId);

            // then
            await Assert.ThrowsAsync <ExamFeeDependencyException>(() => deleteExamFeeTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        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 ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            ExamFee        someExamFee          = CreateRandomExamFee(randomDateTime);

            someExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedExamFeeException             = new LockedExamFeeException(databaseUpdateConcurrencyException);

            var expectedExamFeeDependencyException =
                new ExamFeeDependencyException(lockedExamFeeException);

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

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

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

            // then
            await Assert.ThrowsAsync <ExamFeeDependencyException>(() =>
                                                                  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(
                                                                    expectedExamFeeDependencyException))),
                                          Times.Once);

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