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