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

            var failedFeeServiceException =
                new FailedFeeServiceException(serviceException);

            var expectedFeeServiceException =
                new FeeServiceException(failedFeeServiceException);

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

            // when . then
            Assert.Throws <FeeServiceException>(() =>
                                                this.feeService.RetrieveAllFees());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime  = GetRandomDateTime();
            Fee            randomFee = CreateRandomFee(dateTime);
            Fee            inputFee  = randomFee;

            inputFee.UpdatedBy = inputFee.CreatedBy;
            var serviceException = new Exception();

            var failedFeeServiceException =
                new FailedFeeServiceException(serviceException);

            var expectedFeeServiceException =
                new FeeServiceException(failedFeeServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertFeeAsync(inputFee))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Fee> createFeeTask =
                this.feeService.AddFeeAsync(inputFee);

            // then
            await Assert.ThrowsAsync <FeeServiceException>(() =>
                                                           createFeeTask.AsTask());

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

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

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Fee            someFee = CreateRandomFee(randomDateTime);

            someFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedFeeServiceException =
                new FailedFeeServiceException(serviceException);

            var expectedFeeServiceException =
                new FeeServiceException(failedFeeServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectFeeByIdAsync(someFee.Id))
            .ThrowsAsync(serviceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDateTime);

            // when
            ValueTask <Fee> modifyFeeTask =
                this.feeService.ModifyFeeAsync(someFee);

            // then
            await Assert.ThrowsAsync <FeeServiceException>(() =>
                                                           modifyFeeTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectFeeByIdAsync(someFee.Id),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 4
0
        private async ValueTask <Fee> TryCatch(ReturningFeeFunction returningFeeFunction)
        {
            try
            {
                return(await returningFeeFunction());
            }
            catch (NullFeeException nullFeeException)
            {
                throw CreateAndLogValidationException(nullFeeException);
            }
            catch (InvalidFeeException invalidFeeInputException)
            {
                throw CreateAndLogValidationException(invalidFeeInputException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundFeeException notFoundFeeException)
            {
                throw CreateAndLogValidationException(notFoundFeeException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsFeeException =
                    new AlreadyExistsFeeException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsFeeException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedFeeException = new LockedFeeException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedFeeException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedFeeServiceException =
                    new FailedFeeServiceException(exception);

                throw CreateAndLogServiceException(failedFeeServiceException);
            }
        }
Ejemplo n.º 5
0
        private IQueryable <Fee> TryCatch(ReturningQueryableFeeFunction returningQueryableFeeFunction)
        {
            try
            {
                return(returningQueryableFeeFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedFeeServiceException =
                    new FailedFeeServiceException(exception);

                throw CreateAndLogServiceException(failedFeeServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someFeeId        = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedFeeServiceException =
                new FailedFeeServiceException(serviceException);

            var expectedFeeServiceException =
                new FeeServiceException(failedFeeServiceException);

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

            // when
            ValueTask <Fee> retrieveByIdFeeTask =
                this.feeService.RetrieveFeeByIdAsync(someFeeId);

            // then
            await Assert.ThrowsAsync <FeeServiceException>(() =>
                                                           retrieveByIdFeeTask.AsTask());

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

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

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