summaryrefslogtreecommitdiffstats
path: root/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java
blob: 19af43fc87d20e05c7ba5a68308b4ea67bb9024f (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019-2020 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.apex.services.onappf.comm;

import com.google.gson.JsonObject;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;

public class TestListenerUtils {

    /**
     * Method to create PdpUpdate message from the arguments passed.
     *
     * @param pdpStatus pdp status
     * @param toscaPolicies list of tosca policies
     *
     * @return PdpUpdate message
     */
    public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPolicies) {
        final PdpUpdate pdpUpdateMsg = new PdpUpdate();
        pdpUpdateMsg.setDescription("dummy pdp status for test");
        pdpUpdateMsg.setPdpGroup("pdpGroup");
        pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
        pdpUpdateMsg.setName(pdpStatus.getName());
        pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
        pdpUpdateMsg.setPolicies(toscaPolicies);
        return pdpUpdateMsg;
    }

    /**
     * Method to create ToscaPolicy using the arguments passed.
     *
     * @param policyName the name of the policy
     * @param policyVersion the version of the policy
     * @param policyFilePath the path of the policy content
     *
     * @return PdpUpdate message
     * @throws CoderException exception while reading the file to object
     */
    public static ToscaPolicy createToscaPolicy(String policyName, String policyVersion, String policyFilePath)
        throws CoderException {
        final ToscaPolicy toscaPolicy = new ToscaPolicy();
        toscaPolicy.setType("apexpolicytype");
        toscaPolicy.setVersion(policyVersion);
        toscaPolicy.setName(policyName);
        final Map<String, Object> propertiesMap = new LinkedHashMap<>();
        JsonObject properties = new StandardCoder().decode(new File(policyFilePath), JsonObject.class);
        properties.entrySet().forEach(entry -> {
            propertiesMap.put(entry.getKey(), entry.getValue());
        });
        toscaPolicy.setProperties(propertiesMap);
        return toscaPolicy;
    }

    /**
     * Method to create PdpStateChange message from the arguments passed.
     *
     * @param state the new pdp state
     * @param pdpGroup name of the pdpGroup
     * @param pdpSubgroup name of the pdpSubGroup
     * @param name the name of the message
     *
     * @return PdpStateChange message
     */
    public static PdpStateChange createPdpStateChangeMsg(PdpState state, String pdpGroup, String pdpSubgroup,
        String name) {
        final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
        pdpStateChangeMsg.setState(state);
        pdpStateChangeMsg.setPdpGroup(pdpGroup);
        pdpStateChangeMsg.setPdpSubgroup(pdpSubgroup);
        pdpStateChangeMsg.setName(name);
        return pdpStateChangeMsg;
    }
}