public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someTeacherId    = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedAttachmentException          = new LockedTeacherAttachmentException(databaseUpdateConcurrencyException);

            var expectedTeacherAttachmentException =
                new TeacherAttachmentDependencyException(lockedAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <TeacherAttachment> retrieveTeacherAttachmentTask =
                this.teacherAttachmentService.RetrieveTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId);

            // then
            await Assert.ThrowsAsync <TeacherAttachmentDependencyException>(() =>
                                                                            retrieveTeacherAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 2
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);
            }
        }