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