You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/09/28 08:32:34 UTC

[camel] branch camel-3.4.x updated: CAMEL-15581: Fix loading of XML rest definitions with camel-main

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

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


The following commit(s) were added to refs/heads/camel-3.4.x by this push:
     new 3223d28  CAMEL-15581: Fix loading of XML rest definitions with camel-main
3223d28 is described below

commit 3223d28c05734c2511c09286f6e8c8f21a85d7dc
Author: James Netherton <ja...@gmail.com>
AuthorDate: Mon Sep 28 07:25:37 2020 +0100

    CAMEL-15581: Fix loading of XML rest definitions with camel-main
---
 .../apache/camel/main/DefaultRoutesCollector.java  |   2 +-
 .../apache/camel/main/xml/MainXmlRestsTest.java    | 113 +++++++++++++++++++++
 .../org/apache/camel/main/xml/MainXmlTest.java     |  16 +--
 .../main/xml/{camel-dummy.xml => rests/rests.xml}  |  13 +--
 .../camel/main/xml/{ => routes}/camel-dummy.xml    |   0
 .../camel/main/xml/{ => routes}/camel-scan.xml     |   0
 6 files changed, 129 insertions(+), 15 deletions(-)

diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
index 9fd329c..9246af2 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
@@ -146,7 +146,7 @@ public class DefaultRoutesCollector implements RoutesCollector {
                 for (InputStream is : set) {
                     log.debug("Found XML rest from location: {}", part);
                     ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
-                    RestsDefinition rests = (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRoutesDefinition(ecc, is);
+                    RestsDefinition rests = (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRestsDefinition(ecc, is);
                     answer.add(rests);
                     IOHelper.close(is);
                     count += rests.getRests().size();
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlRestsTest.java b/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlRestsTest.java
new file mode 100644
index 0000000..dd42581
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlRestsTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.main.xml;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.main.Main;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.model.rest.GetVerbDefinition;
+import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.model.rest.VerbDefinition;
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.spi.RestConsumerFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class MainXmlRestsTest {
+
+    @Test
+    public void testMainRestsCollector() throws Exception {
+        // will load XML from target/classes when testing
+        doTestMain("org/apache/camel/main/xml/rests/rests.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorScan() throws Exception {
+        // will load XML from target/classes when testing
+        doTestMain("org/apache/camel/main/xml/rests/res*.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorScanWildcardDirClasspathPath() throws Exception {
+        // will load XML from target/classes when testing
+        doTestMain("org/apache/camel/main/**/res*.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorScanClasspathPrefix() throws Exception {
+        // will load XML from target/classes when testing
+        doTestMain("classpath:org/apache/camel/main/xml/rests/res*.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorScanInDir() throws Exception {
+        doTestMain("file:src/test/resources/org/apache/camel/main/xml/rests/res*.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorScanWildcardDirFilePath() throws Exception {
+        doTestMain("file:src/test/resources/**/res*.xml");
+    }
+
+    @Test
+    public void testMainRestsCollectorFile() throws Exception {
+        doTestMain(
+                "file:src/test/resources/org/apache/camel/main/xml/rests/rests.xml,");
+    }
+
+    protected void doTestMain(String xmlRests) throws Exception {
+        Main main = new Main();
+        main.bind("restConsumerFactory", new MockRestConsumerFactory());
+        main.configure().withXmlRests(xmlRests);
+        main.start();
+
+        CamelContext camelContext = main.getCamelContext();
+        assertNotNull(camelContext);
+
+        List<RestDefinition> restDefinitions = camelContext.adapt(ModelCamelContext.class).getRestDefinitions();
+        assertEquals(1, restDefinitions.size());
+
+        RestDefinition restDefinition = restDefinitions.get(0);
+        assertEquals("bar", restDefinition.getId());
+        assertEquals("/say/hello", restDefinition.getPath());
+
+        List<VerbDefinition> verbs = restDefinition.getVerbs();
+        assertNotNull(verbs);
+        assertEquals(1, verbs.size());
+
+        VerbDefinition verbDefinition = verbs.get(0);
+        assertTrue(verbDefinition instanceof GetVerbDefinition);
+
+        main.stop();
+    }
+
+    private static final class MockRestConsumerFactory implements RestConsumerFactory {
+        @Override
+        public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes,
+                String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
+            return null;
+        }
+    }
+}
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlTest.java b/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlTest.java
index 837a1b8..4728346 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/xml/MainXmlTest.java
@@ -28,25 +28,25 @@ public class MainXmlTest {
     @Test
     public void testMainRoutesCollector() throws Exception {
         // will load XML from target/classes when testing
-        doTestMain("org/apache/camel/main/xml/camel-dummy.xml,org/apache/camel/main/xml/camel-scan.xml");
+        doTestMain("org/apache/camel/main/xml/routes/camel-dummy.xml,org/apache/camel/main/xml/routes/camel-scan.xml");
     }
 
     @Test
     public void testMainRoutesCollectorScan() throws Exception {
         // will load XML from target/classes when testing
-        doTestMain("org/apache/camel/main/xml/camel-*.xml");
+        doTestMain("org/apache/camel/main/xml/routes/camel-*.xml");
     }
 
     @Test
     public void testMainRoutesCollectorScanWildcardDirClasspathPath() throws Exception {
         // will load XML from target/classes when testing
-        doTestMain("org/apache/camel/main/**/*.xml");
+        doTestMain("org/apache/camel/main/**/camel-*.xml");
     }
 
     @Test
     public void testMainRoutesCollectorScanClasspathPrefix() throws Exception {
         // will load XML from target/classes when testing
-        doTestMain("classpath:org/apache/camel/main/xml/camel-*.xml");
+        doTestMain("classpath:org/apache/camel/main/xml/routes/camel-*.xml");
     }
 
     @Test
@@ -57,22 +57,22 @@ public class MainXmlTest {
 
     @Test
     public void testMainRoutesCollectorScanInDir() throws Exception {
-        doTestMain("file:src/test/resources/org/apache/camel/main/xml/camel-*.xml");
+        doTestMain("file:src/test/resources/org/apache/camel/main/xml/routes/camel-*.xml");
     }
 
     @Test
     public void testMainRoutesCollectorScanWildcardDirFilePath() throws Exception {
-        doTestMain("file:src/test/resources/**/*.xml");
+        doTestMain("file:src/test/resources/**/camel-*.xml");
     }
 
     @Test
     public void testMainRoutesCollectorFile() throws Exception {
-        doTestMain("file:src/test/resources/org/apache/camel/main/xml/camel-dummy.xml,file:src/test/resources/org/apache/camel/main/xml/camel-scan.xml,");
+        doTestMain("file:src/test/resources/org/apache/camel/main/xml/routes/camel-dummy.xml,file:src/test/resources/org/apache/camel/main/xml/routes/camel-scan.xml,");
     }
 
     @Test
     public void testMainRoutesCollectorScanInJarAndDir() throws Exception {
-        doTestMain("classpath:org/apache/camel/main/xml/*dummy.xml,file:src/test/resources/org/apache/camel/main/xml/*scan.xml");
+        doTestMain("classpath:org/apache/camel/main/xml/routes/*dummy.xml,file:src/test/resources/org/apache/camel/main/xml/routes/*scan.xml");
     }
 
     protected void doTestMain(String xmlRoutes) throws Exception {
diff --git a/core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-dummy.xml b/core/camel-main/src/test/resources/org/apache/camel/main/xml/rests/rests.xml
similarity index 82%
copy from core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-dummy.xml
copy to core/camel-main/src/test/resources/org/apache/camel/main/xml/rests/rests.xml
index 901aa8d..30a111a 100644
--- a/core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-dummy.xml
+++ b/core/camel-main/src/test/resources/org/apache/camel/main/xml/rests/rests.xml
@@ -17,9 +17,10 @@
     limitations under the License.
 
 -->
-<routes id="camel" xmlns="http://camel.apache.org/schema/spring">
-    <route>
-        <from uri="direct:dummy"/>
-        <to uri="mock:dummy"/>
-    </route>
-</routes>
+<rests xmlns="http://camel.apache.org/schema/spring">
+    <rest id="bar" path="/say/hello">
+        <get uri="/bar">
+            <to uri="mock:bar"/>
+        </get>
+    </rest>
+</rests>
diff --git a/core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-dummy.xml b/core/camel-main/src/test/resources/org/apache/camel/main/xml/routes/camel-dummy.xml
similarity index 100%
rename from core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-dummy.xml
rename to core/camel-main/src/test/resources/org/apache/camel/main/xml/routes/camel-dummy.xml
diff --git a/core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-scan.xml b/core/camel-main/src/test/resources/org/apache/camel/main/xml/routes/camel-scan.xml
similarity index 100%
rename from core/camel-main/src/test/resources/org/apache/camel/main/xml/camel-scan.xml
rename to core/camel-main/src/test/resources/org/apache/camel/main/xml/routes/camel-scan.xml