You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/08/06 18:08:45 UTC

[camel] branch master updated (33bae98 -> 7cfb95d)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 33bae98  CAMEL-12699: Allow hystrix EIP to inherit error handler so you can combine Camels error handler for redeliveries with the circuit breaker.
     new fed4461  Polished
     new 7cfb95d  CAMEL-12565: Added unit tests. Fixed a little issue in validator to ensure exception is set on exchange to allow advice to keep executing the next ones.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/camel/builder/ValidatorBuilder.java |  17 ++--
 .../org/apache/camel/model/RouteDefinition.java    |   2 +-
 .../org/apache/camel/processor/ContractAdvice.java |  55 ++++++-----
 .../validator/BeanValidatorInputValidateTest.java  |  97 +++++++++++++++++++
 .../validator/BeanValidatorOutputValidateTest.java | 104 +++++++++++++++++++++
 5 files changed, 243 insertions(+), 32 deletions(-)
 create mode 100644 camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorInputValidateTest.java
 create mode 100644 camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorOutputValidateTest.java


[camel] 02/02: CAMEL-12565: Added unit tests. Fixed a little issue in validator to ensure exception is set on exchange to allow advice to keep executing the next ones.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 7cfb95d46f46de910106c9c81637fb2123beb7f6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 6 20:07:35 2018 +0200

    CAMEL-12565: Added unit tests. Fixed a little issue in validator to ensure exception is set on exchange to allow advice to keep executing the next ones.
---
 .../org/apache/camel/model/RouteDefinition.java    |   2 +-
 .../org/apache/camel/processor/ContractAdvice.java |  55 ++++++-----
 .../validator/BeanValidatorInputValidateTest.java  |  97 +++++++++++++++++++
 .../validator/BeanValidatorOutputValidateTest.java | 104 +++++++++++++++++++++
 4 files changed, 235 insertions(+), 23 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
