Exemplo n.º 1
0
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomSemesterCourseId             = Guid.NewGuid();
            Guid inputSemesterCourseId              = randomSemesterCourseId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedSemesterCourseException = new LockedSemesterCourseException(databaseUpdateConcurrencyException);

            var expectedSemesterCourseException = new SemesterCourseDependencyException(lockedSemesterCourseException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <SemesterCourse> deleteSemesterCourseTask =
                this.semesterCourseService.RemoveSemesterCourseByIdAsync(inputSemesterCourseId);

            // then
            await Assert.ThrowsAsync <SemesterCourseDependencyException>(() => deleteSemesterCourseTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 2
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(randomDateTime);
            SemesterCourse someSemesterCourse   = randomSemesterCourse;

            someSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedSemesterCourseException      = new LockedSemesterCourseException(databaseUpdateConcurrencyException);

            var expectedSemesterCourseDependencyException =
                new SemesterCourseDependencyException(lockedSemesterCourseException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // when
            ValueTask <SemesterCourse> modifySemesterCourseTask =
                this.semesterCourseService.ModifySemesterCourseAsync(someSemesterCourse);

            // then
            await Assert.ThrowsAsync <SemesterCourseDependencyException>(() =>
                                                                         modifySemesterCourseTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <SemesterCourse> TryCatch(
            ReturningSemesterCourseFunction returningSemesterCourseFunction)
        {
            try
            {
                return(await returningSemesterCourseFunction());
            }
            catch (NullSemesterCourseException nullSemesterCourseException)
            {
                throw CreateAndLogValidationException(nullSemesterCourseException);
            }
            catch (InvalidSemesterCourseException invalidSemesterCourseInputException)
            {
                throw CreateAndLogValidationException(invalidSemesterCourseInputException);
            }
            catch (NotFoundSemesterCourseException nullSemesterCourseException)
            {
                throw CreateAndLogValidationException(nullSemesterCourseException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsSemesterCourseException =
                    new AlreadyExistsSemesterCourseException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsSemesterCourseException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedSemesterCourseException = new LockedSemesterCourseException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedSemesterCourseException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedSemesterCourseServiceException =
                    new FailedSemesterCourseServiceException(exception);

                throw CreateAndLogServiceException(failedSemesterCourseServiceException);
            }
        }