Exemple #1
0
        public void ShouldThrowServiceExceptionOnRetrieveAllAssignmentAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedAssignmentAttachmentServiceException =
                new FailedAssignmentAttachmentServiceException(serviceException);

            var expectedAssignmentAttachmentServiceException =
                new AssignmentAttachmentServiceException(failedAssignmentAttachmentServiceException);

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

            // when
            Action retrieveAllAssignmentAttachmentsAction = () =>
                                                            this.assignmentAttachmentService.RetrieveAllAssignmentAttachments();

            // then
            Assert.Throws <AssignmentAttachmentServiceException>(
                retrieveAllAssignmentAttachmentsAction);

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someAssignmentId = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedAssignmentAttachmentServiceException =
                new FailedAssignmentAttachmentServiceException(serviceException);

            var expectedAssignmentAttachmentException =
                new AssignmentAttachmentServiceException(failedAssignmentAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAssignmentAttachmentByIdAsync(
                                             It.IsAny <Guid>(),
                                             It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <AssignmentAttachment> retrieveAssignmentAttachmentTask =
                this.assignmentAttachmentService.RetrieveAssignmentAttachmentByIdAsync(
                    someAssignmentId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <AssignmentAttachmentServiceException>(() =>
                                                                            retrieveAssignmentAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #3
0
        private IQueryable <AssignmentAttachment> TryCatch(
            ReturningAssignmentAttachmentsFunction returningAssignmentAttachmentsFunction)
        {
            try
            {
                return(returningAssignmentAttachmentsFunction());
            }
            catch (SqlException sqlException)
            {
                var failedAssigmentAttachmentStorageException =
                    new FailedAssignmentAttachmentStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedAssigmentAttachmentStorageException);
            }
            catch (Exception exception)
            {
                var failedAssignmentAttachmentServiceException =
                    new FailedAssignmentAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedAssignmentAttachmentServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            AssignmentAttachment someAssignmentAttachment = CreateRandomAssignmentAttachment();
            var serviceException = new Exception();

            var failedAssignmentAttachmentServiceException =
                new FailedAssignmentAttachmentServiceException(serviceException);

            var expectedAssignmentAttachmentServiceException =
                new AssignmentAttachmentServiceException(
                    failedAssignmentAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAssignmentAttachmentAsync(someAssignmentAttachment))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <AssignmentAttachment> addAssignmentAttachmentTask =
                this.assignmentAttachmentService.AddAssignmentAttachmentAsync(someAssignmentAttachment);

            // then
            await Assert.ThrowsAsync <AssignmentAttachmentServiceException>(() =>
                                                                            addAssignmentAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #5
0
        private async ValueTask <AssignmentAttachment> TryCatch(
            ReturningAssignmentAttachmentFunction returningAssignmentAttachmentFunction)
        {
            try
            {
                return(await returningAssignmentAttachmentFunction());
            }
            catch (NullAssignmentAttachmentException nullAssignmentAttachmentInputException)
            {
                throw CreateAndLogValidationException(nullAssignmentAttachmentInputException);
            }
            catch (InvalidAssignmentAttachmentException invalidAssignmentAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidAssignmentAttachmentInputException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsAssignmentAttachmentException =
                    new AlreadyExistsAssignmentAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsAssignmentAttachmentException);
            }
            catch (DbUpdateConcurrencyException databaseUpdateConcurrencyException)
            {
                var lockedAssignmentAttachmentException =
                    new LockedAssignmentAttachmentException(databaseUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedAssignmentAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidAssignmentAttachmentReferenceException =
                    new InvalidAssignmentAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidAssignmentAttachmentReferenceException);
            }
            catch (SqlException sqlException)
            {
                var failedAssigmentAttachmentStorageException =
                    new FailedAssignmentAttachmentStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedAssigmentAttachmentStorageException);
            }
            catch (DbUpdateException databaseUpdateException)
            {
                var failedAssigmentAttachmentStorageException =
                    new FailedAssignmentAttachmentStorageException(databaseUpdateException);

                throw CreateAndLogDependencyException(failedAssigmentAttachmentStorageException);
            }
            catch (NotFoundAssignmentAttachmentException notFoundAssignmentAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundAssignmentAttachmentException);
            }
            catch (Exception exception)
            {
                var failedAssignmentAttachmentServiceException =
                    new FailedAssignmentAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedAssignmentAttachmentServiceException);
            }
        }