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();
        }
        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 ShouldThrowValidationExceptionOnCreateWhenAttachmentIsNullAndLogItAsync()
        {
            // given
            Attachment randomAttachment        = null;
            Attachment nullAttachment          = randomAttachment;
            var        nullAttachmentException = new NullAttachmentException();

            var expectedAttachmentValidationException =
                new AttachmentValidationException(nullAttachmentException);

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

            // then
            await Assert.ThrowsAsync <AttachmentValidationException>(() =>
                                                                     createAttachmentTask.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();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenAttachmentIsNullAndLogItAsync()
        {
            //given
            Attachment invalidAttachment       = null;
            var        nullAttachmentException = new NullAttachmentException();

            var expectedAttachmentValidationException =
                new AttachmentValidationException(nullAttachmentException);

            //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 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();
        }
        private AttachmentValidationException CreateAndLogValidationException(Exception exception)
        {
            var attachmentValidationException = new AttachmentValidationException(exception);

            this.loggingBroker.LogError(attachmentValidationException);

            return(attachmentValidationException);
        }
Пример #7
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();
        }
Пример #8
0
        public async void ShouldThrowValidationExceptionOnCreateWhenAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime                = GetRandomDateTime();
            Attachment     randomAttachment        = CreateRandomAttachment(dateTime);
            Attachment     alreadyExistsAttachment = randomAttachment;

            alreadyExistsAttachment.UpdatedBy = alreadyExistsAttachment.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsAttachmentException =
                new AlreadyExistsAttachmentException(duplicateKeyException);

            var expectedAttachmentValidationException =
                new AttachmentValidationException(alreadyExistsAttachmentException);

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

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

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

            // then
            await Assert.ThrowsAsync <AttachmentValidationException>(() =>
                                                                     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(
                                                                    expectedAttachmentValidationException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #9
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();
        }
Пример #10
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();
        }
Пример #11
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();
        }