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 void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            AssignmentAttachment randomAssignmentAttachment  = CreateRandomAssignmentAttachment();
            AssignmentAttachment invalidAssignmentAttachment = randomAssignmentAttachment;
            string randomMessage    = GetRandomMessage();
            string exceptionMessage = randomMessage;
            var    foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidAssignmentAttachmentReferenceException =
                new InvalidAssignmentAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedAssignmentAttachmentValidationException =
                new AssignmentAttachmentValidationException(invalidAssignmentAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAssignmentAttachmentAsync(invalidAssignmentAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <AssignmentAttachment> addAssignmentAttachmentTask =
                this.assignmentAttachmentService.AddAssignmentAttachmentAsync(invalidAssignmentAttachment);

            // then
            await Assert.ThrowsAsync <AssignmentAttachmentValidationException>(() =>
                                                                               addAssignmentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertAssignmentAttachmentAsync(invalidAssignmentAttachment),
                                          Times.Once);

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