public async Task ShouldThrowValidationExceptionOnModifyWhenAttachmentLabelIsInvalidAndLogItAsync(
            string invalidAttachmentLabel)
        {
            // given
            Attachment randomAttachment  = CreateRandomAttachment(DateTime.Now);
            Attachment invalidAttachment = randomAttachment;

            invalidAttachment.Label = invalidAttachmentLabel;

            var invalidAttachmentException = new InvalidAttachmentException(
                parameterName: nameof(Attachment.Label),
                parameterValue: invalidAttachment.Label);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(invalidAttachmentException);

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime         = GetRandomDateTime();
            Attachment     randomAttachment = CreateRandomAttachment(dateTime);
            Attachment     inputAttachment  = randomAttachment;

            var invalidAttachmentInputException = new InvalidAttachmentException(
                parameterName: nameof(Attachment.UpdatedDate),
                parameterValue: inputAttachment.UpdatedDate);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(invalidAttachmentInputException);

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #3
0
 public static void ValidateNotEmpty(string b64PartOnly)
 {
     if (b64PartOnly.Equals("data:"))
     {
         throw InvalidAttachmentException.BlankFileNotAllowed();
     }
 }
        public async void ShouldThrowValidationExceptionOnRetrieveWhenIdIsInvalidAndLogItAsync()
        {
            //given
            Guid randomAttachmentId = default;
            Guid inputAttachmentId  = randomAttachmentId;

            var invalidAttachmentInputException = new InvalidAttachmentException(
                parameterName: nameof(Attachment.Id),
                parameterValue: inputAttachmentId);

            var expectedAttachmentValidationException = new AttachmentValidationException(invalidAttachmentInputException);

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

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #5
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Attachment     randomAttachment      = CreateRandomAttachment(randomDate);
            Attachment     invalidAttachment     = randomAttachment;

            invalidAttachment.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Attachment storageAttachment = randomAttachment.DeepClone();
            Guid       attachmentId      = invalidAttachment.Id;

            invalidAttachment.CreatedBy = invalidCreatedBy;

            var invalidAttachmentInputException = new InvalidAttachmentException(
                parameterName: nameof(Attachment.CreatedBy),
                parameterValue: invalidAttachment.CreatedBy);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(invalidAttachmentInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(attachmentId))
            .ReturnsAsync(storageAttachment);

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

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Example #6
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset dateTime         = GetRandomDateTime();
            Attachment     randomAttachment = CreateRandomAttachment(dateTime);
            Attachment     inputAttachment  = randomAttachment;

            inputAttachment.UpdatedBy   = inputAttachment.CreatedBy;
            inputAttachment.CreatedDate = dateTime.AddMinutes(minutes);
            inputAttachment.UpdatedDate = inputAttachment.CreatedDate;

            var invalidAttachmentInputException = new InvalidAttachmentException(
                parameterName: nameof(Attachment.CreatedDate),
                parameterValue: inputAttachment.CreatedDate);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(invalidAttachmentInputException);

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

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

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

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

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

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

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