Remove policies field from PdpUpdate message
[policy/models.git] / models-sim / policy-models-sim-pdp / src / test / java / org / onap / policy / models / sim / pdp / comm / TestPdpUpdateListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp.comm;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.utils.services.Registry;
39 import org.onap.policy.models.pdp.concepts.PdpStatus;
40 import org.onap.policy.models.pdp.concepts.PdpUpdate;
41 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
42 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
43 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
44 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
45 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
46 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48
49 /**
50  * Class to perform unit test of {@link PdpUpdateListener}.
51  *
52  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
53  */
54 public class TestPdpUpdateListener {
55     private PdpUpdateListener pdpUpdateMessageListener;
56     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
57     private static final String TOPIC = "my-topic";
58     private PdpSimulatorActivator activator;
59
60     /**
61      * Method for setup before each test.
62      *
63      * @throws Exception if an error occurs
64      */
65     @Before
66     public void setUp() throws Exception {
67         Registry.newRegistry();
68         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
69         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
70         PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
71         // The arguments return a string if there is a message to print and we should
72         // exit
73         final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
74         if (argumentMessage != null) {
75             return;
76         }
77         // Validate that the arguments are sane
78         arguments.validate();
79
80         // Read the parameters
81         pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
82
83         activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup);
84         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
85         activator.initialize();
86         pdpUpdateMessageListener = new PdpUpdateListener();
87     }
88
89     /**
90      * Method for cleanup after each test.
91      *
92      * @throws Exception if an error occurs
93      */
94     @After
95     public void teardown() throws Exception {
96
97         // clear the pdp simulator activator
98         if (activator != null && activator.isAlive()) {
99             activator.terminate();
100         }
101     }
102
103     @Test
104     public void testPdpUpdateMssageListener() {
105         final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
106         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
107         pdpUpdateMsg.setDescription("dummy pdp status for test");
108         pdpUpdateMsg.setPdpGroup("pdpGroup");
109         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
110         pdpUpdateMsg.setName(pdpStatus.getName());
111         pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
112         final ToscaPolicy toscaPolicy = new ToscaPolicy();
113         toscaPolicy.setType("apexpolicytype");
114         toscaPolicy.setVersion("1.0");
115         toscaPolicy.setName("apex policy name");
116         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
117         String properties;
118         try {
119             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
120                     StandardCharsets.UTF_8);
121             propertiesMap.put("content", properties);
122         } catch (final IOException e) {
123             propertiesMap.put("content", "");
124         }
125         toscaPolicy.setProperties(propertiesMap);
126         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
127         toscaPolicies.add(toscaPolicy);
128         pdpUpdateMsg.setPoliciesToBeDeployed(toscaPolicies);
129         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
130         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
131         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
132         assertEquals(pdpStatus.getPolicies(),
133                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
134     }
135 }