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/10/25 08:46:21 UTC

[camel] branch camel-2.22.x updated: CAMEL-12900: Route contract validate does not throw validation exception when validation fails

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

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


The following commit(s) were added to refs/heads/camel-2.22.x by this push:
     new f5af2ed  CAMEL-12900: Route contract validate does not throw validation exception when validation fails
f5af2ed is described below

commit f5af2ede2cab33d85f762486df754fc74284af12
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 25 10:43:38 2018 +0200

    CAMEL-12900: Route contract validate does not throw validation exception when validation fails
---
 .../camel/impl/validator/ProcessorValidator.java   |  9 ++-
 .../main/java/org/apache/camel/spi/Validator.java  |  2 +-
 .../impl/validator/ValidatorXmlSchemaTest.java     | 70 ++++++++++++++++++++++
 .../resources/org/apache/camel/impl/validate.xsd   | 29 +++++++++
 4 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java b/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
index fead39a..9f64619 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
@@ -55,16 +55,23 @@ public class ProcessorValidator extends Validator {
         Exchange exchange = message.getExchange();
         
         LOG.debug("Sending to validate processor '{}'", processor);
+        // create a new exchange to use during validation to avoid side-effects on original exchange
         DefaultExchange validateExchange = new DefaultExchange(exchange);
         validateExchange.setIn(message);
         validateExchange.setProperties(exchange.getProperties());
         try {
             processor.process(validateExchange);
+
+            // if the validation failed then propagate the exception
+            if (validateExchange.getException() != null) {
+                exchange.setException(validateExchange.getException());
+            }
+
         } catch (Exception e) {
             if (e instanceof ValidationException) {
                 throw (ValidationException)e;
             } else {
-                throw new ValidationException(String.format("Validation failed for '%s'", type), validateExchange, e);
+                throw new ValidationException(String.format("Validation failed for '%s'", type), exchange, e);
             }
         }
     }
diff --git a/camel-core/src/main/java/org/apache/camel/spi/Validator.java b/camel-core/src/main/java/org/apache/camel/spi/Validator.java
index 50b2d86..1429cc1 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/Validator.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/Validator.java
@@ -31,7 +31,7 @@ import org.apache.camel.support.ServiceSupport;
  * {@link ContractAdvice} applies Validator if input/output type is declared on
  * a route with validation enabled.
  *  
- * @see {@link ContractAdvice}
+ * @see ContractAdvice
  * {@link InputTypeDefinition} {@link OutputTypeDefinition}
  */
 public abstract class Validator extends ServiceSupport implements CamelContextAware {
diff --git a/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.java b/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.java
new file mode 100644
index 0000000..0e3b4e9
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.java
@@ -0,0 +1,70 @@
+/**
+ * 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.ValidationException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class ValidatorXmlSchemaTest extends ContextTestSupport {
+
+    @Test
+    public void shouldPass() throws Exception {
+        final String body = "<user><name>Jan</name></user>";
+        MockEndpoint mock = getMockEndpoint("mock:result");
+
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived(body);
+
+        template.sendBody("direct:in", body);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void shouldThrowException() throws Exception {
+        final String body = "<fail/>";
+        MockEndpoint mock = getMockEndpoint("mock:result");
+
+        mock.expectedMessageCount(0);
+        try {
+            template.sendBody("direct:in", body);
+            fail("Should throw exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(ValidationException.class, e.getCause());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                validator().type("xml").withUri("validator:org/apache/camel/impl/validate.xsd");
+
+                from("direct:in").inputTypeWithValidate("xml")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}
diff --git a/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd b/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd
new file mode 100644
index 0000000..784dc78
--- /dev/null
+++ b/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="name" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>
\ No newline at end of file