This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch yaml-ns in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5279cd686392e67c019c1a42d2c9a562fa81594c Author: Adriano Machado <[email protected]> AuthorDate: Wed Nov 6 14:03:16 2024 +0100 CAMEL-21420 - Failing tests --- core/camel-xml-io/pom.xml | 11 ++ .../org/apache/camel/xml/out/ModelWriterTest.java | 147 +++++++++++++++++++++ .../test/resources/routeXPathAndXmlNamespaces.xml | 43 ++++++ core/camel-yaml-io/pom.xml | 10 ++ .../org/apache/camel/yaml/out/ModelWriter.java | 2 +- .../apache/camel/yaml/out/XPathNamespacesTest.java | 121 +++++++++++++++++ .../src/main/resources/velocity/model-writer.vm | 3 +- 7 files changed, 335 insertions(+), 2 deletions(-) diff --git a/core/camel-xml-io/pom.xml b/core/camel-xml-io/pom.xml index 152afbf8b48..f7e5e363d99 100644 --- a/core/camel-xml-io/pom.xml +++ b/core/camel-xml-io/pom.xml @@ -54,6 +54,17 @@ <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.xmlunit</groupId> + <artifactId>xmlunit-assertj3</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>${commons-io-version}</version> + <scope>test</scope> + </dependency> <!-- logging --> <dependency> <groupId>org.apache.logging.log4j</groupId> diff --git a/core/camel-xml-io/src/test/java/org/apache/camel/xml/out/ModelWriterTest.java b/core/camel-xml-io/src/test/java/org/apache/camel/xml/out/ModelWriterTest.java index 66a1b732ad5..2ba6084b9b6 100644 --- a/core/camel-xml-io/src/test/java/org/apache/camel/xml/out/ModelWriterTest.java +++ b/core/camel-xml-io/src/test/java/org/apache/camel/xml/out/ModelWriterTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.xml.out; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOError; import java.io.IOException; import java.io.InputStream; @@ -44,13 +46,19 @@ import org.apache.camel.model.app.BeansDefinition; import org.apache.camel.model.rest.RestsDefinition; import org.apache.camel.util.StringHelper; import org.apache.camel.xml.in.ModelParser; +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.xmlunit.assertj3.XmlAssert; +import org.xmlunit.diff.DefaultNodeMatcher; +import org.xmlunit.diff.ElementSelectors; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.fail; @@ -73,6 +81,35 @@ public class ModelWriterTest { } } + @ParameterizedTest + @MethodSource("routes") + @DisplayName("Test xml roundtrip for <routes>, then compare generated XML") + void testRoutesWithDiff(String xml, String ns) throws Exception { + String original; + try (InputStream is = getClass().getClassLoader().getResourceAsStream(xml); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + assertNotNull(is); + IOUtils.copy(is, baos); + original = baos.toString(); + } + + assertThat(original).isNotBlank(); + RoutesDefinition expected + = new ModelParser(new ByteArrayInputStream(original.getBytes()), NAMESPACE).parseRoutesDefinition().get(); + StringWriter sw = new StringWriter(); + new ModelWriter(sw, ns).writeRoutesDefinition(expected); + String generatedXml = sw.toString(); + assertThat(generatedXml).isNotBlank(); + + XmlAssert.assertThat(generatedXml) + .and(original) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) + .ignoreWhitespace() + .ignoreElementContentWhitespace() + .ignoreComments() + .areSimilar(); + } + @ParameterizedTest @MethodSource("rests") @DisplayName("Test xml roundtrip for <rests>") @@ -86,6 +123,34 @@ public class ModelWriterTest { } } + @ParameterizedTest + @MethodSource("rests") + @DisplayName("Test xml roundtrip for <rests>, then compare generated XML") + void testRestsWithDiff(String xml, String ns) throws Exception { + String original; + try (InputStream is = getClass().getClassLoader().getResourceAsStream(xml); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + assertNotNull(is); + IOUtils.copy(is, baos); + original = baos.toString(); + } + + RestsDefinition expected + = new ModelParser(new ByteArrayInputStream(original.getBytes()), NAMESPACE).parseRestsDefinition().get(); + StringWriter sw = new StringWriter(); + new ModelWriter(sw, ns).writeRestsDefinition(expected); + String generatedXml = sw.toString(); + assertThat(generatedXml).isNotBlank(); + + XmlAssert.assertThat(generatedXml) + .and(original) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) + .ignoreWhitespace() + .ignoreElementContentWhitespace() + .ignoreComments() + .areSimilar(); + } + @ParameterizedTest @MethodSource("routeTemplates") @DisplayName("Test xml roundtrip for <routeTemplates>") @@ -100,6 +165,34 @@ public class ModelWriterTest { } } + @ParameterizedTest + @MethodSource("routeTemplates") + @DisplayName("Test xml roundtrip for <routeTemplates> then compare generated XML") + void testRouteTemplatesWithDiff(String xml, String ns) throws Exception { + String original; + try (InputStream is = getClass().getClassLoader().getResourceAsStream(xml); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + assertNotNull(is); + IOUtils.copy(is, baos); + original = baos.toString(); + } + + RouteTemplatesDefinition expected = new ModelParser(new ByteArrayInputStream(original.getBytes()), NAMESPACE) + .parseRouteTemplatesDefinition().get(); + StringWriter sw = new StringWriter(); + new ModelWriter(sw, ns).writeRouteTemplatesDefinition(expected); + String generatedXml = sw.toString(); + assertThat(generatedXml).isNotBlank(); + + XmlAssert.assertThat(generatedXml) + .and(original) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) + .ignoreWhitespace() + .ignoreElementContentWhitespace() + .ignoreComments() + .areSimilar(); + } + @ParameterizedTest @MethodSource("templatedRoutes") @DisplayName("Test xml roundtrip for <templatedRoutes>") @@ -114,6 +207,33 @@ public class ModelWriterTest { } } + @ParameterizedTest + @MethodSource("templatedRoutes") + @DisplayName("Test xml roundtrip for <templatedRoutes> then compare generated XML") + void testTemplatedRoutesWithDiff(String xml, String ns) throws Exception { + String original; + try (InputStream is = getClass().getClassLoader().getResourceAsStream(xml); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + assertNotNull(is); + IOUtils.copy(is, baos); + original = baos.toString(); + } + TemplatedRoutesDefinition expected = new ModelParser(new ByteArrayInputStream(original.getBytes()), NAMESPACE) + .parseTemplatedRoutesDefinition().get(); + StringWriter sw = new StringWriter(); + new ModelWriter(sw, ns).writeTemplatedRoutesDefinition(expected); + String generatedXml = sw.toString(); + assertThat(generatedXml).isNotBlank(); + + XmlAssert.assertThat(generatedXml) + .and(original) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) + .ignoreWhitespace() + .ignoreElementContentWhitespace() + .ignoreComments() + .areSimilar(); + } + @ParameterizedTest @MethodSource("beans") @DisplayName("Test xml roundtrip for <beans>") @@ -128,6 +248,33 @@ public class ModelWriterTest { } } + @ParameterizedTest + @MethodSource("beans") + @DisplayName("Test xml roundtrip for <beans> then compare generated XML") + void testBeansWithDiff(String xml, String ns) throws Exception { + String original; + try (InputStream is = getClass().getClassLoader().getResourceAsStream(xml); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + assertNotNull(is); + IOUtils.copy(is, baos); + original = baos.toString(); + } + BeansDefinition expected + = new ModelParser(new ByteArrayInputStream(original.getBytes()), NAMESPACE).parseBeansDefinition().get(); + StringWriter sw = new StringWriter(); + new ModelWriter(sw, ns).writeBeansDefinition(expected); + String generatedXml = sw.toString(); + assertThat(generatedXml).isNotBlank(); + + XmlAssert.assertThat(generatedXml) + .and(original) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) + .ignoreWhitespace() + .ignoreElementContentWhitespace() + .ignoreComments() + .areSimilar(); + } + private static Stream<Arguments> routes() { return definitions("routes"); } diff --git a/core/camel-xml-io/src/test/resources/routeXPathAndXmlNamespaces.xml b/core/camel-xml-io/src/test/resources/routeXPathAndXmlNamespaces.xml new file mode 100644 index 00000000000..74cdae56b39 --- /dev/null +++ b/core/camel-xml-io/src/test/resources/routeXPathAndXmlNamespaces.xml @@ -0,0 +1,43 @@ +<?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. + +--> +<routes xmlns="http://camel.apache.org/schema/spring" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:routes-ns-def="http://www.example.com/schema" + xsi:schemaLocation="http://camel.apache.org/schema/spring https://camel.apache.org/schema/spring/camel-spring.xsd"> + + <route id="direct:route-with-xpath-expression-custom-namespace" + xmlns:route-ns-def="http://www.example.com/schema"> + + <from uri="direct:route-with-xpath-expression-custom-namespace"/> + + <setProperty name="child-expression-namespace-from-routes"> + <xpath saxon="true" resultType="java.lang.String">/routes-ns-def:parent/routes-ns-def:child</xpath> + </setProperty> + + <setProperty name="child-expression-namespace-from-route"> + <xpath saxon="true" resultType="java.lang.String">/route-ns-def:parent/route-ns-def:child</xpath> + </setProperty> + + <setProperty name="child-expression-namespace-from-xpath"> + <xpath saxon="true" resultType="java.lang.String" xmlns:expression-ns-def="http://www.example.com/schema">/expression-ns-def:parent/expression-ns-def:child</xpath> + </setProperty> + </route> + +</routes> diff --git a/core/camel-yaml-io/pom.xml b/core/camel-yaml-io/pom.xml index 058c56f5b63..3f3176dbc21 100644 --- a/core/camel-yaml-io/pom.xml +++ b/core/camel-yaml-io/pom.xml @@ -66,6 +66,16 @@ <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.xmlunit</groupId> + <artifactId>xmlunit-assertj3</artifactId> + <scope>test</scope> + </dependency> <!-- logging --> <dependency> <groupId>org.apache.logging.log4j</groupId> diff --git a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java index fcfbc2c9ad8..f2361c77925 100644 --- a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java +++ b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java @@ -41,7 +41,7 @@ import org.apache.camel.model.tokenizer.*; import org.apache.camel.model.transformer.*; import org.apache.camel.model.validator.*; -@Generated("org.apache.camel.maven.packaging.XmlModelWriterGeneratorMojo") +@Generated("org.apache.camel.maven.packaging.YamlModelWriterGeneratorMojo") public class ModelWriter extends BaseWriter { public ModelWriter(Writer writer, String namespace) throws IOException { diff --git a/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/XPathNamespacesTest.java b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/XPathNamespacesTest.java new file mode 100644 index 00000000000..c0d00525354 --- /dev/null +++ b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/XPathNamespacesTest.java @@ -0,0 +1,121 @@ +/* + * 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.yaml.out; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.util.Optional; + +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.RoutesDefinition; +import org.apache.camel.xml.in.ModelParser; +import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class XPathNamespacesTest { + + @Test + void test() throws Exception { + try (ByteArrayInputStream is = new ByteArrayInputStream(XML.getBytes(Charset.defaultCharset()))) { + Optional<RoutesDefinition> routesDefinition = new ModelParser(is, XmlToYamlTest.NAMESPACE).parseRoutesDefinition(); + assertThat(routesDefinition).isPresent() + .get(InstanceOfAssertFactories.type(RoutesDefinition.class)) + .extracting(RoutesDefinition::getRoutes, InstanceOfAssertFactories.list(RouteDefinition.class)) + .singleElement() + .extracting(RouteDefinition::getOutputs, InstanceOfAssertFactories.list(ProcessorDefinition.class)) + .hasSize(3); + + StringWriter sw = new StringWriter(); + new org.apache.camel.yaml.out.ModelWriter(sw).writeRoutesDefinition(routesDefinition.get()); + + assertThat(sw).hasToString(EXPECTED_YAML); + } + } + + //language=XML + private static final String XML + = """ + <routes xmlns="http://camel.apache.org/schema/spring" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:routes-ns-def="http://www.example.com/schema" + xsi:schemaLocation="http://camel.apache.org/schema/spring https://camel.apache.org/schema/spring/camel-spring.xsd"> + + <route id="direct:route-with-xpath-expression-custom-namespace" + xmlns:route-ns-def="http://www.example.com/schema"> + + <from uri="direct:route-with-xpath-expression-custom-namespace"/> + + <setProperty name="child-expression-namespace-from-routes"> + <xpath saxon="true" resultType="java.lang.String">/routes-ns-def:parent/routes-ns-def:child</xpath> + </setProperty> + + <setProperty name="child-expression-namespace-from-route"> + <xpath saxon="true" resultType="java.lang.String">/route-ns-def:parent/route-ns-def:child</xpath> + </setProperty> + + <setProperty name="child-expression-namespace-from-xpath"> + <xpath saxon="true" resultType="java.lang.String" xmlns:expression-ns-def="http://www.example.com/schema">/expression-ns-def:parent/expression-ns-def:child</xpath> + </setProperty> + </route> + + </routes> + """; + + //language=yaml + private static final String EXPECTED_YAML = """ + - route: + id: direct:route-with-xpath-expression-custom-namespace + from: + uri: direct:route-with-xpath-expression-custom-namespace + steps: + - setProperty: + name: child-expression-namespace-from-routes + xpath: + resultType: java.lang.String + saxon: "true" + namespace: + xsi: http://www.w3.org/2001/XMLSchema-instance + routes-ns-def: http://www.example.com/schema + route-ns-def: http://www.example.com/schema + expression: /routes-ns-def:parent/routes-ns-def:child + - setProperty: + name: child-expression-namespace-from-route + xpath: + resultType: java.lang.String + saxon: "true" + namespace: + xsi: http://www.w3.org/2001/XMLSchema-instance + routes-ns-def: http://www.example.com/schema + route-ns-def: http://www.example.com/schema + expression: /route-ns-def:parent/route-ns-def:child + - setProperty: + name: child-expression-namespace-from-xpath + xpath: + resultType: java.lang.String + saxon: "true" + namespace: + xsi: http://www.w3.org/2001/XMLSchema-instance + routes-ns-def: http://www.example.com/schema + route-ns-def: http://www.example.com/schema + expression-ns-def: http://www.example.com/schema + expression: /expression-ns-def:parent/expression-ns-def:child + """; +} diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-writer.vm b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-writer.vm index ee132961438..60e4b647916 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-writer.vm +++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-writer.vm @@ -44,6 +44,7 @@ import java.util.List; import javax.annotation.processing.Generated; #set( $pkgs = $mojo.newTreeSet() ) +#set( $mojoClassName = $mojo.getClass().getName() ) #foreach( $clazz in $model ) #set( $foo = $pkgs.add($clazz.getPackageName()) ) #end @@ -51,7 +52,7 @@ import javax.annotation.processing.Generated; import ${pkg}.*; #end -@Generated("org.apache.camel.maven.packaging.XmlModelWriterGeneratorMojo") +@Generated("${mojoClassName}") public class ModelWriter extends BaseWriter { public ModelWriter(Writer writer, String namespace) throws IOException {
