5d8881eb3ebe5558fa8fa47f020346e3bbe26064
[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.common.utils.network.NetworkUtil;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40
41 /**
42  * Class to hold/create all parameters for test cases.
43  */
44 public class CommonTestData {
45     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
46     public static final String DESCRIPTION = "Participant description";
47     public static final long TIME_INTERVAL = 2000;
48     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
49     private static final String REST_CLIENT_PASSWORD = "password";
50     private static final String REST_CLIENT_USER = "admin";
51     private static final String REST_CLAMP_HOST = "0.0.0.0";
52     private static final String REST_CONSUL_HOST = "0.0.0.0";
53     private static final boolean REST_CLAMP_HTTPS = false;
54     private static final boolean REST_CONSUL_HTTPS = false;
55     private static final boolean REST_CLIENT_AAF = false;
56
57     public static final Coder coder = new StandardCoder();
58
59     /**
60      * Converts the contents of a map to a parameter class.
61      *
62      * @param <T> specific type of ParameterGroup class
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      * @throws ControlLoopRuntimeException on errors
67      */
68     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
69         try {
70             return coder.convert(source, clazz);
71
72         } catch (final CoderException e) {
73             throw new ControlLoopRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
74                     "cannot create " + clazz.getName() + " from map", e);
75         }
76     }
77
78     /**
79      * Returns a property map for a ApexStarterParameterGroup map for test cases.
80      *
81      * @param name name of the parameters
82      *
83      * @return a property map suitable for constructing an object
84      */
85     public Map<String, Object> getParticipantParameterGroupMap(final String name) {
86         final Map<String, Object> map = new TreeMap<>();
87
88         map.put("name", name);
89         map.put("clampClientParameters", getClampClientParametersMap(false));
90         map.put("consulClientParameters", getConsulClientParametersMap(false));
91         map.put("intermediaryParameters", getIntermediaryParametersMap(false));
92         map.put("databaseProviderParameters", getDatabaseProviderParametersMap(false));
93         return map;
94     }
95
96     /**
97      * Returns a property map for a RestServerParameters map for test cases.
98      *
99      * @param isEmpty boolean value to represent that object created should be empty or not
100      * @return a property map suitable for constructing an object
101      * @throws ControlLoopRuntimeException on errors
102      */
103     public Map<String, Object> getClampClientParametersMap(final boolean isEmpty) {
104         final Map<String, Object> map = new TreeMap<>();
105         map.put("clientName", "Clamp");
106         map.put("https", REST_CLAMP_HTTPS);
107         map.put("aaf", REST_CLIENT_AAF);
108
109         if (!isEmpty) {
110             map.put("hostname", REST_CLAMP_HOST);
111             try {
112                 map.put("port", NetworkUtil.allocPort());
113             } catch (IOException e) {
114                 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
115             }
116             map.put("userName", REST_CLIENT_USER);
117             map.put("password", REST_CLIENT_PASSWORD);
118         }
119
120         return map;
121     }
122
123     /**
124      * Returns a property map for a RestServerParameters map for test cases.
125      *
126      * @param isEmpty boolean value to represent that object created should be empty or not
127      * @return a property map suitable for constructing an object
128      * @throws ControlLoopRuntimeException on errors
129      */
130     public Map<String, Object> getConsulClientParametersMap(final boolean isEmpty) {
131         final Map<String, Object> map = new TreeMap<>();
132         map.put("clientName", "Consul");
133         map.put("https", REST_CONSUL_HTTPS);
134         map.put("aaf", REST_CLIENT_AAF);
135
136         if (!isEmpty) {
137             map.put("hostname", REST_CONSUL_HOST);
138             try {
139                 map.put("port", NetworkUtil.allocPort());
140             } catch (IOException e) {
141                 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
142             }
143             map.put("userName", REST_CLIENT_USER);
144             map.put("password", REST_CLIENT_PASSWORD);
145         }
146
147         return map;
148     }
149
150     /**
151      * Returns a property map for a databaseProviderParameters map for test cases.
152      *
153      * @param isEmpty boolean value to represent that object created should be empty or not
154      * @return a property map suitable for constructing an object
155      */
156     public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
157         final Map<String, Object> map = new TreeMap<>();
158         if (!isEmpty) {
159             map.put("name", "PolicyProviderParameterGroup");
160             map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
161             map.put("databaseDriver", "org.h2.Driver");
162             map.put("databaseUrl", "jdbc:h2:mem:testdb");
163             map.put("databaseUser", "policy");
164             map.put("databasePassword", "P01icY");
165             map.put("persistenceUnit", "ToscaConceptTest");
166         }
167
168         return map;
169     }
170
171     /**
172      * Returns a property map for a intermediaryParameters map for test cases.
173      *
174      * @param isEmpty boolean value to represent that object created should be empty or not
175      * @return a property map suitable for constructing an object
176      */
177     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
178         final Map<String, Object> map = new TreeMap<>();
179         if (!isEmpty) {
180             map.put("name", "Participant parameters");
181             map.put("reportingTimeInterval", TIME_INTERVAL);
182             map.put("description", DESCRIPTION);
183             map.put("participantId", getParticipantId());
184             map.put("participantType", getParticipantId());
185             map.put("clampControlLoopTopics", getTopicParametersMap(false));
186         }
187
188         return map;
189     }
190
191     /**
192      * Returns a property map for a TopicParameters map for test cases.
193      *
194      * @param isEmpty boolean value to represent that object created should be empty or not
195      * @return a property map suitable for constructing an object
196      */
197     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
198         final Map<String, Object> map = new TreeMap<>();
199         if (!isEmpty) {
200             map.put("topicSources", TOPIC_PARAMS);
201             map.put("topicSinks", TOPIC_PARAMS);
202         }
203         return map;
204     }
205
206     /**
207      * Returns topic parameters for test cases.
208      *
209      * @return topic parameters
210      */
211     public static TopicParameters getTopicParams() {
212         final TopicParameters topicParams = new TopicParameters();
213         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
214         topicParams.setTopicCommInfrastructure("dmaap");
215         topicParams.setServers(Arrays.asList("localhost"));
216         return topicParams;
217     }
218
219     /**
220      * Returns participantId for test cases.
221      *
222      * @return participant Id
223      */
224     public static ToscaConceptIdentifier getParticipantId() {
225         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
226         participantId.setName("DCAEParticipant0");
227         participantId.setVersion("1.0.0");
228         return participantId;
229     }
230
231     /**
232      * Gets the standard participant parameters.
233      *
234      * @param port port to be inserted into the parameters
235      * @return the standard participant parameters
236      * @throws ControlLoopRuntimeException on errors
237      */
238     public ParticipantDcaeParameters getParticipantParameterGroup(int port) {
239         try {
240             return coder.decode(getParticipantParameterGroupAsString(port), ParticipantDcaeParameters.class);
241
242         } catch (CoderException e) {
243             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
244                     e);
245         }
246     }
247
248     /**
249      * Gets the standard participant parameters, as a String.
250      *
251      * @param port port to be inserted into the parameters
252      * @return the standard participant parameters
253      * @throws ControlLoopRuntimeException on errors
254      */
255     public static String getParticipantParameterGroupAsString(int port) {
256
257         try {
258             File file = new File(getParamFile());
259             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
260
261             json = json.replace("${port}", String.valueOf(port));
262             json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
263
264             return json;
265
266         } catch (IOException e) {
267             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
268                     e);
269
270         }
271     }
272
273     /**
274      * Gets the full path to the parameter file, which may vary depending on whether or
275      * not this is an end-to-end test.
276      *
277      * @return the parameter file name
278      */
279     private static String getParamFile() {
280         return "src/test/resources/parameters/TestParametersStd.json";
281     }
282
283     /**
284      * Nulls out a field within a JSON string.
285      *
286      * @param json JSON string
287      * @param field field to be nulled out
288      * @return a new JSON string with the field nulled out
289      */
290     public String nullifyField(String json, String field) {
291         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
292     }
293
294     /**
295      * Create Json response from getstatus call.
296      *
297      * @param status the status of Partecipant
298      * @return the JSON
299      * @throws ControlLoopRuntimeException on errors
300      */
301     public static String createJsonStatus(String status) {
302         try {
303             File file = new File("src/test/resources/rest/status.json");
304             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
305             return json.replace("${status}", status);
306
307         } catch (IOException e) {
308             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);
309         }
310     }
311 }