Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-onappf / src / test / java / org / onap / policy / apex / services / onappf / comm / TestListenerUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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.comm;
22
23 import com.google.gson.JsonObject;
24 import java.io.File;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.models.pdp.concepts.PdpStateChange;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35
36 public class TestListenerUtils {
37
38     /**
39      * Method to create PdpUpdate message from the arguments passed.
40      *
41      * @param pdpStatus pdp status
42      * @param toscaPolicies list of tosca policies
43      *
44      * @return PdpUpdate message
45      */
46     public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPolicies) {
47         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
48         pdpUpdateMsg.setDescription("dummy pdp status for test");
49         pdpUpdateMsg.setPdpGroup("pdpGroup");
50         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
51         pdpUpdateMsg.setName(pdpStatus.getName());
52         pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
53         pdpUpdateMsg.setPolicies(toscaPolicies);
54         return pdpUpdateMsg;
55     }
56
57     /**
58      * Method to create ToscaPolicy using the arguments passed.
59      *
60      * @param policyName the name of the policy
61      * @param policyVersion the version of the policy
62      * @param policyFilePath the path of the policy content
63      *
64      * @return PdpUpdate message
65      * @throws CoderException exception while reading the file to object
66      */
67     public static ToscaPolicy createToscaPolicy(String policyName, String policyVersion, String policyFilePath)
68         throws CoderException {
69         final ToscaPolicy toscaPolicy = new ToscaPolicy();
70         toscaPolicy.setType("apexpolicytype");
71         toscaPolicy.setVersion(policyVersion);
72         toscaPolicy.setName(policyName);
73         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
74         JsonObject properties = new StandardCoder().decode(new File(policyFilePath), JsonObject.class);
75         properties.entrySet().forEach(entry -> {
76             propertiesMap.put(entry.getKey(), entry.getValue());
77         });
78         toscaPolicy.setProperties(propertiesMap);
79         return toscaPolicy;
80     }
81
82     /**
83      * Method to create PdpStateChange message from the arguments passed.
84      *
85      * @param state the new pdp state
86      * @param pdpGroup name of the pdpGroup
87      * @param pdpSubgroup name of the pdpSubGroup
88      * @param name the name of the message
89      *
90      * @return PdpStateChange message
91      */
92     public static PdpStateChange createPdpStateChangeMsg(PdpState state, String pdpGroup, String pdpSubgroup,
93         String name) {
94         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
95         pdpStateChangeMsg.setState(state);
96         pdpStateChangeMsg.setPdpGroup(pdpGroup);
97         pdpStateChangeMsg.setPdpSubgroup(pdpSubgroup);
98         pdpStateChangeMsg.setName(name);
99         return pdpStateChangeMsg;
100     }
101 }