2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.parameters;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
29 import java.lang.reflect.Method;
30 import java.util.List;
31 import org.apache.commons.lang3.StringUtils;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.GroupValidationResult;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
39 * Class to perform unit test of {@link TopicParameterGroup}.
41 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43 public class TopicParameterGroupTest {
44 private static CommonTestData testData = new CommonTestData();
45 private static final Coder coder = new StandardCoder();
48 public void test() throws CoderException {
49 final TopicParameterGroup topicParameterGroup =
50 testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class);
51 final GroupValidationResult validationResult = topicParameterGroup.validate();
52 assertTrue(validationResult.isValid());
53 assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks());
54 assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources());
56 // these should default to true
57 assertTrue(new TopicParameters().isManaged());
58 assertTrue(coder.decode("{}", TopicParameters.class).isManaged());
60 // but can be overridden
61 assertFalse(coder.decode("{'managed':false}".replace('\'', '"'), TopicParameters.class).isManaged());
65 public void testValidate() {
66 final TopicParameterGroup topicParameterGroup =
67 testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class);
68 final GroupValidationResult result = topicParameterGroup.validate();
69 assertNull(result.getResult());
70 assertTrue(result.isValid());
74 public void test_valid() throws Exception {
75 String json = testData.getParameterGroupAsString(
76 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_valid.json");
77 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
78 final GroupValidationResult result = topicParameterGroup.validate();
79 assertNull(result.getResult());
80 assertTrue(result.isValid());
84 public void test_invalid() throws Exception {
85 String json = testData.getParameterGroupAsString(
86 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_invalid.json");
87 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
88 final GroupValidationResult result = topicParameterGroup.validate();
89 assertFalse(result.isValid());
90 assertTrue(result.getResult().contains("INVALID"));
94 public void test_missing_mandatory_params() throws Exception {
95 String json = testData.getParameterGroupAsString(
96 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json");
97 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
98 final GroupValidationResult result = topicParameterGroup.validate();
99 assertTrue(result.getResult().contains("Mandatory parameters are missing"));
100 assertFalse(result.isValid());
104 public void test_allparams() throws Exception {
105 String json = testData.getParameterGroupAsString(
106 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json");
107 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
108 final GroupValidationResult result = topicParameterGroup.validate();
109 assertNull(result.getResult());
110 assertTrue(result.isValid());
111 assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSinks()));
112 assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSources()));
116 * Method to check if all parameters in TopicParameters are set.
117 * Any parameters added to @link TopicParameters or @link BusTopicParams must be added to
118 * TopicParameters_all_params.json.
120 * @param topicParameters topic parameters
121 * @return true if all parameters are not empty (if string) or true (if boolean)
122 * @throws Exception the exception
124 private boolean checkIfAllParamsNotEmpty(List<TopicParameters> topicParametersList) throws Exception {
125 for (TopicParameters topicParameters : topicParametersList) {
126 for (Method m : topicParameters.getClass().getMethods()) {
127 if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
128 final Object parameter = m.invoke(topicParameters);
129 if ((parameter instanceof String && StringUtils.isBlank(parameter.toString()))
130 || (parameter instanceof Boolean && !(Boolean) parameter)
131 || (parameter instanceof Number && ((Number)parameter).longValue() == 0)) {