Beispiel #1
0
        private async ValueTask <TeacherAttachment> TryCatch(
            ReturningTeacherAttachmentFunction returningTeacherAttachmentFunction)
        {
            try
            {
                return(await returningTeacherAttachmentFunction());
            }
            catch (NullTeacherAttachmentException nullTeacherAttachmentInputException)
            {
                throw CreateAndLogValidationException(nullTeacherAttachmentInputException);
            }
            catch (InvalidTeacherAttachmentException invalidTeacherAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidTeacherAttachmentInputException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundTeacherAttachmentException notFoundTeacherAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundTeacherAttachmentException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsTeacherAttachmentException =
                    new AlreadyExistsTeacherAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsTeacherAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidTeacherAttachmentReferenceException =
                    new InvalidTeacherAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidTeacherAttachmentReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTeacherAttachmentException =
                    new LockedTeacherAttachmentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedTeacherAttachmentException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedTeacherAttachmentServiceException =
                    new FailedTeacherAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedTeacherAttachmentServiceException);
            }
        }
Beispiel #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenTeacherAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            TeacherAttachment randomTeacherAttachment        = CreateRandomTeacherAttachment();
            TeacherAttachment alreadyExistsTeacherAttachment = randomTeacherAttachment;
            string            randomMessage    = GetRandomMessage();
            string            exceptionMessage = randomMessage;
            var duplicateKeyException          = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsTeacherAttachmentException =
                new AlreadyExistsTeacherAttachmentException(duplicateKeyException);

            var expectedTeacherAttachmentValidationException =
                new TeacherAttachmentValidationException(alreadyExistsTeacherAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherAttachmentAsync(alreadyExistsTeacherAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <TeacherAttachment> addTeacherAttachmentTask =
                this.teacherAttachmentService.AddTeacherAttachmentAsync(alreadyExistsTeacherAttachment);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() =>
                                                                            addTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherAttachmentAsync(alreadyExistsTeacherAttachment),
                                          Times.Once);

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