public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomStudentExamId = Guid.NewGuid(); Guid inputStudentExamId = randomStudentExamId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentExamException = new LockedStudentExamException(databaseUpdateConcurrencyException); var expectedStudentExamException = new StudentExamDependencyException(lockedStudentExamException); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamByIdAsync(inputStudentExamId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <StudentExam> deleteStudentExamTask = this.studentExamService.RemoveStudentExamByIdAsync(inputStudentExamId); // then await Assert.ThrowsAsync <StudentExamDependencyException>(() => deleteStudentExamTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamByIdAsync(inputStudentExamId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentExam randomStudentExam = CreateRandomStudentExam(randomDateTime); StudentExam someStudentExam = randomStudentExam; someStudentExam.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentExamException = new LockedStudentExamException(databaseUpdateConcurrencyException); var expectedStudentExamDependencyException = new StudentExamDependencyException(lockedStudentExamException); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamByIdAsync (someStudentExam.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentExam> modifyStudentExamTask = this.studentExamService.ModifyStudentExamAsync(someStudentExam); // then await Assert.ThrowsAsync <StudentExamDependencyException>(() => modifyStudentExamTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamByIdAsync(It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentExam> TryCatch( ReturningStudentExamFunction returningStudentExamFunction) { try { return(await returningStudentExamFunction()); } catch (NullStudentExamException nullStudentExamException) { throw CreateAndLogValidationException(nullStudentExamException); } catch (InvalidStudentExamException invalidStudentExamException) { throw CreateAndLogValidationException(invalidStudentExamException); } catch (NotFoundStudentExamException nullStudentExamException) { throw CreateAndLogValidationException(nullStudentExamException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedStudentExamException = new LockedStudentExamException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedStudentExamException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentExamServiceException = new FailedStudentExamServiceException(exception); throw CreateAndLogServiceException(failedStudentExamServiceException); } }