099663af9b017acd64af4862cda8d2f5af4eafe1
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023-2024 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.apex.services.onappf.parameters;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.TreeMap;
26 import org.onap.policy.common.endpoints.parameters.TopicParameters;
27 import org.onap.policy.common.parameters.ParameterGroup;
28 import org.onap.policy.common.utils.coder.Coder;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31
32 /**
33  * Class to hold/create all parameters for test cases.
34  *
35  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
36  */
37 public class CommonTestData {
38
39     public static final String APEX_STARTER_GROUP_NAME = "ApexStarterParameterGroup";
40     public static final long TIME_INTERVAL = 2000;
41     public static final String PDP_NAME = "apex-pdp";
42     public static final String VERSION = "0.0.1";
43     public static final String PDP_TYPE = "apex";
44     public static final String PDP_GROUP = "defaultGroup";
45     public static final String DESCRIPTION = "Pdp status for HealthCheck";
46     public static final String POLICY_NAME = "onap.controllloop.operational.apex.BBS";
47     public static final String POLICY_VERSION = "0.0.1";
48     public static final List<ToscaPolicyTypeIdentifierParameters> SUPPORTED_POLICY_TYPES =
49         List.of(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION));
50     public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
51     private static final String REST_SERVER_PASSWORD = "zb!XztG34";
52     private static final String REST_SERVER_USER = "healthcheck";
53     private static final int REST_SERVER_PORT = 6969;
54     private static final String REST_SERVER_HOST = "0.0.0.0";
55     private static final boolean REST_SERVER_HTTPS = true;
56     private static final boolean REST_SERVER_AAF = false;
57
58     public static final Coder coder = new StandardCoder();
59
60     /**
61      * Returns supported policy types for test cases.
62      *
63      * @return supported policy types
64      */
65     public static ToscaPolicyTypeIdentifierParameters getSupportedPolicyTypes(final String name, final String version) {
66         final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters = new ToscaPolicyTypeIdentifierParameters();
67         policyTypeIdentParameters.setName(name);
68         policyTypeIdentParameters.setVersion(version);
69         return policyTypeIdentParameters;
70     }
71
72     /**
73      * Returns topic parameters for test cases.
74      *
75      * @return topic parameters
76      */
77     public static TopicParameters getTopicParams() {
78         final TopicParameters topicParams = new TopicParameters();
79         topicParams.setTopic("policy-pdp-pap");
80         topicParams.setTopicCommInfrastructure("noop");
81         topicParams.setServers(List.of("localhost"));
82         return topicParams;
83     }
84
85     /**
86      * Converts the contents of a map to a parameter class.
87      *
88      * @param source property map
89      * @param clazz  class of object to be created from the map
90      * @return a new object represented by the map
91      */
92     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
93         try {
94             return coder.decode(coder.encode(source), clazz);
95
96         } catch (final CoderException e) {
97             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
98         }
99     }
100
101     /**
102      * Returns a property map for a ApexStarterParameterGroup map for test cases.
103      *
104      * @param name name of the parameters
105      * @return a property map suitable for constructing an object
106      */
107     public Map<String, Object> getApexStarterParameterGroupMap(final String name) {
108         final Map<String, Object> map = new TreeMap<>();
109
110         map.put("name", name);
111         map.put("restServerParameters", getRestServerParametersMap(false));
112         map.put("pdpStatusParameters", getPdpStatusParametersMap(false));
113         map.put("topicParameterGroup", getTopicParametersMap(false));
114         return map;
115     }
116
117     /**
118      * Returns a property map for a RestServerParameters map for test cases.
119      *
120      * @param isEmpty boolean value to represent that object created should be empty or not
121      * @return a property map suitable for constructing an object
122      */
123     public Map<String, Object> getRestServerParametersMap(final boolean isEmpty) {
124         final Map<String, Object> map = new TreeMap<>();
125         map.put("https", REST_SERVER_HTTPS);
126         map.put("aaf", REST_SERVER_AAF);
127
128         if (!isEmpty) {
129             map.put("host", REST_SERVER_HOST);
130             map.put("port", REST_SERVER_PORT);
131             map.put("userName", REST_SERVER_USER);
132             map.put("password", REST_SERVER_PASSWORD);
133         }
134
135         return map;
136     }
137
138     /**
139      * Returns a property map for a PdpStatusParameters map for test cases.
140      *
141      * @param isEmpty boolean value to represent that object created should be empty or not
142      * @return a property map suitable for constructing an object
143      */
144     public Map<String, Object> getPdpStatusParametersMap(final boolean isEmpty) {
145         final Map<String, Object> map = new TreeMap<>();
146         if (!isEmpty) {
147             map.put("pdpGroup", PDP_GROUP);
148             map.put("timeIntervalMs", TIME_INTERVAL);
149             map.put("pdpName", PDP_NAME);
150             map.put("version", VERSION);
151             map.put("pdpType", PDP_TYPE);
152             map.put("description", DESCRIPTION);
153             map.put("supportedPolicyTypes", SUPPORTED_POLICY_TYPES);
154         }
155
156         return map;
157     }
158
159     /**
160      * Returns a property map for a TopicParameters map for test cases.
161      *
162      * @param isEmpty boolean value to represent that object created should be empty or not
163      * @return a property map suitable for constructing an object
164      */
165     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
166         final Map<String, Object> map = new TreeMap<>();
167         if (!isEmpty) {
168             map.put("topicSources", TOPIC_PARAMS);
169             map.put("topicSinks", TOPIC_PARAMS);
170         }
171         return map;
172     }
173 }