Example #1
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            GuardianContact randomGuardianContact   = CreateRandomGuardianContact();
            GuardianContact inputGuardianContact    = randomGuardianContact;
            var             databaseUpdateException = new DbUpdateException();

            var expectedGuardianContactDependencyException =
                new GuardianContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianContactAsync(inputGuardianContact))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <GuardianContact> addGuardianContactTask =
                this.guardianContactService.AddGuardianContactAsync(inputGuardianContact);

            // then
            await Assert.ThrowsAsync <GuardianContactDependencyException>(() =>
                                                                          addGuardianContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            Guid someContactId           = Guid.NewGuid();
            Guid someGuardianId          = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var expectedGuardianContactDependencyException =
                new GuardianContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <GuardianContact> retrieveGuardianContactTask =
                this.guardianContactService.RetrieveGuardianContactByIdAsync(
                    someGuardianId, someContactId);

            // then
            await Assert.ThrowsAsync <GuardianContactDependencyException>(
                () => retrieveGuardianContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId),
                                          Times.Once);

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

            var expectedGuardianContactDependencyException =
                new GuardianContactDependencyException(sqlException);

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

            //when. then
            Assert.Throws <GuardianContactDependencyException>(() =>
                                                               this.guardianContactService.RetrieveAllGuardianContacts());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private GuardianContactDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var guardianContactDependencyException = new GuardianContactDependencyException(exception);

            this.loggingBroker.LogError(guardianContactDependencyException);

            return(guardianContactDependencyException);
        }
Example #5
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            var          randomContactId  = Guid.NewGuid();
            var          randomGuardianId = Guid.NewGuid();
            Guid         someContactId    = randomContactId;
            Guid         someGuardianId   = randomGuardianId;
            SqlException sqlException     = GetSqlException();

            var expectedGuardianContactDependencyException
                = new GuardianContactDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <GuardianContact> removeGuardianContactTask =
                this.guardianContactService.RemoveGuardianContactByIdAsync(
                    someGuardianId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <GuardianContactDependencyException>(() =>
                                                                          removeGuardianContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId),
                                          Times.Once);

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

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