e4946dd2b5c89fc6a471c3971a6da95a8cc4ee25
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27
28 import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters;
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
34 /**
35  * Class to hold/create all parameters for test cases.
36  *
37  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
38  */
39 public class CommonTestData {
40
41     public static final String APEX_STARTER_GROUP_NAME = "ApexStarterParameterGroup";
42     public static final long TIME_INTERVAL = 2000;
43     public static final String PDP_NAME = "apex-pdp";
44     public static final String VERSION = "0.0.1";
45     public static final String PDP_TYPE = "apex";
46     public static final String DESCRIPTION = "Pdp status for HealthCheck";
47     public static final String POLICY_NAME = "onap.controllloop.operational.apex.BBS";
48     public static final String POLICY_VERSION = "0.0.1";
49     public static final List<ToscaPolicyTypeIdentifierParameters> SUPPORTED_POLICY_TYPES =
50             Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION));
51
52     public static final Coder coder = new StandardCoder();
53
54     /**
55      * Returns supported policy types for test cases.
56      *
57      * @return supported policy types
58      */
59     public static ToscaPolicyTypeIdentifierParameters getSupportedPolicyTypes(final String name, final String version) {
60         final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters = new ToscaPolicyTypeIdentifierParameters();
61         policyTypeIdentParameters.setName(name);
62         policyTypeIdentParameters.setVersion(version);
63         return policyTypeIdentParameters;
64     }
65
66     /**
67      * Converts the contents of a map to a parameter class.
68      *
69      * @param source property map
70      * @param clazz class of object to be created from the map
71      * @return a new object represented by the map
72      */
73     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
74         try {
75             return coder.decode(coder.encode(source), clazz);
76
77         } catch (final CoderException e) {
78             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
79         }
80     }
81
82     /**
83      * Returns a property map for a ApexStarterParameterGroup map for test cases.
84      *
85      * @param name name of the parameters
86      *
87      * @return a property map suitable for constructing an object
88      */
89     public Map<String, Object> getApexStarterParameterGroupMap(final String name) {
90         final Map<String, Object> map = new TreeMap<>();
91
92         map.put("name", name);
93         map.put("pdpStatusParameters", getPdpStatusParametersMap(false));
94
95         return map;
96     }
97
98     /**
99      * Returns a property map for a PdpStatusParameters map for test cases.
100      *
101      * @param isEmpty boolean value to represent that object created should be empty or not
102      * @return a property map suitable for constructing an object
103      */
104     public Map<String, Object> getPdpStatusParametersMap(final boolean isEmpty) {
105         final Map<String, Object> map = new TreeMap<>();
106         if (!isEmpty) {
107             map.put("timeIntervalMs", TIME_INTERVAL);
108             map.put("pdpName", PDP_NAME);
109             map.put("version", VERSION);
110             map.put("pdpType", PDP_TYPE);
111             map.put("description", DESCRIPTION);
112             map.put("supportedPolicyTypes", SUPPORTED_POLICY_TYPES);
113         }
114
115         return map;
116     }
117 }