示例#1
0
        private async ValueTask <StudentRegistration> TryCatch(
            ReturningStudentRegistrationFunction returningStudentRegistrationFunction)
        {
            try
            {
                return(await returningStudentRegistrationFunction());
            }
            catch (NullStudentRegistrationException nullStudentRegistrationException)
            {
                throw CreateAndLogValidationException(nullStudentRegistrationException);
            }
            catch (InvalidStudentRegistrationException invalidStudentRegistrationException)
            {
                throw CreateAndLogValidationException(invalidStudentRegistrationException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundStudentRegistrationException nullStudentRegistrationException)
            {
                throw CreateAndLogValidationException(nullStudentRegistrationException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentRegistrationException =
                    new AlreadyExistsStudentRegistrationException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsStudentRegistrationException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentRegistrationException =
                    new LockedStudentRegistrationException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentRegistrationException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidStudentRegistrationReferenceException =
                    new InvalidStudentRegistrationReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidStudentRegistrationReferenceException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentRegistrationServiceException =
                    new FailedStudentRegistrationServiceException(exception);

                throw CreateAndLogServiceException(failedStudentRegistrationServiceException);
            }
        }
示例#2
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId      = Guid.NewGuid();
            Guid inputStudentId       = randomStudentId;
            Guid randomRegistrationId = Guid.NewGuid();
            Guid inputRegistrationId  = randomRegistrationId;
            StudentRegistration someStudentRegistration = CreateRandomStudentRegistration();

            var databaseUpdateConcurrencyException =
                new DbUpdateConcurrencyException();

            var lockedStudentRegistrationException =
                new LockedStudentRegistrationException(databaseUpdateConcurrencyException);

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(lockedStudentRegistrationException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync(someStudentRegistration);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteStudentRegistrationAsync(It.IsAny <StudentRegistration>()))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <StudentRegistration> deleteStudentRegistrationTask =
                this.studentRegistrationService.RemoveStudentRegistrationByIdsAsync(
                    inputStudentId,
                    inputRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationDependencyException>(() =>
                                                                              deleteStudentRegistrationTask.AsTask());

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

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

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

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