private IQueryable <Teacher> TryCatch(ReturningQueryableTeacherFunction returningQueryableTeacherFunction)
        {
            try
            {
                return(returningQueryableTeacherFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTeacherException = new LockedTeacherException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedTeacherException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomTeacherId = Guid.NewGuid();
            Guid inputTeacherId  = randomTeacherId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedTeacherException             = new LockedTeacherException(databaseUpdateConcurrencyException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(lockedTeacherException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherByIdAsync(inputTeacherId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Teacher> deleteTeacherTask =
                this.teacherService.DeleteTeacherByIdAsync(inputTeacherId);

            // then
            await Assert.ThrowsAsync <TeacherDependencyException>(() =>
                                                                  deleteTeacherTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #3
0
        private async ValueTask <Teacher> TryCatch(ReturningTeacherFunction returningTeacherFunction)
        {
            try
            {
                return(await returningTeacherFunction());
            }
            catch (NullTeacherException nullTeacherException)
            {
                throw CreateAndLogValidationException(nullTeacherException);
            }
            catch (InvalidTeacherException invalidTeacherException)
            {
                throw CreateAndLogValidationException(invalidTeacherException);
            }
            catch (NotFoundTeacherException notFoundTeacherException)
            {
                throw CreateAndLogValidationException(notFoundTeacherException);
            }
            catch (SqlException sqlException)
            {
                var failedTeacherStorageExceptin =
                    new FailedTeacherStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedTeacherStorageExceptin);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsTeacherException =
                    new AlreadyExistsTeacherException(duplicateKeyException);

                throw CreateAndLogDependencyValidationException(alreadyExistsTeacherException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTeacherException = new LockedTeacherException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedTeacherException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedTeacherStorageException =
                    new FailedTeacherStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedTeacherStorageException);
            }
            catch (Exception exception)
            {
                var failedTeacherServiceException =
                    new FailedTeacherServiceException(exception);

                throw CreateAndLogServiceException(failedTeacherServiceException);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Teacher        randomTeacher        = CreateRandomTeacher(randomDateTime);
            Teacher        someTeacher          = randomTeacher;

            someTeacher.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedTeacherException             = new LockedTeacherException(databaseUpdateConcurrencyException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(lockedTeacherException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherByIdAsync(someTeacher.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // when
            ValueTask <Teacher> modifyTeacherTask =
                this.teacherService.ModifyTeacherAsync(someTeacher);

            // then
            await Assert.ThrowsAsync <TeacherDependencyException>(() =>
                                                                  modifyTeacherTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherByIdAsync(someTeacher.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #5
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Teacher someTeacher = CreateRandomTeacher();
            var     databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedTeacherException =
                new LockedTeacherException(databaseUpdateConcurrencyException);

            var expectedTeacherDependencyValidationException =
                new TeacherDependencyValidationException(lockedTeacherException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(databaseUpdateConcurrencyException);

            // when
            ValueTask <Teacher> modifyTeacherTask =
                this.teacherService.ModifyTeacherAsync(someTeacher);

            // then
            await Assert.ThrowsAsync <TeacherDependencyValidationException>(() =>
                                                                            modifyTeacherTask.AsTask());

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

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

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

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

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