Example #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);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime   = GetRandomDateTime();
            Registration   someRegistration = CreateRandomRegistration(dateTime: randomDateTime);
            var            serviceException = new Exception();

            var failedRegistrationServiceException =
                new FailedRegistrationServiceException(serviceException);

            var expectedRegistrationServiceException =
                new RegistrationServiceException(failedRegistrationServiceException);

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

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

            // when
            ValueTask <Registration> modifyRegistrationTask =
                this.registrationService.ModifyRegistrationAsync(someRegistration);

            // then
            await Assert.ThrowsAsync <RegistrationServiceException>(() =>
                                                                    modifyRegistrationTask.AsTask());

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            Registration   randomRegistration = CreateRandomRegistration(dateTime);
            Registration   inputRegistration  = randomRegistration;

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

            var failedRegistrationServiceException =
                new FailedRegistrationServiceException(serviceException);

            var expectedRegistrationServiceException =
                new RegistrationServiceException(failedRegistrationServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertRegistrationAsync(inputRegistration))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Registration> createRegistrationTask =
                this.registrationService.AddRegistrationAsync(inputRegistration);

            // then
            await Assert.ThrowsAsync <RegistrationServiceException>(() =>
                                                                    createRegistrationTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #4
0
        private IQueryable <Registration> TryCatch(ReturningQueryableRegistrationFunction returningQueryableRegistrationFunction)
        {
            try
            {
                return(returningQueryableRegistrationFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedRegistrationServiceException =
                    new FailedRegistrationServiceException(exception);

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

            var failedRegistrationServiceException =
                new FailedRegistrationServiceException(serviceException);

            var expectedRegistrationServiceException =
                new RegistrationServiceException(failedRegistrationServiceException);

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

            // when
            ValueTask <Registration> retrieveByIdRegistrationTask =
                this.registrationService.RetrieveRegistrationByIdAsync(someRegistrationId);

            // then
            await Assert.ThrowsAsync <RegistrationServiceException>(() =>
                                                                    retrieveByIdRegistrationTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllRegistrationsWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedRegistrationServiceException =
                new FailedRegistrationServiceException(serviceException);

            var expectedRegistrationServiceException =
                new RegistrationServiceException(failedRegistrationServiceException);

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

            // when
            Action retrieveAllRegistrations = () =>
                                              this.registrationService.RetrieveAllRegistrations();

            // then
            Assert.Throws <RegistrationServiceException>(
                retrieveAllRegistrations);

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

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

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