index 1f483f7..48890a1 100644
--- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
@@ -774,7 +774,7 @@ public class RouteDefinition extends ProcessorDefinition<RouteDefinition> {
 
     /**
      * Declare the expected data type of the output message with content validation enabled.
-     * If the actual message type is different at runtime, camel look for a required
+     * If the actual message type is different at runtime, Camel look for a required
      * {@link Transformer} and apply if exists, and then applies {@link Validator} as well.
      * The type name consists of two parts, 'scheme' and 'name' connected with ':'. For Java type 'name'
      * is a fully qualified class name. For example {@code java:java.lang.String}, {@code json:ABCOrder}.
diff --git a/camel-core/src/main/java/org/apache/camel/processor/ContractAdvice.java b/camel-core/src/main/java/org/apache/camel/processor/ContractAdvice.java
index f2e792a..521ea12 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/ContractAdvice.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/ContractAdvice.java
@@ -61,19 +61,24 @@ public class ContractAdvice implements CamelInternalProcessorAdvice {
         if (!(exchange.getIn() instanceof DataTypeAware)) {
             return null;
         }
-        DataType to = contract.getInputType();
-        if (to != null) {
-            DataTypeAware target = (DataTypeAware)exchange.getIn();
-            DataType from = target.getDataType();
-            if (!to.equals(from)) {
-                LOG.debug("Looking for transformer for INPUT: from='{}', to='{}'", from, to);
-                doTransform(exchange.getIn(), from, to);
-                target.setDataType(to);
-            }
-            if (contract.isValidateInput()) {
-                doValidate(exchange.getIn(), to);
+        try {
+            DataType to = contract.getInputType();
+            if (to != null) {
+                DataTypeAware target = (DataTypeAware) exchange.getIn();
+                DataType from = target.getDataType();
+                if (!to.equals(from)) {
+                    LOG.debug("Looking for transformer for INPUT: from='{}', to='{}'", from, to);
+                    doTransform(exchange.getIn(), from, to);
+                    target.setDataType(to);
+                }
+                if (contract.isValidateInput()) {
+                    doValidate(exchange.getIn(), to);
+                }
             }
+        } catch (Exception e) {
+            exchange.setException(e);
         }
+
         return null;
     }
     
@@ -88,18 +93,22 @@ public class ContractAdvice implements CamelInternalProcessorAdvice {
         if (!(target instanceof DataTypeAware)) {
             return;
         }
-        DataType to = contract.getOutputType();
-        if (to != null) {
-            DataTypeAware typeAwareTarget = (DataTypeAware)target;
-            DataType from = typeAwareTarget.getDataType();
-            if (!to.equals(from)) {
-                LOG.debug("Looking for transformer for OUTPUT: from='{}', to='{}'", from, to);
-                doTransform(target, from, to);
-                typeAwareTarget.setDataType(to);
-            }
-            if (contract.isValidateOutput()) {
-                doValidate(target, to);
+        try {
+            DataType to = contract.getOutputType();
+            if (to != null) {
+                DataTypeAware typeAwareTarget = (DataTypeAware)target;
+                DataType from = typeAwareTarget.getDataType();
+                if (!to.equals(from)) {
+                    LOG.debug("Looking for transformer for OUTPUT: from='{}', to='{}'", from, to);
+                    doTransform(target, from, to);
+                    typeAwareTarget.setDataType(to);
+                }
+                if (contract.isValidateOutput()) {
+                    doValidate(target, to);
+                }
             }
+        } catch (Exception e) {
+            exchange.setException(e);
         }
     }
     
@@ -154,6 +163,7 @@ public class ContractAdvice implements CamelInternalProcessorAdvice {
         }
         return false;
     }
+
     private boolean applyMatchedTransformer(Message message, DataType from, DataType to) throws Exception {
         Transformer transformer = message.getExchange().getContext().resolveTransformer(from, to);
         return applyTransformer(transformer, message, from, to);
@@ -186,4 +196,5 @@ public class ContractAdvice implements CamelInternalProcessorAdvice {
             throw new ValidationException(message.getExchange(), String.format("No Validator found for '%s'", type));
         }
     }
+
 }
\ No newline at end of file
diff --git a/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorInputValidateTest.java b/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorInputValidateTest.java
new file mode 100644
index 0000000..886fb99
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorInputValidateTest.java
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.validator;
+
+import org.apache.camel.ContextTestSupport;
+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.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BeanValidatorInputValidateTest extends ContextTestSupport {
+
+    @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")
+                    .inputTypeWithValidate("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();
+    }
+}
diff --git a/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorOutputValidateTest.java b/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorOutputValidateTest.java
new file mode 100644
index 0000000..ac5ed49
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/validator/BeanValidatorOutputValidateTest.java
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.validator;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+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.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BeanValidatorOutputValidateTest extends ContextTestSupport {
+
+    @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(1);
+        getMockEndpoint("mock:invalid").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:in", "wrong");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(ValidationException.class, e.getCause());
+            assertTrue(e.getCause().getMessage().startsWith("Wrong content"));
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+}


[camel] 01/02: Polished

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit fed44616ba12088bb23037c593cce4097cf66efa
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 6 19:19:05 2018 +0200

    Polished
---
 .../java/org/apache/camel/builder/ValidatorBuilder.java | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/ValidatorBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ValidatorBuilder.java
index 8ca1efa..7193c3d 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ValidatorBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ValidatorBuilder.java
@@ -19,7 +19,6 @@ package org.apache.camel.builder;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
-import org.apache.camel.impl.validator.ProcessorValidator;
 import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.validator.CustomValidatorDefinition;
 import org.apache.camel.model.validator.EndpointValidatorDefinition;
@@ -68,7 +67,7 @@ public class ValidatorBuilder {
 
     /**
      * Set the URI to be used for the endpoint {@link Validator}.
-     * @see {@link EndpointValidatorDefinition}, {@link ProcessorValidator}
+     * @see EndpointValidatorDefinition, ProcessorValidator
      * 
      * @param uri endpoint URI
      */
@@ -80,7 +79,7 @@ public class ValidatorBuilder {
 
     /**
      * Set the {@link Expression} to be used for the predicate {@link Validator}.
-     * @see {@link PredicateValidatorDefinition}, {@link ProcessorValidator}
+     * @see PredicateValidatorDefinition, ProcessorValidator
      * 
      * @param expression validation expression
      */
@@ -92,7 +91,7 @@ public class ValidatorBuilder {
 
     /**
      * Set the {@link Predicate} to be used for the predicate {@link Validator}.
-     * @see {@link PredicateValidatorDefinition}, {@link ProcessorValidator}
+     * @see PredicateValidatorDefinition, ProcessorValidator
      * 
      * @param predicate validation predicate
      */
@@ -104,7 +103,7 @@ public class ValidatorBuilder {
 
     /**
      * Set the Java {@code Class} represents a custom {@code Validator} implementation class.
-     * @see {@code CustomValidatorDefinition}
+     * @see CustomValidatorDefinition
      * 
      * @param clazz {@code Class} object represents custom validator implementation
      */
@@ -116,7 +115,7 @@ public class ValidatorBuilder {
 
     /**
      * Set the Java Bean name to be used for custom {@code Validator}.
-     * @see {@code CustomValidatorDefinition}
+     * @see CustomValidatorDefinition
      * 
      * @param ref bean name for the custom {@code Validator}
      */
@@ -134,10 +133,10 @@ public class ValidatorBuilder {
     }
 
     /**
-     * Configure a Validator according to the configurations built on this builder
-     * and register it into given {@code CamelContext}.
+     * Configures a new Validator according to the configurations built on this builder
+     * and register it into the given {@code CamelContext}.
      * 
-     * @param camelContext {@code CamelContext}
+     * @param camelContext the given CamelContext
      */
     public void configure(CamelContext camelContext) {
         ValidatorDefinition validator;