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 b0f91f3 Add test coverage for additional JMS message types
b0f91f3 is described below
commit b0f91f326089c22e6f98391f4667828d8b9934e2
Author: James Netherton <[email protected]>
AuthorDate: Tue May 18 09:50:46 2021 +0100
Add test coverage for additional JMS message types
---
.../ROOT/pages/reference/extensions/jms.adoc | 8 ++
extensions/jms/runtime/src/main/doc/usage.adoc | 4 +
integration-tests/messaging/pom.xml | 17 ++++
.../component/messaging/it/JmsProducers.java | 44 +++++++--
.../component/messaging/it/JmsResource.java | 108 +++++++++++++++++++--
.../quarkus/component/messaging/it/JmsRoutes.java | 13 ++-
.../quarkus/component/messaging/it/Person.java | 35 +++++++
.../quarkus/component/messaging/it/JmsTest.java | 58 ++++++++++-
8 files changed, 262 insertions(+), 25 deletions(-)
diff --git a/docs/modules/ROOT/pages/reference/extensions/jms.adoc
b/docs/modules/ROOT/pages/reference/extensions/jms.adoc
index 6abf885..8935c37 100644
--- a/docs/modules/ROOT/pages/reference/extensions/jms.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/jms.adoc
@@ -38,3 +38,11 @@ Or add the coordinates to your existing project:
----
Check the xref:user-guide/index.adoc[User guide] for more information about
writing Camel Quarkus applications.
+
+== Usage
+
+=== Message mapping with `org.w3c.dom.Node`
+
+The Camel JMS component supports message mapping between `javax.jms.Message`
and `org.apache.camel.Message`. When wanting to convert a Camel message body
type of `org.w3c.dom.Node`,
+you must ensure that the `camel-quarkus-jaxp` extension is present on the
classpath.
+
diff --git a/extensions/jms/runtime/src/main/doc/usage.adoc
b/extensions/jms/runtime/src/main/doc/usage.adoc
new file mode 100644
index 0000000..7e3adcf
--- /dev/null
+++ b/extensions/jms/runtime/src/main/doc/usage.adoc
@@ -0,0 +1,4 @@
+=== Message mapping with `org.w3c.dom.Node`
+
+The Camel JMS component supports message mapping between `javax.jms.Message`
and `org.apache.camel.Message`. When wanting to convert a Camel message body
type of `org.w3c.dom.Node`,
+you must ensure that the `camel-quarkus-jaxp` extension is present on the
classpath.
diff --git a/integration-tests/messaging/pom.xml
b/integration-tests/messaging/pom.xml
index 5e0369e..c34c6e1 100644
--- a/integration-tests/messaging/pom.xml
+++ b/integration-tests/messaging/pom.xml
@@ -55,6 +55,10 @@
<artifactId>camel-quarkus-sjms2</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.camel.quarkus</groupId>
+ <artifactId>camel-quarkus-xml-jaxp</artifactId>
+ </dependency>
+ <dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
@@ -163,6 +167,19 @@
</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>
diff --git
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsProducers.java
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsProducers.java
index 08bf046..c29064a 100644
---
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsProducers.java
+++
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsProducers.java
@@ -17,27 +17,30 @@
package org.apache.camel.quarkus.component.messaging.it;
import javax.inject.Named;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.Session;
+import javax.jms.TextMessage;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
-import javax.ws.rs.Produces;
import org.apache.camel.component.jms.MessageListenerContainerFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
+import org.springframework.jms.support.converter.MessageConversionException;
+import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
public class JmsProducers {
- @Produces
- @Named("customMessageListener")
- public MessageListenerContainerFactory
createCustomMessageListenerContainerFactory() {
+ @Named
+ public MessageListenerContainerFactory customMessageListener() {
return jmsEndpoint -> new DefaultMessageListenerContainer();
}
- @Produces
- @Named("customDestinationResolver")
- public DestinationResolver createCustomDestinationResolver() {
+ @Named
+ public DestinationResolver customDestinationResolver() {
return (session, destinationName, pubSubDomain) -> {
if (destinationName.equals("ignored")) {
// Ignore and override the original queue name
@@ -47,9 +50,32 @@ public class JmsProducers {
};
}
- @Produces
- PlatformTransactionManager createTransactionManager(UserTransaction
userTransaction,
+ @Named
+ public PlatformTransactionManager transactionManager(UserTransaction
userTransaction,
TransactionManager transactionManager) {
return new JtaTransactionManager(userTransaction, transactionManager);
}
+
+ @Named
+ public MessageConverter customMessageConverter() {
+ return new MessageConverter() {
+ @Override
+ public Message toMessage(Object o, Session session) throws
JMSException, MessageConversionException {
+ if (o instanceof String) {
+ TextMessage message = session.createTextMessage("converter
prefix " + o);
+ return message;
+ }
+ return null;
+ }
+
+ @Override
+ public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
+ if (message instanceof TextMessage) {
+ TextMessage textMessage = (TextMessage) message;
+ return textMessage.getText() + " converter suffix";
+ }
+ return null;
+ }
+ };
+ }
}
diff --git
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsResource.java
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsResource.java
index 57fb443..22ec50f 100644
---
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsResource.java
+++
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsResource.java
@@ -17,12 +17,17 @@
package org.apache.camel.quarkus.component.messaging.it;
import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.inject.Inject;
+import javax.jms.BytesMessage;
import javax.jms.MapMessage;
+import javax.jms.TextMessage;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
@@ -31,6 +36,10 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
@@ -74,14 +83,39 @@ public class JmsResource {
}
@Path("/jms/type/{type}")
- @GET
+ @POST
+ @Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
- public Response jmsMessageType(@PathParam("type") String type) throws
Exception {
+ public Response jmsMessageType(@PathParam("type") String type, String
messageBody) throws Exception {
MockEndpoint mockEndpoint = context.getEndpoint("mock:jmsType",
MockEndpoint.class);
mockEndpoint.reset();
mockEndpoint.expectedMessageCount(1);
- producerTemplate.sendBodyAndHeader("jms:queue:typeTest", "test message
payload", "type", type);
+ Object payload;
+ switch (type) {
+ case "bytes":
+ payload = messageBody.getBytes(StandardCharsets.UTF_8);
+ break;
+ case "file":
+ java.nio.file.Path path = Files.createTempFile("jms", ".txt");
+ Files.write(path, messageBody.getBytes(StandardCharsets.UTF_8));
+ payload = path.toFile();
+ break;
+ case "node":
+ DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
+ Document document = factory.newDocumentBuilder().newDocument();
+ Element element = document.createElement("test");
+ element.setTextContent(messageBody);
+ payload = element;
+ break;
+ case "string":
+ payload = messageBody;
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown type: " + type);
+ }
+
+ producerTemplate.sendBody("jms:queue:typeTest", payload);
mockEndpoint.assertIsSatisfied(5000);
@@ -90,11 +124,19 @@ public class JmsResource {
.getMessage(JmsMessage.class)
.getJmsMessage();
- boolean result;
- if (type.equals("Text")) {
- result = (message instanceof javax.jms.TextMessage);
+ Object result;
+ if (type.equals("string") || type.equals("node")) {
+ assert message instanceof javax.jms.TextMessage;
+ TextMessage textMessage = (TextMessage) message;
+ result = textMessage.getText();
} else {
- result = (message instanceof javax.jms.BytesMessage);
+ assert message instanceof javax.jms.BytesMessage;
+ BytesMessage byteMessage = (BytesMessage) message;
+ byteMessage.reset();
+ byte[] byteData;
+ byteData = new byte[(int) byteMessage.getBodyLength()];
+ byteMessage.readBytes(byteData);
+ result = new String(byteData);
}
return Response.ok().entity(result).build();
@@ -130,6 +172,7 @@ public class JmsResource {
@Path("/jms/custom/message/listener/factory")
@POST
+ @Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String customMessageListenerContainerFactory(String message) {
producerTemplate.sendBody("jms:queue:listener?messageListenerContainerFactory=#customMessageListener",
message);
@@ -138,6 +181,7 @@ public class JmsResource {
@Path("/jms/custom/destination/resolver")
@POST
+ @Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String customDestinationResolver(String message) {
producerTemplate.sendBody("jms:queue:ignored?destinationResolver=#customDestinationResolver",
message);
@@ -146,6 +190,15 @@ public class JmsResource {
return consumerTemplate.receiveBody("jms:queue:destinationOverride",
5000, String.class);
}
+ @Path("/jms/custom/message/converter")
+ @POST
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ public String customMessageConverter(String message) {
+
producerTemplate.sendBody("jms:queue:converter?messageConverter=#customMessageConverter",
message);
+ return
consumerTemplate.receiveBody("jms:queue:converter?messageConverter=#customMessageConverter",
5000, String.class);
+ }
+
@Path("/jms/selector/{expression}")
@GET
@Produces(MediaType.TEXT_PLAIN)
@@ -169,7 +222,7 @@ public class JmsResource {
producerTemplate.sendBody("jms:queue:txTest?transacted=true", "Test
JMS Transaction");
- mockEndpoint.assertIsSatisfied(15000);
+ mockEndpoint.assertIsSatisfied(5000);
Exchange exchange = mockEndpoint.getExchanges().get(0);
Message message = exchange.getMessage();
@@ -177,7 +230,46 @@ public class JmsResource {
return Response.ok().entity(message.getBody()).build();
}
+ @Path("/jms/object")
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ @POST
+ public Response testObjectMessage(String name) throws InterruptedException
{
+ MockEndpoint mockEndpoint =
context.getEndpoint("mock:objectTestResult", MockEndpoint.class);
+ mockEndpoint.expectedMessageCount(1);
+
+ producerTemplate.sendBody("jms:queue:objectTest", new Person(name));
+
+ mockEndpoint.assertIsSatisfied();
+
+ List<Exchange> exchanges = mockEndpoint.getExchanges();
+ Exchange exchange = exchanges.get(0);
+ Person body = exchange.getMessage().getBody(Person.class);
+
+ return Response.ok().entity(body.getName()).build();
+ }
+
+ @Path("/jms/transfer/exchange")
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ @POST
+ public Response testTransferExchange(String message) throws
InterruptedException {
+ MockEndpoint mockEndpoint =
context.getEndpoint("mock:transferExchangeResult", MockEndpoint.class);
+ mockEndpoint.expectedMessageCount(1);
+
+
producerTemplate.sendBody("jms:queue:transferExchange?transferExchange=true",
message);
+
+ mockEndpoint.assertIsSatisfied();
+
+ List<Exchange> exchanges = mockEndpoint.getExchanges();
+ Exchange exchange = exchanges.get(0);
+ String result = exchange.getMessage().getBody(String.class);
+
+ return Response.ok().entity(result).build();
+ }
+
@Path("/jms/topic")
+ @Consumes(MediaType.TEXT_PLAIN)
@POST
public void topicPubSub(String message) throws Exception {
MockEndpoint topicResultA = context.getEndpoint("mock:topicResultA",
MockEndpoint.class);
diff --git
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsRoutes.java
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsRoutes.java
index 8ee25ce..bb14394 100644
---
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsRoutes.java
+++
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/JmsRoutes.java
@@ -27,11 +27,10 @@ public class JmsRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
- // jmsMessageType text / binary routes
- from("jms:queue:typeTest?jmsMessageType=Text")
- .toD("jms:queue:typeTestResult?jmsMessageType=${header.type}");
+ from("jms:queue:typeTest?concurrentConsumers=5")
+ .toD("jms:queue:typeTestResult");
- from("jms:queue:typeTestResult")
+ from("jms:queue:typeTestResult?artemisStreamingEnabled=false")
.to("mock:jmsType");
// Map message type routes
@@ -59,6 +58,12 @@ public class JmsRoutes extends RouteBuilder {
})
.to("mock:txResult");
+ from("jms:queue:transferExchange?transferExchange=true")
+ .to("mock:transferExchangeResult");
+
+ from("jms:queue:objectTest")
+ .to("mock:objectTestResult");
+
// Topic routes
from("jms:topic:test?clientId=123&durableSubscriptionName=camel-quarkus")
.to("mock:topicResultA");
diff --git
a/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/Person.java
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/Person.java
new file mode 100644
index 0000000..ba64958
--- /dev/null
+++
b/integration-tests/messaging/src/main/java/org/apache/camel/quarkus/component/messaging/it/Person.java
@@ -0,0 +1,35 @@
+/*
+ * 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.quarkus.component.messaging.it;
+
+import java.io.Serializable;
+
+public class Person implements Serializable {
+ String name;
+
+ public Person(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git
a/integration-tests/messaging/src/test/java/org/apache/camel/quarkus/component/messaging/it/JmsTest.java
b/integration-tests/messaging/src/test/java/org/apache/camel/quarkus/component/messaging/it/JmsTest.java
index d260111..a16905c 100644
---
a/integration-tests/messaging/src/test/java/org/apache/camel/quarkus/component/messaging/it/JmsTest.java
+++
b/integration-tests/messaging/src/test/java/org/apache/camel/quarkus/component/messaging/it/JmsTest.java
@@ -20,14 +20,17 @@ import java.util.HashMap;
import java.util.Map;
import io.quarkus.test.common.QuarkusTestResource;
+import io.quarkus.test.junit.DisabledOnNativeImage;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.core.Is.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@QuarkusTestResource(ActiveMQTestResource.class)
@@ -54,13 +57,20 @@ class JmsTest {
}
@ParameterizedTest
- @ValueSource(strings = { "Text", "Bytes" })
+ @ValueSource(strings = { "bytes", "file", "node", "string" })
public void testJmsMessageType(String type) {
+ String message = "Message type " + type;
+ String expected = message;
+ if (type.equals("node")) {
+ expected = "<test>" + message + "</test>";
+ }
+
RestAssured.given()
- .get("/messaging/jms/type/" + type)
+ .body(message)
+ .post("/messaging/jms/type/" + type)
.then()
.statusCode(200)
- .body(is("true"));
+ .body(is(expected));
}
@Test
@@ -101,13 +111,29 @@ class JmsTest {
}
@Test
+ public void testJmsMessageConverter() {
+ String result = RestAssured.given()
+ .body("a test message")
+ .post("/messaging/jms/custom/message/converter")
+ .then()
+ .statusCode(200)
+ .extract()
+ .body()
+ .asString();
+
+ assertTrue(result.startsWith("converter prefix"));
+ assertTrue(result.endsWith("converter suffix"));
+ }
+
+ @Test
+ @Disabled("https://github.com/apache/camel-quarkus/issues/2608")
public void testJmsTopic() {
String message = "Camel JMS Topic Message";
RestAssured.given()
.body(message)
.post("/messaging/jms/topic")
.then()
- .statusCode(201);
+ .statusCode(204);
}
@Test
@@ -120,6 +146,30 @@ class JmsTest {
}
@Test
+
@DisabledOnNativeImage("https://github.com/apache/camel-quarkus/issues/2599")
+ public void testJmsObject() {
+ String message = "Mr Test Person";
+ RestAssured.given()
+ .body(message)
+ .post("/messaging/jms/object")
+ .then()
+ .statusCode(200)
+ .body(is(message));
+ }
+
+ @Test
+
@DisabledOnNativeImage("https://github.com/apache/camel-quarkus/issues/2599")
+ public void testJmsTransferExchange() {
+ String message = "Test transfer message";
+ RestAssured.given()
+ .body(message)
+ .post("/messaging/jms/transfer/exchange")
+ .then()
+ .statusCode(200)
+ .body(is(message));
+ }
+
+ @Test
public void testJmsTransaction() {
RestAssured.given()
.get("/messaging/jms/transaction")