Making the topic property utility method more generic - addressing Jorge's review comment

Making the method to update topic properties object more generic so that
it can be used by other components such as drools if needed.

Change-Id: I29c0f8fde5f4e1dd464072a9b06f2d93cc15b137
Issue-ID: POLICY-1744
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java
index 7e22712..0d59fd2 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java
@@ -25,6 +25,7 @@
     /* Generic property suffixes */
 
     String PROPERTY_TOPIC_SERVERS_SUFFIX = ".servers";
+    String PROPERTY_TOPIC_TOPICS_SUFFIX = ".topics";
     String PROPERTY_TOPIC_API_KEY_SUFFIX = ".apiKey";
     String PROPERTY_TOPIC_API_SECRET_SUFFIX = ".apiSecret";
     String PROPERTY_TOPIC_AAF_MECHID_SUFFIX = ".aafMechId";
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/utils/ParameterUtils.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/utils/ParameterUtils.java
index 9e7c69a..40ce8fa 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/utils/ParameterUtils.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/utils/ParameterUtils.java
@@ -25,6 +25,7 @@
 
 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
 import org.onap.policy.common.endpoints.parameters.TopicParameters;
+import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
 
 /**
  * This is common utility class with utility methods for parameters.
@@ -54,24 +55,35 @@
         // for each topicCommInfrastructure, there could be multiple topics (specified as comma separated string)
         // for each such topics, there could be multiple servers (specified as comma separated string)
         for (TopicParameters source : topicSources) {
-            addTopicProperties(topicProperties, ".source.topics", source);
+            updateTopicProperties(topicProperties, "source", source.getTopicCommInfrastructure(), source.getTopic(),
+                    source.getServers());
         }
         for (TopicParameters sink : topicSinks) {
-            addTopicProperties(topicProperties, ".sink.topics", sink);
+            updateTopicProperties(topicProperties, "sink", sink.getTopicCommInfrastructure(), sink.getTopic(),
+                    sink.getServers());
         }
 
         return topicProperties;
     }
 
-    private static void addTopicProperties(Properties topicProperties, String keyName, TopicParameters topicParameter) {
-        String propKey = topicParameter.getTopicCommInfrastructure() + keyName;
+    /**
+     * Common method to update topic properties object using the parameters passed.
+     *
+     * @param topicProperties the topic properties object which is to be updated
+     * @param keyName either it is source or sink
+     * @param topicCommInfra the infra such as  dmaap, ueb or noop
+     * @param topicName the topic
+     * @param servers the list of server names for the topic
+     */
+    public static void updateTopicProperties(Properties topicProperties, String keyName, String topicCommInfra,
+            String topicName, List<String> servers) {
+        String propKey = topicCommInfra + "." + keyName + PolicyEndPointProperties.PROPERTY_TOPIC_TOPICS_SUFFIX;
         if (topicProperties.containsKey(propKey)) {
-            topicProperties.setProperty(propKey,
-                    topicProperties.getProperty(propKey) + "," + topicParameter.getTopic());
+            topicProperties.setProperty(propKey, topicProperties.getProperty(propKey) + "," + topicName);
         } else {
-            topicProperties.setProperty(propKey, topicParameter.getTopic());
+            topicProperties.setProperty(propKey, topicName);
         }
-        topicProperties.setProperty(propKey + "." + topicParameter.getTopic() + ".servers",
-                String.join(",", topicParameter.getServers()));
+        topicProperties.setProperty(propKey + "." + topicName + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX,
+                String.join(",", servers));
     }
 }
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/utils/ParameterUtilsTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/utils/ParameterUtilsTest.java
index e1c0dbc..7e5c3a1 100644
--- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/utils/ParameterUtilsTest.java
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/utils/ParameterUtilsTest.java
@@ -22,6 +22,7 @@
 
 import static org.junit.Assert.assertEquals;
 
+import java.util.Arrays;
 import java.util.Properties;
 import org.junit.Test;
 import org.onap.policy.common.endpoints.parameters.CommonTestData;
@@ -52,4 +53,21 @@
         assertEquals(CommonTestData.TOPIC_SERVER, topicProperties
             .getProperty(CommonTestData.TOPIC_INFRA + ".sink.topics." + CommonTestData.TOPIC_NAME + ".servers"));
     }
+
+    @Test
+    public void testUpdateTopicProperties() {
+        Properties topicProperties = new Properties();
+        ParameterUtils.updateTopicProperties(topicProperties, "source", CommonTestData.TOPIC_INFRA,
+            CommonTestData.TOPIC_NAME, Arrays.asList(CommonTestData.TOPIC_SERVER));
+        assertEquals(CommonTestData.TOPIC_NAME,
+            topicProperties.getProperty(CommonTestData.TOPIC_INFRA + ".source.topics"));
+        assertEquals(CommonTestData.TOPIC_SERVER, topicProperties
+            .getProperty(CommonTestData.TOPIC_INFRA + ".source.topics." + CommonTestData.TOPIC_NAME + ".servers"));
+        ParameterUtils.updateTopicProperties(topicProperties, "sink", CommonTestData.TOPIC_INFRA,
+            CommonTestData.TOPIC_NAME, Arrays.asList(CommonTestData.TOPIC_SERVER));
+        assertEquals(CommonTestData.TOPIC_NAME,
+            topicProperties.getProperty(CommonTestData.TOPIC_INFRA + ".sink.topics"));
+        assertEquals(CommonTestData.TOPIC_SERVER, topicProperties
+            .getProperty(CommonTestData.TOPIC_INFRA + ".sink.topics." + CommonTestData.TOPIC_NAME + ".servers"));
+    }
 }