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

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git


The following commit(s) were added to refs/heads/master by this push:
     new b5dd480  yaml-loader: support route definition
     new f0db6d0  Merge pull request #139 from lburgazzoli/yaml-route
b5dd480 is described below

commit b5dd480e23a0eecd16ab0d8b1d7cbe9b8b85988d
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Wed Sep 11 16:20:42 2019 +0200

    yaml-loader: support route definition
---
 .../camel/k/loader/yaml/YamlRoutesLoader.java      |  37 +++----
 .../org/apache/camel/k/loader/yaml/model/Node.java |  37 +++++++
 .../apache/camel/k/loader/yaml/model/Route.java    |  86 ---------------
 .../k/loader/yaml/parser/RouteStepParser.java      |  91 ++++++++++++++++
 .../org/apache/camel/k/loader/yaml-parser/route    |  14 +--
 .../camel/k/loader/yaml/RouteDefinitionTest.groovy | 121 +++++++++++++--------
 .../src/test/resources/route.yaml                  |  23 ++--
 7 files changed, 231 insertions(+), 178 deletions(-)

diff --git 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/YamlRoutesLoader.java
 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/YamlRoutesLoader.java
index 00e08db..26f158b 100644
--- 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/YamlRoutesLoader.java
+++ 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/YamlRoutesLoader.java
@@ -25,7 +25,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.k.RoutesLoader;
 import org.apache.camel.k.Source;
-import org.apache.camel.k.loader.yaml.model.Route;
+import org.apache.camel.k.loader.yaml.model.Step;
 import org.apache.camel.k.loader.yaml.parser.StartStepParser;
 import org.apache.camel.k.loader.yaml.parser.StepParser;
 import org.apache.camel.k.support.URIResolver;
