private async ValueTask <TeacherAttachment> TryCatch( ReturningTeacherAttachmentFunction returningTeacherAttachmentFunction) { try { return(await returningTeacherAttachmentFunction()); } catch (NullTeacherAttachmentException nullTeacherAttachmentInputException) { throw CreateAndLogValidationException(nullTeacherAttachmentInputException); } catch (InvalidTeacherAttachmentException invalidTeacherAttachmentInputException) { throw CreateAndLogValidationException(invalidTeacherAttachmentInputException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (NotFoundTeacherAttachmentException notFoundTeacherAttachmentException) { throw CreateAndLogValidationException(notFoundTeacherAttachmentException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsTeacherAttachmentException = new AlreadyExistsTeacherAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsTeacherAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidTeacherAttachmentReferenceException = new InvalidTeacherAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidTeacherAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedTeacherAttachmentException = new LockedTeacherAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedTeacherAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(exception); throw CreateAndLogServiceException(failedTeacherAttachmentServiceException); } }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given var randomAttachmentId = Guid.NewGuid(); var randomTeacherId = Guid.NewGuid(); Guid someAttachmentId = randomAttachmentId; Guid someTeacherId = randomTeacherId; var serviceException = new Exception(); var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(serviceException); var expectedTeacherAttachmentException = new TeacherAttachmentServiceException(failedTeacherAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectTeacherAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(serviceException); // when ValueTask <TeacherAttachment> removeTeacherAttachmentTask = this.teacherAttachmentService.RemoveTeacherAttachmentByIdAsync( someTeacherId, someAttachmentId); // then await Assert.ThrowsAsync <TeacherAttachmentServiceException>(() => removeTeacherAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteTeacherAttachmentAsync(It.IsAny <TeacherAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private IQueryable <TeacherAttachment> TryCatch( ReturningTeacherAttachmentsFunction returningTeacherAttachmentsFunction) { try { return(returningTeacherAttachmentsFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(exception); throw CreateAndLogServiceException(failedTeacherAttachmentServiceException); } }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment(); TeacherAttachment inputTeacherAttachment = randomTeacherAttachment; var serviceException = new Exception(); var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(serviceException); var expectedTeacherAttachmentServiceException = new TeacherAttachmentServiceException(failedTeacherAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.InsertTeacherAttachmentAsync(It.IsAny <TeacherAttachment>())) .ThrowsAsync(serviceException); // when ValueTask <TeacherAttachment> addTeacherAttachmentTask = this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment); // then await Assert.ThrowsAsync <TeacherAttachmentServiceException>(() => addTeacherAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAttachmentAsync(It.IsAny <TeacherAttachment>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllTeacherAttachmentsWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(serviceException); var expectedTeacherAttachmentServiceException = new TeacherAttachmentServiceException(failedTeacherAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllTeacherAttachments()) .Throws(serviceException); // when Action retrieveAllTeacherAttachmentAction = () => this.teacherAttachmentService.RetrieveAllTeacherAttachments(); // then Assert.Throws <TeacherAttachmentServiceException>( retrieveAllTeacherAttachmentAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllTeacherAttachments(), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }