Beispiel #1
0
        private async ValueTask <Registration> TryCatch(ReturningRegistrationFunction returningRegistrationFunction)
        {
            try
            {
                return(await returningRegistrationFunction());
            }
            catch (NullRegistrationException nullRegistrationException)
            {
                throw CreateAndLogValidationException(nullRegistrationException);
            }
            catch (InvalidRegistrationException invalidRegistrationInputException)
            {
                throw CreateAndLogValidationException(invalidRegistrationInputException);
            }
            catch (NotFoundRegistrationException notFoundRegistrationException)
            {
                throw CreateAndLogValidationException(notFoundRegistrationException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsRegistrationException =
                    new AlreadyExistsRegistrationException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsRegistrationException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidRegistrationReferenceException =
                    new InvalidRegistrationReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidRegistrationReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedRegistrationException =
                    new LockedRegistrationException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedRegistrationException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedRegistrationServiceException =
                    new FailedRegistrationServiceException(exception);

                throw CreateAndLogServiceException(failedRegistrationServiceException);
            }
        }
Beispiel #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenRegistrationAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime                  = GetRandomDateTime();
            Registration   randomRegistration        = CreateRandomRegistration(dateTime);
            Registration   alreadyExistsRegistration = randomRegistration;

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

            var alreadyExistsRegistrationException =
                new AlreadyExistsRegistrationException(duplicateKeyException);

            var expectedRegistrationValidationException =
                new RegistrationValidationException(alreadyExistsRegistrationException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertRegistrationAsync(alreadyExistsRegistration))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Registration> registerRegistrationTask =
                this.registrationService.AddRegistrationAsync(alreadyExistsRegistration);

            // then
            await Assert.ThrowsAsync <RegistrationValidationException>(() =>
                                                                       registerRegistrationTask.AsTask());

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

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

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

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