private async ValueTask <StudentAttachment> TryCatch(
            ReturningStudentAttachmentFunction returningStudentAttachmentFunction)
        {
            try
            {
                return(await returningStudentAttachmentFunction());
            }
            catch (NullStudentAttachmentException nullStudentAttachmentInputException)
            {
                throw CreateAndLogValidationException(nullStudentAttachmentInputException);
            }
            catch (InvalidStudentAttachmentException invalidStudentAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidStudentAttachmentInputException);
            }
            catch (NotFoundStudentAttachmentException notFoundStudentAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundStudentAttachmentException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentAttachmentException =
                    new AlreadyExistsStudentAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsStudentAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidStudentAttachmentReferenceException =
                    new InvalidStudentAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidStudentAttachmentReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedAttachmentException =
                    new LockedStudentAttachmentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedAttachmentException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentAttachmentServiceException =
                    new FailedStudentAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedStudentAttachmentServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            var  randomAttachmentId = Guid.NewGuid();
            var  randomStudentId    = Guid.NewGuid();
            Guid someAttachmentId   = randomAttachmentId;
            Guid someStudentId      = randomStudentId;
            var  serviceException   = new Exception();

            var failedStudentAttachmentServiceException =
                new FailedStudentAttachmentServiceException(serviceException);

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(failedStudentAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentAttachment> removeStudentAttachmentTask =
                this.studentAttachmentService.RemoveStudentAttachmentByIdAsync(
                    someStudentId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         removeStudentAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <StudentAttachment> TryCatch(ReturningStudentAttachmentsFunction returningStudentAttachmentsFunction)
        {
            try
            {
                return(returningStudentAttachmentsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedStudentAttachmentServiceException =
                    new FailedStudentAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedStudentAttachmentServiceException);
            }
        }
Пример #4
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment();
            StudentAttachment inputStudentAttachment  = randomStudentAttachment;
            var serviceException = new Exception();

            var failedStudentAttachmentServiceException =
                new FailedStudentAttachmentServiceException(serviceException);

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(failedStudentAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentAttachmentAsync(inputStudentAttachment))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentAttachment> addStudentAttachmentTask =
                this.studentAttachmentService.AddStudentAttachmentAsync(inputStudentAttachment);

            // then
            await Assert.ThrowsAsync <StudentAttachmentServiceException>(() =>
                                                                         addStudentAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #5
0
        public void ShouldThrowServiceExceptionOnRetrieveAllStudentAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedStudentAttachmentServiceException =
                new FailedStudentAttachmentServiceException(serviceException);

            var expectedStudentAttachmentServiceException =
                new StudentAttachmentServiceException(failedStudentAttachmentServiceException);

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

            // when
            Action retrieveAllStudentAttachmentsAction = () =>
                                                         this.studentAttachmentService.RetrieveAllStudentAttachments();

            // then
            Assert.Throws <StudentAttachmentServiceException>(
                retrieveAllStudentAttachmentsAction);

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

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

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