[ 
https://issues.apache.org/jira/browse/CAMEL-12565?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Michael Dzuba updated CAMEL-12565:
----------------------------------
    Description: 
{code:java}
package org.mike.tests;

import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ValidatorTests extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                validator()
                        .type(String.class)
                        
.withExpression(bodyAs(String.class).isEqualToIgnoreCase("valid"));

                onException(ValidationException.class)
                        .handled(true)
                        .log("Invalid predicate: ${exception.message}")
                        .to("mock:invalid");

                from("direct:in")
                        
//.validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
                        .outputTypeWithValidate(String.class) // or 
.inputTypeWithValidate(String.class)
                        .to("mock:out");
            }
        };
    }

    @Test
    public void testValid() throws InterruptedException {

        getMockEndpoint("mock:out").expectedMessageCount(1);
        getMockEndpoint("mock:invalid").expectedMessageCount(0);

        template.sendBody("direct:in", "valid");

        assertMockEndpointsSatisfied();
    }

    @Test
    public void testInvalid() throws InterruptedException {
        getMockEndpoint("mock:out").expectedMessageCount(0);
        getMockEndpoint("mock:invalid").expectedMessageCount(1);

        template.sendBody("direct:in", "wrong");

        assertMockEndpointsSatisfied();
    }
}


{code}
Expected result: both tests pass

Actual result: 'testValid' - passed, 'testInvalid' - failed

If uncomment line 25 & comment 26
{code:java}
.validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
//.inputTypeWithValidate(String.class)
{code}
tests will OK

 

Other test case with same results

 
{code:java}
package org.mike.tests;

import org.apache.camel.Message;
import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.spi.DataType;
import org.apache.camel.spi.Validator;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeanValidatorTest extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                validator()
                        .type("toValidate")
                        .withBean("testValidator");

                onException(ValidationException.class)
                        .handled(true)
                        .log("Invalid validation: ${exception.message}")
                        .to("mock:invalid");

                from("direct:in")
                        .outputTypeWithValidate("toValidate")
                        .to("mock:out");
            }
        };
    }

    public static class TestValidator extends Validator {
        private static final Logger LOG = 
LoggerFactory.getLogger(TestValidator.class);

        @Override
        public void validate(Message message, DataType type) throws 
ValidationException {
            Object body = message.getBody();
            LOG.info("Validating : [{}]", body);
            if (body instanceof String && body.equals("valid")) {
                LOG.info("OK");
            } else {
                throw new ValidationException(message.getExchange(), "Wrong 
content");
            }
        }
    }


    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();

        registry.bind("testValidator", new TestValidator());

        return registry;
    }

    @Test
    public void testValid() throws InterruptedException {

        getMockEndpoint("mock:out").expectedMessageCount(1);
        getMockEndpoint("mock:invalid").expectedMessageCount(0);

        template.sendBody("direct:in", "valid");

        assertMockEndpointsSatisfied();
    }

    @Test
    public void testInvalid() throws InterruptedException {
        getMockEndpoint("mock:out").expectedMessageCount(0);
        getMockEndpoint("mock:invalid").expectedMessageCount(1);

        template.sendBody("direct:in", "wrong");

        assertMockEndpointsSatisfied();
    }
}

{code}

  was:
{code:java}
package org.mike.tests;

import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ValidatorTests extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                validator()
                        .type(String.class)
                        
.withExpression(bodyAs(String.class).isEqualToIgnoreCase("valid"));

                onException(ValidationException.class)
                        .handled(true)
                        .log("Invalid predicate: ${exception.message}")
                        .to("mock:invalid");

                from("direct:in")
                        
//.validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
                        .inputTypeWithValidate(String.class)
                        .to("mock:out");
            }
        };
    }

    @Test
    public void testValid() throws InterruptedException {

        getMockEndpoint("mock:out").expectedMessageCount(1);
        getMockEndpoint("mock:invalid").expectedMessageCount(0);

        template.sendBody("direct:in", "valid");

        assertMockEndpointsSatisfied();
    }

    @Test
    public void testInvalid() throws InterruptedException {
        getMockEndpoint("mock:out").expectedMessageCount(0);
        getMockEndpoint("mock:invalid").expectedMessageCount(1);

        template.sendBody("direct:in", "wrong");

        assertMockEndpointsSatisfied();
    }
}


{code}
Expected result: both tests pass

Actual result: 'testValid' - passed, 'testInvalid' - failed

If uncomment line 25 & comment 26
{code:java}
.validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
//.inputTypeWithValidate(String.class)
{code}
tests will OK

 

Other test case with same results

 
{code:java}
package org.mike.tests;

