9ae8d370a1b1271b17176e323a5e4d208d309483
[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 java.util.Properties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.utils.ParameterUtils;
40 import org.onap.policy.common.utils.services.Registry;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.models.pdp.concepts.PdpUpdate;
43 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
44 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
45 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
46 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
47 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
48 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50
51 /**
52  * Class to perform unit test of {@link PdpUpdateListener}.
53  *
54  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
55  */
56 public class TestPdpUpdateListener {
57     private PdpUpdateListener pdpUpdateMessageListener;
58     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59     private static final String TOPIC = "my-topic";
60     private PdpSimulatorActivator activator;
61
62     /**
63      * Method for setup before each test.
64      *
65      * @throws Exception if an error occurs
66      */
67     @Before
68     public void setUp() throws Exception {
69         Registry.newRegistry();
70         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
71         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
72         PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
73         // The arguments return a string if there is a message to print and we should
74         // exit
75         final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
76         if (argumentMessage != null) {
77             return;
78         }
79         // Validate that the arguments are sane
80         arguments.validate();
81
82         // Read the parameters
83         pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
84
85         // Read the properties
86         final Properties topicProperties =
87             ParameterUtils.getTopicProperties(pdpSimulatorParameterGroup.getTopicParameterGroup());
88         activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties);
89         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
90         activator.initialize();
91         pdpUpdateMessageListener = new PdpUpdateListener();
92     }
93
94     /**
95      * Method for cleanup after each test.
96      *
97      * @throws Exception if an error occurs
98      */
99     @After
100     public void teardown() throws Exception {
101
102         // clear the pdp simulator activator
103         if (activator != null && activator.isAlive()) {
104             activator.terminate();
105         }
106     }
107
108     @Test
109     public void testPdpUpdateMssageListener() {
110         final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
111         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
112         pdpUpdateMsg.setDescription("dummy pdp status for test");
113         pdpUpdateMsg.setPdpGroup("pdpGroup");
114         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
115         pdpUpdateMsg.setName(pdpStatus.getName());
116         pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
117         final ToscaPolicy toscaPolicy = new ToscaPolicy();
118         toscaPolicy.setType("apexpolicytype");
119         toscaPolicy.setVersion("1.0");
120         toscaPolicy.setName("apex policy name");
121         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
122         String properties;
123         try {
124             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
125                     StandardCharsets.UTF_8);
126             propertiesMap.put("content", properties);
127         } catch (final IOException e) {
128             propertiesMap.put("content", "");
129         }
130         toscaPolicy.setProperties(propertiesMap);
131         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
132         toscaPolicies.add(toscaPolicy);
133         pdpUpdateMsg.setPolicies(toscaPolicies);
134         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
135         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
136         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
137         assertEquals(pdpStatus.getPolicies(),
138                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
139     }
140 }