예제 #1
0
        public async Task ShouldThrowValidationExceptionOnModifyWhenCategoryTitleIsInvalidAndLogItAsync(
            string invalidCategoryName)
        {
            // given
            DateTimeOffset dateTime        = GetRandomDateTime();
            Category       randomCategory  = CreateRandomCategory(dateTime);
            Category       invalidCategory = randomCategory;

            invalidCategory.Title = invalidCategoryName;

            var invalidCategoryException = new InvalidCategoryException(
                parameterName: nameof(Category.Title),
                parameterValue: invalidCategory.Title);

            var expectedCategoryValidationException =
                new CategoryValidationException(invalidCategoryException);

            // when
            ValueTask <Category> modifyCategoryTask =
                this.categoryService.ModifyCategoryAsync(invalidCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   modifyCategoryTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
예제 #2
0
        public async void ShouldThrowValidationExceptionOnModifyWhenCategoryIsNullAndLogItAsync()
        {
            // given
            Category randomCategory        = null;
            Category nullCategory          = randomCategory;
            var      nullCategoryException = new NullCategoryException();

            var expectedCategoryValidationException =
                new CategoryValidationException(nullCategoryException);

            // when
            ValueTask <Category> modifyCategoryTask =
                this.categoryService.ModifyCategoryAsync(nullCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   modifyCategoryTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #3
0
        private CategoryValidationException CreateAndLogValidationException(Exception exception)
        {
            var CategoryValidationException = new CategoryValidationException(exception);

            this.loggingBroker.LogError((Exception)CategoryValidationException);

            return(CategoryValidationException);
        }
예제 #4
0
        public async void ShouldThrowValidationExceptionOnAddWhenCategoryAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime              = GetRandomDateTime();
            Category       randomCategory        = CreateRandomCategory(dateTime);
            Category       alreadyExistsCategory = randomCategory;

            alreadyExistsCategory.UpdatedBy = alreadyExistsCategory.CreatedBy;

            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsCategoryException =
                new AlreadyExistsCategoryException(duplicateKeyException);

            var expectedCategoryValidationException =
                new CategoryValidationException(alreadyExistsCategoryException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCategoryAsync(alreadyExistsCategory))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Category> createCategoryTask =
                this.categoryService.AddCategoryAsync(alreadyExistsCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   createCategoryTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #5
0
        public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync(int minutes)
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Category       randomCategory = CreateRandomCategory(dateTime);
            Category       inputCategory  = randomCategory;

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

            var invalidCategoryValidationException = new InvalidCategoryException(
                parameterName: nameof(Category.CreatedDate),
                parameterValue: inputCategory.CreatedDate);

            var expectedCategoryValidationException =
                new CategoryValidationException(invalidCategoryValidationException);

            // when
            ValueTask <Category> createCategoryTask =
                this.categoryService.AddCategoryAsync(inputCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   createCategoryTask.AsTask());

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

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

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


            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenStorageCategoryIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime            = GetRandomDateTime();
            Category       randomCategory      = CreateRandomCategory(dateTime);
            Guid           inputCategoryId     = randomCategory.Id;
            Category       inputCategory       = randomCategory;
            Category       nullStorageCategory = null;

            var notFoundCategoryException = new NotFoundCategoryException(inputCategoryId);

            var expectedCategoryValidationException =
                new CategoryValidationException(notFoundCategoryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(inputCategoryId))
            .ReturnsAsync(nullStorageCategory);

            // when
            ValueTask <Category> actualCategoryTask =
                this.categoryService.RemoveCategoryByIdAsync(inputCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() => actualCategoryTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
예제 #7
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenStorageCategoryIsNullAndLogItAsync()
        {
            // given
            Guid     randomCategoryId          = Guid.NewGuid();
            Guid     someCategoryId            = randomCategoryId;
            Category invalidStorageCategory    = null;
            var      notFoundCategoryException = new NotFoundCategoryException(someCategoryId);

            var exceptionCategoryValidationException =
                new CategoryValidationException(notFoundCategoryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(invalidStorageCategory);

            // when
            ValueTask <Category> retrieveCategoryByIdTask =
                this.categoryService.RetrieveCategoryByIdAsync(someCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   retrieveCategoryByIdTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #8
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomCategoryId = default;
            Guid inputCategoryId  = randomCategoryId;

            var invalidCategoryInputException = new InvalidCategoryInputException(
                parameterName: nameof(Category.Id),
                parameterValue: inputCategoryId);

            var expectedCategoryValidationException =
                new CategoryValidationException(invalidCategoryInputException);

            // when
            ValueTask <Category> retrieveCategoryByIdTask =
                this.categoryService.RetrieveCategoryByIdAsync(inputCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   retrieveCategoryByIdTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
예제 #9
0
        public async void ShouldThrowValidationExceptionOnModifyWhenIdIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Category       randomCategory = CreateRandomCategory(dateTime);
            Category       inputCategory  = randomCategory;

            inputCategory.Id = default;

            var invalidCategoryInputException = new InvalidCategoryException(
                parameterName: nameof(Category.Id),
                parameterValue: inputCategory.Id);

            var expectedCategoryValidationException =
                new CategoryValidationException(invalidCategoryInputException);

            // when
            ValueTask <Category> modifyCategoryTask =
                this.categoryService.ModifyCategoryAsync(inputCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   modifyCategoryTask.AsTask());

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

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

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