public void ShouldThrowDependencyExceptionOnRetrieveAllAssignmentAttachmentsWhenSqlExceptionOccursAndLogIt() { // given var sqlException = GetSqlException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(sqlException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.SelectAllAssignmentAttachments()) .Throws(sqlException); // when . then Assert.Throws <AssignmentAttachmentDependencyException>(() => this.assignmentAttachmentService.RetrieveAllAssignmentAttachments()); this.storageBrokerMock.Verify(broker => broker.SelectAllAssignmentAttachments(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <AssignmentAttachment> TryCatch( ReturningAssignmentAttachmentFunction returningAssignmentAttachmentFunction) { try { return(await returningAssignmentAttachmentFunction()); } catch (NullAssignmentAttachmentException nullAssignmentAttachmentInputException) { throw CreateAndLogValidationException(nullAssignmentAttachmentInputException); } catch (InvalidAssignmentAttachmentException invalidAssignmentAttachmentInputException) { throw CreateAndLogValidationException(invalidAssignmentAttachmentInputException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsAssignmentAttachmentException = new AlreadyExistsAssignmentAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsAssignmentAttachmentException); } catch (DbUpdateConcurrencyException databaseUpdateConcurrencyException) { var lockedAssignmentAttachmentException = new LockedAssignmentAttachmentException(databaseUpdateConcurrencyException); throw CreateAndLogDependencyValidationException(lockedAssignmentAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidAssignmentAttachmentReferenceException = new InvalidAssignmentAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidAssignmentAttachmentReferenceException); } catch (SqlException sqlException) { var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(sqlException); throw CreateAndLogCriticalDependencyException(failedAssigmentAttachmentStorageException); } catch (DbUpdateException databaseUpdateException) { var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(databaseUpdateException); throw CreateAndLogDependencyException(failedAssigmentAttachmentStorageException); } catch (NotFoundAssignmentAttachmentException notFoundAssignmentAttachmentException) { throw CreateAndLogValidationException(notFoundAssignmentAttachmentException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
private IQueryable <AssignmentAttachment> TryCatch( ReturningAssignmentAttachmentsFunction returningAssignmentAttachmentsFunction) { try { return(returningAssignmentAttachmentsFunction()); } catch (SqlException sqlException) { var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(sqlException); throw CreateAndLogCriticalDependencyException(failedAssigmentAttachmentStorageException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someAssignmentId = Guid.NewGuid(); var databaseUpdateException = new DbUpdateException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(databaseUpdateException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.SelectAssignmentAttachmentByIdAsync( It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateException); // when ValueTask <AssignmentAttachment> retrieveAssignmentAttachmentTask = this.assignmentAttachmentService.RetrieveAssignmentAttachmentByIdAsync( someAssignmentId, someAttachmentId); // then await Assert.ThrowsAsync <AssignmentAttachmentDependencyException>(() => retrieveAssignmentAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentAttachmentByIdAsync( It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someAssignmentId = Guid.NewGuid(); SqlException sqlException = GetSqlException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(sqlException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.SelectAssignmentAttachmentByIdAsync(someAssignmentId, someAttachmentId)) .ThrowsAsync(sqlException); // when ValueTask <AssignmentAttachment> removeAssignmentAttachmentTask = this.assignmentAttachmentService.RemoveAssignmentAttachmentByIdAsync( someAssignmentId, someAttachmentId); // then await Assert.ThrowsAsync <AssignmentAttachmentDependencyException>(() => removeAssignmentAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentAttachmentByIdAsync(someAssignmentId, someAttachmentId), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync() { // given AssignmentAttachment randomAssignmentAttachment = CreateRandomAssignmentAttachment(); AssignmentAttachment inputAssignmentAttachment = randomAssignmentAttachment; var databaseUpdateException = new DbUpdateException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(databaseUpdateException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment)) .ThrowsAsync(databaseUpdateException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(inputAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentDependencyException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(inputAssignmentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnAddWhenSqlExceptionOccursAndLogItAsync() { // given AssignmentAttachment someAssignmentAttachment = CreateRandomAssignmentAttachment(); var sqlException = GetSqlException(); var failedAssigmentAttachmentStorageException = new FailedAssignmentAttachmentStorageException(sqlException); var expectedAssignmentAttachmentDependencyException = new AssignmentAttachmentDependencyException(failedAssigmentAttachmentStorageException); this.storageBrokerMock.Setup(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>())) .ThrowsAsync(sqlException); // when ValueTask <AssignmentAttachment> addAssignmentAttachmentTask = this.assignmentAttachmentService.AddAssignmentAttachmentAsync(someAssignmentAttachment); // then await Assert.ThrowsAsync <AssignmentAttachmentDependencyException>(() => addAssignmentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs( expectedAssignmentAttachmentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }