Beispiel #1
0
        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);
            }
        }
        public async Task ShouldThrowDependencyValidationOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someAssignmentId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedAttachmentException =
                new LockedAssignmentAttachmentException(databaseUpdateConcurrencyException);

            var expectedAssignmentAttachmentDependencyValidationException =
                new AssignmentAttachmentDependencyValidationException(lockedAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAssignmentAttachmentByIdAsync(someAssignmentId, someAttachmentId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <AssignmentAttachment> removeAssignmentAttachmentTask =
                this.assignmentAttachmentService
                .RemoveAssignmentAttachmentByIdAsync(
                    assignmentId: someAssignmentId,
                    attachmentId: someAttachmentId);

            // then
            await Assert.ThrowsAsync <AssignmentAttachmentDependencyValidationException>(() =>
                                                                                         removeAssignmentAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAssignmentAttachmentByIdAsync(someAssignmentId, someAttachmentId),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedAssignmentAttachmentDependencyValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteAssignmentAttachmentAsync(It.IsAny <AssignmentAttachment>()),
                                          Times.Never);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }