private async ValueTask <CourseAttachment> TryCatch( ReturningCourseAttachmentFunction returningCourseAttachmentFunction) { try { return(await returningCourseAttachmentFunction()); } catch (NullCourseAttachmentException nullCourseAttachmentException) { throw CreateAndLogValidationException(nullCourseAttachmentException); } catch (InvalidCourseAttachmentException invalidCourseAttachmentException) { throw CreateAndLogValidationException(invalidCourseAttachmentException); } catch (NotFoundCourseAttachmentException notFoundCourseAttachmentException) { throw CreateAndLogValidationException(notFoundCourseAttachmentException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCourseAttachmentException = new AlreadyExistsCourseAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCourseAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidCourseAttachmentReferenceException = new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidCourseAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseAttachmentException = new LockedCourseAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedCourseAttachmentServiceException = new FailedCourseAttachmentServiceException(exception); throw CreateAndLogServiceException(failedCourseAttachmentServiceException); } }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someCourseId = Guid.NewGuid(); var serviceException = new Exception(); var failedCourseAttachmentServiceException = new FailedCourseAttachmentServiceException(serviceException); var expectedCourseAttachmenServiceException = new CourseAttachmentServiceException(failedCourseAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId)) .ThrowsAsync(serviceException); // when ValueTask <CourseAttachment> removeCourseAttachmentTask = this.courseAttachmentService.RemoveCourseAttachmentByIdAsync( someCourseId, someAttachmentId); // then await Assert.ThrowsAsync <CourseAttachmentServiceException>(() => removeCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmenServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private IQueryable <CourseAttachment> TryCatch( ReturningCourseAttachmentsFunction returningCourseAttachmentsFunction) { try { return(returningCourseAttachmentsFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedCourseAttachmentServiceException = new FailedCourseAttachmentServiceException(exception); throw CreateAndLogServiceException(failedCourseAttachmentServiceException); } }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given CourseAttachment someCourseAttachment = CreateRandomCourseAttachment(); var serviceException = new Exception(); var failedCourseAttachmentServiceException = new FailedCourseAttachmentServiceException(serviceException); var expectedCourseAttachmentServiceException = new CourseAttachmentServiceException(failedCourseAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>())) .ThrowsAsync(serviceException); // when ValueTask <CourseAttachment> addCourseAttachmentTask = this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment); // then await Assert.ThrowsAsync <CourseAttachmentServiceException>(() => addCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmentServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllCourseAttachmentsWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedCourseAttachmentServiceException = new FailedCourseAttachmentServiceException(serviceException); var expectedCourseAttachmentServiceException = new CourseAttachmentServiceException(failedCourseAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllCourseAttachments()) .Throws(serviceException); // when Action retrieveAllCourseAttachmentAction = () => this.courseAttachmentService.RetrieveAllCourseAttachments(); // then Assert.Throws <CourseAttachmentServiceException>( retrieveAllCourseAttachmentAction); this.storageBrokerMock.Verify(broker => broker.SelectAllCourseAttachments(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCourseAttachmentServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }