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

tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new e1c818d143 ARTEMIS-5078 support divert management via JSON
e1c818d143 is described below

commit e1c818d1436216559928a7a3fa3141d66f7a8c48
Author: Justin Bertram <[email protected]>
AuthorDate: Tue Oct 1 10:55:47 2024 -0500

    ARTEMIS-5078 support divert management via JSON
---
 .../api/core/management/ActiveMQServerControl.java |  31 +++++
 .../core/config/TransformerConfiguration.java      |  17 +++
 .../artemis/core/config/BridgeConfiguration.java   |   8 +-
 .../artemis/core/config/DivertConfiguration.java   | 138 ++++++++++++++++++++-
 .../management/impl/ActiveMQServerControlImpl.java |  44 +++++--
 .../core/config/DivertConfigurationTest.java       |  88 +++++++++++++
 .../management/ActiveMQServerControlTest.java      |  67 +++++++++-
 .../ActiveMQServerControlUsingCoreTest.java        |  10 ++
 8 files changed, 377 insertions(+), 26 deletions(-)

diff --git 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java
 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java
index 1a783f4df1..6f4e1e66f5 100644
--- 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java
+++ 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java
@@ -1708,6 +1708,10 @@ public interface ActiveMQServerControl {
       return getDivertNames();
    }
 
+   /**
+    * @deprecated Deprecated in favor of {@link #createDivert(String)}
+    */
+   @Deprecated
    @Operation(desc = "Create a Divert", impact = MBeanOperationInfo.ACTION)
    void createDivert(@Parameter(name = "name", desc = "Name of the divert") 
String name,
                      @Parameter(name = "routingName", desc = "Routing name of 
the divert") String routingName,
@@ -1717,6 +1721,10 @@ public interface ActiveMQServerControl {
                      @Parameter(name = "filterString", desc = "Filter of the 
divert") String filterString,
                      @Parameter(name = "transformerClassName", desc = "Class 
name of the divert's transformer") String transformerClassName) throws 
Exception;
 
+   /**
+    * @deprecated Deprecated in favor of {@link #createDivert(String)}
+    */
+   @Deprecated
    @Operation(desc = "Create a Divert", impact = MBeanOperationInfo.ACTION)
    void createDivert(@Parameter(name = "name", desc = "Name of the divert") 
String name,
                      @Parameter(name = "routingName", desc = "Routing name of 
the divert") String routingName,
@@ -1727,6 +1735,10 @@ public interface ActiveMQServerControl {
                      @Parameter(name = "transformerClassName", desc = "Class 
name of the divert's transformer") String transformerClassName,
                      @Parameter(name = "routingType", desc = "How should the 
routing-type on the diverted messages be set?") String routingType) throws 
Exception;
 
+   /**
+    * @deprecated Deprecated in favor of {@link #createDivert(String)}
+    */
+   @Deprecated
    @Operation(desc = "Create a Divert", impact = MBeanOperationInfo.ACTION)
    void createDivert(@Parameter(name = "name", desc = "Name of the divert") 
String name,
                      @Parameter(name = "routingName", desc = "Routing name of 
the divert") String routingName,
@@ -1738,6 +1750,10 @@ public interface ActiveMQServerControl {
                      @Parameter(name = "transformerProperties", desc = 
"Configuration properties of the divert's transformer") Map<String, String> 
transformerProperties,
                      @Parameter(name = "routingType", desc = "How should the 
routing-type on the diverted messages be set?") String routingType) throws 
Exception;
 
+   /**
+    * @deprecated Deprecated in favor of {@link #createDivert(String)}
+    */
+   @Deprecated
    @Operation(desc = "Create a Divert", impact = MBeanOperationInfo.ACTION)
    void createDivert(@Parameter(name = "name", desc = "Name of the divert") 
String name,
                      @Parameter(name = "routingName", desc = "Routing name of 
the divert") String routingName,
@@ -1749,9 +1765,18 @@ public interface ActiveMQServerControl {
                      @Parameter(name = "transformerPropertiesAsJSON", desc = 
"Configuration properties of the divert's transformer in JSON form") String 
transformerPropertiesAsJSON,
                      @Parameter(name = "routingType", desc = "How should the 
routing-type on the diverted messages be set?") String routingType) throws 
Exception;
 
+   /**
+    * Create a Divert.
+    *
+    * @param divertConfiguration the configuration of the divert in JSON format
+    */
+   @Operation(desc = "Create a Divert", impact = MBeanOperationInfo.ACTION)
+   void createDivert(@Parameter(name = "divertConfiguration", desc = "the 
configuration of the divert in JSON format") String divertConfiguration) throws 
Exception;
+
    /**
     * update a divert
     */
+   @Deprecated
    @Operation(desc = "Update a divert", impact = MBeanOperationInfo.ACTION)
    void updateDivert(@Parameter(name = "name", desc = "Name of the queue") 
String name,
                      @Parameter(name = "forwardingAddress", desc = "Address to 
divert to") String forwardingAddress,
@@ -1760,6 +1785,12 @@ public interface ActiveMQServerControl {
                      @Parameter(name = "transformerProperties", desc = 
"Configuration properties of the divert's transformer") Map<String, String> 
transformerProperties,
                      @Parameter(name = "routingType", desc = "How should the 
routing-type on the diverted messages be set?") String routingType) throws 
Exception;
 
+   /**
+    * update a divert
+    */
+   @Operation(desc = "Update a divert", impact = MBeanOperationInfo.ACTION)
+   void updateDivert(@Parameter(name = "divertConfiguration", desc = "the 
configuration of the divert in JSON format") String divertConfiguration) throws 
Exception;
+
    @Operation(desc = "Destroy a Divert", impact = MBeanOperationInfo.ACTION)
    void destroyDivert(@Parameter(name = "name", desc = "Name of the divert") 
String name) throws Exception;
 
diff --git 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/config/TransformerConfiguration.java
 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/config/TransformerConfiguration.java
index 84e76da967..9d1788802a 100644
--- 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/config/TransformerConfiguration.java
+++ 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/config/TransformerConfiguration.java
@@ -16,6 +16,7 @@
  */
 package org.apache.activemq.artemis.core.config;
 
+import org.apache.activemq.artemis.json.JsonObjectBuilder;
 import org.apache.activemq.artemis.utils.JsonLoader;
 
 import org.apache.activemq.artemis.json.JsonObject;
@@ -92,6 +93,16 @@ public final class TransformerConfiguration implements 
Serializable {
       return result;
    }
 
+   public JsonObjectBuilder createJsonObjectBuilder() {
+      JsonObjectBuilder tcBuilder = 
JsonLoader.createObjectBuilder().add(TransformerConfiguration.CLASS_NAME, 
getClassName());
+      if (getProperties() != null && getProperties().size() > 0) {
+         JsonObjectBuilder propBuilder = JsonLoader.createObjectBuilder();
+         getProperties().forEach(propBuilder::add);
+         tcBuilder.add(TransformerConfiguration.PROPERTIES, propBuilder);
+      }
+      return tcBuilder;
+   }
+
    /**
     * @param properties the properties to set
     */
@@ -133,4 +144,10 @@ public final class TransformerConfiguration implements 
Serializable {
       return true;
    }
 
+   @Override
+   public String toString() {
+      return "TransformerConfiguration [" +
+         "className=" + className +
+         ", properties=" + properties + "]";
+   }
 }
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/BridgeConfiguration.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/BridgeConfiguration.java
index 3f691a82e0..fa2994a962 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/BridgeConfiguration.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/BridgeConfiguration.java
@@ -694,13 +694,7 @@ public final class BridgeConfiguration implements 
Serializable {
 
       TransformerConfiguration tc = getTransformerConfiguration();
       if (tc != null) {
-         JsonObjectBuilder tcBuilder = 
JsonLoader.createObjectBuilder().add(TransformerConfiguration.CLASS_NAME, 
tc.getClassName());
-         if (tc.getProperties() != null && tc.getProperties().size() > 0) {
-            JsonObjectBuilder propBuilder = JsonLoader.createObjectBuilder();
-            tc.getProperties().forEach(propBuilder::add);
-            tcBuilder.add(TransformerConfiguration.PROPERTIES, propBuilder);
-         }
-         builder.add(TRANSFORMER_CONFIGURATION, tcBuilder);
+         builder.add(TRANSFORMER_CONFIGURATION, tc.createJsonObjectBuilder());
       }
 
       return builder.build().toString();
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/DivertConfiguration.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/DivertConfiguration.java
index 64127082c9..7ed2465116 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/DivertConfiguration.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/DivertConfiguration.java
@@ -17,20 +17,35 @@
 package org.apache.activemq.artemis.core.config;
 
 import java.io.Serializable;
+import java.io.StringReader;
 import java.util.Map;
 
 import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
 import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
 import org.apache.activemq.artemis.core.journal.EncodingSupport;
 import 
org.apache.activemq.artemis.core.server.ComponentConfigurationRoutingType;
+import org.apache.activemq.artemis.json.JsonObject;
+import org.apache.activemq.artemis.json.JsonObjectBuilder;
+import org.apache.activemq.artemis.json.JsonString;
+import org.apache.activemq.artemis.json.JsonValue;
 import org.apache.activemq.artemis.utils.BufferHelper;
 import org.apache.activemq.artemis.utils.DataConstants;
+import org.apache.activemq.artemis.utils.JsonLoader;
 import org.apache.activemq.artemis.utils.UUIDGenerator;
 
 public class DivertConfiguration implements Serializable, EncodingSupport {
 
    private static final long serialVersionUID = 6910543740464269629L;
 
+   public static String NAME = "name";
+   public static String ROUTING_NAME = "routing-name";
+   public static String ADDRESS = "address";
+   public static String FORWARDING_ADDRESS = "forwarding-address";
+   public static String EXCLUSIVE = "exclusive";
+   public static String FILTER_STRING = "filter-string";
+   public static String TRANSFORMER_CONFIGURATION = 
"transformer-configuration";
+   public static String ROUTING_TYPE = "routing-type";
+
    private String name = null;
 
    private String routingName = 
UUIDGenerator.getInstance().generateStringUUID();
@@ -50,6 +65,54 @@ public class DivertConfiguration implements Serializable, 
EncodingSupport {
    public DivertConfiguration() {
    }
 
+   /**
+    * Set the value of a parameter based on its "key" {@code String}. Valid 
key names and corresponding {@code static}
+    * {@code final} are:
+    * <p><ul>
+    * <li>name: {@link #NAME}
+    * <li>routing-name: {@link #ROUTING_NAME}
+    * <li>address: {@link #ADDRESS}
+    * <li>forwarding-address: {@link #FORWARDING_ADDRESS}
+    * <li>exclusive: {@link #EXCLUSIVE}
+    * <li>filter-string: {@link #FILTER_STRING}
+    * <li>transformer-configuration: {@link #TRANSFORMER_CONFIGURATION}
+    * <li>routing-type: {@link #ROUTING_TYPE}
+    * </ul><p>
+    * The {@code String}-based values will be converted to the proper value 
types based on the underlying property. For
+    * example, if you pass the value "TRUE" for the key "exclusive" the {@code 
String} "TRUE" will be converted to
+    * the {@code Boolean} {@code true}.
+    *
+    * @param key the key to set to the value
+    * @param value the value to set for the key
+    * @return this {@code DivertConfiguration}
+    */
+   public DivertConfiguration set(String key, String value) {
+      if (key != null) {
+         if (key.equals(NAME)) {
+            setName(value);
+         } else if (key.equals(ROUTING_NAME)) {
+            setRoutingName(value);
+         } else if (key.equals(ADDRESS)) {
+            setAddress(value);
+         } else if (key.equals(FORWARDING_ADDRESS)) {
+            setForwardingAddress(value);
+         } else if (key.equals(EXCLUSIVE)) {
+            setExclusive(Boolean.parseBoolean(value));
+         } else if (key.equals(FILTER_STRING)) {
+            setFilterString(value);
+         } else if (key.equals(TRANSFORMER_CONFIGURATION)) {
+            // create a transformer instance from a JSON string
+            TransformerConfiguration transformerConfiguration = 
TransformerConfiguration.fromJSON(value);
+            if (transformerConfiguration != null) {
+               setTransformerConfiguration(transformerConfiguration);
+            }
+         } else if (key.equals(ROUTING_TYPE)) {
+            setRoutingType(ComponentConfigurationRoutingType.valueOf(value));
+         }
+      }
+      return this;
+   }
+
    public String getName() {
       return name;
    }
@@ -150,6 +213,71 @@ public class DivertConfiguration implements Serializable, 
EncodingSupport {
       return this;
    }
 
+   /**
+    * This method returns a JSON-formatted {@code String} representation of 
this {@code DivertConfiguration}. It is a
+    * simple collection of key/value pairs. The keys used are referenced in 
{@link #set(String, String)}.
+    *
+    * @return a JSON-formatted {@code String} representation of this {@code 
DivertConfiguration}
+    */
+   public String toJSON() {
+      JsonObjectBuilder builder = JsonLoader.createObjectBuilder();
+
+      if (getName() != null) {
+         builder.add(NAME, getName());
+      }
+      if (getRoutingName() != null) {
+         builder.add(ROUTING_NAME, getRoutingName());
+      }
+      if (getAddress() != null) {
+         builder.add(ADDRESS, getAddress());
+      }
+      if (getForwardingAddress() != null) {
+         builder.add(FORWARDING_ADDRESS, getForwardingAddress());
+      }
+
+      builder.add(EXCLUSIVE, isExclusive());
+
+      if (getFilterString() != null) {
+         builder.add(FILTER_STRING, getFilterString());
+      }
+
+      TransformerConfiguration tc = getTransformerConfiguration();
+      if (tc != null) {
+         builder.add(TRANSFORMER_CONFIGURATION, tc.createJsonObjectBuilder());
+      }
+
+      if (getRoutingType() != null) {
+         builder.add(ROUTING_TYPE, getRoutingType().name());
+      }
+
+      return builder.build().toString();
+   }
+
+   /**
+    * This method returns a {@code DivertConfiguration} created from the 
JSON-formatted input {@code String}. The input
+    * should be a simple object of key/value pairs. Valid keys are referenced 
in {@link #set(String, String)}.
+    *
+    * @param jsonString json string
+    * @return the {@code DivertConfiguration} created from the JSON-formatted 
input {@code String}
+    */
+   public static DivertConfiguration fromJSON(String jsonString) {
+      JsonObject json = JsonLoader.readObject(new StringReader(jsonString));
+
+      DivertConfiguration result = new DivertConfiguration();
+
+      for (Map.Entry<String, JsonValue> entry : json.entrySet()) {
+         if (entry.getValue().getValueType() == JsonValue.ValueType.STRING) {
+            result.set(entry.getKey(), ((JsonString) 
entry.getValue()).getString());
+         } else if (entry.getValue().getValueType() == 
JsonValue.ValueType.NULL) {
+            result.set(entry.getKey(), null);
+         } else {
+            result.set(entry.getKey(), entry.getValue().toString());
+         }
+      }
+
+      return result;
+   }
+
    @Override
    public int hashCode() {
       final int prime = 31;
@@ -262,7 +390,15 @@ public class DivertConfiguration implements Serializable, 
EncodingSupport {
 
    @Override
    public String toString() {
-      return "DivertConfiguration{" + "name='" + name + '\'' + ", 
routingName='" + routingName + '\'' + ", address='" + address + '\'' + ", 
forwardingAddress='" + forwardingAddress + '\'' + ", exclusive=" + exclusive + 
", filterString='" + filterString + '\'' + ", transformerConfiguration=" + 
transformerConfiguration + '}';
+      return "DivertConfiguration [" +
+         "name=" + name +
+         ", routingName=" + routingName +
+         ", address=" + address +
+         ", forwardingAddress=" + forwardingAddress +
+         ", exclusive=" + exclusive +
+         ", filterString=" + filterString +
+         ", routing-type=" + routingType +
+         ", transformerConfiguration=" + transformerConfiguration + "]";
    }
 
    @Override
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java
index 8adb67ce53..3c8e7449a8 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java
@@ -3669,16 +3669,24 @@ public class ActiveMQServerControlImpl extends 
AbstractControl implements Active
                             final String transformerClassName,
                             final Map<String, String> transformerProperties,
                             final String routingType) throws Exception {
+      TransformerConfiguration transformerConfiguration = transformerClassName 
== null || transformerClassName.isEmpty() ? null : new 
TransformerConfiguration(transformerClassName).setProperties(transformerProperties);
+      createDivert(new 
DivertConfiguration().setName(name).setRoutingName(routingName).setAddress(address).setForwardingAddress(forwardingAddress).setExclusive(exclusive).setFilterString(filterString).setTransformerConfiguration(transformerConfiguration).setRoutingType(ComponentConfigurationRoutingType.valueOf(routingType)));
+   }
+
+   @Override
+   public void createDivert(final String divertConfiguration) throws Exception 
{
+      createDivert(DivertConfiguration.fromJSON(divertConfiguration));
+   }
+
+
+   private void createDivert(final DivertConfiguration config) throws 
Exception {
       if (AuditLogger.isBaseLoggingEnabled()) {
-         AuditLogger.createDivert(this.server, name, routingName, address, 
forwardingAddress,
-                  exclusive, filterString, transformerClassName, 
transformerProperties, routingType);
+         AuditLogger.createDivert(this.server, config);
       }
       checkStarted();
 
       clearIO();
       try {
-         TransformerConfiguration transformerConfiguration = 
transformerClassName == null || transformerClassName.isEmpty() ? null : new 
TransformerConfiguration(transformerClassName).setProperties(transformerProperties);
-         DivertConfiguration config = new 
DivertConfiguration().setName(name).setRoutingName(routingName).setAddress(address).setForwardingAddress(forwardingAddress).setExclusive(exclusive).setFilterString(filterString).setTransformerConfiguration(transformerConfiguration).setRoutingType(ComponentConfigurationRoutingType.valueOf(routingType));
          server.deployDivert(config);
       } finally {
          blockOnIO();
@@ -3692,22 +3700,32 @@ public class ActiveMQServerControlImpl extends 
AbstractControl implements Active
                             final String transformerClassName,
                             final Map<String, String> transformerProperties,
                             final String routingType) throws Exception {
+      TransformerConfiguration transformerConfiguration = transformerClassName 
== null || transformerClassName.isEmpty() ? null : new 
TransformerConfiguration(transformerClassName).setProperties(transformerProperties);
+
+      DivertConfiguration config = new DivertConfiguration()
+         .setName(name)
+         .setForwardingAddress(forwardingAddress)
+         .setFilterString(filterString)
+         .setTransformerConfiguration(transformerConfiguration)
+         
.setRoutingType(ComponentConfigurationRoutingType.valueOf(routingType));
+
+      updateDivert(config);
+   }
+
+   @Override
+   public void updateDivert(final String divertConfiguration) throws Exception 
{
+      updateDivert(DivertConfiguration.fromJSON(divertConfiguration));
+   }
+
+   private void updateDivert(final DivertConfiguration config) throws 
Exception {
       if (AuditLogger.isBaseLoggingEnabled()) {
-         AuditLogger.updateDivert(this.server, name, forwardingAddress, 
filterString,
-                                  transformerClassName, transformerProperties, 
routingType);
+         AuditLogger.updateDivert(this.server, config);
       }
       checkStarted();
 
       clearIO();
 
       try {
-         TransformerConfiguration transformerConfiguration = 
transformerClassName == null  || transformerClassName.isEmpty() ? null :
-            new 
TransformerConfiguration(transformerClassName).setProperties(transformerProperties);
-
-         DivertConfiguration config = new 
DivertConfiguration().setName(name).setForwardingAddress(forwardingAddress).
-            
setFilterString(filterString).setTransformerConfiguration(transformerConfiguration).
-            
setRoutingType(ComponentConfigurationRoutingType.valueOf(routingType));
-
          final Divert divert = server.updateDivert(config);
          if (divert == null) {
             throw 
ActiveMQMessageBundle.BUNDLE.divertDoesNotExist(config.getName());
diff --git 
a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/DivertConfigurationTest.java
 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/DivertConfigurationTest.java
new file mode 100644
index 0000000000..9a44dfc055
--- /dev/null
+++ 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/DivertConfigurationTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.activemq.artemis.core.config;
+
+import java.io.StringReader;
+import java.util.Set;
+
+import 
org.apache.activemq.artemis.core.server.ComponentConfigurationRoutingType;
+import org.apache.activemq.artemis.json.JsonObject;
+import org.apache.activemq.artemis.json.JsonObjectBuilder;
+import org.apache.activemq.artemis.utils.JsonLoader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DivertConfigurationTest {
+
+   @Test
+   public void testFromJSON() {
+      String jsonString = createFullJsonObject().toString();
+
+      DivertConfiguration divertConfiguration = 
DivertConfiguration.fromJSON(jsonString);
+
+      assertNotNull(divertConfiguration);
+      assertEquals("name", divertConfiguration.getName());
+      assertEquals("forwarding-address", 
divertConfiguration.getForwardingAddress());
+      assertEquals("filter-string", divertConfiguration.getFilterString());
+      assertEquals("ClassName", 
divertConfiguration.getTransformerConfiguration().getClassName());
+      Set keys = 
divertConfiguration.getTransformerConfiguration().getProperties().keySet();
+      assertTrue(keys.contains("prop1"), keys + " doesn't contain prop1");
+      assertTrue(keys.contains("prop2"), keys + " doesn't contain prop2");
+      assertEquals("val1", 
divertConfiguration.getTransformerConfiguration().getProperties().get("prop1"));
+      assertEquals("val2", 
divertConfiguration.getTransformerConfiguration().getProperties().get("prop2"));
+      assertEquals(ComponentConfigurationRoutingType.MULTICAST, 
divertConfiguration.getRoutingType());
+   }
+
+   @Test
+   public void testToJSON() {
+      // create bc instance from a JSON object, all attributes are set
+      JsonObject jsonObject = createFullJsonObject();
+      DivertConfiguration divertConfiguration = 
DivertConfiguration.fromJSON(jsonObject.toString());
+      assertNotNull(divertConfiguration);
+
+      // serialize it back to JSON
+      String serializedDivertConfiguration = divertConfiguration.toJSON();
+      JsonObject serializedDivertConfigurationJsonObject = 
JsonLoader.readObject(new StringReader(serializedDivertConfiguration));
+
+      // verify that the original JSON object is identical to the one 
serialized via the toJSON() method
+      assertEquals(jsonObject, serializedDivertConfigurationJsonObject);
+   }
+
+   private static JsonObject createFullJsonObject() {
+      JsonObjectBuilder objectBuilder = JsonLoader.createObjectBuilder();
+
+      objectBuilder.add(DivertConfiguration.NAME, "name");
+      objectBuilder.add(DivertConfiguration.ROUTING_NAME, "routing-name");
+      objectBuilder.add(DivertConfiguration.ADDRESS, "address");
+      objectBuilder.add(DivertConfiguration.FORWARDING_ADDRESS, 
"forwarding-address");
+      objectBuilder.add(DivertConfiguration.EXCLUSIVE, false);
+      objectBuilder.add(DivertConfiguration.FILTER_STRING, "filter-string");
+      objectBuilder.add(DivertConfiguration.TRANSFORMER_CONFIGURATION,
+            JsonLoader.createObjectBuilder()
+                  .add("class-name", "ClassName")
+                  .add("properties",
+                        JsonLoader.createObjectBuilder()
+                              .add("prop1", "val1")
+                              .add("prop2", "val2")));
+      objectBuilder.add(DivertConfiguration.ROUTING_TYPE, "MULTICAST");
+
+      return objectBuilder.build();
+   }
+}
diff --git 
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java
 
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java
index a55886fd2e..1e7f56e339 100644
--- 
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java
+++ 
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java
@@ -80,6 +80,7 @@ import 
org.apache.activemq.artemis.core.client.impl.ClientSessionImpl;
 import org.apache.activemq.artemis.core.config.BridgeConfiguration;
 import org.apache.activemq.artemis.core.config.ClusterConnectionConfiguration;
 import org.apache.activemq.artemis.core.config.Configuration;
+import org.apache.activemq.artemis.core.config.DivertConfiguration;
 import 
org.apache.activemq.artemis.core.config.brokerConnectivity.BrokerConnectConfiguration;
 import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;
 import org.apache.activemq.artemis.core.management.impl.view.ConnectionField;
@@ -1728,6 +1729,15 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
 
    @TestTemplate
    public void testNullRouteNameOnDivert() throws Exception {
+      testNullRouteNameOnDivert(false);
+   }
+
+   @TestTemplate
+   public void testNullRouteNameOnDivertJSON() throws Exception {
+      testNullRouteNameOnDivert(true);
+   }
+
+   private void testNullRouteNameOnDivert(boolean json) throws Exception {
       String address = RandomUtil.randomString();
       String name = RandomUtil.randomString();
       String forwardingAddress = RandomUtil.randomString();
@@ -1737,13 +1747,26 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
       checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       assertEquals(0, serverControl.getDivertNames().length);
 
-      serverControl.createDivert(name.toString(), null, address, 
forwardingAddress, true, null, null);
+      if (json) {
+         serverControl.createDivert(new 
DivertConfiguration().setName(name).setAddress(address).setForwardingAddress(forwardingAddress).toJSON());
+      } else {
+         serverControl.createDivert(name.toString(), null, address, 
forwardingAddress, true, null, null);
+      }
 
       checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
    }
 
    @TestTemplate
    public void testCreateAndDestroyDivert() throws Exception {
+      testCreateAndDestroyDivert(false);
+   }
+
+   @TestTemplate
+   public void testCreateAndDestroyDivertJSON() throws Exception {
+      testCreateAndDestroyDivert(true);
+   }
+
+   private void testCreateAndDestroyDivert(boolean json) throws Exception {
       String address = RandomUtil.randomString();
       String name = RandomUtil.randomString();
       String routingName = RandomUtil.randomString();
@@ -1754,7 +1777,11 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
       checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       assertEquals(0, serverControl.getDivertNames().length);
 
-      serverControl.createDivert(name.toString(), routingName, address, 
forwardingAddress, true, null, null);
+      if (json) {
+         serverControl.createDivert(new 
DivertConfiguration().setName(name.toString()).setRoutingName(routingName).setAddress(address).setForwardingAddress(forwardingAddress).setExclusive(true).toJSON());
+      } else {
+         serverControl.createDivert(name.toString(), routingName, address, 
forwardingAddress, true, null, null);
+      }
 
       checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       DivertControl divertControl = 
ManagementControlHelper.createDivertControl(name.toString(), address, 
mbeanServer);
@@ -1829,6 +1856,15 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
 
    @TestTemplate
    public void testCreateAndDestroyDivertServerRestart() throws Exception {
+      testCreateAndDestroyDivertServerRestart(false);
+   }
+
+   @TestTemplate
+   public void testCreateAndDestroyDivertServerRestartJSON() throws Exception {
+      testCreateAndDestroyDivertServerRestart(true);
+   }
+
+   private void testCreateAndDestroyDivertServerRestart(boolean json) throws 
Exception {
       String address = RandomUtil.randomString();
       String name = RandomUtil.randomString();
 
@@ -1837,7 +1873,11 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
       checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       assertEquals(0, serverControl.getDivertNames().length);
 
-      serverControl.createDivert(name.toString(), RandomUtil.randomString(), 
address, RandomUtil.randomString(), true, null, null);
+      if (json) {
+         serverControl.createDivert(new 
DivertConfiguration().setName(name.toString()).setRoutingName(RandomUtil.randomString()).setAddress(address).setForwardingAddress(RandomUtil.randomString()).setExclusive(true).toJSON());
+      } else {
+         serverControl.createDivert(name.toString(), 
RandomUtil.randomString(), address, RandomUtil.randomString(), true, null, 
null);
+      }
 
       checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       assertEquals(1, serverControl.getDivertNames().length);
@@ -1857,6 +1897,15 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
 
    @TestTemplate
    public void testCreateAndUpdateDivert() throws Exception {
+      testCreateAndUpdateDivert(false);
+   }
+
+   @TestTemplate
+   public void testCreateAndUpdateDivertJSON() throws Exception {
+      testCreateAndUpdateDivert(true);
+   }
+
+   private void testCreateAndUpdateDivert(boolean json) throws Exception {
       String address = RandomUtil.randomString();
       String name = RandomUtil.randomString();
       String routingName = RandomUtil.randomString();
@@ -1868,7 +1917,11 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
       checkNoResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       assertEquals(0, serverControl.getDivertNames().length);
 
-      serverControl.createDivert(name.toString(), routingName, address, 
forwardingAddress, true, null, null);
+      if (json) {
+         serverControl.createDivert(new 
DivertConfiguration().setName(name.toString()).setRoutingName(routingName).setAddress(address).setForwardingAddress(forwardingAddress).setExclusive(true).toJSON());
+      } else {
+         serverControl.createDivert(name.toString(), routingName, address, 
forwardingAddress, true, null, null);
+      }
 
       checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       DivertControl divertControl = 
ManagementControlHelper.createDivertControl(name.toString(), address, 
mbeanServer);
@@ -1920,7 +1973,11 @@ public class ActiveMQServerControlTest extends 
ManagementTestBase {
       assertEquals(text, message.getStringProperty("prop"));
       assertNull(updatedDivertedConsumer.receiveImmediate());
 
-      serverControl.updateDivert(name.toString(), updatedForwardingAddress, 
null, null, null, ActiveMQDefaultConfiguration.getDefaultDivertRoutingType());
+      if (json) {
+         serverControl.updateDivert(new 
DivertConfiguration().setName(name.toString()).setForwardingAddress(updatedForwardingAddress).toJSON());
+      } else {
+         serverControl.updateDivert(name.toString(), updatedForwardingAddress, 
null, null, null, ActiveMQDefaultConfiguration.getDefaultDivertRoutingType());
+      }
 
       checkResource(ObjectNameBuilder.DEFAULT.getDivertObjectName(name, 
address));
       divertControl = 
ManagementControlHelper.createDivertControl(name.toString(), address, 
mbeanServer);
diff --git 
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java
 
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java
index 2a9e4a2512..92057507d7 100644
--- 
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java
+++ 
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java
@@ -1513,6 +1513,11 @@ public class ActiveMQServerControlUsingCoreTest extends 
ActiveMQServerControlTes
             proxy.invokeOperation("createDivert", name, routingName, address, 
forwardingAddress, exclusive, filterString, transformerClassName, 
transformerPropertiesAsJSON, routingType);
          }
 
+         @Override
+         public void createDivert(String divertConfiguration) throws Exception 
{
+            proxy.invokeOperation("createDivert", divertConfiguration);
+         }
+
          @Override
          public void updateDivert(String name,
                                   String forwardingAddress,
@@ -1523,6 +1528,11 @@ public class ActiveMQServerControlUsingCoreTest extends 
ActiveMQServerControlTes
             proxy.invokeOperation("updateDivert", name, forwardingAddress, 
filterString, transformerClassName, transformerProperties, routingType);
          }
 
+         @Override
+         public void updateDivert(String divertConfiguration) throws Exception 
{
+            proxy.invokeOperation("updateDivert", divertConfiguration);
+         }
+
          @Override
          public void destroyDivert(String name) throws Exception {
             proxy.invokeOperation("destroyDivert", name);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact



Reply via email to