2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 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.beans.PropertyDescriptor;
30 import java.lang.reflect.Field;
31 import java.lang.reflect.Modifier;
32 import java.util.List;
33 import org.apache.commons.lang3.StringUtils;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
36 import org.onap.policy.common.parameters.ValidationResult;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
42 * Class to perform unit test of {@link TopicParameterGroup}.
44 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
46 public class TopicParameterGroupTest {
47 private static CommonTestData testData = new CommonTestData();
48 private static final Coder coder = new StandardCoder();
51 public void test() throws CoderException {
52 final TopicParameterGroup topicParameterGroup =
53 testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class);
54 final ValidationResult validationResult = topicParameterGroup.validate();
55 assertTrue(validationResult.isValid());
56 assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks());
57 assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources());
59 // these should default to true
60 assertTrue(new TopicParameters().isManaged());
61 assertTrue(coder.decode("{}", TopicParameters.class).isManaged());
63 // but can be overridden
64 assertFalse(coder.decode("{'managed':false}".replace('\'', '"'), TopicParameters.class).isManaged());
68 public void testValidate() {
69 final TopicParameterGroup topicParameterGroup =
70 testData.toObject(testData.getTopicParameterGroupMap(false), TopicParameterGroup.class);
71 final ValidationResult result = topicParameterGroup.validate();
72 assertNull(result.getResult());
73 assertTrue(result.isValid());
77 public void test_valid() throws Exception {
78 String json = testData.getParameterGroupAsString(
79 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_valid.json");
80 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
81 final ValidationResult result = topicParameterGroup.validate();
82 assertNull(result.getResult());
83 assertTrue(result.isValid());
87 public void test_invalid() throws Exception {
88 String json = testData.getParameterGroupAsString(
89 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_invalid.json");
90 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
91 final ValidationResult result = topicParameterGroup.validate();
92 assertFalse(result.isValid());
93 assertTrue(result.getResult().contains("INVALID"));
97 public void test_missing_mandatory_params() throws Exception {
98 String json = testData.getParameterGroupAsString(
99 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json");
100 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
101 final ValidationResult result = topicParameterGroup.validate();
102 assertTrue(result.getResult().contains("Mandatory parameters are missing"));
103 assertFalse(result.isValid());
107 public void test_allparams() throws Exception {
108 String json = testData.getParameterGroupAsString(
109 "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json");
110 TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
111 final ValidationResult result = topicParameterGroup.validate();
112 assertNull(result.getResult());
113 assertTrue(result.isValid());
114 assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSinks()));
115 assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSources()));
119 * Method to check if all parameters in TopicParameters are set.
120 * Any parameters added to @link TopicParameters or @link BusTopicParams must be added to
121 * TopicParameters_all_params.json.
123 * @param topicParameters topic parameters
124 * @return true if all parameters are not empty (if string) or true (if boolean)
125 * @throws Exception the exception
127 private boolean checkIfAllParamsNotEmpty(List<TopicParameters> topicParametersList) throws Exception {
128 for (TopicParameters topicParameters : topicParametersList) {
129 Field[] fields = BusTopicParams.class.getDeclaredFields();
130 for (Field field : fields) {
131 if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers())) {
132 Object parameter = new PropertyDescriptor(field.getName(), TopicParameters.class).getReadMethod()
133 .invoke(topicParameters);
134 if ((parameter instanceof String && StringUtils.isBlank(parameter.toString()))
135 || (parameter instanceof Boolean && !(Boolean) parameter)
136 || (parameter instanceof Number && ((Number) parameter).longValue() == 0)) {