public async void ShouldThrowValidationExceptionOnAddWhenStudentGuardianAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime); StudentGuardian alreadyExistsStudentGuardian = randomStudentGuardian; alreadyExistsStudentGuardian.UpdatedBy = alreadyExistsStudentGuardian.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsStudentGuardianException = new AlreadyExistsStudentGuardianException(duplicateKeyException); var expectedStudentGuardianValidationException = new StudentGuardianValidationException(alreadyExistsStudentGuardianException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentGuardianAsync(alreadyExistsStudentGuardian)) .ThrowsAsync(duplicateKeyException); // when ValueTask <StudentGuardian> createStudentGuardianTask = this.studentGuardianService.AddStudentGuardianAsync(alreadyExistsStudentGuardian); // then await Assert.ThrowsAsync <StudentGuardianValidationException>(() => createStudentGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentGuardianValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentGuardianAsync(alreadyExistsStudentGuardian), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentGuardian> TryCatch( ReturningStudentGuardianFunction returningStudentGuardianFunction) { try { return(await returningStudentGuardianFunction()); } catch (NullStudentGuardianException nullStudentGuardianException) { throw CreateAndLogValidationException(nullStudentGuardianException); } catch (InvalidStudentGuardiantException invalidStudentGuardianInputException) { throw CreateAndLogValidationException(invalidStudentGuardianInputException); } catch (NotFoundStudentGuardianException notFoundStudentGuardianException) { throw CreateAndLogValidationException(notFoundStudentGuardianException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianException = new AlreadyExistsStudentGuardianException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedSemesterCourseException = new LockedStudentGuardianException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedSemesterCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentGuardianServiceException = new FailedStudentGuardianServiceException(exception); throw CreateAndLogServiceException(failedStudentGuardianServiceException); } }