Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
2.21.1
-
None
-
Tested on camel 2.21.1, Java 8, Win
-
Unknown
Description
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(); } }
Expected result: both tests pass
Actual result: 'testValid' - passed, 'testInvalid' - failed
If uncomment line 25 & comment 26
.validate(bodyAs(String.class).isEqualToIgnoreCase("valid")) //.inputTypeWithValidate(String.class)
tests will OK
Other test case with same results
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(); } }