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