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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.parameters;
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;
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;
42 * Class to hold/create all parameters for test cases.
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;
57 public static final Coder coder = new StandardCoder();
60 * Converts the contents of a map to a parameter class.
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
68 public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
70 return coder.convert(source, clazz);
72 } catch (final CoderException e) {
73 throw new ControlLoopRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
74 "cannot create " + clazz.getName() + " from map", e);
79 * Returns a property map for a ApexStarterParameterGroup map for test cases.
81 * @param name name of the parameters
83 * @return a property map suitable for constructing an object
85 public Map<String, Object> getParticipantParameterGroupMap(final String name) {
86 final Map<String, Object> map = new TreeMap<>();
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));
97 * Returns a property map for a RestServerParameters map for test cases.
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
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);
110 map.put("hostname", REST_CLAMP_HOST);
112 map.put("port", NetworkUtil.allocPort());
113 } catch (IOException e) {
114 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
116 map.put("userName", REST_CLIENT_USER);
117 map.put("password", REST_CLIENT_PASSWORD);
124 * Returns a property map for a RestServerParameters map for test cases.
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
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);
137 map.put("hostname", REST_CONSUL_HOST);
139 map.put("port", NetworkUtil.allocPort());
140 } catch (IOException e) {
141 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
143 map.put("userName", REST_CLIENT_USER);
144 map.put("password", REST_CLIENT_PASSWORD);
151 * Returns a property map for a databaseProviderParameters map for test cases.
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
156 public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
157 final Map<String, Object> map = new TreeMap<>();
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");
172 * Returns a property map for a intermediaryParameters map for test cases.
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
177 public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
178 final Map<String, Object> map = new TreeMap<>();
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));
192 * Returns a property map for a TopicParameters map for test cases.
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
197 public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
198 final Map<String, Object> map = new TreeMap<>();
200 map.put("topicSources", TOPIC_PARAMS);
201 map.put("topicSinks", TOPIC_PARAMS);
207 * Returns topic parameters for test cases.
209 * @return topic parameters
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"));
220 * Returns participantId for test cases.
222 * @return participant Id
224 public static ToscaConceptIdentifier getParticipantId() {
225 final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
226 participantId.setName("DCAEParticipant0");
227 participantId.setVersion("1.0.0");
228 return participantId;
232 * Gets the standard participant parameters.
234 * @param port port to be inserted into the parameters
235 * @return the standard participant parameters
236 * @throws ControlLoopRuntimeException on errors
238 public ParticipantDcaeParameters getParticipantParameterGroup(int port) {
240 return coder.decode(getParticipantParameterGroupAsString(port), ParticipantDcaeParameters.class);
242 } catch (CoderException e) {
243 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
249 * Gets the standard participant parameters, as a String.
251 * @param port port to be inserted into the parameters
252 * @return the standard participant parameters
253 * @throws ControlLoopRuntimeException on errors
255 public static String getParticipantParameterGroupAsString(int port) {
258 File file = new File(getParamFile());
259 String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
261 json = json.replace("${port}", String.valueOf(port));
262 json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
266 } catch (IOException e) {
267 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
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.
277 * @return the parameter file name
279 private static String getParamFile() {
280 return "src/test/resources/parameters/TestParametersStd.json";
284 * Nulls out a field within a JSON string.
286 * @param json JSON string
287 * @param field field to be nulled out
288 * @return a new JSON string with the field nulled out
290 public String nullifyField(String json, String field) {
291 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
295 * Create Json response from getstatus call.
297 * @param status the status of Partecipant
299 * @throws ControlLoopRuntimeException on errors
301 public static String createJsonStatus(String status) {
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);
307 } catch (IOException e) {
308 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);