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.PdpStateChange;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
46 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
47 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
48 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
49 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 * Class to perform unit test of {@link PdpStateChangeListener}.
55 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
57 public class TestPdpStateChangeListener {
58 private static final String PDP_SUBGROUP = "pdpSubgroup";
59 private static final String PDP_GROUP = "pdpGroup";
60 private PdpUpdateListener pdpUpdateMessageListener;
61 private PdpStateChangeListener pdpStateChangeListener;
62 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
63 private static final String TOPIC = "my-topic";
64 private PdpSimulatorActivator activator;
67 * Method for setup before each test.
69 * @throws Exception if an error occurs
72 public void setUp() throws Exception {
73 pdpUpdateMessageListener = new PdpUpdateListener();
74 pdpStateChangeListener = new PdpStateChangeListener();
75 Registry.newRegistry();
76 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json",
77 "-p", "src/test/resources/topic.properties" };
78 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
79 PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
80 // The arguments return a string if there is a message to print and we should
82 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
83 if (argumentMessage != null) {
86 // Validate that the arguments are sane
89 // Read the parameters
90 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
92 // Read the properties
93 final Properties topicProperties = new Properties();
94 final String propFile = arguments.getFullPropertyFilePath();
95 try (FileInputStream stream = new FileInputStream(propFile)) {
96 topicProperties.load(stream);
98 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties);
99 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
100 activator.initialize();
104 * Method for cleanup after each test.
106 * @throws Exception if an error occurs
109 public void teardown() throws Exception {
111 // clear the pdp simulator activator
112 if (activator != null && activator.isAlive()) {
113 activator.terminate();
118 * Method to initiate a PdpUpdate.
120 * @param instance the instance id
121 * @return PdpUpdate the pdp update message
123 private PdpUpdate performPdpUpdate(final String instance) {
124 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
125 pdpUpdateMsg.setDescription("dummy pdp status for test");
126 pdpUpdateMsg.setPdpGroup(PDP_GROUP);
127 pdpUpdateMsg.setPdpSubgroup(PDP_SUBGROUP);
128 pdpUpdateMsg.setName(instance);
129 final ToscaPolicy toscaPolicy = new ToscaPolicy();
130 toscaPolicy.setType("apexpolicytype");
131 toscaPolicy.setVersion("1.0");
132 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
136 properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
137 StandardCharsets.UTF_8);
138 propertiesMap.put("content", properties);
139 } catch (final IOException e) {
140 propertiesMap.put("content", "");
142 toscaPolicy.setProperties(propertiesMap);
143 final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
144 toscaPolicies.add(toscaPolicy);
145 pdpUpdateMsg.setPolicies(toscaPolicies);
146 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
151 public void testPdpStateChangeMessageListener_passivetopassive() {
152 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
153 performPdpUpdate(pdpStatus.getName());
154 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
155 pdpStateChangeMsg.setState(PdpState.PASSIVE);
156 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
157 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
158 pdpStateChangeMsg.setName(pdpStatus.getName());
159 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
161 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
165 public void testPdpStateChangeMessageListener_activetoactive() {
166 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
167 performPdpUpdate(pdpStatus.getName());
168 pdpStatus.setState(PdpState.ACTIVE);
169 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
170 pdpStateChangeMsg.setState(PdpState.ACTIVE);
171 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
172 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
173 pdpStateChangeMsg.setName(pdpStatus.getName());
174 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
176 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());