public async Task ShouldThrowCriticalDependencyExceptionOnDeleteWhenSqlErrorOccursAndLogItAsync()
        {
            // given
            Guid         randomClassroomId = Guid.NewGuid();
            Guid         inputClassroomId  = randomClassroomId;
            SqlException sqlException      = GetSqlException();
            var          failedClassroomStorageException = new FailedClassroomStorageException(sqlException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectClassroomByIdAsync(inputClassroomId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <Classroom> deleteClassroomTask =
                this.classroomService.RemoveClassroomAsync(inputClassroomId);

            // then
            await Assert.ThrowsAsync <ClassroomDependencyException>(() =>
                                                                    deleteClassroomTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <Classroom> TryCatch(ReturningClassroomFunction returningClassroomFunction)
        {
            try
            {
                return(await returningClassroomFunction());
            }
            catch (NullClassroomException nullClassroomException)
            {
                throw CreateAndLogValidationException(nullClassroomException);
            }
            catch (InvalidClassroomException invalidClassroomInputException)
            {
                throw CreateAndLogValidationException(invalidClassroomInputException);
            }
            catch (NotFoundClassroomException notFoundClassroomException)
            {
                throw CreateAndLogValidationException(notFoundClassroomException);
            }
            catch (SqlException sqlException)
            {
                var failedClassroomStorageException =
                    new FailedClassroomStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedClassroomStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsClassroomException =
                    new AlreadyExistsClassroomException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsClassroomException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedClassroomException = new LockedClassroomException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedClassroomException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedClassroomStorageException =
                    new FailedClassroomStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedClassroomStorageException);
            }
            catch (Exception exception)
            {
                var failedClassroomServiceException =
                    new FailedClassroomServiceException(exception);

                throw CreateAndLogServiceException(failedClassroomServiceException);
            }
        }
Example #3
0
        public async Task ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Classroom      randomClassroom      = CreateRandomClassroom(randomDateTime);
            Classroom      someClassroom        = randomClassroom;

            someClassroom.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            SqlException sqlException = GetSqlException();
            var          failedClassroomStorageException = new FailedClassroomStorageException(sqlException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomStorageException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectClassroomByIdAsync(someClassroom.Id))
            .ThrowsAsync(sqlException);

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

            // when
            ValueTask <Classroom> modifyClassroomTask =
                this.classroomService.ModifyClassroomAsync(someClassroom);

            // then
            await Assert.ThrowsAsync <ClassroomDependencyException>(() =>
                                                                    modifyClassroomTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectClassroomByIdAsync(someClassroom.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <Classroom> TryCatch(
            ReturningQueryableClassroomFunction returningQueryableClassroomFunction)
        {
            try
            {
                return(returningQueryableClassroomFunction());
            }
            catch (SqlException sqlException)
            {
                var failedClassroomStorageException = new FailedClassroomStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedClassroomStorageException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }
        public async Task ShouldThrowCriticalDependencyExceptionOnCreateWhenSqlErrorOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Classroom      someClassroom = CreateRandomClassroom(dateTime);
            var            sqlException  = GetSqlException();

            var failedClassroomStorageException =
                new FailedClassroomStorageException(sqlException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomStorageException);

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

            // when
            ValueTask <Classroom> createClassroomTask =
                this.classroomService.CreateClassroomAsync(someClassroom);

            // then
            await Assert.ThrowsAsync <ClassroomDependencyException>(() =>
                                                                    createClassroomTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #6
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            Classroom randomClassroom         = CreateRandomClassroom();
            var       databaseUpdateException = new DbUpdateException();

            var failedClassroomException =
                new FailedClassroomStorageException(databaseUpdateException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomException);

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

            // when
            ValueTask <Classroom> modifyClassroomTask =
                this.classroomService.ModifyClassroomAsync(randomClassroom);

            // then
            await Assert.ThrowsAsync <ClassroomDependencyException>(() =>
                                                                    modifyClassroomTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var failedClassroomStorageException =
                new FailedClassroomStorageException(sqlException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomStorageException);

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

            // when
            Action retrieveAllCountriesAction = () => this.classroomService.RetrieveAllClassrooms();

            // then
            Assert.Throws <ClassroomDependencyException>(retrieveAllCountriesAction);

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

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

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

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

            var failedClassroomException =
                new FailedClassroomStorageException(databaseUpdateException);

            var expectedClassroomDependencyException =
                new ClassroomDependencyException(failedClassroomException);

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

            // when
            ValueTask <Classroom> deleteClassroomTask =
                this.classroomService.RemoveClassroomAsync(someClassroomId);

            // then
            await Assert.ThrowsAsync <ClassroomDependencyException>(() =>
                                                                    deleteClassroomTask.AsTask());

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

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

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