public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomAttachmentId = Guid.NewGuid(); Guid inputAttachmentId = randomAttachmentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedAttachmentException(databaseUpdateConcurrencyException); var expectedAttachmentDependencyException = new AttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectAttachmentByIdAsync(inputAttachmentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Attachment> deleteAttachmentTask = this.attachmentService.RemoveAttachmentByIdAsync(inputAttachmentId); // then await Assert.ThrowsAsync <AttachmentDependencyException>(() => deleteAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttachmentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttachmentByIdAsync(inputAttachmentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Attachment randomAttachment = CreateRandomAttachment(randomDateTime); Attachment someAttachment = randomAttachment; someAttachment.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedAttachmentException(databaseUpdateConcurrencyException); var expectedAttachmentDependencyException = new AttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectAttachmentByIdAsync(someAttachment.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Attachment> modifyAttachmentTask = this.attachmentService.ModifyAttachmentAsync(someAttachment); // then await Assert.ThrowsAsync <AttachmentDependencyException>(() => modifyAttachmentTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttachmentByIdAsync(someAttachment.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttachmentDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.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); } }