public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedGuardianContactServiceException = new FailedGuardianContactServiceException(serviceException); var expectedGuardianContactServiceException = new GuardianContactServiceException(failedGuardianContactServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllGuardianContacts()) .Throws(serviceException); // when . then Assert.Throws <GuardianContactServiceException>(() => this.guardianContactService.RetrieveAllGuardianContacts()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianContactServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllGuardianContacts(), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <GuardianContact> TryCatch( ReturningGuardianContactFunction returningGuardianContactFunction) { try { return(await returningGuardianContactFunction()); } catch (NullGuardianContactException nullGuardianContactException) { throw CreateAndLogValidationException(nullGuardianContactException); } catch (InvalidGuardianContactException invalidGuardianContactException) { throw CreateAndLogValidationException(invalidGuardianContactException); } catch (NotFoundGuardianContactException notFoundGuardianContactException) { throw CreateAndLogValidationException(notFoundGuardianContactException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianContactException = new AlreadyExistsGuardianContactException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianContactException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidGuardianContactReferenceException = new InvalidGuardianContactReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidGuardianContactReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedGuardianContactException = new LockedGuardianContactException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedGuardianContactException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedGuardianContactServiceException = new FailedGuardianContactServiceException(exception); throw CreateAndLogServiceException(failedGuardianContactServiceException); } }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given var randomContactId = Guid.NewGuid(); var randomGuardianId = Guid.NewGuid(); Guid someContactId = randomContactId; Guid someGuardianId = randomGuardianId; var serviceException = new Exception(); var failedGuardianContactServiceException = new FailedGuardianContactServiceException(serviceException); var expectedGuardianContactServiceException = new GuardianContactServiceException(failedGuardianContactServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId)) .ThrowsAsync(serviceException); // when ValueTask <GuardianContact> removeGuardianContactTask = this.guardianContactService.RemoveGuardianContactByIdAsync( someGuardianId, someContactId); // then await Assert.ThrowsAsync <GuardianContactServiceException>(() => removeGuardianContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianContactServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianContactAsync(It.IsAny <GuardianContact>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private IQueryable <GuardianContact> TryCatch( ReturningGuardianContactsFunction returningGuardianContactsFunction) { try { return(returningGuardianContactsFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedGuardianContactServiceException = new FailedGuardianContactServiceException(exception); throw CreateAndLogServiceException(failedGuardianContactServiceException); } }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given GuardianContact randomGuardianContact = CreateRandomGuardianContact(); GuardianContact inputGuardianContact = randomGuardianContact; var serviceException = new Exception(); var failedGuardianContactServiceException = new FailedGuardianContactServiceException(serviceException); var expectedGuardianContactServiceException = new GuardianContactServiceException(failedGuardianContactServiceException); this.storageBrokerMock.Setup(broker => broker.InsertGuardianContactAsync(inputGuardianContact)) .ThrowsAsync(serviceException); // when ValueTask <GuardianContact> addGuardianContactTask = this.guardianContactService.AddGuardianContactAsync(inputGuardianContact); // then await Assert.ThrowsAsync <GuardianContactServiceException>(() => addGuardianContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianContactServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianContactAsync(inputGuardianContact), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }