You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zh...@apache.org on 2023/09/26 07:02:36 UTC
[camel-quarkus] 03/06: Add basic test coverage for xml-jaxp type converters
This is an automated email from the ASF dual-hosted git repository.
zhfeng pushed a commit to branch 3.2.x
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit f2fea176fadfc069ea4bef6df60fd2dafe056625
Author: James Netherton <ja...@gmail.com>
AuthorDate: Mon Sep 25 11:09:17 2023 +0100
Add basic test coverage for xml-jaxp type converters
Fixes #5345
---
integration-tests/pom.xml | 1 +
integration-tests/xml-jaxp/pom.xml | 126 ++++++++++++++
.../component/xml/jaxp/XmlJaxpResource.java | 63 +++++++
.../quarkus/component/xml/jaxp/XmlJaxpRoutes.java | 148 ++++++++++++++++
.../quarkus/component/xml/jaxp/XmlJaxpIT.java | 23 +++
.../quarkus/component/xml/jaxp/XmlJaxpTest.java | 192 +++++++++++++++++++++
tooling/scripts/test-categories.yaml | 1 +
7 files changed, 554 insertions(+)
diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml
index 98cdd05910..208f9f9865 100644
--- a/integration-tests/pom.xml
+++ b/integration-tests/pom.xml
@@ -231,6 +231,7 @@
<module>weather</module>
<!--<module>xchange</module> https://github.com/apache/camel-quarkus/issues/4516 -->
<module>xml-grouped</module>
+ <module>xml-jaxp</module>
<module>xmlsecurity</module>
<module>xpath</module>
<module>xslt-saxon</module>
diff --git a/integration-tests/xml-jaxp/pom.xml b/integration-tests/xml-jaxp/pom.xml
new file mode 100644
index 0000000000..5c73c3ac48
--- /dev/null
+++ b/integration-tests/xml-jaxp/pom.xml
@@ -0,0 +1,126 @@
+<?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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-build-parent-it</artifactId>
+ <version>3.2.1-SNAPSHOT</version>
+ <relativePath>../../poms/build-parent-it/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>camel-quarkus-integration-test-xml-jaxp</artifactId>
+ <name>Camel Quarkus :: Integration Tests :: XML JAXP</name>
+ <description>Integration tests for Camel Quarkus XML JAXP</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-xml-jaxp</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-direct</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.quarkus</groupId>
+ <artifactId>quarkus-resteasy</artifactId>
+ </dependency>
+
+ <!-- test dependencies -->
+ <dependency>
+ <groupId>io.quarkus</groupId>
+ <artifactId>quarkus-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>io.rest-assured</groupId>
+ <artifactId>rest-assured</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <profiles>
+ <profile>
+ <id>native</id>
+ <activation>
+ <property>
+ <name>native</name>
+ </property>
+ </activation>
+ <properties>
+ <quarkus.package.type>native</quarkus.package.type>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>integration-test</goal>
+ <goal>verify</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
+ <id>virtualDependencies</id>
+ <activation>
+ <property>
+ <name>!noVirtualDependencies</name>
+ </property>
+ </activation>
+ <dependencies>
+ <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
+ <dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-direct-deployment</artifactId>
+ <version>${project.version}</version>
+ <type>pom</type>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>*</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-xml-jaxp-deployment</artifactId>
+ <version>${project.version}</version>
+ <type>pom</type>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>*</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+</project>
diff --git a/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpResource.java b/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpResource.java
new file mode 100644
index 0000000000..1e4748d5fb
--- /dev/null
+++ b/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpResource.java
@@ -0,0 +1,63 @@
+/*
+ * 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 apache.camel.quarkus.component.xml.jaxp;
+
+import javax.xml.transform.OutputKeys;
+
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.converter.jaxp.XmlConverter;
+
+@Path("/xml/jaxp")
+public class XmlJaxpResource {
+ @Inject
+ ProducerTemplate producerTemplate;
+
+ @Inject
+ CamelContext context;
+
+ @Path("convert")
+ @Consumes(MediaType.APPLICATION_XML)
+ @Produces(MediaType.APPLICATION_XML)
+ @POST
+ public String applyXmlJaxpTypeConversion(@QueryParam("endpointUri") String endpointUri, String xml) {
+ return producerTemplate.requestBody(endpointUri, xml, String.class);
+ }
+
+ @Path("convert/context/global/options")
+ @Consumes(MediaType.APPLICATION_XML)
+ @Produces(MediaType.APPLICATION_XML)
+ @POST
+ public String applyXmlJaxpTypeConversionWithContextGlobalProperties(@QueryParam("endpointUri") String endpointUri,
+ String xml) {
+ try {
+ context.getGlobalOptions().put(XmlConverter.OUTPUT_PROPERTIES_PREFIX + OutputKeys.ENCODING, "UTF-8");
+ context.getGlobalOptions().put(XmlConverter.OUTPUT_PROPERTIES_PREFIX + OutputKeys.STANDALONE, "no");
+ return applyXmlJaxpTypeConversion(endpointUri, xml);
+ } finally {
+ context.getGlobalOptions().remove(XmlConverter.OUTPUT_PROPERTIES_PREFIX);
+ context.getGlobalOptions().remove(XmlConverter.OUTPUT_PROPERTIES_PREFIX);
+ }
+ }
+}
diff --git a/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpRoutes.java b/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpRoutes.java
new file mode 100644
index 0000000000..f2c1cf6d75
--- /dev/null
+++ b/integration-tests/xml-jaxp/src/main/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpRoutes.java
@@ -0,0 +1,148 @@
+/*
+ * 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 apache.camel.quarkus.component.xml.jaxp;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.xml.BytesSource;
+
+public class XmlJaxpRoutes extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:documentConvert")
+ .convertBodyTo(Document.class)
+ .process(exchange -> {
+ Document document = exchange.getMessage().getBody(Document.class);
+ Element element = document.createElement("bar");
+ element.setTextContent("Bar Text");
+ document.getDocumentElement().appendChild(element);
+ })
+ .convertBodyTo(String.class);
+
+ from("direct:elementConvert")
+ .convertBodyTo(Document.class)
+ .process(exchange -> {
+ Document document = exchange.getMessage().getBody(Document.class);
+ Element element = document.createElement("bar");
+ element.setTextContent("Bar Text");
+ document.getDocumentElement().appendChild(element);
+ })
+ .convertBodyTo(Element.class)
+ .convertBodyTo(String.class);
+
+ from("direct:byteSourceConvert")
+ .convertBodyTo(byte[].class)
+ .convertBodyTo(BytesSource.class)
+ .convertBodyTo(String.class);
+
+ from("direct:sourceConvert")
+ .convertBodyTo(Source.class)
+ .convertBodyTo(String.class);
+
+ from("direct:domSourceConvert")
+ .convertBodyTo(byte[].class)
+ .convertBodyTo(DOMSource.class)
+ .convertBodyTo(String.class);
+
+ from("direct:saxSourceConvert")
+ .convertBodyTo(SAXSource.class)
+ .convertBodyTo(String.class);
+
+ from("direct:staxSourceConvert")
+ .convertBodyTo(StAXSource.class)
+ .convertBodyTo(String.class);
+
+ from("direct:streamSourceConvert")
+ .convertBodyTo(StreamSource.class)
+ .convertBodyTo(String.class);
+
+ from("direct:streamSourceReaderConvert")
+ .convertBodyTo(StreamSource.class)
+ .convertBodyTo(Reader.class)
+ .convertBodyTo(String.class);
+
+ from("direct:xmlStreamReaderCharsetConvert")
+ .convertBodyTo(InputStream.class, StandardCharsets.ISO_8859_1.name())
+ .convertBodyTo(XMLStreamReader.class)
+ .process(exchange -> {
+ Message message = exchange.getMessage();
+ XMLStreamReader reader = message.getBody(XMLStreamReader.class);
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ exchange.setProperty(Exchange.CHARSET_NAME, StandardCharsets.UTF_8.name());
+ XMLStreamWriter writer = exchange.getContext().getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class,
+ exchange, output);
+ while (reader.hasNext()) {
+ reader.next();
+ switch (reader.getEventType()) {
+ case XMLStreamConstants.START_DOCUMENT:
+ writer.writeStartDocument();
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ writer.writeEndDocument();
+ break;
+ case XMLStreamConstants.START_ELEMENT:
+ writer.writeStartElement(reader.getName().getLocalPart());
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ writer.writeCharacters(reader.getText());
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ writer.writeEndElement();
+ break;
+ default:
+ break;
+ }
+ }
+ reader.close();
+ writer.close();
+ message.setBody(output);
+ })
+ .convertBodyTo(String.class, StandardCharsets.UTF_8.name());
+
+ from("direct:nodeListConvert")
+ .convertBodyTo(Document.class)
+ .process(exchange -> {
+ Message message = exchange.getMessage();
+ Document document = message.getBody(Document.class);
+ NodeList nodeList = document.getElementsByTagName("foo");
+ message.setBody(nodeList);
+ })
+ .convertBodyTo(Node.class)
+ .convertBodyTo(String.class);
+ }
+}
diff --git a/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpIT.java b/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpIT.java
new file mode 100644
index 0000000000..bdc75f1777
--- /dev/null
+++ b/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpIT.java
@@ -0,0 +1,23 @@
+/*
+ * 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 apache.camel.quarkus.component.xml.jaxp;
+
+import io.quarkus.test.junit.QuarkusIntegrationTest;
+
+@QuarkusIntegrationTest
+class XmlJaxpIT extends XmlJaxpTest {
+}
diff --git a/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpTest.java b/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpTest.java
new file mode 100644
index 0000000000..2eb81ce4b1
--- /dev/null
+++ b/integration-tests/xml-jaxp/src/test/java/apache/camel/quarkus/component/xml/jaxp/XmlJaxpTest.java
@@ -0,0 +1,192 @@
+/*
+ * 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 apache.camel.quarkus.component.xml.jaxp;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class XmlJaxpTest {
+ private static final String XML_PAYLOAD = "<root><foo>Foo Text</foo></root>";
+ private static final String XML_PAYLOAD_WITH_UMLAUT = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root><foo>Foo Text With Umlaut \u00E4\u00F6\u00FC</foo></root>";
+
+ @Test
+ void documentConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:documentConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"),
+ "root.bar", equalTo("Bar Text"));
+ }
+
+ @Test
+ void elementConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:elementConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"),
+ "root.bar", equalTo("Bar Text"));
+ }
+
+ @Test
+ void byteSourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:byteSourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void sourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:sourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void domSourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:domSourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void saxSourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:saxSourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void staxSourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:staxSourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void streamSourceConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:streamSourceConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void streamSourceReaderConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:streamSourceReaderConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text"));
+ }
+
+ @Test
+ void xmlStreamReaderCharsetConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:xmlStreamReaderCharsetConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD_WITH_UMLAUT)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "root.foo", equalTo("Foo Text With Umlaut äöü"));
+ }
+
+ @Test
+ void contextGlobalOptionsConversion() {
+ String result = RestAssured.given()
+ .queryParam("endpointUri", "direct:documentConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert/context/global/options")
+ .then()
+ .statusCode(200)
+ .extract()
+ .body()
+ .asString();
+
+ assertTrue(result.contains("encoding=\"UTF-8\""));
+ assertTrue(result.contains("standalone=\"no\""));
+ }
+
+ @Test
+ void nodeListConversion() {
+ RestAssured.given()
+ .queryParam("endpointUri", "direct:nodeListConvert")
+ .contentType("application/xml")
+ .body(XML_PAYLOAD)
+ .post("/xml/jaxp/convert")
+ .then()
+ .statusCode(200)
+ .body(
+ "foo", equalTo("Foo Text"));
+ }
+}
diff --git a/tooling/scripts/test-categories.yaml b/tooling/scripts/test-categories.yaml
index 4f255251f6..4fe8c66dcb 100644
--- a/tooling/scripts/test-categories.yaml
+++ b/tooling/scripts/test-categories.yaml
@@ -136,6 +136,7 @@ group-08:
- tika
- twitter
- vertx
+ - xml-jaxp
- zendesk
group-09:
- activemq