diff options
Diffstat (limited to 'policy-endpoints')
5 files changed, 127 insertions, 32 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroup.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroup.java index f633b0ef..eab9c12f 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroup.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroup.java @@ -56,22 +56,38 @@ public class TopicParameterGroup extends ParameterGroupImpl { @Override public GroupValidationResult validate() { GroupValidationResult result = super.validate(); - if (result.isValid() && (checkMissingMandatoryParams(topicSources) - || checkMissingMandatoryParams(topicSinks))) { - result.setResult(ValidationStatus.INVALID, "Mandatory parameters are missing. topic, servers " - + "and topicCommInfrastructure must be specified."); + if (result.isValid()) { + StringBuilder errorMsg = new StringBuilder(); + StringBuilder missingSourceParams = checkMissingMandatoryParams(topicSources); + if (missingSourceParams.length() > 0) { + errorMsg.append(missingSourceParams.append("missing in topicSources. ")); + } + StringBuilder missingSinkParams = checkMissingMandatoryParams(topicSinks); + if (missingSinkParams.length() > 0) { + errorMsg.append(missingSinkParams.append("missing in topicSinks.")); + } + + if (errorMsg.length() > 0) { + errorMsg.insert(0, "Mandatory parameters are missing. "); + result.setResult(ValidationStatus.INVALID, errorMsg.toString()); + } } return result; } - private boolean checkMissingMandatoryParams(List<TopicParameters> topicParametersList) { + private StringBuilder checkMissingMandatoryParams(List<TopicParameters> topicParametersList) { + StringBuilder missingParams = new StringBuilder(); for (TopicParameters topicParameters : topicParametersList) { - if (StringUtils.isBlank(topicParameters.getTopic()) - || StringUtils.isBlank(topicParameters.getTopicCommInfrastructure()) - || topicParameters.getServers().isEmpty()) { - return true; + if (StringUtils.isBlank(topicParameters.getTopic())) { + missingParams.append("topic, "); + } + if (StringUtils.isBlank(topicParameters.getTopicCommInfrastructure())) { + missingParams.append("topicCommInfrastructure, "); + } + if (null == topicParameters.getServers() || topicParameters.getServers().isEmpty()) { + missingParams.append("servers, "); } } - return false; + return missingParams; } } 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 cabfe58f..5e93cfa4 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 @@ -21,12 +21,19 @@ package org.onap.policy.common.endpoints.utils; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Properties; - +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; import org.onap.policy.common.endpoints.parameters.TopicParameters; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This is common utility class with utility methods for parameters. @@ -36,6 +43,11 @@ import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; public class ParameterUtils { /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class); + + /** * Private constructor used to prevent sub class instantiation. */ private ParameterUtils() { @@ -56,12 +68,10 @@ public class ParameterUtils { // 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) { - updateTopicProperties(topicProperties, "source", source.getTopicCommInfrastructure(), source.getTopic(), - source.getServers()); + updateTopicProperties(topicProperties, "source", source); } for (TopicParameters sink : topicSinks) { - updateTopicProperties(topicProperties, "sink", sink.getTopicCommInfrastructure(), sink.getTopic(), - sink.getServers()); + updateTopicProperties(topicProperties, "sink", sink); } return topicProperties; @@ -72,19 +82,45 @@ public class ParameterUtils { * * @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 + * @param topicParameters the topic parameters object */ - public static void updateTopicProperties(Properties topicProperties, String keyName, String topicCommInfra, - String topicName, List<String> servers) { + public static void updateTopicProperties(Properties topicProperties, String keyName, + TopicParameters topicParameters) { + String topicCommInfra = topicParameters.getTopicCommInfrastructure(); + String topicName = topicParameters.getTopic(); + List<String> servers = topicParameters.getServers(); + String propKey = topicCommInfra + "." + keyName + PolicyEndPointProperties.PROPERTY_TOPIC_TOPICS_SUFFIX; if (topicProperties.containsKey(propKey)) { topicProperties.setProperty(propKey, topicProperties.getProperty(propKey) + "," + topicName); } else { topicProperties.setProperty(propKey, topicName); } - topicProperties.setProperty(propKey + "." + topicName + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX, + String propWithTopicKey = propKey + "." + topicName; + topicProperties.setProperty(propWithTopicKey + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX, String.join(",", servers)); + + Field[] fields = BusTopicParams.class.getDeclaredFields(); + for (Field field : fields) { + if (field.isSynthetic()) { + continue; + } + try { + Object parameter = new PropertyDescriptor(field.getName(), TopicParameters.class) + .getReadMethod().invoke(topicParameters); + if ((parameter instanceof String && StringUtils.isNotBlank(parameter.toString())) + || (parameter instanceof Number && ((Number) parameter).longValue() > 0)) { + topicProperties.setProperty(propWithTopicKey + "." + field.getName(), parameter.toString()); + } + if (parameter instanceof Boolean && (Boolean) parameter) { + topicProperties.setProperty(propWithTopicKey + "." + field.getName(), + Boolean.toString((Boolean) parameter)); + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | IntrospectionException e) { + logger.error("Error while creating Properties object from TopicParameters for {}", field.getName(), e); + } + } + } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/CommonTestData.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/CommonTestData.java index 346ac5d6..e3f0878f 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/CommonTestData.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/CommonTestData.java @@ -53,7 +53,7 @@ public class CommonTestData { public static final String TOPIC_INFRA = "dmaap"; public static final String TOPIC_SERVER = "message-router"; - protected static final List<TopicParameters> TOPIC_PARAMS = + public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParameters(TOPIC_NAME, TOPIC_INFRA, TOPIC_SERVER)); protected static final Coder coder = new StandardCoder(); diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroupTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroupTest.java index e288009a..f1507365 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroupTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroupTest.java @@ -26,10 +26,12 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.lang.reflect.Method; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -123,12 +125,14 @@ public class TopicParameterGroupTest { */ private boolean checkIfAllParamsNotEmpty(List<TopicParameters> topicParametersList) throws Exception { for (TopicParameters topicParameters : topicParametersList) { - for (Method m : topicParameters.getClass().getMethods()) { - if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) { - final Object parameter = m.invoke(topicParameters); + Field[] fields = BusTopicParams.class.getDeclaredFields(); + for (Field field : fields) { + if (!field.isSynthetic()) { + Object parameter = new PropertyDescriptor(field.getName(), TopicParameters.class).getReadMethod() + .invoke(topicParameters); if ((parameter instanceof String && StringUtils.isBlank(parameter.toString())) || (parameter instanceof Boolean && !(Boolean) parameter) - || (parameter instanceof Number && ((Number)parameter).longValue() == 0)) { + || (parameter instanceof Number && ((Number) parameter).longValue() == 0)) { return false; } } 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 06c16e9c..54a14606 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 @@ -23,12 +23,16 @@ package org.onap.policy.common.endpoints.utils; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; -import java.util.Arrays; import java.util.Properties; import org.junit.Test; import org.onap.policy.common.endpoints.parameters.CommonTestData; import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; /** * Class to perform unit test of {@link ParameterUtils}. @@ -36,7 +40,11 @@ import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; * @author Ajith Sreekumar (ajith.sreekumar@est.tech) */ public class ParameterUtilsTest { + private static final String SOURCE_TOPICS_POLICY_PDP_PAP1 = ".source.topics.POLICY-PDP-PAP1."; + private static final String SINK_TOPICS_POLICY_PDP_PAP1 = ".sink.topics.POLICY-PDP-PAP1."; private static final String SERVERS = ".servers"; + private static CommonTestData testData = new CommonTestData(); + private static final Coder coder = new StandardCoder(); /** * Test getTopicProperties from TopicParameterGroup. @@ -60,17 +68,48 @@ public class ParameterUtilsTest { @Test public void testUpdateTopicProperties() { Properties topicProperties = new Properties(); - ParameterUtils.updateTopicProperties(topicProperties, "source", CommonTestData.TOPIC_INFRA, - CommonTestData.TOPIC_NAME, Arrays.asList(CommonTestData.TOPIC_SERVER)); + ParameterUtils.updateTopicProperties(topicProperties, "source", CommonTestData.TOPIC_PARAMS.get(0)); 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)); + ParameterUtils.updateTopicProperties(topicProperties, "sink", CommonTestData.TOPIC_PARAMS.get(0)); 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)); } + + @Test + public void testGetTopicProperties_all_props() throws Exception { + String json = testData.getParameterGroupAsString( + "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json"); + TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class); + final GroupValidationResult result = topicParameterGroup.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + Properties topicProperties = ParameterUtils.getTopicProperties(topicParameterGroup); + assertEquals("true", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "managed")); + assertEquals("true", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SINK_TOPICS_POLICY_PDP_PAP1 + "managed")); + assertEquals("123", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "port")); + assertEquals("123", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SINK_TOPICS_POLICY_PDP_PAP1 + "port")); + assertEquals("my-api-key", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "apiKey")); + assertEquals("my-api-key", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SINK_TOPICS_POLICY_PDP_PAP1 + "apiKey")); + assertEquals("my-effective-topic", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "effectiveTopic")); + assertEquals("true", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "useHttps")); + assertEquals("username", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "userName")); + assertEquals("password", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "password")); + assertEquals("true", topicProperties.getProperty( + CommonTestData.TOPIC_INFRA + SOURCE_TOPICS_POLICY_PDP_PAP1 + "allowSelfSignedCerts")); + } } |