public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(randomDateTime); Attendance someAttendance = randomAttendance; someAttendance.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedAttendanceServiceException = new FailedAttendanceServiceException(serviceException); var expectedAttendanceServiceException = new AttendanceServiceException(failedAttendanceServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(someAttendance.Id)) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Attendance> modifyAttendanceTask = this.attendanceService.ModifyAttendanceAsync(someAttendance); // then await Assert.ThrowsAsync <AttendanceServiceException>(() => 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( expectedAttendanceServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(dateTime); Attendance inputAttendance = randomAttendance; inputAttendance.UpdatedBy = inputAttendance.CreatedBy; var serviceException = new Exception(); var failedAttendanceServiceException = new FailedAttendanceServiceException(serviceException); var expectedAttendanceServiceException = new AttendanceServiceException(failedAttendanceServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertAttendanceAsync(inputAttendance)) .ThrowsAsync(serviceException); // when ValueTask <Attendance> createAttendanceTask = this.attendanceService.CreateAttendanceAsync(inputAttendance); // then await Assert.ThrowsAsync <AttendanceServiceException>(() => createAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAttendanceAsync(inputAttendance), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.AtLeastOnce); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.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); } }
private IQueryable <Attendance> TryCatch(ReturningQueryableAttendanceFunction returningQueryableAttendanceFunction) { try { return(returningQueryableAttendanceFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedAttendanceServiceException = new FailedAttendanceServiceException(exception); throw CreateAndLogServiceException(failedAttendanceServiceException); } }
public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync() { // given Guid someAttendanceId = Guid.NewGuid(); var serviceException = new Exception(); var failedAttendanceServiceException = new FailedAttendanceServiceException(serviceException); var expectedAttendanceServiceException = new AttendanceServiceException(failedAttendanceServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(someAttendanceId)) .ThrowsAsync(serviceException); // when ValueTask <Attendance> retrieveAttendanceByIdTask = this.attendanceService.RetrieveAttendanceByIdAsync(someAttendanceId); // then await Assert.ThrowsAsync <AttendanceServiceException>(() => retrieveAttendanceByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(someAttendanceId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedAttendanceServiceException = new FailedAttendanceServiceException(serviceException); var expectedAttendanceServiceException = new AttendanceServiceException(failedAttendanceServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllAttendances()) .Throws(serviceException); // when Action retrieveAllAttendancesAction = () => this.attendanceService.RetrieveAllAttendances(); // then Assert.Throws <AttendanceServiceException>( retrieveAllAttendancesAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllAttendances(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }