public async void ShouldThrowValidationExceptionOnCreateWhenAttachmentAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Attachment randomAttachment = CreateRandomAttachment(dateTime); Attachment alreadyExistsAttachment = randomAttachment; alreadyExistsAttachment.UpdatedBy = alreadyExistsAttachment.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsAttachmentException = new AlreadyExistsAttachmentException(duplicateKeyException); var expectedAttachmentValidationException = new AttachmentValidationException(alreadyExistsAttachmentException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertAttachmentAsync(It.IsAny <Attachment>())) .ThrowsAsync(duplicateKeyException); // when ValueTask <Attachment> createAttachmentTask = this.attachmentService.AddAttachmentAsync(alreadyExistsAttachment); // then await Assert.ThrowsAsync <AttachmentValidationException>(() => createAttachmentTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAttachmentAsync(It.IsAny <Attachment>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttachmentValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Attachment> TryCatch(ReturningAttachmentFunction returningAttachmentFunction) { try { return(await returningAttachmentFunction()); } catch (NullAttachmentException nullAttachmentException) { throw CreateAndLogValidationException(nullAttachmentException); } catch (InvalidAttachmentException invalidAttachmentInputException) { throw CreateAndLogValidationException(invalidAttachmentInputException); } catch (NotFoundAttachmentException notFoundAttachmentException) { throw CreateAndLogValidationException(notFoundAttachmentException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsAttachmentException = new AlreadyExistsAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedAttachmentException = new LockedAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedAttachmentServiceException = new FailedAttachmentServiceException(exception); throw CreateAndLogServiceException(failedAttachmentServiceException); } }