Пример #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);
            }
        }
Пример #2
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            var  randomContactId = Guid.NewGuid();
            var  randomStudentId = Guid.NewGuid();
            Guid someContactId   = randomContactId;
            Guid someStudentId   = randomStudentId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedContactException =
                new LockedStudentContactException(databaseUpdateConcurrencyException);

            var expectedStudentContactException =
                new StudentContactDependencyException(lockedContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentContactByIdAsync(someStudentId, someContactId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <StudentContact> removeStudentContactTask =
                this.studentContactService.RemoveStudentContactByIdAsync(someStudentId, someContactId);

            // then
            await Assert.ThrowsAsync <StudentContactDependencyException>(() =>
                                                                         removeStudentContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentContactByIdAsync(someStudentId, someContactId),
                                          Times.Once);

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

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