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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.pdp.comm;
24 import static org.junit.Assert.assertEquals;
26 import java.io.FileInputStream;
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;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
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;
52 * Class to perform unit test of {@link PdpUpdateListener}.
54 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
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;
63 * Method for setup before each test.
65 * @throws Exception if an error occurs
68 public void setUp() throws Exception {
69 Registry.newRegistry();
70 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json",
71 "-p", "src/test/resources/topic.properties" };
72 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
73 PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
74 // The arguments return a string if there is a message to print and we should
76 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
77 if (argumentMessage != null) {
80 // Validate that the arguments are sane
83 // Read the parameters
84 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
86 // Read the properties
87 final Properties topicProperties = new Properties();
88 final String propFile = arguments.getFullPropertyFilePath();
89 try (FileInputStream stream = new FileInputStream(propFile)) {
90 topicProperties.load(stream);
92 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties);
93 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
94 activator.initialize();
95 pdpUpdateMessageListener = new PdpUpdateListener();
99 * Method for cleanup after each test.
101 * @throws Exception if an error occurs
104 public void teardown() throws Exception {
106 // clear the pdp simulator activator
107 if (activator != null && activator.isAlive()) {
108 activator.terminate();
113 public void testPdpUpdateMssageListener() {
114 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
115 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
116 pdpUpdateMsg.setDescription("dummy pdp status for test");
117 pdpUpdateMsg.setPdpGroup("pdpGroup");
118 pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
119 pdpUpdateMsg.setName(pdpStatus.getName());
120 pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
121 final ToscaPolicy toscaPolicy = new ToscaPolicy();
122 toscaPolicy.setType("apexpolicytype");
123 toscaPolicy.setVersion("1.0");
124 toscaPolicy.setName("apex policy name");
125 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
128 properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
129 StandardCharsets.UTF_8);
130 propertiesMap.put("content", properties);
131 } catch (final IOException e) {
132 propertiesMap.put("content", "");
134 toscaPolicy.setProperties(propertiesMap);
135 final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
136 toscaPolicies.add(toscaPolicy);
137 pdpUpdateMsg.setPolicies(toscaPolicies);
138 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
139 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
140 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
141 assertEquals(pdpStatus.getPolicies(),
142 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));