Get policy type from policy-api
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / parameters / CommonTestData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2019 Nordix Foundation.
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.pdpx.main.parameters;
23
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
29 import org.onap.policy.common.endpoints.parameters.TopicParameters;
30 import org.onap.policy.common.parameters.ParameterGroup;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34
35 /**
36  * Class to hold/create all parameters for test cases.
37  *
38  */
39 public class CommonTestData {
40
41     private static final String PASS_KEY = "password";
42     private static final String USER_KEY = "userName";
43     private static final String PORT_KEY = "port";
44     private static final String HOST_KEY = "host";
45     private static final String AAF_KEY = "aaf";
46     private static final String HTTPS_KEY = "https";
47
48     private static final String REST_SERVER_PASS = "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 = false;
53     private static final boolean REST_SERVER_AAF = false;
54
55     private static final String POLICY_API_PASS = "zb!XztG34";
56     private static final String POLICY_API_USER = "healthcheck";
57     private static final int POLICY_API_PORT = 6970;
58     private static final String POLICY_API_HOST = "0.0.0.0";
59     private static final boolean POLICY_API_HTTPS = false;
60     private static final boolean POLICY_API_AAF = false;
61
62     public static final String PDPX_GROUP_NAME = "XacmlPdpGroup";
63     public static final List<TopicParameters> TOPIC_PARAMS =
64                     Collections.unmodifiableList(Arrays.asList(getTopicParams()));
65
66     public static final Coder coder = new StandardCoder();
67
68     /**
69      * Returns topic parameters for test cases.
70      *
71      * @return topic parameters
72      */
73     public static TopicParameters getTopicParams() {
74         final TopicParameters topicParams = new TopicParameters();
75         topicParams.setTopic("POLICY-PDP-PAP");
76         topicParams.setTopicCommInfrastructure("noop");
77         topicParams.setServers(Arrays.asList("message-router"));
78         return topicParams;
79     }
80
81     /**
82      * Returns a property map for a RestServerParameters map for test cases.
83      *
84      * @param isEmpty boolean value to represent that object created should be empty or not
85      * @return a property map suitable for constructing an object
86      */
87     public Map<String, Object> getRestServerParametersMap(final boolean isEmpty) {
88         final Map<String, Object> map = new TreeMap<>();
89         map.put(HTTPS_KEY, REST_SERVER_HTTPS);
90         map.put(AAF_KEY, REST_SERVER_AAF);
91
92         if (!isEmpty) {
93             map.put(HOST_KEY, REST_SERVER_HOST);
94             map.put(PORT_KEY, REST_SERVER_PORT);
95             map.put(USER_KEY, REST_SERVER_USER);
96             map.put(PASS_KEY, REST_SERVER_PASS);
97         }
98
99         return map;
100     }
101
102     /**
103      * Returns a property map for a RestServerParameters map for test cases.
104      *
105      * @param port the port for RestServer
106      * @return a property map suitable for constructing an object
107      */
108     public Map<String, Object> getRestServerParametersMap(final int port) {
109         final Map<String, Object> map = new TreeMap<>();
110         map.put(HTTPS_KEY, REST_SERVER_HTTPS);
111         map.put(AAF_KEY, REST_SERVER_AAF);
112         map.put(HOST_KEY, REST_SERVER_HOST);
113         map.put(PORT_KEY, port);
114         map.put(USER_KEY, REST_SERVER_USER);
115         map.put(PASS_KEY, REST_SERVER_PASS);
116
117         return map;
118     }
119
120     /**
121      * Converts the contents of a map to a parameter class.
122      *
123      * @param source property map
124      * @param clazz class of object to be created from the map
125      * @return a new object represented by the map
126      */
127     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
128         try {
129             return coder.decode(coder.encode(source), clazz);
130
131         } catch (final CoderException e) {
132             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
133         }
134     }
135
136     /**
137      * Returns a property map for a RestServerParameters map for test cases.
138      *
139      * @param isEmpty boolean value to represent that object created should be empty or not
140      * @return a property map suitable for constructing an object
141      */
142     public Map<String, Object> getPolicyApiParametersMap(final boolean isEmpty) {
143         final Map<String, Object> map = new TreeMap<>();
144         map.put(HTTPS_KEY, POLICY_API_HTTPS);
145         map.put(AAF_KEY, POLICY_API_AAF);
146
147         if (!isEmpty) {
148             map.put(HOST_KEY, POLICY_API_HOST);
149             map.put(PORT_KEY, POLICY_API_PORT);
150             map.put(USER_KEY, POLICY_API_USER);
151             map.put(PASS_KEY, POLICY_API_PASS);
152         }
153
154         return map;
155     }
156
157     /**
158      * Returns a property map for a TopicParameters map for test cases.
159      *
160      * @param isEmpty boolean value to represent that object created should be empty or not
161      * @return a property map suitable for constructing an object
162      */
163     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
164         final Map<String, Object> map = new TreeMap<>();
165         if (!isEmpty) {
166             map.put("topicSources", TOPIC_PARAMS);
167             map.put("topicSinks", TOPIC_PARAMS);
168         }
169         return map;
170     }
171 }