From ba6422d45f80d986fbec72fae01892d93813401b Mon Sep 17 00:00:00 2001 From: Domenico Francesco Bruscino Date: Wed, 26 May 2021 16:50:50 +0200 Subject: [PATCH] ARTEMIS-3315 Fix JSON serialization of AMQP messages Replace the JSON serialization errors for unknown types with their respective string representation. --- .../activemq/artemis/api/core/JsonUtil.java | 9 ++-- .../integration/amqp/JMXManagementTest.java | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java index 0576cc9fe5..58cac51541 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java @@ -34,7 +34,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.apache.activemq.artemis.core.client.ActiveMQClientMessageBundle; import org.apache.activemq.artemis.utils.Base64; import org.apache.activemq.artemis.utils.JsonLoader; import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader; @@ -159,6 +158,8 @@ public final class JsonUtil { jsonObjectBuilder.add(key, (Short) param); } else if (param instanceof Byte) { jsonObjectBuilder.add(key, ((Byte) param).shortValue()); + } else if (param instanceof Number) { + jsonObjectBuilder.add(key, ((Number)param).doubleValue()); } else if (param instanceof SimpleString) { jsonObjectBuilder.add(key, param.toString()); } else if (param == null) { @@ -173,7 +174,7 @@ public final class JsonUtil { } jsonObjectBuilder.add(key, objectArrayBuilder); } else { - throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); + jsonObjectBuilder.add(key, param.toString()); } } @@ -195,6 +196,8 @@ public final class JsonUtil { jsonArrayBuilder.add((Short) param); } else if (param instanceof Byte) { jsonArrayBuilder.add(((Byte) param).shortValue()); + } else if (param instanceof Number) { + jsonArrayBuilder.add(((Number)param).doubleValue()); } else if (param == null) { jsonArrayBuilder.addNull(); } else if (param instanceof byte[]) { @@ -216,7 +219,7 @@ public final class JsonUtil { } jsonArrayBuilder.add(objectArrayBuilder); } else { - throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); + jsonArrayBuilder.add(param.toString()); } } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/JMXManagementTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/JMXManagementTest.java index 2429cc69e8..49622dcd03 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/JMXManagementTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/JMXManagementTest.java @@ -29,6 +29,9 @@ import org.apache.activemq.transport.amqp.client.AmqpMessage; import org.apache.activemq.transport.amqp.client.AmqpSender; import org.apache.activemq.transport.amqp.client.AmqpSession; import org.apache.qpid.proton.amqp.Binary; +import org.apache.qpid.proton.amqp.Decimal128; +import org.apache.qpid.proton.amqp.Decimal32; +import org.apache.qpid.proton.amqp.Decimal64; import org.junit.Assert; import org.junit.Test; @@ -39,8 +42,10 @@ import javax.jms.Session; import javax.jms.TextMessage; import javax.json.JsonArray; import javax.json.JsonObject; +import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.util.Map; +import java.util.UUID; public class JMXManagementTest extends JMSClientTestSupport { @@ -137,6 +142,45 @@ public class JMXManagementTest extends JMSClientTestSupport { } } + @Test + public void testGetFirstMessageWithAMQPTypes() throws Exception { + AmqpClient client = createAmqpClient(); + AmqpConnection connection = addConnection(client.connect()); + + try { + UUID uuid = UUID.randomUUID(); + Character character = new Character('C'); + AmqpSession session = connection.createSession(); + AmqpSender sender = session.createSender(getQueueName()); + + session.begin(); + AmqpMessage message = new AmqpMessage(); + message.setApplicationProperty("TEST_UUID", uuid); + message.setApplicationProperty("TEST_CHAR", character); + message.setApplicationProperty("TEST_DECIMAL_32", new Decimal32(BigDecimal.ONE)); + message.setApplicationProperty("TEST_DECIMAL_64", new Decimal64(BigDecimal.ONE)); + message.setApplicationProperty("TEST_DECIMAL_128", new Decimal128(BigDecimal.ONE)); + + sender.send(message); + session.commit(); + + SimpleString queue = new SimpleString(getQueueName()); + QueueControl queueControl = createManagementControl(queue, queue); + String firstMessageAsJSON = queueControl.getFirstMessageAsJSON(); + Assert.assertNotNull(firstMessageAsJSON); + + JsonObject firstMessageObject = JsonUtil.readJsonArray(firstMessageAsJSON).getJsonObject(0); + + Assert.assertEquals(uuid.toString(), firstMessageObject.getString("TEST_UUID")); + Assert.assertEquals(character.toString(), firstMessageObject.getString("TEST_CHAR")); + Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_32")); + Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_64")); + Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_128")); + } finally { + connection.close(); + } + } + @Test public void testAddressSizeOnDelete() throws Exception { AmqpClient client = createAmqpClient();