a81263a9c589cb5b003d39c0aee3ed42647eee37
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2024 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.parameters;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
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.jupiter.api.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;
40
41 /**
42  * Class to perform unit test of {@link TopicParameterGroup}.
43  *
44  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
45  */
46 class TopicParameterGroupTest {
47     private static CommonTestData testData = new CommonTestData();
48     private static final Coder coder = new StandardCoder();
49
50     @Test
51     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());
58
59         // these should default to true
60         assertTrue(new TopicParameters().isManaged());
61         assertTrue(coder.decode("{}", TopicParameters.class).isManaged());
62
63         // but can be overridden
64         assertFalse(coder.decode("{'managed':false}".replace('\'', '"'), TopicParameters.class).isManaged());
65     }
66
67     @Test
68     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());
74     }
75
76     @Test
77     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());
84     }
85
86     @Test
87     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"));
94     }
95
96     @Test
97     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());
104     }
105
106     @Test
107     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()));
116     }
117
118     /**
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.
122      *
123      * @param topicParametersList list of topic parameters
124      * @return true if all parameters are not empty (if string) or true (if boolean)
125      * @throws Exception the exception
126      */
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)) {
137                         return false;
138                     }
139                 }
140             }
141         }
142         return true;
143     }
144 }