import org.apache.camel.Message;
import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.spi.DataType;
import org.apache.camel.spi.Validator;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeanValidatorTest extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                validator()
                        .type("toValidate")
                        .withBean("testValidator");

                onException(ValidationException.class)
                        .handled(true)
                        .log("Invalid validation: ${exception.message}")
                        .to("mock:invalid");

                from("direct:in")
                        .outputTypeWithValidate("toValidate")
                        .to("mock:out");
            }
        };
    }

    public static class TestValidator extends Validator {
        private static final Logger LOG = 
LoggerFactory.getLogger(TestValidator.class);

        @Override
        public void validate(Message message, DataType type) throws 
ValidationException {
            Object body = message.getBody();
            LOG.info("Validating : [{}]", body);
            if (body instanceof String && body.equals("valid")) {
                LOG.info("OK");
            } else {
                throw new ValidationException(message.getExchange(), "Wrong 
content");
            }
        }
    }


    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();

        registry.bind("testValidator", new TestValidator());

        return registry;
    }

    @Test
    public void testValid() throws InterruptedException {

        getMockEndpoint("mock:out").expectedMessageCount(1);
        getMockEndpoint("mock:invalid").expectedMessageCount(0);

        template.sendBody("direct:in", "valid");

        assertMockEndpointsSatisfied();
    }

    @Test
    public void testInvalid() throws InterruptedException {
        getMockEndpoint("mock:out").expectedMessageCount(0);
        getMockEndpoint("mock:invalid").expectedMessageCount(1);

        template.sendBody("direct:in", "wrong");

        assertMockEndpointsSatisfied();
    }
}

{code}


> inputTypeWithValidate + validator()... doesn't work as expected
> ---------------------------------------------------------------
>
>                 Key: CAMEL-12565
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12565
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 2.21.1
>         Environment: Tested on camel 2.21.1, Java 8, Win
>            Reporter: Michael Dzuba
>            Priority: Major
>
> {code:java}
> package org.mike.tests;
> import org.apache.camel.ValidationException;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
> public class ValidatorTests extends CamelTestSupport {
>     @Override
>     protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 validator()
>                         .type(String.class)
>                         
> .withExpression(bodyAs(String.class).isEqualToIgnoreCase("valid"));
>                 onException(ValidationException.class)
>                         .handled(true)
>                         .log("Invalid predicate: ${exception.message}")
>                         .to("mock:invalid");
>                 from("direct:in")
>                         
> //.validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
>                         .outputTypeWithValidate(String.class) // or 
> .inputTypeWithValidate(String.class)
>                         .to("mock:out");
>             }
>         };
>     }
>     @Test
>     public void testValid() throws InterruptedException {
>         getMockEndpoint("mock:out").expectedMessageCount(1);
>         getMockEndpoint("mock:invalid").expectedMessageCount(0);
>         template.sendBody("direct:in", "valid");
>         assertMockEndpointsSatisfied();
>     }
>     @Test
>     public void testInvalid() throws InterruptedException {
>         getMockEndpoint("mock:out").expectedMessageCount(0);
>         getMockEndpoint("mock:invalid").expectedMessageCount(1);
>         template.sendBody("direct:in", "wrong");
>         assertMockEndpointsSatisfied();
>     }
> }
> {code}
> Expected result: both tests pass
> Actual result: 'testValid' - passed, 'testInvalid' - failed
> If uncomment line 25 & comment 26
> {code:java}
> .validate(bodyAs(String.class).isEqualToIgnoreCase("valid"))
> //.inputTypeWithValidate(String.class)
> {code}
> tests will OK
>  
> Other test case with same results
>  
> {code:java}
> package org.mike.tests;
> import org.apache.camel.Message;
> import org.apache.camel.ValidationException;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.impl.JndiRegistry;
> import org.apache.camel.spi.DataType;
> import org.apache.camel.spi.Validator;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
> public class BeanValidatorTest extends CamelTestSupport {
>     @Override
>     protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 validator()
>                         .type("toValidate")
>                         .withBean("testValidator");
>                 onException(ValidationException.class)
>                         .handled(true)
>                         .log("Invalid validation: ${exception.message}")
>                         .to("mock:invalid");
>                 from("direct:in")
>                         .outputTypeWithValidate("toValidate")
>                         .to("mock:out");
>             }
>         };
>     }
>     public static class TestValidator extends Validator {
>         private static final Logger LOG = 
> LoggerFactory.getLogger(TestValidator.class);
>         @Override
>         public void validate(Message message, DataType type) throws 
> ValidationException {
>             Object body = message.getBody();
>             LOG.info("Validating : [{}]", body);
>             if (body instanceof String && body.equals("valid")) {
>                 LOG.info("OK");
>             } else {
>                 throw new ValidationException(message.getExchange(), "Wrong 
> content");
>             }
>         }
>     }
>     @Override
>     protected JndiRegistry createRegistry() throws Exception {
>         JndiRegistry registry = super.createRegistry();
>         registry.bind("testValidator", new TestValidator());
>         return registry;
>     }
>     @Test
>     public void testValid() throws InterruptedException {
>         getMockEndpoint("mock:out").expectedMessageCount(1);
>         getMockEndpoint("mock:invalid").expectedMessageCount(0);
>         template.sendBody("direct:in", "valid");
>         assertMockEndpointsSatisfied();
>     }
>     @Test
>     public void testInvalid() throws InterruptedException {
>         getMockEndpoint("mock:out").expectedMessageCount(0);
>         getMockEndpoint("mock:invalid").expectedMessageCount(1);
>         template.sendBody("direct:in", "wrong");
>         assertMockEndpointsSatisfied();
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to