コード例 #1
0
        public async void ShouldThrowValidationExceptionOnAddWhenGuardianAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime              = GetRandomDateTime();
            Guardian       randomGuardian        = CreateRandomGuardian(dateTime);
            Guardian       alreadyExistsGuardian = randomGuardian;

            alreadyExistsGuardian.UpdatedBy = alreadyExistsGuardian.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsGuardianException =
                new AlreadyExistsGuardianException(duplicateKeyException);

            var expectedGuardianValidationException =
                new GuardianValidationException(alreadyExistsGuardianException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianAsync(alreadyExistsGuardian))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Guardian> createGuardianTask =
                this.guardianService.CreateGuardianAsync(alreadyExistsGuardian);

            // then
            await Assert.ThrowsAsync <GuardianValidationException>(() =>
                                                                   createGuardianTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        private async ValueTask <Guardian> TryCatch(ReturningGuardianFunction returningGuardianFunction)
        {
            try
            {
                return(await returningGuardianFunction());
            }
            catch (NullGuardianException nullGuardianException)
            {
                throw CreateAndLogValidationException(nullGuardianException);
            }
            catch (InvalidGuardianException invalidGuardianException)
            {
                throw CreateAndLogValidationException(invalidGuardianException);
            }
            catch (NotFoundGuardianException notFoundGuardianException)
            {
                throw CreateAndLogValidationException(notFoundGuardianException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsGuardianException =
                    new AlreadyExistsGuardianException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsGuardianException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedGuardianException = new LockedGuardianException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedGuardianException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedGuardianServiceException =
                    new FailedGuardianServiceException(exception);

                throw CreateAndLogServiceException(failedGuardianServiceException);
            }
        }