Пример #1
0
        private async ValueTask <StudentContact> TryCatch(
            ReturningStudentContactFunction returningStudentContactFunction)
        {
            try
            {
                return(await returningStudentContactFunction());
            }
            catch (NullStudentContactException nullStudentContactException)
            {
                throw CreateAndLogValidationException(nullStudentContactException);
            }
            catch (InvalidStudentContactException invalidStudentContactException)
            {
                throw CreateAndLogValidationException(invalidStudentContactException);
            }
            catch (NotFoundStudentContactException notFoundStudentContactException)
            {
                throw CreateAndLogValidationException(notFoundStudentContactException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentContactException =
                    new AlreadyExistsStudentContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsStudentContactException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidStudentContactReferenceException =
                    new InvalidStudentContactReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidStudentContactReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentContactException =
                    new LockedStudentContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentContactException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentContactServiceException =
                    new FailedStudentContactServiceException(exception);

                throw CreateAndLogServiceException(failedStudentContactServiceException);
            }
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentContactAlreadyExistsAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact        = CreateRandomStudentContact();
            StudentContact alreadyExistsStudentContact = randomStudentContact;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsStudentContactException =
                new AlreadyExistsStudentContactException(duplicateKeyException);

            var expectedStudentContactValidationException =
                new StudentContactValidationException(alreadyExistsStudentContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentContactAsync(alreadyExistsStudentContact))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <StudentContact> addStudentContactTask =
                this.studentContactService.AddStudentContactAsync(alreadyExistsStudentContact);

            // then
            await Assert.ThrowsAsync <StudentContactValidationException>(() =>
                                                                         addStudentContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertStudentContactAsync(alreadyExistsStudentContact),
                                          Times.Once);

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