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 source property map
63 * @param clazz class of object to be created from the map
64 * @return a new object represented by the map
66 public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
68 return coder.convert(source, clazz);
70 } catch (final CoderException e) {
71 throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
76 * Returns a property map for a ApexStarterParameterGroup map for test cases.
78 * @param name name of the parameters
80 * @return a property map suitable for constructing an object
82 public Map<String, Object> getParticipantParameterGroupMap(final String name) {
83 final Map<String, Object> map = new TreeMap<>();
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));
94 * Returns a property map for a RestServerParameters map for test cases.
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
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);
106 map.put("hostname", REST_CLAMP_HOST);
108 map.put("port", NetworkUtil.allocPort());
109 } catch (IOException e) {
110 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
112 map.put("userName", REST_CLIENT_USER);
113 map.put("password", REST_CLIENT_PASSWORD);
120 * Returns a property map for a RestServerParameters map for test cases.
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
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);
132 map.put("hostname", REST_CONSUL_HOST);
134 map.put("port", NetworkUtil.allocPort());
135 } catch (IOException e) {
136 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
138 map.put("userName", REST_CLIENT_USER);
139 map.put("password", REST_CLIENT_PASSWORD);
146 * Returns a property map for a databaseProviderParameters map for test cases.
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
151 public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
152 final Map<String, Object> map = new TreeMap<>();
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");
167 * Returns a property map for a intermediaryParameters map for test cases.
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
172 public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
173 final Map<String, Object> map = new TreeMap<>();
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));
187 * Returns a property map for a TopicParameters map for test cases.
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
192 public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
193 final Map<String, Object> map = new TreeMap<>();
195 map.put("topicSources", TOPIC_PARAMS);
196 map.put("topicSinks", TOPIC_PARAMS);
202 * Returns topic parameters for test cases.
204 * @return topic parameters
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"));
215 * Returns participantId for test cases.
217 * @return participant Id
219 public static ToscaConceptIdentifier getParticipantId() {
220 final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
221 participantId.setName("DCAEParticipant0");
222 participantId.setVersion("1.0.0");
223 return participantId;
227 * Gets the standard participant parameters.
229 * @param port port to be inserted into the parameters
230 * @return the standard participant parameters
232 public ParticipantDcaeParameters getParticipantParameterGroup(int port) {
234 return coder.decode(getParticipantParameterGroupAsString(port), ParticipantDcaeParameters.class);
236 } catch (CoderException e) {
237 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
243 * Gets the standard participant parameters, as a String.
245 * @param port port to be inserted into the parameters
246 * @return the standard participant parameters
248 public static String getParticipantParameterGroupAsString(int port) {
251 File file = new File(getParamFile());
252 String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
254 json = json.replace("${port}", String.valueOf(port));
255 json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
259 } catch (IOException e) {
260 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
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.
270 * @return the parameter file name
272 private static String getParamFile() {
273 return "src/test/resources/parameters/TestParametersStd.json";
277 * Nulls out a field within a JSON string.
279 * @param json JSON string
280 * @param field field to be nulled out
281 * @return a new JSON string with the field nulled out
283 public String nullifyField(String json, String field) {
284 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
288 * Create Json response from getstatus call.
290 * @param status the status of Partecipant
293 public static String createJsonStatus(String status) {
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);
299 } catch (IOException e) {
300 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);