private async ValueTask <Teacher> TryCatch(ReturningTeacherFunction returningTeacherFunction) { try { return(await returningTeacherFunction()); } catch (NullTeacherException nullTeacherException) { throw CreateAndLogValidationException(nullTeacherException); } catch (InvalidTeacherException invalidTeacherException) { throw CreateAndLogValidationException(invalidTeacherException); } catch (NotFoundTeacherException notFoundTeacherException) { throw CreateAndLogValidationException(notFoundTeacherException); } catch (SqlException sqlException) { var failedTeacherStorageExceptin = new FailedTeacherStorageException(sqlException); throw CreateAndLogCriticalDependencyException(failedTeacherStorageExceptin); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsTeacherException = new AlreadyExistsTeacherException(duplicateKeyException); throw CreateAndLogDependencyValidationException(alreadyExistsTeacherException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedTeacherException = new LockedTeacherException(dbUpdateConcurrencyException); throw CreateAndLogDependencyValidationException(lockedTeacherException); } catch (DbUpdateException dbUpdateException) { var failedTeacherStorageException = new FailedTeacherStorageException(dbUpdateException); throw CreateAndLogDependencyException(failedTeacherStorageException); } catch (Exception exception) { var failedTeacherServiceException = new FailedTeacherServiceException(exception); throw CreateAndLogServiceException(failedTeacherServiceException); } }
public async void ShouldThrowValidationExceptionOnCreateWhenTeacherAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Teacher randomTeacher = CreateRandomTeacher(dateTime); Teacher alreadyExistsTeacher = randomTeacher; alreadyExistsTeacher.UpdatedBy = alreadyExistsTeacher.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsTeacherException = new AlreadyExistsTeacherException(duplicateKeyException); var expectedTeacherValidationException = new TeacherValidationException(alreadyExistsTeacherException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertTeacherAsync(alreadyExistsTeacher)) .ThrowsAsync(duplicateKeyException); // when ValueTask <Teacher> registerTeacherTask = this.teacherService.CreateTeacherAsync(alreadyExistsTeacher); // then await Assert.ThrowsAsync <TeacherValidationException>(() => registerTeacherTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAsync(alreadyExistsTeacher), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedTeacherValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyValidationExceptionOnAddIfTeacherAlreadyExsitsAndLogItAsync() { // given var someTeacher = CreateRandomTeacher(); var alreadyExistsTeacher = someTeacher; string randomMessage = GetRandomMessage(); var duplicateKeyException = new DuplicateKeyException(randomMessage); var alreadyExistsTeacherException = new AlreadyExistsTeacherException(duplicateKeyException); var expectedTeacherDependencyValidationException = new TeacherDependencyValidationException(alreadyExistsTeacherException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Throws(duplicateKeyException); // when ValueTask <Teacher> addTeacherTask = this.teacherService.CreateTeacherAsync(alreadyExistsTeacher); // then await Assert.ThrowsAsync <TeacherDependencyValidationException>(() => addTeacherTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherDependencyValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAsync(It.IsAny <Teacher>()), Times.Never()); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }