public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomAttendanceId = Guid.NewGuid(); Guid inputAttendanceId = randomAttendanceId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttendanceException = new LockedAttendanceException(databaseUpdateConcurrencyException); var expectedAttendanceDependencyException = new AttendanceDependencyException(lockedAttendanceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Attendance> deleteAttendanceTask = this.attendanceService.RemoveAttendanceByIdAsync(inputAttendanceId); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => deleteAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttendanceDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(randomDateTime); Attendance someAttendance = randomAttendance; someAttendance.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttendanceException = new LockedAttendanceException(databaseUpdateConcurrencyException); var expectedAttendanceDependencyException = new AttendanceDependencyException(lockedAttendanceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(someAttendance.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Attendance> modifyAttendanceTask = this.attendanceService.ModifyAttendanceAsync(someAttendance); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => modifyAttendanceTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(someAttendance.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Attendance> TryCatch(ReturningAttendanceFunction returningAttendanceFunction) { try { return(await returningAttendanceFunction()); } catch (NullAttendanceException nullAttendanceException) { throw CreateAndLogValidationException(nullAttendanceException); } catch (InvalidAttendanceException invalidAttendanceException) { throw CreateAndLogValidationException(invalidAttendanceException); } catch (NotFoundAttendanceException notFoundAttendanceException) { throw CreateAndLogValidationException(notFoundAttendanceException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsAttendanceException = new AlreadyExistsAttendanceException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsAttendanceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedAttendanceException = new LockedAttendanceException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedAttendanceException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedAttendanceServiceException = new FailedAttendanceServiceException(exception); throw CreateAndLogServiceException(failedAttendanceServiceException); } }