public async Task WhenNameIsNotBlank_BehaviorShouldNotReturnAnError()
        {
            var sut   = new RequestValidationBehavior <SomeQuery, Guid>(validators);
            var query = new SomeQuery()
            {
                Name = "Scott"
            };

            var exception = await Record.ExceptionAsync(() =>
                                                        sut.Handle(
                                                            query,
                                                            new CancellationToken(),
                                                            mockHandler.Object));

            exception.Should().BeNull();
            mockHandler.Verify(_ => _(), Times.Once);
        }
        public async Task WhenNoValidatorsPresent_BehaviorShouldCallHandlerAndNotReturnAnError()
        {
            var sut   = new RequestValidationBehavior <SomeQuery, Guid>(Enumerable.Empty <IValidator <SomeQuery> >());
            var query = new SomeQuery()
            {
                Name = ""
            };

            var exception = await Record.ExceptionAsync(() =>
                                                        sut.Handle(
                                                            query,
                                                            new CancellationToken(),
                                                            mockHandler.Object));

            exception.Should().BeNull();
            mockHandler.Verify(_ => _(), Times.Once);
        }
        public async Task NameIsBlank_BehaviorShouldReturnAnErrorTypeOfValidationException()
        {
            var sut   = new RequestValidationBehavior <SomeQuery, Guid>(validators);
            var query = new SomeQuery()
            {
                Name = ""
            };

            var exception = await Record.ExceptionAsync(() =>
                                                        sut.Handle(
                                                            query,
                                                            new CancellationToken(),
                                                            mockHandler.Object));


            exception.Should().NotBeNull();
            exception.Should().BeOfType <CleanFunc.Application.Common.Exceptions.ValidationException>();
            (exception as CleanFunc.Application.Common.Exceptions.ValidationException).Errors.Count().Should().Be(1);
            mockHandler.Verify(_ => _(), Times.Never);
        }
Example #4
0
        public async Task Should_Not_Throw_Validation_Exception_When_Object_Is_Valid()
        {
            var query = new GetTestDataQuery()
            {
                Id    = Guid.NewGuid(),
                Query = "q_random_query"
            };

            var expectedResponse = new TestDataResponse();

            var behaviour = new RequestValidationBehavior <GetTestDataQuery, TestDataResponse>(
                new List <IValidator <GetTestDataQuery> >()
            {
                new GetTestDataQuery.GetTestDataQueryValidator()
            }
                );

            var response =
                await behaviour.Handle(query, CancellationToken.None, () => Task.FromResult(expectedResponse));

            Assert.Equal(expectedResponse, response);
        }
        public void Handle_InvalidValidator_ThrowsValidationException()
        {
            //Arrange
            var sut = new RequestValidationBehavior <GetCustomerQuery, Customer>(
                new List <IValidator <GetCustomerQuery> >
            {
                new GetCustomerQueryValidator()
            }
                );

            //Act
            Func <Task> func = async() =>
                               await sut.Handle(
                new GetCustomerQuery(),
                CancellationToken.None,
                () => Task.FromResult(new Customer())
                );

            //Assert
            func.Should().Throw <ValidationException>()
            .Where(ex => ex.Errors.ToList().Count == 1);
        }
        public async Task Handle_ValidValidator_ReturnsResponse(
            Customer customer
            )
        {
            //Arrange
            var sut = new RequestValidationBehavior <GetCustomerQuery, Customer>(
                new List <IValidator <GetCustomerQuery> >
            {
                new GetCustomerQueryValidator()
            }
                );

            //Act

            var result = await sut.Handle(
                new GetCustomerQuery { CustomerNumber = "1" },
                CancellationToken.None,
                () => Task.FromResult(customer)
                );

            //Assert
            result.Should().Be(customer);
        }
Example #7
0
        public async Task Should_Throw_Validation_Exception_When_Object_Is_Invalid()
        {
            var query = new GetTestDataQuery();

            var behaviour = new RequestValidationBehavior <GetTestDataQuery, TestDataResponse>(
                new List <IValidator <GetTestDataQuery> >()
            {
                new GetTestDataQuery.GetTestDataQueryValidator()
            }
                );

            var ex = await Assert.ThrowsAsync <Caravel.Exceptions.ValidationException>(() =>
                                                                                       behaviour.Handle(query, CancellationToken.None, () => Task.FromResult(new TestDataResponse()))
                                                                                       );

            Assert.Equal("invalid_fields", ex.Error.Code);
            Assert.Equal("Payload contains invalid fields.", ex.Error.Message);
            Assert.Equal(Errors.Severity.Low, ex.Error.Severity);
            Assert.Equal(2, ex.Errors.Count);

            Assert.Equal("'Id' must not be empty.", ex.Errors["Id"][0]);
            Assert.Equal("'Query' must not be empty.", ex.Errors["Query"][0]);
            Assert.Equal("The specified condition was not met for 'Query'.", ex.Errors["Query"][1]);
        }