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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.pdp.comm;
23 import static org.junit.Assert.assertEquals;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
35 import java.util.Properties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
45 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
46 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
47 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
48 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
49 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
50 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
54 * Class to perform unit test of {@link PdpUpdateListener}.
56 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
58 public class TestPdpUpdateListener {
59 private PdpUpdateListener pdpUpdateMessageListener;
60 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
61 private static final String TOPIC = "my-topic";
62 private PdpSimulatorActivator activator;
65 * Method for setup before each test.
67 * @throws PdpSimulatorException if some error occurs while starting up the pdp simulator
68 * @throws FileNotFoundException if the file is missing
69 * @throws IOException if IO exception occurs
72 public void setUp() throws PdpSimulatorException, FileNotFoundException, IOException {
73 Registry.newRegistry();
74 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json",
75 "-p", "src/test/resources/topic.properties" };
76 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
77 PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
78 // The arguments return a string if there is a message to print and we should
80 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
81 if (argumentMessage != null) {
84 // Validate that the arguments are sane
87 // Read the parameters
88 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
90 // Read the properties
91 final Properties topicProperties = new Properties();
92 final String propFile = arguments.getFullPropertyFilePath();
93 try (FileInputStream stream = new FileInputStream(propFile)) {
94 topicProperties.load(stream);
96 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties);
97 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
98 activator.initialize();
99 pdpUpdateMessageListener = new PdpUpdateListener();
103 * Method for cleanup after each test.
105 * @throws Exception if an error occurs
108 public void teardown() throws Exception {
110 // clear the pdp simulator activator
111 if (activator != null && activator.isAlive()) {
112 activator.terminate();
117 public void testPdpUpdateMssageListener() {
118 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
119 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
120 pdpUpdateMsg.setDescription("dummy pdp status for test");
121 pdpUpdateMsg.setPdpGroup("pdpGroup");
122 pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
123 pdpUpdateMsg.setName(pdpStatus.getName());
124 pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
125 final ToscaPolicy toscaPolicy = new ToscaPolicy();
126 toscaPolicy.setType("apexpolicytype");
127 toscaPolicy.setVersion("1.0");
128 toscaPolicy.setName("apex policy name");
129 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
132 properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
133 StandardCharsets.UTF_8);
134 propertiesMap.put("content", properties);
135 } catch (final IOException e) {
136 propertiesMap.put("content", "");
138 toscaPolicy.setProperties(propertiesMap);
139 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
140 toscaPolicies.add(toscaPolicy);
141 pdpUpdateMsg.setPolicies(toscaPolicies);
142 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
143 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
144 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
145 assertEquals(pdpStatus.getPolicies(),
146 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));