Skip to content

Commit

Permalink
Fix apache#5139 to expand xslt-saxon test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Aug 14, 2023
1 parent 9c98b7e commit e48d940
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 48 deletions.
2 changes: 1 addition & 1 deletion extensions/xslt-saxon/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
<artifactId>camel-quarkus-xslt-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/xslt-saxon/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
<artifactId>camel-quarkus-xslt</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ name: "Camel XSLT Saxon"
description: "Transform XML payloads using an XSLT template using Saxon"
icon-url: "https://camel.apache.org/_/img/logo-d-f21b25ba38.svg"
metadata:
unlisted: true
guide: "https://camel.apache.org/camel-quarkus/latest/reference/extensions/xslt-saxon.html"
categories:
- "integration"
status:
- "preview"
- "stable"
4 changes: 4 additions & 0 deletions integration-test-groups/xml/native/classpath/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xslt</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xslt-saxon</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xpath</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.apache.camel.CamelContext;
import net.sf.saxon.trans.XPathException;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.jboss.logging.Logger;

@Path("/xml")
Expand All @@ -47,34 +46,45 @@ public class XsltResource {
@Inject
ConsumerTemplate consumerTemplate;

@Inject
CamelContext camelContext;
@Path("/stax")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String stax(String body) {
return producerTemplate.requestBody("xslt-saxon:xslt/classpath-transform.xsl?allowStAX=true", body, String.class);
}

@Path("/xslt")
@Path("/{component}")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String classpath(@QueryParam("output") @DefaultValue("string") String output, String body) {
public String classpath(@PathParam("component") String component,
@QueryParam("output") @DefaultValue("string") String output, String body) {
if (output.equals("file")) {
return producerTemplate.requestBodyAndHeader("xslt:xslt/classpath-transform.xsl?output=file",
return producerTemplate.requestBodyAndHeader(component + ":xslt/classpath-transform.xsl?output=file",
body, Exchange.XSLT_FILE_NAME, "target/xsltme.xml", String.class);
}
return producerTemplate.requestBody("xslt:xslt/classpath-transform.xsl?output=" + output, body, String.class);
return producerTemplate.requestBody(component + ":xslt/classpath-transform.xsl?output=" + output, body, String.class);
}

@Path("/xslt_include")
@Path("/{component}/include")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String xsltInclude(String body) {
return producerTemplate.requestBody("xslt:xslt/include.xsl", body, String.class);
public String xsltInclude(@PathParam("component") String component, String body) {
return producerTemplate.requestBody(component + ":xslt/include.xsl", body, String.class);
}

@Path("/xslt_terminate")
@Path("/{component}/terminate")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String xsltTerminate(String body) {
Exchange out = producerTemplate.request("xslt:xslt/terminate.xsl", exchange -> exchange.getIn().setBody(body));
Exception warning = out.getProperty(Exchange.XSLT_WARNING, Exception.class);
return warning.getMessage();
public String xsltTerminate(@PathParam("component") String component, String body) throws Exception {
Exchange out = producerTemplate.request(component + ":xslt/terminate.xsl", exchange -> exchange.getIn().setBody(body));
if (component.equals("xslt")) {
Exception warning = out.getProperty(Exchange.XSLT_WARNING, Exception.class);
return warning.getMessage();
} else if (component.equals("xslt-saxon")) {
Exception error = out.getProperty(Exchange.XSLT_FATAL_ERROR, Exception.class);
return ((XPathException) error).getErrorObject().head().getStringValue();
}
return "";
}

@Path("/xslt-extension-function")
Expand All @@ -84,28 +94,22 @@ public String extensionFunction(String body) {
return producerTemplate.requestBody("xslt:xslt/extension-function.xsl", body, String.class);
}

@Path("/xslt-custom-uri-resolver")
@Path("/xslt-saxon-extension-function")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String customURIResolver(String body) {
return producerTemplate.requestBody("xslt:xslt/include_not_existing_resource.xsl?uriResolver=#customURIResolver", body,
public String saxonExtensionFunction(String body) {
return producerTemplate.requestBody(
"xslt-saxon:xslt/saxon-extension-function.xsl?saxonExtensionFunctions=#function1,#function2", body,
String.class);
}

@Path("/aggregate")
@GET
@Path("/{component}/custom-uri-resolver")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String aggregate() throws Exception {
MockEndpoint mock = camelContext.getEndpoint("mock:transformed", MockEndpoint.class);
mock.expectedMessageCount(1);

producerTemplate.sendBody("direct:aggregate", "<item>A</item>");
producerTemplate.sendBody("direct:aggregate", "<item>B</item>");
producerTemplate.sendBody("direct:aggregate", "<item>C</item>");

mock.assertIsSatisfied();
return mock.getExchanges().get(0).getIn().getBody(String.class);

public String customURIResolver(@PathParam("component") String component, String body) {
return producerTemplate.requestBody(
component + ":xslt/include_not_existing_resource.xsl?uriResolver=#customURIResolver", body,
String.class);
}

@Path("/html-transform")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import org.w3c.dom.Document;

import org.apache.camel.BindToRegistry;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.quarkus.test.support.xslt.MyExtensionFunction1;
import org.apache.camel.quarkus.test.support.xslt.MyExtensionFunction2;
import org.apache.camel.support.builder.Namespaces;
import org.apache.xpath.XPathAPI;

Expand Down Expand Up @@ -55,4 +58,10 @@ public void configure() {
.to("seda:xtokenize-result");

}

@BindToRegistry
private MyExtensionFunction1 function1 = new MyExtensionFunction1();

@BindToRegistry
private MyExtensionFunction2 function2 = new MyExtensionFunction2();
}
Original file line number Diff line number Diff line change
@@ -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.
-->
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://mytest/"
exclude-result-prefixes="func">

<xsl:output method="xml" encoding="UTF-8" indent="no" />

<xsl:template match="/">
<Test1>
<xsl:value-of select="func:myExtensionFunction1(1, 2)"/>
</Test1>
<Test2>
<xsl:value-of select="func:myExtensionFunction2('abc', 'cde')"/>
</Test2>
<Test3>
<xsl:value-of select="func:myExtensionFunction2('xyz')"/>
</Test3>
</xsl:template>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import io.quarkus.test.junit.DisabledOnIntegrationTest;
import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -28,6 +29,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.Matchers.is;
Expand All @@ -36,12 +39,24 @@
public class XsltTest {
private static final String BODY = "<mail><subject>Hey</subject><body>Hello world!</body></mail>";

private static Stream<Arguments> matrix() {
return Stream.of(
Arguments.of("xslt", "string"),
Arguments.of("xslt", "bytes"),
Arguments.of("xslt", "dom"),
Arguments.of("xslt", "file"),
Arguments.of("xslt-saxon", "string"),
Arguments.of("xslt-saxon", "bytes"),
Arguments.of("xslt-saxon", "dom"),
Arguments.of("xslt-saxon", "file"));
}

@ParameterizedTest
@ValueSource(strings = { "string", "bytes", "dom", "file" })
public void xslt(String output) throws Exception {
@MethodSource("matrix")
public void xslt(String component, String output) throws Exception {
final String actual = RestAssured.given()
.body(BODY)
.post("/xml/xslt?output=" + output)
.post("/xml/" + component + "?output=" + output)
.then()
.statusCode(200)
.extract().body().asString().trim().replaceAll(">\\s+<", "><");
Expand All @@ -64,6 +79,20 @@ public void xslt(String output) throws Exception {
}
}

@Test
public void saxonStAX() {
final String actual = RestAssured.given()
.body(BODY)
.post("/xml/stax")
.then()
.statusCode(200)
.extract().body().asString().trim().replaceAll(">\\s+<", "><");

Assertions.assertEquals(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><classpath-xsl subject=\"Hey\"><cheese><mail><subject>Hey</subject><body>Hello world!</body></mail></cheese></classpath-xsl>",
actual);
}

@Test
public void xsltExtensionFunction() {
final String actual = RestAssured.given()
Expand All @@ -79,10 +108,26 @@ public void xsltExtensionFunction() {
}

@Test
public void xsltCustomURIResolver() {
@DisabledOnIntegrationTest("xslt-saxon extension function does not be supported in native mode")
public void saxonExtensionFunction() {
final String actual = RestAssured.given()
.body(BODY)
.post("/xml/xslt-custom-uri-resolver")
.post("/xml/xslt-saxon-extension-function")
.then()
.statusCode(200)
.extract().body().asString().trim().replaceAll(">\\s+<", "><");

Assertions.assertEquals(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test1>3</Test1><Test2>abccde</Test2><Test3>xyz</Test3>",
actual);
}

@ParameterizedTest
@ValueSource(strings = { "xslt", "xslt-saxon" })
public void xsltCustomURIResolver(String component) {
final String actual = RestAssured.given()
.body(BODY)
.post("/xml/" + component + "/custom-uri-resolver")
.then()
.statusCode(200)
.extract().body().asString().trim().replaceAll(">\\s+<", "><");
Expand All @@ -92,12 +137,13 @@ public void xsltCustomURIResolver() {
actual);
}

@Test
@ParameterizedTest
@ValueSource(strings = { "xslt", "xslt-saxon" })
@DisabledOnIntegrationTest("Generating xslt templates dynamically does not be supported in native mode")
public void xsltInclude() {
public void xsltInclude(String component) {
final String actual = RestAssured.given()
.body(BODY)
.post("/xml/xslt_include")
.post("/xml/" + component + "/include")
.then()
.statusCode(200)
.extract().body().asString().trim().replaceAll(">\\s+<", "><");
Expand All @@ -107,12 +153,13 @@ public void xsltInclude() {
actual);
}

@Test
@ParameterizedTest
@ValueSource(strings = { "xslt", "xslt-saxon" })
@DisabledOnIntegrationTest("forwarding xslt error and warn messages does not be supported in native mode")
public void xsltTerminate() {
public void xsltTerminate(String component) {
RestAssured.given()
.body("<staff><programmer><name>Daisy Duck</name><dob></dob></programmer></staff>")
.post("/xml/xslt_terminate")
.post("/xml/" + component + "/terminate")
.then()
.statusCode(200)
.body(is("Error: DOB is an empty string!"));
Expand Down
7 changes: 7 additions & 0 deletions integration-tests-support/xslt-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@

<artifactId>camel-quarkus-integration-tests-support-xslt</artifactId>
<name>Camel Quarkus :: Integration Tests :: Support :: XSLT</name>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xslt-saxon</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit e48d940

Please sign in to comment.