Ejemplo n.º 1
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomAttachmentId = Guid.NewGuid();
            Guid inputAttachmentId  = randomAttachmentId;
            var  exception          = new Exception();

            var expectedAttachmentServiceException =
                new AttachmentServiceException(exception);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(inputAttachmentId))
            .ThrowsAsync(exception);

            // when
            ValueTask <Attachment> deleteAttachmentTask =
                this.attachmentService.RemoveAttachmentByIdAsync(inputAttachmentId);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 2
0
        public void ShouldThrowServiceExceptionOnRetrieveAllAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedAttachmentServiceException =
                new AttachmentServiceException(exception);

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

            // when . then
            Assert.Throws <AttachmentServiceException>(() =>
                                                       this.attachmentService.RetrieveAllAttachments());

            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();
        }
        private AttachmentServiceException CreateAndLogServiceException(Exception exception)
        {
            var attachmentServiceException = new AttachmentServiceException(exception);

            this.loggingBroker.LogError(attachmentServiceException);

            return(attachmentServiceException);
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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();
        }
        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();
        }