public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var failedUserContactStorageException =
                new FailedUserContactStorageException(sqlException);

            var expectedUserContactDependencyException =
                new UserContactDependencyException(failedUserContactStorageException);

            this.storageBrokerMock.Setup(broker => broker.SelectAllUserContacts())
            .Throws(sqlException);

            // when
            Action retrieveAllUserContactsAction = () =>
                                                   this.userContactService.RetrieveAllUserContacts();

            // then
            Assert.Throws <UserContactDependencyException>(
                retrieveAllUserContactsAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <UserContact> TryCatch(ReturningUserContactFunction returningUserContactFunction)
        {
            try
            {
                return(await returningUserContactFunction());
            }
            catch (NullUserContactException nullUserContactException)
            {
                throw CreateAndLogValidationException(nullUserContactException);
            }
            catch (InvalidUserContactException invalidUserContactException)
            {
                throw CreateAndLogValidationException(invalidUserContactException);
            }
            catch (SqlException sqlException)
            {
                var failedUserContactStorageException =
                    new FailedUserContactStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedUserContactStorageException);
            }
            catch (NotFoundUserContactException notFoundUserContactException)
            {
                throw CreateAndLogValidationException(notFoundUserContactException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsUserContactException =
                    new AlreadyExistsUserContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsUserContactException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidUserContactReferenceException =
                    new InvalidUserContactReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidUserContactReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedUserContactException =
                    new LockedUserContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedUserContactException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedUserContactStorageException =
                    new FailedUserContactStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedUserContactStorageException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }
        private IQueryable <UserContact> TryCatch(
            ReturningUserContactsFunction returningUserContactsFunction)
        {
            try
            {
                return(returningUserContactsFunction());
            }
            catch (SqlException sqlException)
            {
                var failedUserContactStorageException =
                    new FailedUserContactStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedUserContactStorageException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbExceptionOccursAndLogItAsync()
        {
            // given

            Guid someContactId           = Guid.NewGuid();
            Guid someUserId              = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var failedUserContactStorageException =
                new FailedUserContactStorageException(databaseUpdateException);

            var expectedUserContactDependencyException =
                new UserContactDependencyException(failedUserContactStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <UserContact> removeUserContactTask =
                this.userContactService.RemoveUserContactByIdAsync
                    (someUserId, someContactId);

            // then
            await Assert.ThrowsAsync <UserContactDependencyException>(() =>
                                                                      removeUserContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectUserContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            var          randomContactId = Guid.NewGuid();
            var          inputContactId  = randomContactId;
            Guid         randomUserId    = Guid.NewGuid();
            Guid         inputUserId     = randomUserId;
            SqlException sqlException    = GetSqlException();

            var failedUserContactStorageException =
                new FailedUserContactStorageException(sqlException);

            var expectedUserContactDependencyException =
                new UserContactDependencyException(failedUserContactStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(inputUserId, inputContactId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <UserContact> retrieveUserContactTask =
                this.userContactService.RetrieveUserContactByIdAsync(
                    inputUserId,
                    inputContactId);

            // then
            await Assert.ThrowsAsync <UserContactDependencyException>(() =>
                                                                      retrieveUserContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectUserContactByIdAsync(inputUserId, inputContactId),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #6
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            UserContact randomUserContact       = CreateRandomUserContact();
            UserContact inputUserContact        = randomUserContact;
            var         databaseUpdateException = new DbUpdateException();

            var failedUserContactStorageException =
                new FailedUserContactStorageException(databaseUpdateException);

            var expectedUserContactDependencyException =
                new UserContactDependencyException(failedUserContactStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertUserContactAsync(It.IsAny <UserContact>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <UserContact> addUserContactTask =
                this.userContactService.AddUserContactAsync(inputUserContact);

            // then
            await Assert.ThrowsAsync <UserContactDependencyException>(() =>
                                                                      addUserContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertUserContactAsync(It.IsAny <UserContact>()),
                                          Times.Once);

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