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.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.network.NetworkUtil;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 * Class to hold/create all parameters for test cases.
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 String REST_CLAMP_HOST = "0.0.0.0";
51 private static final String REST_CONSUL_HOST = "0.0.0.0";
52 private static final boolean REST_CLAMP_HTTPS = false;
53 private static final boolean REST_CONSUL_HTTPS = false;
54 private static final boolean REST_CLIENT_AAF = false;
56 public static final Coder CODER = new StandardCoder();
59 * Get ParticipantDcaeParameters.
61 * @return ParticipantDcaeParameters
63 public ParticipantDcaeParameters getParticipantDcaeParameters() {
65 return CODER.convert(getParticipantParameterGroupMap(PARTICIPANT_GROUP_NAME),
66 ParticipantDcaeParameters.class);
67 } catch (final CoderException e) {
68 throw new RuntimeException("cannot create ParticipantDcaeParameters from map", e);
73 * Returns a property map for a ApexStarterParameterGroup map for test cases.
75 * @param name name of the parameters
77 * @return a property map suitable for constructing an object
79 public Map<String, Object> getParticipantParameterGroupMap(final String name) {
80 final Map<String, Object> map = new TreeMap<>();
82 map.put("name", name);
83 map.put("checkCount", 10);
84 map.put("secCount", 10);
85 map.put("jsonBodyConsulPath", "src/main/resources/parameters/consul.json");
86 map.put("clampClientParameters", getClampClientParametersMap(false));
87 map.put("consulClientParameters", getConsulClientParametersMap(false));
88 map.put("intermediaryParameters", getIntermediaryParametersMap(false));
89 map.put("clampClientEndPoints", getClampClientEndPoints());
90 map.put("consulClientEndPoints", getConsulClientEndPoints());
94 private Map<String, Object> getConsulClientEndPoints() {
95 final Map<String, Object> map = new TreeMap<>();
96 map.put("deploy", "/v1/kv/dcae-pmsh:");
100 private Map<String, Object> getClampClientEndPoints() {
101 final Map<String, Object> map = new TreeMap<>();
102 map.put("status", "/restservices/clds/v2/loop/getstatus/");
103 map.put("create", "/restservices/clds/v2/loop/create/%s?templateName=%s");
104 map.put("deploy", "/restservices/clds/v2/loop/deploy/");
105 map.put("stop", "/restservices/clds/v2/loop/stop/");
106 map.put("delete", "/restservices/clds/v2/loop/delete/");
107 map.put("undeploy", "/restservices/clds/v2/loop/undeploy/");
112 * Returns a property map for a RestServerParameters map for test cases.
114 * @param isEmpty boolean value to represent that object created should be empty or not
115 * @return a property map suitable for constructing an object
116 * @throws ControlLoopRuntimeException on errors
118 public Map<String, Object> getClampClientParametersMap(final boolean isEmpty) {
119 final Map<String, Object> map = new TreeMap<>();
120 map.put("clientName", "Clamp");
121 map.put("https", REST_CLAMP_HTTPS);
122 map.put("aaf", REST_CLIENT_AAF);
125 map.put("hostname", REST_CLAMP_HOST);
127 map.put("port", NetworkUtil.allocPort());
128 } catch (IOException e) {
129 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
131 map.put("userName", REST_CLIENT_USER);
132 map.put("password", REST_CLIENT_PASSWORD);
139 * Returns a property map for a RestServerParameters map for test cases.
141 * @param isEmpty boolean value to represent that object created should be empty or not
142 * @return a property map suitable for constructing an object
143 * @throws ControlLoopRuntimeException on errors
145 public Map<String, Object> getConsulClientParametersMap(final boolean isEmpty) {
146 final Map<String, Object> map = new TreeMap<>();
147 map.put("clientName", "Consul");
148 map.put("https", REST_CONSUL_HTTPS);
149 map.put("aaf", REST_CLIENT_AAF);
152 map.put("hostname", REST_CONSUL_HOST);
154 map.put("port", NetworkUtil.allocPort());
155 } catch (IOException e) {
156 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
158 map.put("userName", REST_CLIENT_USER);
159 map.put("password", REST_CLIENT_PASSWORD);
166 * Returns a property map for a intermediaryParameters map for test cases.
168 * @param isEmpty boolean value to represent that object created should be empty or not
169 * @return a property map suitable for constructing an object
171 public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
172 final Map<String, Object> map = new TreeMap<>();
174 map.put("name", "Participant parameters");
175 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
176 map.put("description", DESCRIPTION);
177 map.put("participantId", getParticipantId());
178 map.put("participantType", getParticipantId());
179 map.put("clampControlLoopTopics", getTopicParametersMap(false));
186 * Returns a property map for a TopicParameters map for test cases.
188 * @param isEmpty boolean value to represent that object created should be empty or not
189 * @return a property map suitable for constructing an object
191 public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
192 final Map<String, Object> map = new TreeMap<>();
194 map.put("topicSources", TOPIC_PARAMS);
195 map.put("topicSinks", TOPIC_PARAMS);
201 * Returns topic parameters for test cases.
203 * @return topic parameters
205 public static TopicParameters getTopicParams() {
206 final TopicParameters topicParams = new TopicParameters();
207 topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
208 topicParams.setTopicCommInfrastructure("dmaap");
209 topicParams.setServers(Arrays.asList("localhost"));
214 * Returns participantId for test cases.
216 * @return participant Id
218 public static ToscaConceptIdentifier getParticipantId() {
219 final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
220 participantId.setName("DCAEParticipant0");
221 participantId.setVersion("1.0.0");
222 return participantId;
226 * Nulls out a field within a JSON string.
228 * @param json JSON string
229 * @param field field to be nulled out
230 * @return a new JSON string with the field nulled out
232 public String nullifyField(String json, String field) {
233 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
237 * Create Json response from getstatus call.
239 * @param status the status of Partecipant
241 * @throws ControlLoopRuntimeException on errors
243 public static String createJsonStatus(String status) {
245 File file = new File("src/test/resources/rest/status.json");
246 String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
247 return json.replace("${status}", status);
249 } catch (IOException e) {
250 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);