public async void ShouldThrowValidationExceptionOnCreateWhenSemesterCourseAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(dateTime); SemesterCourse alreadyExistsSemesterCourse = randomSemesterCourse; alreadyExistsSemesterCourse.UpdatedBy = alreadyExistsSemesterCourse.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsSemesterCourseException = new AlreadyExistsSemesterCourseException(duplicateKeyException); var expectedSemesterCourseValidationException = new SemesterCourseValidationException(alreadyExistsSemesterCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertSemesterCourseAsync(alreadyExistsSemesterCourse)) .ThrowsAsync(duplicateKeyException); // when ValueTask <SemesterCourse> createSemesterCourseTask = this.semesterCourseService.CreateSemesterCourseAsync(alreadyExistsSemesterCourse); // then await Assert.ThrowsAsync <SemesterCourseValidationException>(() => createSemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertSemesterCourseAsync(alreadyExistsSemesterCourse), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <SemesterCourse> TryCatch( ReturningSemesterCourseFunction returningSemesterCourseFunction) { try { return(await returningSemesterCourseFunction()); } catch (NullSemesterCourseException nullSemesterCourseException) { throw CreateAndLogValidationException(nullSemesterCourseException); } catch (InvalidSemesterCourseException invalidSemesterCourseInputException) { throw CreateAndLogValidationException(invalidSemesterCourseInputException); } catch (NotFoundSemesterCourseException nullSemesterCourseException) { throw CreateAndLogValidationException(nullSemesterCourseException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsSemesterCourseException = new AlreadyExistsSemesterCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsSemesterCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedSemesterCourseException = new LockedSemesterCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedSemesterCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedSemesterCourseServiceException = new FailedSemesterCourseServiceException(exception); throw CreateAndLogServiceException(failedSemesterCourseServiceException); } }