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