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);
            }
        }
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime        = GetRandomDateTime();
            Classroom      randomClassroom = CreateRandomClassroom(dateTime);
            Classroom      inputClassroom  = randomClassroom;

            inputClassroom.UpdatedBy   = inputClassroom.CreatedBy;
            inputClassroom.UpdatedDate = inputClassroom.CreatedDate;
            var serviceException = new Exception();

            var failedClassroomServiceException =
                new FailedClassroomServiceException(serviceException);

            var expectedClassroomServiceException =
                new ClassroomServiceException(failedClassroomServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertClassroomAsync(inputClassroom))
            .ThrowsAsync(serviceException);

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 3
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Classroom      randomClassroom      = CreateRandomClassroom(randomDateTime);
            Classroom      someClassroom        = randomClassroom;

            someClassroom.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedClassroomServiceException =
                new FailedClassroomServiceException(serviceException);

            var expectedClassroomServiceException =
                new ClassroomServiceException(failedClassroomServiceException);

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

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

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

            // then
            await Assert.ThrowsAsync <ClassroomServiceException>(() =>
                                                                 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.LogError(It.Is(SameExceptionAs(
                                                                    expectedClassroomServiceException))),
                                          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)
            {
                var failedClassroomServiceException =
                    new FailedClassroomServiceException(exception);

                throw CreateAndLogServiceException(failedClassroomServiceException);
            }
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedClassroomServiceException =
                new FailedClassroomServiceException(serviceException);

            var expectedClassroomServiceException =
                new ClassroomServiceException(failedClassroomServiceException);

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

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

            // then
            Assert.Throws <ClassroomServiceException>(retrieveAllClassroomsAction);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 6
0
        public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomClassroomId = Guid.NewGuid();
            Guid inputClassroomId  = randomClassroomId;
            var  serviceException  = new Exception();

            var failedClassroomServiceException =
                new FailedClassroomServiceException(serviceException);

            var expectedClassroomServiceException =
                new ClassroomServiceException(failedClassroomServiceException);

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

            // when
            ValueTask <Classroom> retrieveClassroomTask =
                this.classroomService.RetrieveClassroomById(inputClassroomId);

            // then
            await Assert.ThrowsAsync <ClassroomServiceException>(() =>
                                                                 retrieveClassroomTask.AsTask());

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

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

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