@@ -55,29 +55,22 @@ public class YamlRoutesLoader implements RoutesLoader {
                 final List<RestDefinition> rests = new ArrayList<>();
 
                 try {
-                    for (Route route : Yaml.MAPPER.readValue(is, 
Route[].class)) {
-                        Route.Definition definition = route.getDefinition();
+                    for (Step step : Yaml.MAPPER.readValue(is, Step[].class)) {
+                        final StepParser.Context context = new 
StepParser.Context(camelContext, step.node);
+                        final ProcessorDefinition<?> root = 
StartStepParser.invoke(context, step.id);
 
-                        if (definition != null) {
-                            StepParser.Context context = new 
StepParser.Context(camelContext, definition.getData());
-                            ProcessorDefinition<?> root = 
StartStepParser.invoke(context, definition.getType());
-
-                            if (root == null) {
-                                throw new IllegalStateException("No route 
definition");
-                            }
-                            if (!(root instanceof RouteDefinition)) {
-                                throw new IllegalStateException("Root 
definition should be of type RouteDefinition");
-                            }
-
-                            RouteDefinition r = (RouteDefinition) root;
-                            if (r.getRestDefinition() == null) {
-                                routes.add(r);
-                            } else {
-                                rests.add(r.getRestDefinition());
-                            }
+                        if (root == null) {
+                            throw new IllegalStateException("No route 
definition");
+                        }
+                        if (!(root instanceof RouteDefinition)) {
+                            throw new IllegalStateException("Root definition 
should be of type RouteDefinition");
+                        }
 
-                            route.getId().ifPresent(root::routeId);
-                            route.getGroup().ifPresent(root::routeGroup);
+                        RouteDefinition r = (RouteDefinition) root;
+                        if (r.getRestDefinition() == null) {
+                            routes.add(r);
+                        } else {
+                            rests.add(r.getRestDefinition());
                         }
                     }
 
diff --git 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Node.java
 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Node.java
new file mode 100644
index 0000000..9149228
--- /dev/null
+++ 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Node.java
@@ -0,0 +1,37 @@
+/*
+ * 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.k.loader.yaml.model;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+public class Node {
+    private final String type;
+    private final JsonNode data;
+
+    public Node(String type, JsonNode data) {
+        this.type = type;
+        this.data = data;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public JsonNode getData() {
+        return data;
+    }
+}
diff --git 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Route.java
 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Route.java
deleted file mode 100644
index dfdb583..0000000
--- 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/model/Route.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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.k.loader.yaml.model;
-
-import java.util.Optional;
-
-import com.fasterxml.jackson.annotation.JsonAnySetter;
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.databind.JsonNode;
-
-public class Route {
-    private String id;
-    private String group;
-    private Definition definition;
-
-    public Optional<String> getId() {
-        return Optional.ofNullable(id);
-    }
-
-    public void setId(String id) {
-        this.id = id;
-    }
-
-    public Optional<String> getGroup() {
-        return Optional.ofNullable(group);
-    }
-
-    public void setGroup(String group) {
-        this.group = group;
-    }
-
-    @JsonIgnore
-    public Definition getDefinition() {
-        return definition;
-    }
-
-    @JsonIgnore
-    public void setDefinition(Definition routeDefinition) {
-        this.definition = routeDefinition;
-    }
-
-    @JsonIgnore
-    public void setDefinition(String type, JsonNode data) {
-        this.definition = new Definition(type, data);
-    }
-
-    @JsonAnySetter
-    public void handleUnknownField(String id, JsonNode node) {
-        if (definition != null) {
-            throw new IllegalArgumentException("A definition is already set: " 
+ definition.type);
-        }
-        setDefinition(id, node);
-    }
-
-    public static class Definition {
-        private final String type;
-        private final JsonNode data;
-
-        public Definition(String type, JsonNode data) {
-            this.type = type;
-            this.data = data;
-        }
-
-        public String getType() {
-            return type;
-        }
-
-        public JsonNode getData() {
-            return data;
-        }
-    }
-}
diff --git 
a/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/RouteStepParser.java
 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/RouteStepParser.java
new file mode 100644
index 0000000..a83a596
--- /dev/null
+++ 
b/camel-k-loader-yaml/src/main/java/org/apache/camel/k/loader/yaml/parser/RouteStepParser.java
@@ -0,0 +1,91 @@
+/*
+ * 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.k.loader.yaml.parser;
+
+import java.util.Optional;
+
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.camel.k.loader.yaml.model.Node;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.RouteDefinition;
+
+public class RouteStepParser implements StartStepParser {
+    @Override
+    public ProcessorDefinition<?> toStartProcessor(Context context) {
+        final Definition definition = context.node(Definition.class);
+
+        final ProcessorDefinition<?> root = StartStepParser.invoke(
+            ProcessorStepParser.Context.of(context, 
definition.getRoot().getData()),
+            definition.getRoot().getType());
+
+        if (root == null) {
+            throw new IllegalStateException("No route definition");
+        }
+        if (!(root instanceof RouteDefinition)) {
+            throw new IllegalStateException("Root definition should be of type 
RouteDefinition");
+        }
+
+        definition.getId().ifPresent(root::routeId);
+        definition.getGroup().ifPresent(root::routeGroup);
+
+        return root;
+    }
+
+    public static final class Definition {
+        private String id;
+        private String group;
+        private Node root;
+
+        public Optional<String> getId() {
+            return Optional.ofNullable(id);
+        }
+
+        public void setId(String id) {
+            this.id = id;
+        }
+
+        public Optional<String> getGroup() {
+            return Optional.ofNullable(group);
+        }
+
+        public void setGroup(String group) {
+            this.group = group;
+        }
+
+        @JsonIgnore
+        public Node getRoot() {
+            return root;
+        }
+
+        @JsonIgnore
+        public void setRoot(Node routeDefinition) {
+            this.root = routeDefinition;
+        }
+
+        @JsonAnySetter
+        public void handleUnknownField(String id, JsonNode node) {
+            if (root != null) {
+                throw new IllegalArgumentException("A root is already set: " + 
root.getType());
+            }
+            setRoot(new Node(id, node));
+        }
+    }
+
+}
+
diff --git a/camel-k-runtime-knative/src/test/resources/route.yaml 
b/camel-k-loader-yaml/src/main/resources/META-INF/services/org/apache/camel/k/loader/yaml-parser/route
similarity index 75%
copy from camel-k-runtime-knative/src/test/resources/route.yaml
copy to 
camel-k-loader-yaml/src/main/resources/META-INF/services/org/apache/camel/k/loader/yaml-parser/route
index f6c2a20..caf0878 100644
--- a/camel-k-runtime-knative/src/test/resources/route.yaml
+++ 
b/camel-k-loader-yaml/src/main/resources/META-INF/services/org/apache/camel/k/loader/yaml-parser/route
@@ -6,7 +6,7 @@
 # (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
+# 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,
@@ -15,14 +15,4 @@
 # limitations under the License.
 #
 
-- id: "knative"
-  group: "flows"
-  knative:
-    type: "endpoint"
-    name: "from"
-    steps:
-      - to:
-          uri: "log:info"
-      - knative:
-          type: "endpoint"
-          name: "to"
\ No newline at end of file
+class=org.apache.camel.k.loader.yaml.parser.RouteStepParser
\ No newline at end of file
diff --git 
a/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/RouteDefinitionTest.groovy
 
b/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/RouteDefinitionTest.groovy
index 5365d2d..5096fc5 100644
--- 
a/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/RouteDefinitionTest.groovy
+++ 
b/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/RouteDefinitionTest.groovy
@@ -29,27 +29,15 @@ import java.nio.charset.StandardCharsets
 
 class RouteDefinitionTest extends TestSupport {
 
-    def "route with cbr"() {
+    def "route with id"() {
         given:
             def content = '''
-                 - from:
+             - route:
+                 id: my-route-id    
+                 group: my-route-group 
+                 from:
                      uri: "direct:start"
                      steps:
-                       - choice:
-                           when:
-                             - simple: "${body.startsWith(\\"a\\")}"
-                               steps:
-                                 - to:
-                                     uri: "log:when-a"
-                             - expression:
-                                 simple: "${body.startsWith(\\"b\\")}"
-                               steps:
-                                 - to:
-                                     uri: "log:when-b"
-                           otherwise:
-                             steps:
-                               - to:
-                                   uri: "log:otherwise"
                        - to:
                            uri: "log:info"
             '''.stripMargin('|')
@@ -59,6 +47,45 @@ class RouteDefinitionTest extends TestSupport {
         when:
             camelContext.addRoutes(YamlRoutesLoader.builder(istream))
         then:
+            camelContext.routeDefinitions[0].id == 'my-route-id'
+            camelContext.routeDefinitions[0].group == 'my-route-group'
+            camelContext.routeDefinitions[0].input.endpointUri == 
'direct:start'
+
+            with(camelContext.routeDefinitions[0].outputs[0], ToDefinition) {
+                endpointUri == 'log:info'
+            }
+    }
+
+    def "route with cbr"() {
+        given:
+            def content = '''
+             - from:
+                 uri: "direct:start"
+                 steps:
+                   - choice:
+                       when:
+                         - simple: "${body.startsWith(\\"a\\")}"
+                           steps:
+                             - to:
+                                 uri: "log:when-a"
+                         - expression:
+                             simple: "${body.startsWith(\\"b\\")}"
+                           steps:
+                             - to:
+                                 uri: "log:when-b"
+                       otherwise:
+                         steps:
+                           - to:
+                               uri: "log:otherwise"
+                   - to:
+                       uri: "log:info"
+            '''.stripMargin('|')
+
+            def camelContext = new DefaultCamelContext()
+            def istream = IOUtils.toInputStream(content, 
StandardCharsets.UTF_8)
+        when:
+            camelContext.addRoutes(YamlRoutesLoader.builder(istream))
+        then:
             camelContext.routeDefinitions[0].input.endpointUri == 
'direct:start'
 
             with(camelContext.routeDefinitions[0].outputs[0] as 
ChoiceDefinition) {
@@ -89,15 +116,15 @@ class RouteDefinitionTest extends TestSupport {
     def "route with split"() {
         given:
             def content = '''
-                 - from:
-                     uri: "direct:start"
-                     steps:
-                       - split: 
-                           tokenizer: ","
-                           steps:
-                             - to: "log:split1"
-                             - to: "log:split2"
-                       - to: "log:info"
+             - from:
+                 uri: "direct:start"
+                 steps:
+                   - split: 
+                       tokenizer: ","
+                       steps:
+                         - to: "log:split1"
+                         - to: "log:split2"
+                   - to: "log:info"
             '''.stripMargin('|')
 
             def camelContext = new DefaultCamelContext()
@@ -131,12 +158,12 @@ class RouteDefinitionTest extends TestSupport {
     def "flow style route with split"() {
         given:
             def content = '''
-                 - from:
-                     uri: "direct:start"
-                     steps:
-                       - split: 
-                           tokenizer: ","
-                       - to: "log:info"
+             - from:
+                 uri: "direct:start"
+                 steps:
+                   - split: 
+                       tokenizer: ","
+                   - to: "log:info"
             '''.stripMargin('|')
 
             def camelContext = new DefaultCamelContext()
@@ -163,15 +190,15 @@ class RouteDefinitionTest extends TestSupport {
     def "route with filter"() {
         given:
             def content = '''
-                 - from:
-                     uri: "direct:start"
-                     steps:
-                       - filter: 
-                           simple: "${body.startsWith(\\"a\\")}"
-                           steps:
-                             - to: "log:filter1"
-                             - to: "log:filter2"
-                       - to: "log:info"
+             - from:
+                 uri: "direct:start"
+                 steps:
+                   - filter: 
+                       simple: "${body.startsWith(\\"a\\")}"
+                       steps:
+                         - to: "log:filter1"
+                         - to: "log:filter2"
+                   - to: "log:info"
             '''.stripMargin('|')
 
             def camelContext = new DefaultCamelContext()
@@ -205,12 +232,12 @@ class RouteDefinitionTest extends TestSupport {
     def "flow style route with filter"() {
         given:
             def content = '''
-                 - from:
-                     uri: "direct:start"
-                     steps:
-                       - filter: 
-                           simple: "${body.startsWith(\\"a\\")}"
-                       - to: "log:info"
+             - from:
+                 uri: "direct:start"
+                 steps:
+                   - filter: 
+                       simple: "${body.startsWith(\\"a\\")}"
+                   - to: "log:info"
             '''.stripMargin('|')
 
             def camelContext = new DefaultCamelContext()
diff --git a/camel-k-runtime-knative/src/test/resources/route.yaml 
b/camel-k-runtime-knative/src/test/resources/route.yaml
index f6c2a20..9ba8124 100644
--- a/camel-k-runtime-knative/src/test/resources/route.yaml
+++ b/camel-k-runtime-knative/src/test/resources/route.yaml
@@ -15,14 +15,15 @@
 # limitations under the License.
 #
 
-- id: "knative"
-  group: "flows"
-  knative:
-    type: "endpoint"
-    name: "from"
-    steps:
-      - to:
-          uri: "log:info"
-      - knative:
-          type: "endpoint"
-          name: "to"
\ No newline at end of file
+- route:
+    id: "knative"
+    group: "flows"
+    knative:
+        type: "endpoint"
+        name: "from"
+        steps:
+            - to:
+                uri: "log:info"
+            - knative:
+                type: "endpoint"
+                name: "to"
\ No newline at end of file

Reply via email to