You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2022/11/25 08:54:14 UTC

[camel-quarkus] branch main updated: Extend tests with RAW and CXF_MESSAGE dataFormats

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

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 381fd3e8e0 Extend tests with RAW and CXF_MESSAGE dataFormats
381fd3e8e0 is described below

commit 381fd3e8e0b411162033cf86c9e99288faeadc45
Author: Lukas Lowinger <ll...@redhat.com>
AuthorDate: Mon Nov 21 15:49:21 2022 +0100

    Extend tests with RAW and CXF_MESSAGE dataFormats
---
 .../cxf/soap/client/it/CxfSoapClientResource.java  | 20 +++++++
 .../cxf/soap/client/it/CxfSoapClientRoutes.java    | 43 +++++++++++++-
 .../cxf/soap/client/it/CxfSoapClientTest.java      | 17 ++++++
 .../cxf/soap/server/it/CxfSoapRoutes.java          | 69 ++++++++++++++++++++++
 .../cxf/soap/server/it/CxfSoapServiceTest.java     |  8 +++
 5 files changed, 155 insertions(+), 2 deletions(-)

diff --git a/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientResource.java b/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientResource.java
index 7485357953..4ecee01f68 100644
--- a/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientResource.java
+++ b/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientResource.java
@@ -17,6 +17,8 @@
 package org.apache.camel.quarkus.component.cxf.soap.client.it;
 
 import java.net.URI;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -53,6 +55,24 @@ public class CxfSoapClientResource {
                 .build();
     }
 
+    @Path("/simpleAddDataFormat")
+    @POST
+    @Consumes(MediaType.WILDCARD)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response sendSimpleAddRequestDataFormat(@QueryParam("a") int a,
+            @QueryParam("b") int b, @QueryParam("endpointDataFormat") String endpointDataFormat) throws Exception {
+        Map<String, Object> headers = new LinkedHashMap<>();
+        headers.put("endpointDataFormat", endpointDataFormat);
+
+        final String response = producerTemplate.requestBodyAndHeaders("direct:simpleAddDataFormat", new int[] { a, b },
+                headers,
+                String.class);
+        return Response
+                .created(new URI("https://camel.apache.org/"))
+                .entity(response)
+                .build();
+    }
+
     @Path("/operandsAdd")
     @POST
     @Consumes(MediaType.WILDCARD)
diff --git a/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientRoutes.java b/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientRoutes.java
index c2f5d97823..1f82daac7a 100644
--- a/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientRoutes.java
+++ b/integration-test-groups/cxf-soap/cxf-soap-client/src/main/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientRoutes.java
@@ -16,12 +16,22 @@
  */
 package org.apache.camel.quarkus.component.cxf.soap.client.it;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.SessionScoped;
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 import javax.inject.Named;
+import javax.xml.soap.MessageFactory;
+import javax.xml.soap.SOAPMessage;
 
+import com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cxf.common.DataFormat;
 import org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.component.cxf.jaxws.CxfEndpoint;
 import org.apache.cxf.ext.logging.LoggingFeature;
