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); } }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(randomDateTime); StudentSemesterCourse someStudentSemesterCourse = randomStudentSemesterCourse; someStudentSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentSemesterCourseException = new LockedStudentSemesterCourseException(databaseUpdateConcurrencyException); var expectedStudentSemesterCourseDependencyException = new StudentSemesterCourseDependencyException(lockedStudentSemesterCourseException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync (It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentSemesterCourse> modifyStudentSemesterCourseTask = this.studentSemesterCourseService.ModifyStudentSemesterCourseAsync(someStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseDependencyException>(() => modifyStudentSemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomSemesterCourseId = Guid.NewGuid(); Guid randomStudentId = Guid.NewGuid(); Guid inputSemesterCourseId = randomSemesterCourseId; Guid inputStudentId = randomStudentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedSemesterCourseException = new LockedStudentSemesterCourseException(databaseUpdateConcurrencyException); var expectedStudentSemesterCourseException = new StudentSemesterCourseDependencyException(lockedSemesterCourseException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync(inputSemesterCourseId, inputStudentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <StudentSemesterCourse> deleteStudentSemesterCourseTask = this.studentSemesterCourseService.RemoveStudentSemesterCourseByIdsAsync(inputSemesterCourseId, inputStudentId); // then await Assert.ThrowsAsync <StudentSemesterCourseDependencyException>(() => deleteStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentSemesterCourseException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(inputSemesterCourseId, inputStudentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }