public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            UserContact randomUserContact       = CreateRandomUserContact();
            UserContact inputUserContact        = randomUserContact;
            var         databaseUpdateException = new DbUpdateException();

            var expectedUserContactDependencyException =
                new UserContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertUserContactAsync(inputUserContact))
            .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(inputUserContact),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var expectedUserContactDependencyException =
                new UserContactDependencyException(sqlException);

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

            //when. then
            Assert.Throws <UserContactDependencyException>(() =>
                                                           this.userContactService.RetrieveAllUserContacts());

            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 UserContactDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var UserContactDependencyException = new UserContactDependencyException(exception);

            this.loggingBroker.LogError(UserContactDependencyException);

            return(UserContactDependencyException);
        }
        private UserContactDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var userContactDependencyException = new UserContactDependencyException(exception);

            this.loggingBroker.LogCritical(userContactDependencyException);

            return(userContactDependencyException);
        }
        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 ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            var          randomContactId = Guid.NewGuid();
            var          randomUserId    = Guid.NewGuid();
            Guid         someContactId   = randomContactId;
            Guid         someUserId      = randomUserId;
            SqlException sqlException    = GetSqlException();

            var expectedUserContactDependencyException
                = new UserContactDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(someUserId, someContactId))
            .ThrowsAsync(sqlException);

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

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectUserContactByIdAsync(someUserId, someContactId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #7
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            var  randomContactId         = Guid.NewGuid();
            var  randomUserId            = Guid.NewGuid();
            Guid inputContactId          = randomContactId;
            Guid inputUserId             = randomUserId;
            var  databaseUpdateException = new DbUpdateException();

            var expectedUserContactDependencyException =
                new UserContactDependencyException(databaseUpdateException);

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

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

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

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

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

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