aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java
blob: 21475da8530a520a3a6b6756536bc574acd7b1f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pdpx.main.parameters;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.onap.policy.common.endpoints.parameters.TopicParameters;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;

/**
 * Class to hold/create all parameters for test cases.
 *
 */
public class CommonTestData {

    private static final String PASS_KEY = "password";
    private static final String USER_KEY = "userName";
    private static final String PORT_KEY = "port";
    private static final String SERVER_HOST_KEY = "host";
    private static final String API_HOST_KEY = "hostname";
    private static final String AAF_KEY = "aaf";
    private static final String HTTPS_KEY = "useHttps";

    private static final String REST_SERVER_PASS = "zb!XztG34";
    private static final String REST_SERVER_USER = "healthcheck";
    private static final int REST_SERVER_PORT = 6969;
    private static final String REST_SERVER_HOST = "0.0.0.0";
    private static final boolean REST_SERVER_HTTPS = false;
    private static final boolean REST_SERVER_AAF = false;

    private static final String POLICY_API_PASS = "zb!XztG34";
    private static final String POLICY_API_USER = "healthcheck";
    private static final int POLICY_API_PORT = 6970;
    private static final String POLICY_API_HOST = "0.0.0.0";
    private static final boolean POLICY_API_HTTPS = false;
    private static final boolean POLICY_API_AAF = false;

    public static final String PDPX_PARAMETER_GROUP_NAME = "XacmlPdpParameters";
    public static final String PDPX_GROUP = "XacmlPdpGroup";
    public static final String PDPX_TYPE = "xacml";
    public static final List<TopicParameters> TOPIC_PARAMS =
                    Collections.unmodifiableList(Arrays.asList(getTopicParams()));

    public static final Coder coder = new StandardCoder();

    /**
     * Returns topic parameters for test cases.
     *
     * @return topic parameters
     */
    public static TopicParameters getTopicParams() {
        final TopicParameters topicParams = new TopicParameters();
        topicParams.setTopic("POLICY-PDP-PAP");
        topicParams.setTopicCommInfrastructure("noop");
        topicParams.setServers(Arrays.asList("message-router"));
        return topicParams;
    }

    /**
     * Returns a property map for a RestServerParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getRestServerParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        map.put(HTTPS_KEY, REST_SERVER_HTTPS);
        map.put(AAF_KEY, REST_SERVER_AAF);

        if (!isEmpty) {
            map.put(SERVER_HOST_KEY, REST_SERVER_HOST);
            map.put(PORT_KEY, REST_SERVER_PORT);
            map.put(USER_KEY, REST_SERVER_USER);
            map.put(PASS_KEY, REST_SERVER_PASS);
        }

        return map;
    }

    /**
     * Returns a property map for a RestServerParameters map for test cases.
     *
     * @param port the port for RestServer
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getRestServerParametersMap(final int port) {
        final Map<String, Object> map = new TreeMap<>();
        map.put(HTTPS_KEY, REST_SERVER_HTTPS);
        map.put(AAF_KEY, REST_SERVER_AAF);
        map.put(SERVER_HOST_KEY, REST_SERVER_HOST);
        map.put(PORT_KEY, port);
        map.put(USER_KEY, REST_SERVER_USER);
        map.put(PASS_KEY, REST_SERVER_PASS);

        return map;
    }

    /**
     * Converts the contents of a map to a parameter class.
     *
     * @param source property map
     * @param clazz class of object to be created from the map
     * @return a new object represented by the map
     */
    public <T> T toObject(final Map<String, Object> source, final Class<T> clazz) {
        try {
            return coder.decode(coder.encode(source), clazz);

        } catch (final CoderException e) {
            throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
        }
    }

    /**
     * Returns a property map for a RestServerParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getPolicyApiParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        map.put(HTTPS_KEY, POLICY_API_HTTPS);
        map.put(AAF_KEY, POLICY_API_AAF);

        if (!isEmpty) {
            map.put(API_HOST_KEY, POLICY_API_HOST);
            map.put(PORT_KEY, POLICY_API_PORT);
            map.put(USER_KEY, POLICY_API_USER);
            map.put(PASS_KEY, POLICY_API_PASS);
        }

        return map;
    }

    /**
     * Returns a property map for a TopicParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        if (!isEmpty) {
            map.put("topicSources", TOPIC_PARAMS);
            map.put("topicSinks", TOPIC_PARAMS);
        }
        return map;
    }

    /**
     * Returns a property map for a XacmlApplicationParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @param tempPath Application Path string
     * @param exclusions An optional list of application classnames for exclusion
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getXacmlapplicationParametersMap(boolean isEmpty, String tempPath,
            String... exclusions) {
        final Map<String, Object> map = new TreeMap<>();
        if (!isEmpty) {
            map.put("applicationPath", tempPath);
            if (exclusions != null) {
                map.put("exclusions", List.of(exclusions));
            }
        }
        return map;
    }
}