public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(randomDateTime); StudentGuardian someStudentGuardian = randomStudentGuardian; someStudentGuardian.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentGuardianException = new LockedStudentGuardianException(databaseUpdateConcurrencyException); var expectedStudentGuardianDependencyException = new StudentGuardianDependencyException(lockedStudentGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectStudentGuardianByIdAsync (someStudentGuardian.StudentId, someStudentGuardian.GuardianId)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentGuardian> modifyStudentGuardianTask = this.studentGuardianService.ModifyStudentGuardianAsync(someStudentGuardian); // then await Assert.ThrowsAsync <StudentGuardianDependencyException>(() => modifyStudentGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentGuardianByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentGuardianDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentGuardian> TryCatch( ReturningStudentGuardianFunction returningStudentGuardianFunction) { try { return(await returningStudentGuardianFunction()); } catch (NullStudentGuardianException nullStudentGuardianException) { throw CreateAndLogValidationException(nullStudentGuardianException); } catch (InvalidStudentGuardiantException invalidStudentGuardianInputException) { throw CreateAndLogValidationException(invalidStudentGuardianInputException); } catch (NotFoundStudentGuardianException notFoundStudentGuardianException) { throw CreateAndLogValidationException(notFoundStudentGuardianException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianException = new AlreadyExistsStudentGuardianException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedSemesterCourseException = new LockedStudentGuardianException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedSemesterCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentGuardianServiceException = new FailedStudentGuardianServiceException(exception); throw CreateAndLogServiceException(failedStudentGuardianServiceException); } }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomStudentGuardianId = Guid.NewGuid(); Guid randomStudentId = Guid.NewGuid(); Guid inputStudentGuardianId = randomStudentGuardianId; Guid inputStudentId = randomStudentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentGuardianException = new LockedStudentGuardianException(databaseUpdateConcurrencyException); var expectedStudentGuardianException = new StudentGuardianDependencyException(lockedStudentGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <StudentGuardian> deleteStudentGuardianTask = this.studentGuardianService.DeleteStudentGuardianAsync(inputStudentGuardianId, inputStudentId); // then await Assert.ThrowsAsync <StudentGuardianDependencyException>(() => deleteStudentGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentGuardianException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }