Example #1
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someFeeId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedFeeException = new LockedFeeException(databaseUpdateConcurrencyException);

            var expectedFeeDependencyException =
                new FeeDependencyException(lockedFeeException);

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

            // when
            ValueTask <Fee> removeFeeTask =
                this.feeService.RemoveFeeByIdAsync(someFeeId);

            // then
            await Assert.ThrowsAsync <FeeDependencyException>(() =>
                                                              removeFeeTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #2
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);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Fee            someFee = CreateRandomFee(randomDateTime);

            someFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedFeeException = new LockedFeeException(databaseUpdateConcurrencyException);

            var expectedFeeDependencyException =
                new FeeDependencyException(lockedFeeException);

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

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

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

            // then
            await Assert.ThrowsAsync <FeeDependencyException>(() =>
                                                              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(
                                                                    expectedFeeDependencyException))),
                                          Times.Once);

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