This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 196b6bf178d CAMEL-21967: camel-jbang - Transform route to yaml with CBR returns invalid yaml 196b6bf178d is described below commit 196b6bf178dec160c8c140e2937802c14db094d1 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Apr 16 13:02:17 2025 +0200 CAMEL-21967: camel-jbang - Transform route to yaml with CBR returns invalid yaml --- .../DumpModelAsYamlTransformChoiceRouteTest.java | 73 ++++++++++++++++++++++ .../org/apache/camel/util/split-choice.yaml | 44 +++++++------ .../resources/org/apache/camel/util/transform3.xml | 39 ++++++++++++ .../org/apache/camel/util/transform3.yaml | 53 ++++++++++++++++ .../java/org/apache/camel/yaml/io/EipNode.java | 27 +++++--- 5 files changed, 204 insertions(+), 32 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsYamlTransformChoiceRouteTest.java b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsYamlTransformChoiceRouteTest.java new file mode 100644 index 00000000000..3a92fee8bd8 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsYamlTransformChoiceRouteTest.java @@ -0,0 +1,73 @@ +/* + * 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.util; + +import java.nio.file.Paths; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.support.PluginHelper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class DumpModelAsYamlTransformChoiceRouteTest extends DumpModelAsYamlTestSupport { + + @Test + public void testDumpModelAsXml() throws Exception { + String xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context, context.getRouteDefinition("myRoute")); + assertNotNull(xml); + log.info(xml); + + String expected + = IOHelper.stripLineComments(Paths.get("src/test/resources/org/apache/camel/util/transform3.xml"), "#", true); + expected = StringHelper.after(expected, "-->"); + Assertions.assertEquals(expected, "\n" + xml + "\n"); + } + + @Test + public void testDumpModelAsYaml() throws Exception { + String out = PluginHelper.getModelToYAMLDumper(context).dumpModelAsYaml(context, context.getRouteDefinition("myRoute")); + assertNotNull(out); + log.info(out); + + String expected + = IOHelper.stripLineComments(Paths.get("src/test/resources/org/apache/camel/util/transform3.yaml"), "#", true); + Assertions.assertEquals(expected, out); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("timer:java?period={{time:1000}}").routeId("myRoute") + .setBody() + .simple("Hello Camel from ${routeId}") + .choice() + .when(simple("${header.foo} == 'bar'")) + .to("direct:bar") + .when(simple("${header.foo} == 'cake'")) + .to("direct:cake") + .otherwise() + .to("direct:cheese") + .end() + .log("${body}"); + } + }; + } +} diff --git a/core/camel-core/src/test/resources/org/apache/camel/util/split-choice.yaml b/core/camel-core/src/test/resources/org/apache/camel/util/split-choice.yaml index dccf6d620a8..23155fea850 100644 --- a/core/camel-core/src/test/resources/org/apache/camel/util/split-choice.yaml +++ b/core/camel-core/src/test/resources/org/apache/camel/util/split-choice.yaml @@ -31,29 +31,27 @@ uri: mock:sub - choice: id: choice - steps: - - when: - id: when - header: - expression: foo - steps: - - to: - id: to - uri: mock:foo - - when: - id: when - header: - expression: bar - steps: - - to: - id: to - uri: mock:bar - - otherwise: - id: otherwise - steps: - - to: - id: to - uri: mock:other + when: + - id: when + header: + expression: foo + steps: + - to: + id: to + uri: mock:foo + - id: when + header: + expression: bar + steps: + - to: + id: to + uri: mock:bar + otherwise: + id: otherwise + steps: + - to: + id: to + uri: mock:other - to: id: to uri: mock:last diff --git a/core/camel-core/src/test/resources/org/apache/camel/util/transform3.xml b/core/camel-core/src/test/resources/org/apache/camel/util/transform3.xml new file mode 100644 index 00000000000..0fc9d63de7c --- /dev/null +++ b/core/camel-core/src/test/resources/org/apache/camel/util/transform3.xml @@ -0,0 +1,39 @@ +<?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. + +--> +<route xmlns="http://camel.apache.org/schema/spring" id="myRoute"> + <from id="from" uri="timer:java?period={{time:1000}}"/> + <setBody id="setBody"> + <simple>Hello Camel from ${routeId}</simple> + </setBody> + <choice id="choice"> + <when id="when"> + <simple>${header.foo} == 'bar'</simple> + <to id="to" uri="direct:bar"/> + </when> + <when id="when"> + <simple>${header.foo} == 'cake'</simple> + <to id="to" uri="direct:cake"/> + </when> + <otherwise id="otherwise"> + <to id="to" uri="direct:cheese"/> + </otherwise> + </choice> + <log id="log" message="${body}"/> +</route> diff --git a/core/camel-core/src/test/resources/org/apache/camel/util/transform3.yaml b/core/camel-core/src/test/resources/org/apache/camel/util/transform3.yaml new file mode 100644 index 00000000000..369b5195be5 --- /dev/null +++ b/core/camel-core/src/test/resources/org/apache/camel/util/transform3.yaml @@ -0,0 +1,53 @@ +# +# 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. +# + +- route: + id: myRoute + from: + id: from + uri: "timer:java?period={{time:1000}}" + steps: + - setBody: + id: setBody + simple: + expression: "Hello Camel from ${routeId}" + - choice: + id: choice + when: + - id: when + simple: + expression: "${header.foo} == 'bar'" + steps: + - to: + id: to + uri: direct:bar + - id: when + simple: + expression: "${header.foo} == 'cake'" + steps: + - to: + id: to + uri: direct:cake + otherwise: + id: otherwise + steps: + - to: + id: to + uri: direct:cheese + - log: + id: log + message: "${body}" diff --git a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java index df70acc4d9f..d2906717809 100644 --- a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java +++ b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java @@ -156,19 +156,28 @@ class EipNode { } } if (outputs != null) { - // sort so otherwise is last - outputs.sort((o1, o2) -> { - if ("otherwise".equals(o1.name)) { - return 1; - } else if ("otherwise".equals(o2.name)) { - return -1; - } - return 0; - }); if (("marshal".equals(name) || "unmarshal".equals(name)) && outputs.size() == 1) { EipNode o = outputs.get(0); JsonObject jo = o.asJsonObject(); answer.put(o.getName(), jo); + } else if ("choice".equals(name)) { + // special for choice + JsonArray arr = new JsonArray(); + JsonObject otherwise = null; + for (EipNode o : outputs) { + if ("otherwise".equals(o.getName())) { + otherwise = o.asJsonObject(); + } else { + arr.add(o.asJsonObject()); + } + } + if (!arr.isEmpty()) { + answer.put("when", arr); + } + // otherwise should be last + if (otherwise != null) { + answer.put("otherwise", otherwise); + } } else { JsonArray arr = new JsonArray(); for (EipNode o : outputs) {