public async void ShouldThrowValidationExceptionOnCreateWhenStudentSemesterCourseAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime); StudentSemesterCourse alreadyExistsStudentSemesterCourse = randomStudentSemesterCourse; alreadyExistsStudentSemesterCourse.UpdatedBy = alreadyExistsStudentSemesterCourse.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsStudentSemesterCourseException = new AlreadyExistsStudentSemesterCourseException(duplicateKeyException); var expectedStudentSemesterCourseValidationException = new StudentSemesterCourseValidationException(alreadyExistsStudentSemesterCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentSemesterCourseAsync(alreadyExistsStudentSemesterCourse)) .ThrowsAsync(duplicateKeyException); // when ValueTask <StudentSemesterCourse> createStudentSemesterCourseTask = this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(alreadyExistsStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseValidationException>(() => createStudentSemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentSemesterCourseAsync(alreadyExistsStudentSemesterCourse), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentSemesterCourse> TryCatch( ReturningStudentSemesterCourseFunction returningStudentSemesterCourseFunction) { try { return(await returningStudentSemesterCourseFunction()); } catch (NullStudentSemesterCourseException nullStudentSemesterCourseException) { throw CreateAndLogValidationException(nullStudentSemesterCourseException); } catch (NotFoundStudentSemesterCourseException notFoundStudentSemesterCourseException) { throw CreateAndLogValidationException(notFoundStudentSemesterCourseException); } catch (InvalidStudentSemesterCourseInputException invalidStudentSemesterCourseInputException) { throw CreateAndLogValidationException(invalidStudentSemesterCourseInputException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentSemesterCourseException = new AlreadyExistsStudentSemesterCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentSemesterCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedSemesterCourseException = new LockedStudentSemesterCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedSemesterCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(exception); throw CreateAndLogServiceException(failedStudentSemesterCourseServiceException); } }