public async void ShouldThrowValidationExceptionOnCreateWhenStudentExamFeeAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentExamFee randomStudentExamFee = CreateRandomStudentExamFee(dateTime); StudentExamFee alreadyExistsStudentExamFee = randomStudentExamFee; alreadyExistsStudentExamFee.UpdatedBy = alreadyExistsStudentExamFee.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsStudentExamFeeException = new AlreadyExistsStudentExamFeeException(duplicateKeyException); var expectedExamFeeValidationException = new StudentExamFeeValidationException(alreadyExistsStudentExamFeeException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentExamFeeAsync(alreadyExistsStudentExamFee)) .ThrowsAsync(duplicateKeyException); // when ValueTask <StudentExamFee> createStudentExamFeeTask = this.studentExamFeeService.AddStudentExamFeeAsync(alreadyExistsStudentExamFee); // then await Assert.ThrowsAsync <StudentExamFeeValidationException>(() => createStudentExamFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentExamFeeAsync(alreadyExistsStudentExamFee), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamFeeValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentExamFee> TryCatch( ReturningStudentExamFeeFunction returningStudentExamFeeFunction) { try { return(await returningStudentExamFeeFunction()); } catch (NullStudentExamFeeException nullStudentExamFeeException) { throw CreateAndLogValidationException(nullStudentExamFeeException); } catch (NotFoundStudentExamFeeException notFoundStudentExamFeeException) { throw CreateAndLogValidationException(notFoundStudentExamFeeException); } catch (InvalidStudentExamFeeException invalidStudentExamFeeInputException) { throw CreateAndLogValidationException(invalidStudentExamFeeInputException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentExamFeeException = new AlreadyExistsStudentExamFeeException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentExamFeeException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedStudentExamFeeException = new LockedStudentExamFeeException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedStudentExamFeeException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(exception); throw CreateAndLogServiceException(failedStudentExamFeeServiceException); } }