bcfaf8bb9003c8ae1976cade8823489ee9cc6640
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.parameters;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import javax.ws.rs.core.Response;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
33 import org.onap.policy.common.endpoints.parameters.TopicParameters;
34 import org.onap.policy.common.parameters.ParameterGroup;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 /**
41  * Class to hold/create all parameters for test cases.
42  */
43 public class CommonTestData {
44     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
45     public static final String DESCRIPTION = "Participant description";
46     public static final long TIME_INTERVAL = 2000;
47     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
48     private static final String REST_CLIENT_PASSWORD = "password";
49     private static final String REST_CLIENT_USER = "admin";
50     private static final int REST_CLAMP_PORT = 8443;
51     private static final int REST_CONSUL_PORT = 31321;
52     private static final String REST_CLAMP_HOST = "localhost";
53     private static final String REST_CONSUL_HOST = "consul";
54     private static final boolean REST_CLAMP_HTTPS = false;
55     private static final boolean REST_CONSUL_HTTPS = false;
56     private static final boolean REST_CLIENT_AAF = false;
57
58     public static final Coder coder = new StandardCoder();
59
60     /**
61      * Converts the contents of a map to a parameter class.
62      *
63      * @param source property map
64      * @param clazz class of object to be created from the map
65      * @return a new object represented by the map
66      */
67     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
68         try {
69             return coder.convert(source, clazz);
70
71         } catch (final CoderException e) {
72             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
73         }
74     }
75
76     /**
77      * Returns a property map for a ApexStarterParameterGroup map for test cases.
78      *
79      * @param name name of the parameters
80      *
81      * @return a property map suitable for constructing an object
82      */
83     public Map<String, Object> getParticipantParameterGroupMap(final String name) {
84         final Map<String, Object> map = new TreeMap<>();
85
86         map.put("name", name);
87         map.put("clampClientParameters", getClampClientParametersMap(false));
88         map.put("consulClientParameters", getConsulClientParametersMap(false));
89         map.put("intermediaryParameters", getIntermediaryParametersMap(false));
90         map.put("databaseProviderParameters", getDatabaseProviderParametersMap(false));
91         return map;
92     }
93
94     /**
95      * Returns a property map for a RestServerParameters map for test cases.
96      *
97      * @param isEmpty boolean value to represent that object created should be empty or not
98      * @return a property map suitable for constructing an object
99      */
100     public Map<String, Object> getClampClientParametersMap(final boolean isEmpty) {
101         final Map<String, Object> map = new TreeMap<>();
102         map.put("https", REST_CLAMP_HTTPS);
103         map.put("aaf", REST_CLIENT_AAF);
104
105         if (!isEmpty) {
106             map.put("host", REST_CLAMP_HOST);
107             map.put("port", REST_CLAMP_PORT);
108             map.put("userName", REST_CLIENT_USER);
109             map.put("password", REST_CLIENT_PASSWORD);
110         }
111
112         return map;
113     }
114
115     /**
116      * Returns a property map for a RestServerParameters map for test cases.
117      *
118      * @param isEmpty boolean value to represent that object created should be empty or not
119      * @return a property map suitable for constructing an object
120      */
121     public Map<String, Object> getConsulClientParametersMap(final boolean isEmpty) {
122         final Map<String, Object> map = new TreeMap<>();
123         map.put("https", REST_CONSUL_HTTPS);
124         map.put("aaf", REST_CLIENT_AAF);
125
126         if (!isEmpty) {
127             map.put("host", REST_CONSUL_HOST);
128             map.put("port", REST_CONSUL_PORT);
129             map.put("userName", REST_CLIENT_USER);
130             map.put("password", REST_CLIENT_PASSWORD);
131         }
132
133         return map;
134     }
135
136     /**
137      * Returns a property map for a databaseProviderParameters map for test cases.
138      *
139      * @param isEmpty boolean value to represent that object created should be empty or not
140      * @return a property map suitable for constructing an object
141      */
142     public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
143         final Map<String, Object> map = new TreeMap<>();
144         if (!isEmpty) {
145             map.put("name", "PolicyProviderParameterGroup");
146             map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
147             map.put("databaseDriver", "org.h2.Driver");
148             map.put("databaseUrl", "jdbc:h2:mem:testdb");
149             map.put("databaseUser", "policy");
150             map.put("databasePassword", "P01icY");
151             map.put("persistenceUnit", "ToscaConceptTest");
152         }
153
154         return map;
155     }
156
157     /**
158      * Returns a property map for a intermediaryParameters map for test cases.
159      *
160      * @param isEmpty boolean value to represent that object created should be empty or not
161      * @return a property map suitable for constructing an object
162      */
163     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
164         final Map<String, Object> map = new TreeMap<>();
165         if (!isEmpty) {
166             map.put("name", "Participant parameters");
167             map.put("reportingTimeInterval", TIME_INTERVAL);
168             map.put("description", DESCRIPTION);
169             map.put("participantId", getParticipantId());
170             map.put("participantType", getParticipantId());
171             map.put("clampControlLoopTopics", getTopicParametersMap(false));
172         }
173
174         return map;
175     }
176
177     /**
178      * Returns a property map for a TopicParameters map for test cases.
179      *
180      * @param isEmpty boolean value to represent that object created should be empty or not
181      * @return a property map suitable for constructing an object
182      */
183     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
184         final Map<String, Object> map = new TreeMap<>();
185         if (!isEmpty) {
186             map.put("topicSources", TOPIC_PARAMS);
187             map.put("topicSinks", TOPIC_PARAMS);
188         }
189         return map;
190     }
191
192     /**
193      * Returns topic parameters for test cases.
194      *
195      * @return topic parameters
196      */
197     public static TopicParameters getTopicParams() {
198         final TopicParameters topicParams = new TopicParameters();
199         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
200         topicParams.setTopicCommInfrastructure("dmaap");
201         topicParams.setServers(Arrays.asList("localhost"));
202         return topicParams;
203     }
204
205     /**
206      * Returns participantId for test cases.
207      *
208      * @return participant Id
209      */
210     public static ToscaConceptIdentifier getParticipantId() {
211         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
212         participantId.setName("CDSParticipant0");
213         participantId.setVersion("1.0.0");
214         return participantId;
215     }
216
217     /**
218      * Gets the standard participant parameters.
219      *
220      * @param port port to be inserted into the parameters
221      * @return the standard participant parameters
222      */
223     public ParticipantDcaeParameters getParticipantParameterGroup(int port) {
224         try {
225             return coder.decode(getParticipantParameterGroupAsString(port), ParticipantDcaeParameters.class);
226
227         } catch (CoderException e) {
228             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
229                     e);
230         }
231     }
232
233     /**
234      * Gets the standard participant parameters, as a String.
235      *
236      * @param port port to be inserted into the parameters
237      * @return the standard participant parameters
238      */
239     public static String getParticipantParameterGroupAsString(int port) {
240
241         try {
242             File file = new File(getParamFile());
243             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
244
245             json = json.replace("${port}", String.valueOf(port));
246             json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
247
248             return json;
249
250         } catch (IOException e) {
251             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
252                     e);
253
254         }
255     }
256
257     /**
258      * Gets the full path to the parameter file, which may vary depending on whether or
259      * not this is an end-to-end test.
260      *
261      * @return the parameter file name
262      */
263     private static String getParamFile() {
264         return "src/test/resources/parameters/TestParametersStd.json";
265     }
266
267     /**
268      * Nulls out a field within a JSON string.
269      *
270      * @param json JSON string
271      * @param field field to be nulled out
272      * @return a new JSON string with the field nulled out
273      */
274     public String nullifyField(String json, String field) {
275         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
276     }
277
278     /**
279      * create Json response from getstatus call.
280      *
281      * @param status the status of Partecipant
282      * @return the JSON
283      */
284     public static String createJsonStatus(String status) {
285         try {
286             File file = new File("src/test/resources/rest/status.json");
287             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
288             return json.replace("${status}", status);
289
290         } catch (IOException e) {
291             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);
292         }
293     }
294 }