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.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;
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;
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 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
72 PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
73 // The arguments return a string if there is a message to print and we should
75 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
76 if (argumentMessage != null) {
79 // Validate that the arguments are sane
82 // Read the parameters
83 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
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();
95 * Method for cleanup after each test.
97 * @throws Exception if an error occurs
100 public void teardown() throws Exception {
102 // clear the pdp simulator activator
103 if (activator != null && activator.isAlive()) {
104 activator.terminate();
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<>();
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", "");
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()));