Esempio n. 1
0
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Attachment     someAttachment = CreateRandomAttachment(dateTime);

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

            var failedAttachmentServiceException =
                new FailedAttachmentServiceException(serviceException);

            var expectedAttachmentServiceException =
                new AttachmentServiceException(failedAttachmentServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAttachmentAsync(It.IsAny <Attachment>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Attachment> createAttachmentTask =
                this.attachmentService.AddAttachmentAsync(someAttachment);

            // then
            await Assert.ThrowsAsync <AttachmentServiceException>(() =>
                                                                  createAttachmentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Attachment     someAttachment       = CreateRandomAttachment(randomDateTime);

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

            var failedAttachmentServiceException =
                new FailedAttachmentServiceException(serviceException);

            var expectedAttachmentServiceException =
                new AttachmentServiceException(failedAttachmentServiceException);

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

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

            // when
            ValueTask <Attachment> modifyAttachmentTask =
                this.attachmentService.ModifyAttachmentAsync(someAttachment);

            // then
            await Assert.ThrowsAsync <AttachmentServiceException>(() =>
                                                                  modifyAttachmentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 3
0
        private async ValueTask <Attachment> TryCatch(ReturningAttachmentFunction returningAttachmentFunction)
        {
            try
            {
                return(await returningAttachmentFunction());
            }
            catch (NullAttachmentException nullAttachmentException)
            {
                throw CreateAndLogValidationException(nullAttachmentException);
            }
            catch (InvalidAttachmentException invalidAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidAttachmentInputException);
            }
            catch (NotFoundAttachmentException notFoundAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundAttachmentException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsAttachmentException =
                    new AlreadyExistsAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsAttachmentException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedAttachmentException = new LockedAttachmentException(dbUpdateConcurrencyException);

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

                throw CreateAndLogServiceException(failedAttachmentServiceException);
            }
        }
Esempio n. 4
0
        private IQueryable <Attachment> TryCatch(ReturningAttachmentsFunction returningAttachmentsFunction)
        {
            try
            {
                return(returningAttachmentsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedAttachmentServiceException =
                    new FailedAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedAttachmentServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogIt()
        {
            // given
            var someAttachmentId = Guid.NewGuid();
            var serviceException = new Exception();

            var failedAttachmentServiceException =
                new FailedAttachmentServiceException(serviceException);

            var expectedAttachmentServiceException =
                new AttachmentServiceException(failedAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(It.IsAny <Guid>()))
            .Throws(serviceException);

            // when
            ValueTask <Attachment> retrieveTask =
                this.attachmentService.RetrieveAttachmentByIdAsync(someAttachmentId);

            // then
            await Assert.ThrowsAsync <AttachmentServiceException>(() => retrieveTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 6
0
        public void ShouldThrowServiceExceptionOnRetrieveAllAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedAttachmentServiceException =
                new FailedAttachmentServiceException(serviceException);

            var expectedAttachmentServiceException =
                new AttachmentServiceException(failedAttachmentServiceException);

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

            // when
            Action retrieveAllAttachmentsAction = () =>
                                                  this.attachmentService.RetrieveAllAttachments();

            // then
            Assert.Throws <AttachmentServiceException>(
                retrieveAllAttachmentsAction);

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

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

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