public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomCourseId = Guid.NewGuid(); Guid inputCourseId = randomCourseId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCourseException = new LockedCourseException(databaseUpdateConcurrencyException); var expectedCourseDependencyException = new CourseDependencyException(lockedCourseException); this.storageBrokerMock.Setup(broker => broker.SelectCourseByIdAsync(inputCourseId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Course> deleteCourseTask = this.courseService.DeleteCourseAsync(inputCourseId); // then await Assert.ThrowsAsync <CourseDependencyException>(() => deleteCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(inputCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Course> TryCatch(ReturningCourseFunction returningCourseFunction) { try { return(await returningCourseFunction()); } catch (InvalidCourseInputException invalidCourseInputException) { throw CreateAndLogValidationException(invalidCourseInputException); } catch (NotFoundCourseException notFoundCourseException) { throw CreateAndLogValidationException(notFoundCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
private IQueryable <Course> TryCatch(ReturningQueryableCourseFunction returningQueryableCourseFunction) { try { return(returningQueryableCourseFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCourseException = new AlreadyExistsCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCourseException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(randomDateTime); Course someCourse = randomCourse; someCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCourseException = new LockedCourseException(databaseUpdateConcurrencyException); var expectedCourseDependencyException = new CourseDependencyException(lockedCourseException); this.storageBrokerMock.Setup(broker => broker.SelectCourseByIdAsync(someCourse.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(someCourse); // then await Assert.ThrowsAsync <CourseDependencyException>(() => modifyCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCourseByIdAsync(someCourse.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Course> TryCatch(ReturningCourseFunction returningCourseFunction) { try { return(await returningCourseFunction()); } catch (NullCourseException nullCourseException) { throw CreateAndLogValidationException(nullCourseException); } catch (InvalidCourseException invalidCourseException) { throw CreateAndLogValidationException(invalidCourseException); } catch (NotFoundCourseException nullCourseException) { throw CreateAndLogValidationException(nullCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCourseException = new AlreadyExistsCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCourseException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedCourseServiceException = new FailedCourseServiceException(exception); throw CreateAndLogServiceException(failedCourseServiceException); } }