Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            Teacher randomTeacher           = CreateRandomTeacher();
            var     databaseUpdateException = new DbUpdateException();

            var failedTeacherStorageException =
                new FailedTeacherStorageException(databaseUpdateException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(failedTeacherStorageException);

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

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

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherByIdAsync(randomTeacher.Id),
                                          Times.Never);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        public async Task ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccursAndLogItAsync()
        {
            // given
            Teacher      someTeacher  = CreateRandomTeacher();
            SqlException sqlException = GetSqlException();

            var failedTeacherStorageException =
                new FailedTeacherStorageException(sqlException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(failedTeacherStorageException);

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

            // when
            ValueTask <Teacher> addTeacherTask =
                this.teacherService.CreateTeacherAsync(someTeacher);

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 4
0
        private IQueryable <Teacher> TryCatch(ReturningQueryableTeacherFunction returningQueryableTeacherFunction)
        {
            try
            {
                return(returningQueryableTeacherFunction());
            }
            catch (SqlException sqlException)
            {
                var failedTeacherStorageExceptin =
                    new FailedTeacherStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedTeacherStorageExceptin);
            }
            catch (Exception exception)
            {
                var failedTeacherServiceException =
                    new FailedTeacherServiceException(exception);

                throw CreateAndLogServiceException(failedTeacherServiceException);
            }
        }
        public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllIfSqlErrorOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var failedTeacherStorageException =
                new FailedTeacherStorageException(sqlException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(failedTeacherStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllTeachers())
            .Throws(sqlException);

            // when
            Action retrieveAllTeachersAction = () =>
                                               this.teacherService.RetrieveAllTeachers();

            // then
            Assert.Throws <TeacherDependencyException>(
                retrieveAllTeachersAction);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveIfDbUpdateErrorOccursAndLogItAsync()
        {
            // given
            Guid someTeacherId           = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var failedTeacherStorageException =
                new FailedTeacherStorageException(databaseUpdateException);

            var expectedTeacherDependencyException =
                new TeacherDependencyException(failedTeacherStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <Teacher> retrieveTeacherTask =
                this.teacherService.RetrieveTeacherByIdAsync(someTeacherId);

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

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

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

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