38d8621ebc0283c0e734ab0c6adc6d70552fc75d
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / CommonTestData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.parameters;
23
24 import java.util.Base64;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.onap.policy.common.parameters.ParameterGroup;
30 import org.onap.policy.common.utils.coder.Coder;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
34
35 /**
36  * Class to hold/create all parameters for test cases.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class CommonTestData {
41
42     private static final String REST_SERVER_PASSWORD = "zb!XztG34";
43     private static final String REST_SERVER_USER = "healthcheck";
44     private static final int REST_SERVER_PORT = 6969;
45     private static final String REST_SERVER_HOST = "0.0.0.0";
46     private static final boolean REST_SERVER_HTTPS = true;
47     private static final boolean REST_SERVER_AAF = false;
48     public static final String PAP_GROUP_NAME = "PapGroup";
49
50     private static final Coder coder = new StandardCoder();
51
52     /**
53      * Converts the contents of a map to a parameter class.
54      *
55      * @param source property map
56      * @param clazz class of object to be created from the map
57      * @return a new object represented by the map
58      */
59     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
60         try {
61             return coder.decode(coder.encode(source), clazz);
62
63         } catch (final CoderException e) {
64             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
65         }
66     }
67
68     /**
69      * Returns a property map for a PapParameterGroup map for test cases.
70      *
71      * @param name name of the parameters
72      *
73      * @return a property map suitable for constructing an object
74      */
75     public Map<String, Object> getPapParameterGroupMap(final String name) {
76         final Map<String, Object> map = new TreeMap<>();
77
78         map.put("name", name);
79         map.put("restServerParameters", getRestServerParametersMap(false));
80         map.put("pdpParameters", getPdpParametersMap());
81         map.put("databaseProviderParameters", getPolicyModelsProviderParametersMap());
82
83         return map;
84     }
85
86     /**
87      * Returns a property map for a RestServerParameters 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> getRestServerParametersMap(final boolean isEmpty) {
93         final Map<String, Object> map = new TreeMap<>();
94         map.put("https", REST_SERVER_HTTPS);
95         map.put("aaf", REST_SERVER_AAF);
96
97         if (!isEmpty) {
98             map.put("host", REST_SERVER_HOST);
99             map.put("port", REST_SERVER_PORT);
100             map.put("userName", REST_SERVER_USER);
101             map.put("password", REST_SERVER_PASSWORD);
102         }
103
104         return map;
105     }
106
107     /**
108      * Returns a property map for a PdpParameters map for test cases.
109      *
110      * @return a property map suitable for constructing an object
111      */
112     public Map<String, Object> getPdpParametersMap() {
113         final Map<String, Object> map = new TreeMap<>();
114
115         map.put("updateParameters", getPdpUpdateParametersMap());
116         map.put("stateChangeParameters", getPdpStateChangeParametersMap());
117
118         return map;
119     }
120
121     /**
122      * Returns a property map for a PdpUpdateParameters map for test cases.
123      *
124      * @return a property map suitable for constructing an object
125      */
126     public Map<String, Object> getPdpUpdateParametersMap() {
127         return getPdpRequestParametersMap();
128     }
129
130     /**
131      * Returns a property map for a PdpStateChangeParameters map for test cases.
132      *
133      * @return a property map suitable for constructing an object
134      */
135     public Map<String, Object> getPdpStateChangeParametersMap() {
136         return getPdpRequestParametersMap();
137     }
138
139     /**
140      * Returns a property map for a PdpParameters map for test cases.
141      *
142      * @return a property map suitable for constructing an object
143      */
144     public Map<String, Object> getPdpRequestParametersMap() {
145         final Map<String, Object> map = new HashMap<>();
146         map.put("maxRetryCount", "1");
147         map.put("maxWaitMs", "2");
148
149         return map;
150     }
151
152     /**
153      * Returns a property map for a PdpGroupDeploymentParameters map for test cases.
154      *
155      * @return a property map suitable for constructing an object
156      */
157     public Map<String, Object> getPdpGroupDeploymentParametersMap() {
158         final Map<String, Object> map = new TreeMap<>();
159         map.put("waitResponseMs", "1");
160
161         return map;
162     }
163
164     /**
165      * Returns a property map for a PolicyModelsProviderParameters map for test cases.
166      *
167      * @return a property map suitable for constructing an object
168      */
169     public Map<String, Object> getPolicyModelsProviderParametersMap() {
170         final Map<String, Object> map = new TreeMap<>();
171         map.put("name", PolicyModelsProviderParameters.class.getSimpleName());
172         map.put("implementation", REST_SERVER_HTTPS);
173         map.put("databaseUrl", "jdbc:h2:mem:testdb");
174         map.put("databaseUser", "policy");
175         map.put("databasePassword", Base64.getEncoder().encodeToString("P01icY".getBytes()));
176         map.put("persistenceUnit", "PdpGroupTest");
177
178         return map;
179     }
180 }