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