2c9b1ccd266100a5041dfa9223f3cd278e3c500a
[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.simulator.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 javax.ws.rs.core.Response.Status;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
34 import org.onap.policy.common.endpoints.parameters.TopicParameters;
35 import org.onap.policy.common.parameters.ParameterGroup;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
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_SERVER_PASSWORD = "zb!XztG34";
50     private static final String REST_SERVER_USER = "healthcheck";
51     private static final int REST_SERVER_PORT = 6969;
52     public static final String REST_SERVER_HOST = "0.0.0.0";
53     private static final boolean REST_SERVER_HTTPS = true;
54     private static final boolean REST_SERVER_AAF = false;
55
56     public static final Coder coder = new StandardCoder();
57
58     /**
59      * Converts the contents of a map to a parameter class.
60      *
61      * @param <T> the specific parameter group type to convert
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      * @throws ControlLoopRuntimeException on parameter parsing errors
66      */
67     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
68         try {
69             return coder.convert(source, clazz);
70         } catch (final CoderException e) {
71             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE,
72                 "cannot create " + clazz.getName() + " from map", e);
73         }
74     }
75
76     /**
77      * Returns a property map for a ApexStarterParameterGroup map for test cases.
78      *
79      * @param name name of the parameters
80      *
81      * @return a property map suitable for constructing an object
82      */
83     public Map<String, Object> getParticipantParameterGroupMap(final String name) {
84         final Map<String, Object> map = new TreeMap<>();
85
86         map.put("name", name);
87         map.put("restServerParameters", getRestServerParametersMap(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> getRestServerParametersMap(final boolean isEmpty) {
100         final Map<String, Object> map = new TreeMap<>();
101         map.put("https", REST_SERVER_HTTPS);
102         map.put("aaf", REST_SERVER_AAF);
103
104         if (!isEmpty) {
105             map.put("host", REST_SERVER_HOST);
106             map.put("port", REST_SERVER_PORT);
107             map.put("userName", REST_SERVER_USER);
108             map.put("password", REST_SERVER_PASSWORD);
109         }
110
111         return map;
112     }
113
114     /**
115      * Returns a property map for a databaseProviderParameters map for test cases.
116      *
117      * @param isEmpty boolean value to represent that object created should be empty or not
118      * @return a property map suitable for constructing an object
119      */
120     public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
121         final Map<String, Object> map = new TreeMap<>();
122         if (!isEmpty) {
123             map.put("name", "PolicyProviderParameterGroup");
124             map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
125             map.put("databaseDriver", "org.h2.Driver");
126             map.put("databaseUrl", "jdbc:h2:mem:testdb");
127             map.put("databaseUser", "policy");
128             map.put("databasePassword", "P01icY");
129             map.put("persistenceUnit", "ToscaConceptTest");
130         }
131
132         return map;
133     }
134
135     /**
136      * Returns a property map for a intermediaryParameters map for test cases.
137      *
138      * @param isEmpty boolean value to represent that object created should be empty or not
139      * @return a property map suitable for constructing an object
140      */
141     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
142         final Map<String, Object> map = new TreeMap<>();
143         if (!isEmpty) {
144             map.put("name", "Participant parameters");
145             map.put("reportingTimeInterval", TIME_INTERVAL);
146             map.put("description", DESCRIPTION);
147             map.put("participantId", getParticipantId());
148             map.put("participantType", getParticipantId());
149             map.put("clampControlLoopTopics", getTopicParametersMap(false));
150         }
151
152         return map;
153     }
154
155     /**
156      * Returns a property map for a TopicParameters map for test cases.
157      *
158      * @param isEmpty boolean value to represent that object created should be empty or not
159      * @return a property map suitable for constructing an object
160      */
161     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
162         final Map<String, Object> map = new TreeMap<>();
163         if (!isEmpty) {
164             map.put("topicSources", TOPIC_PARAMS);
165             map.put("topicSinks", TOPIC_PARAMS);
166         }
167         return map;
168     }
169
170     /**
171      * Returns topic parameters for test cases.
172      *
173      * @return topic parameters
174      */
175     public static TopicParameters getTopicParams() {
176         final TopicParameters topicParams = new TopicParameters();
177         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
178         topicParams.setTopicCommInfrastructure("dmaap");
179         topicParams.setServers(Arrays.asList("localhost"));
180         return topicParams;
181     }
182
183     /**
184      * Returns participantId for test cases.
185      *
186      * @return participant Id
187      */
188     public static ToscaConceptIdentifier getParticipantId() {
189         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
190         return participantId;
191     }
192
193     /**
194      * Gets the standard participant parameters.
195      *
196      * @param port port to be inserted into the parameters
197      * @return the standard participant parameters
198      * @throws ControlLoopRuntimeException on parameter read errors
199      */
200     public ParticipantSimulatorParameters getParticipantParameterGroup(int port) {
201         try {
202             return coder.decode(getParticipantParameterGroupAsString(port), ParticipantSimulatorParameters.class);
203
204         } catch (CoderException e) {
205             throw new ControlLoopRuntimeException(Status.NOT_ACCEPTABLE, "cannot read participant parameters", e);
206         }
207     }
208
209     /**
210      * Gets the standard participant parameters, as a String.
211      *
212      * @param port port to be inserted into the parameters
213      * @return the standard participant parameters
214      * @throws ControlLoopRuntimeException on parameter read errors
215      */
216     public static String getParticipantParameterGroupAsString(int port) {
217
218         try {
219             File file = new File(getParamFile());
220             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
221
222             json = json.replace("${port}", String.valueOf(port));
223             json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
224
225             return json;
226
227         } catch (IOException e) {
228             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
229                 e);
230
231         }
232     }
233
234     /**
235      * Gets the full path to the parameter file, which may vary depending on whether or
236      * not this is an end-to-end test.
237      *
238      * @return the parameter file name
239      */
240     private static String getParamFile() {
241         String paramFile = "src/test/resources/parameters/TestParametersStd.json";
242         return paramFile;
243     }
244
245     /**
246      * Nulls out a field within a JSON string.
247      *
248      * @param json JSON string
249      * @param field field to be nulled out
250      * @return a new JSON string with the field nulled out
251      */
252     public String nullifyField(String json, String field) {
253         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
254     }
255 }