public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedUserContactServiceException =
                new FailedUserContactServiceException(serviceException);

            var expectedUserContactServiceException =
                new UserContactServiceException(failedUserContactServiceException);

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

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someContactId    = Guid.NewGuid();
            Guid someUserId       = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedUserContactServiceException =
                new FailedUserContactServiceException(serviceException);

            var expectedUserContactException =
                new UserContactServiceException(failedUserContactServiceException);

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

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

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

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedUserContactException))),
                                          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();
        }
コード例 #3
0
        private IQueryable <UserContact> TryCatch(
            ReturningUserContactsFunction returningUserContactsFunction)
        {
            try
            {
                return(returningUserContactsFunction());
            }
            catch (SqlException sqlException)
            {
                var failedUserContactStorageException =
                    new FailedUserContactStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedUserContactStorageException);
            }
            catch (Exception exception)
            {
                var failedUserContactServiceException =
                    new FailedUserContactServiceException(exception);

                throw CreateAndLogServiceException(failedUserContactServiceException);
            }
        }
コード例 #4
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            UserContact randomUserContact = CreateRandomUserContact();
            UserContact inputUserContact  = randomUserContact;
            var         serviceException  = new Exception();

            var failedUserContactServiceException =
                new FailedUserContactServiceException(serviceException);

            var expectedUserContactServiceException =
                new UserContactServiceException(failedUserContactServiceException);

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

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #5
0
        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)
            {
                var failedUserContactException =
                    new FailedUserContactServiceException(exception);

                throw CreateAndLogServiceException(failedUserContactException);
            }
        }