public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExamFee someStudentExamFee   = CreateRandomStudentExamFee(randomDateTime);

            someStudentExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedStudentExamFeeException =
                new LockedStudentExamFeeException(databaseUpdateConcurrencyException);

            var expectedStudentExamFeeDependencyException =
                new StudentExamFeeDependencyException(lockedStudentExamFeeException);

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

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

            // when
            ValueTask <StudentExamFee> modifyStudentExamFeeTask =
                this.studentExamFeeService.ModifyStudentExamFeeAsync(someStudentExamFee);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         modifyStudentExamFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
예제 #2
0
        private async ValueTask <StudentExamFee> TryCatch(
            ReturningStudentExamFeeFunction returningStudentExamFeeFunction)
        {
            try
            {
                return(await returningStudentExamFeeFunction());
            }
            catch (NullStudentExamFeeException nullStudentExamFeeException)
            {
                throw CreateAndLogValidationException(nullStudentExamFeeException);
            }
            catch (NotFoundStudentExamFeeException notFoundStudentExamFeeException)
            {
                throw CreateAndLogValidationException(notFoundStudentExamFeeException);
            }
            catch (InvalidStudentExamFeeException invalidStudentExamFeeInputException)
            {
                throw CreateAndLogValidationException(invalidStudentExamFeeInputException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentExamFeeException =
                    new AlreadyExistsStudentExamFeeException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsStudentExamFeeException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentExamFeeException =
                    new LockedStudentExamFeeException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentExamFeeException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentExamFeeServiceException =
                    new FailedStudentExamFeeServiceException(exception);

                throw CreateAndLogServiceException(failedStudentExamFeeServiceException);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId = Guid.NewGuid();
            Guid someExamFeeId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedAttachmentException =
                new LockedStudentExamFeeException(databaseUpdateConcurrencyException);

            var expectedStudentExamFeeException =
                new StudentExamFeeDependencyException(lockedAttachmentException);

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

            // when
            ValueTask <StudentExamFee> removeStudentExamFeeTask =
                this.studentExamFeeService.RemoveStudentExamFeeByIdAsync(someStudentId,
                                                                         someExamFeeId);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         removeStudentExamFeeTask.AsTask());

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

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

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

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