@@ -38,16 +48,45 @@ public class CxfSoapClientRoutes extends RouteBuilder {
     @ConfigProperty(name = "camel-quarkus.it.calculator.baseUri")
     String serviceBaseUri;
 
+    public static final String MESSAGE_RAW_SIMPLE_ADD = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://www.jboss.org/eap/quickstarts/wscalculator/Calculator\">\n"
+            +
+            "   <soapenv:Header/>\n" +
+            "   <soapenv:Body>\n" +
+            "      <ser:add>\n" +
+            "         <arg0>%s</arg0>\n" +
+            "         <arg1>%s</arg1>\n" +
+            "      </ser:add>\n" +
+            "   </soapenv:Body>\n" +
+            "</soapenv:Envelope>";
+
     @Override
     public void configure() {
 
         from("direct:simpleUriBean")
-                .to("cxf:bean:soapClientEndpoint?dataFormat=PAYLOAD");
+                .to("cxf:bean:soapClientEndpoint?dataFormat=POJO");
 
         from("direct:simpleUriAddress")
                 .to(String.format("cxf://%s?wsdlURL=%s&dataFormat=POJO&serviceClass=%s", calculatorServiceAddress(),
                         calculatorServiceWsdlUrl(), CalculatorService.class.getName()));
 
+        from("direct:simpleAddDataFormat")
+                .process(exchange -> {
+                    Map<String, Object> headers = exchange.getIn().getHeaders();
+                    String endpointDataFormat = headers.get("endpointDataFormat").toString();
+                    int[] numbers = exchange.getIn().getBody(int[].class);
+                    String xmlRequest = String.format(MESSAGE_RAW_SIMPLE_ADD, numbers[0], numbers[1]);
+                    if (DataFormat.RAW.name().equals(endpointDataFormat)) {
+                        exchange.getIn().setBody(xmlRequest);
+                    } else if (DataFormat.CXF_MESSAGE.name().equals(endpointDataFormat)) {
+                        try (InputStream is = new ByteArrayInputStream(xmlRequest.getBytes(StandardCharsets.UTF_8))) {
+                            SOAPMessage requestMsg = MessageFactory.newInstance().createMessage(null, is);
+                            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "add");
+                            exchange.getIn().setBody(new Message1_1Impl(requestMsg));
+                        }
+                    }
+                })
+                .toD("cxf:bean:soapClientEndpoint?dataFormat=${header.endpointDataFormat}");
+
         from("direct:operandsAdd")
                 .setHeader(CxfConstants.OPERATION_NAME).constant("addOperands")
                 .to("cxf:bean:soapClientEndpoint?dataFormat=POJO");
@@ -63,7 +102,7 @@ public class CxfSoapClientRoutes extends RouteBuilder {
     }
 
     @Produces
-    @ApplicationScoped
+    @SessionScoped
     @Named
     CxfEndpoint soapClientEndpoint() {
         final CxfEndpoint result = new CxfEndpoint();
diff --git a/integration-test-groups/cxf-soap/cxf-soap-client/src/test/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientTest.java b/integration-test-groups/cxf-soap/cxf-soap-client/src/test/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientTest.java
index d5f41a787b..236c35295f 100644
--- a/integration-test-groups/cxf-soap/cxf-soap-client/src/test/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientTest.java
+++ b/integration-test-groups/cxf-soap/cxf-soap-client/src/test/java/org/apache/camel/quarkus/component/cxf/soap/client/it/CxfSoapClientTest.java
@@ -27,6 +27,8 @@ import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import org.eclipse.microprofile.config.ConfigProvider;
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.Matchers;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -52,6 +54,21 @@ class CxfSoapClientTest {
                 .body(equalTo("3"));
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "RAW", "CXF_MESSAGE" })
+    public void simpleSoapClientDataFormats(String endpointDataformat) {
+        RestAssured.given()
+                .queryParam("a", "9")
+                .queryParam("b", "3")
+                .queryParam("endpointDataFormat", endpointDataformat)
+                .post("/cxf-soap/client/simpleAddDataFormat")
+                .then()
+                .statusCode(201)
+                .body(Matchers.hasXPath(
+                        "/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name() = 'addResponse']/*[local-name() = 'return']/text()",
+                        CoreMatchers.is("12")));
+    }
+
     @Test
     public void complexSoapClient() {
         RestAssured.given()
diff --git a/integration-test-groups/cxf-soap/cxf-soap-server/src/main/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapRoutes.java b/integration-test-groups/cxf-soap/cxf-soap-server/src/main/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapRoutes.java
index 42a515ce8b..46fa8ad7a0 100644
--- a/integration-test-groups/cxf-soap/cxf-soap-server/src/main/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapRoutes.java
+++ b/integration-test-groups/cxf-soap/cxf-soap-server/src/main/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapRoutes.java
@@ -16,16 +16,30 @@
  */
 package org.apache.camel.quarkus.component.cxf.soap.server.it;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 import javax.inject.Named;
+import javax.xml.soap.MessageFactory;
+import javax.xml.soap.SOAPMessage;
+import javax.xml.xpath.XPathConstants;
+
+import org.w3c.dom.Element;
 
 import com.helloworld.service.CodeFirstService;
 import com.helloworld.service.HelloPortType;
+import com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cxf.common.DataFormat;
 import org.apache.camel.component.cxf.jaxws.CxfEndpoint;
+import org.apache.camel.converter.jaxp.XmlConverter;
+import org.apache.camel.util.xml.StringSource;
 import org.apache.cxf.ext.logging.LoggingFeature;
+import org.apache.cxf.helpers.XPathUtils;
 
 @ApplicationScoped
 public class CxfSoapRoutes extends RouteBuilder {
@@ -34,6 +48,14 @@ public class CxfSoapRoutes extends RouteBuilder {
     @Named("loggingFeatureServer")
     LoggingFeature loggingFeature;
 
+    public static final String response = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://it.server.soap.cxf.component.quarkus.camel.apache.org/\">\n"
+            +
+            "   <soapenv:Header/>\n" +
+            "   <soapenv:Body>\n" +
+            "      <ser:echoResponse><return>%s</return></ser:echoResponse>\n" +
+            "   </soapenv:Body>\n" +
+            "</soapenv:Envelope>";
+
     @Override
     public void configure() {
         /* Service */
@@ -47,6 +69,29 @@ public class CxfSoapRoutes extends RouteBuilder {
         from("cxf:bean:codeFirstServiceEndpoint")
                 .setBody().constant("Hello CamelQuarkusCXF");
 
+        from("cxf:bean:echoServiceResponseFromRouteCxfMessageDataFormat")
+                .process(exchange -> {
+                    SOAPMessage requestMsg = exchange.getIn().getBody(SOAPMessage.class);
+                    String requestText = requestMsg.getSOAPBody().getElementsByTagName("arg0").item(0).getFirstChild()
+                            .getNodeValue();
+                    String xmlResponse = String.format(response, requestText + " from Camel route");
+                    try (InputStream is = new ByteArrayInputStream(xmlResponse.getBytes(StandardCharsets.UTF_8))) {
+                        SOAPMessage responseMsg = MessageFactory.newInstance().createMessage(null, is);
+
+                        exchange.getIn().setBody(new Message1_1Impl(responseMsg));
+                    }
+                });
+
+        from("cxf:bean:echoServiceResponseFromRouteRawDataFormat")
+                .process(exchange -> {
+                    String rawXmlRequest = exchange.getIn().getBody(String.class);
+                    XPathUtils xu = new XPathUtils();
+                    Element body = new XmlConverter().toDOMElement(new StringSource(rawXmlRequest));
+                    String requestMsg = ((Element) xu.getValue("//arg0", body, XPathConstants.NODE)).getTextContent();
+
+                    exchange.getIn().setBody(String.format(response, requestMsg + " from Camel route"));
+                });
+
         from("cxf:bean:echoServiceResponseFromRoute")
                 .setBody(exchange -> exchange.getMessage().getBody(String.class) + " from Camel route");
 
@@ -87,6 +132,30 @@ public class CxfSoapRoutes extends RouteBuilder {
         return result;
     }
 
+    @Produces
+    @ApplicationScoped
+    @Named
+    CxfEndpoint echoServiceResponseFromRouteRawDataFormat() {
+        final CxfEndpoint result = new CxfEndpoint();
+        result.setServiceClass(EchoServiceImpl.class);
+        result.setAddress("/echo-route-raw-data-format");
+        result.setDataFormat(DataFormat.RAW);
+        result.getFeatures().add(loggingFeature);
+        return result;
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Named
+    CxfEndpoint echoServiceResponseFromRouteCxfMessageDataFormat() {
+        final CxfEndpoint result = new CxfEndpoint();
+        result.setServiceClass(EchoServiceImpl.class);
+        result.setAddress("/echo-route-cxf-message-data-format");
+        result.setDataFormat(DataFormat.CXF_MESSAGE);
+        result.getFeatures().add(loggingFeature);
+        return result;
+    }
+
     @Produces
     @ApplicationScoped
     @Named
diff --git a/integration-test-groups/cxf-soap/cxf-soap-server/src/test/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapServiceTest.java b/integration-test-groups/cxf-soap/cxf-soap-server/src/test/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapServiceTest.java
index b397ac2446..60dec478d7 100644
--- a/integration-test-groups/cxf-soap/cxf-soap-server/src/test/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapServiceTest.java
+++ b/integration-test-groups/cxf-soap/cxf-soap-server/src/test/java/org/apache/camel/quarkus/component/cxf/soap/server/it/CxfSoapServiceTest.java
@@ -73,6 +73,14 @@ class CxfSoapServiceTest {
         org.junit.jupiter.api.Assertions.assertTrue(response.contains("Hello CamelQuarkusCXF"));
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "raw", "cxf-message" })
+    public void testCodeFirstSoapServiceDataFormats(String dataFormat) {
+        final EchoService echo = QuarkusCxfClientTestUtil.getClient(EchoService.class,
+                String.format("/soapservice/echo-route-%s-data-format", dataFormat));
+        Assertions.assertEquals("Hello there! from Camel route", echo.echo("Hello there!"));
+    }
+
     @Test
     public void echoServiceResponseFromRoute() {
         /* We setServiceClass(EchoServiceImpl.class) in org.apache.camel.quarkus.component.cxf.soap.server.it.CxfSoapRoutes.echoServiceResponseFromRoute()