Esempio n. 1
0
        public async Task ShouldThrowValidationExceptionOnModifyIfAttachmentDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime              = GetRandomDateTime();
            Attachment     randomAttachment      = CreateRandomAttachment(dateTime);
            Attachment     nonExistentAttachment = randomAttachment;

            nonExistentAttachment.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            Attachment noAttachment = null;
            var        notFoundAttachmentException = new NotFoundAttachmentException(nonExistentAttachment.Id);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(notFoundAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(nonExistentAttachment.Id))
            .ReturnsAsync(noAttachment);

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

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public async Task ShouldThrowValidatonExceptionOnRemoveWhenStorageAttachmentIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTimeOffset        = GetRandomDateTime();
            Attachment     randomAttachment      = CreateRandomAttachment(dates: dateTimeOffset);
            Guid           inputAttachmentId     = randomAttachment.Id;
            Attachment     inputAttachment       = randomAttachment;
            Attachment     nullStorageAttachment = null;

            var notFoundAttachmentException = new NotFoundAttachmentException(inputAttachmentId);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(notFoundAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(inputAttachmentId))
            .ReturnsAsync(nullStorageAttachment);

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

            // then
            await Assert.ThrowsAsync <AttachmentValidationException>(() => actualAttachmentTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnRetrieveWhenStorageAttachmentIsNullAndLogItAsync()
        {
            //given
            Guid       randomAttachmentId          = Guid.NewGuid();
            Guid       inputAttachmentId           = randomAttachmentId;
            Attachment invalidStorageAttachment    = null;
            var        notFoundAttachmentException = new NotFoundAttachmentException(inputAttachmentId);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(notFoundAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(inputAttachmentId))
            .ReturnsAsync(invalidStorageAttachment);

            //when
            ValueTask <Attachment> retrieveAttachmentByIdTask =
                this.attachmentService.RetrieveAttachmentByIdAsync(inputAttachmentId);

            //then
            await Assert.ThrowsAsync <AttachmentValidationException>(() =>
                                                                     retrieveAttachmentByIdTask.AsTask());

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

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

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

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