public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(randomDateTime); Guardian someGuardian = randomGuardian; someGuardian.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(someGuardian.Id)) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(someGuardian); // then await Assert.ThrowsAsync <GuardianServiceException>(() => modifyGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(someGuardian.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; inputGuardian.UpdatedBy = inputGuardian.CreatedBy; var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAsync(inputGuardian)) .ThrowsAsync(serviceException); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianServiceException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(inputGuardian), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Guardian> TryCatch(ReturningGuardianFunction returningGuardianFunction) { try { return(await returningGuardianFunction()); } catch (NullGuardianException nullGuardianException) { throw CreateAndLogValidationException(nullGuardianException); } catch (InvalidGuardianException invalidGuardianException) { throw CreateAndLogValidationException(invalidGuardianException); } catch (NotFoundGuardianException notFoundGuardianException) { throw CreateAndLogValidationException(notFoundGuardianException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianException = new AlreadyExistsGuardianException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedGuardianException = new LockedGuardianException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedGuardianException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedGuardianServiceException = new FailedGuardianServiceException(exception); throw CreateAndLogServiceException(failedGuardianServiceException); } }
private IQueryable <Guardian> TryCatch(ReturningQueryableGuardianFunction returningQueryableGuardianFunction) { try { return(returningQueryableGuardianFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedGuardianServiceException = new FailedGuardianServiceException(exception); throw CreateAndLogServiceException(failedGuardianServiceException); } }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomGuardianId = Guid.NewGuid(); Guid inputGuardianId = randomGuardianId; var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(inputGuardianId)) .ThrowsAsync(serviceException); // when ValueTask <Guardian> deleteGuardianTask = this.guardianService.RemoveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianServiceException>(() => deleteGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(inputGuardianId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedGuardianServiceException = new FailedGuardianServiceException(serviceException); var expectedGuardianServiceException = new GuardianServiceException(failedGuardianServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllGuardians()) .Throws(serviceException); // when Action retrieveAllGuardiansAction = () => this.guardianService.RetrieveAllGuardians(); // then Assert.Throws <GuardianServiceException>( retrieveAllGuardiansAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllGuardians(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }