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 org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.utils.services.Registry;
39 import org.onap.policy.models.pdp.concepts.PdpStateChange;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.pdp.enums.PdpState;
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.parameters.PdpSimulatorParameterGroup;
47 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 * Class to perform unit test of {@link PdpStateChangeListener}.
53 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
55 public class TestPdpStateChangeListener {
56 private static final String PDP_SUBGROUP = "pdpSubgroup";
57 private static final String PDP_GROUP = "pdpGroup";
58 private PdpUpdateListener pdpUpdateMessageListener;
59 private PdpStateChangeListener pdpStateChangeListener;
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 Exception if an error occurs
70 public void setUp() throws Exception {
71 pdpUpdateMessageListener = new PdpUpdateListener();
72 pdpStateChangeListener = new PdpStateChangeListener();
73 Registry.newRegistry();
74 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
75 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
76 PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
77 // The arguments return a string if there is a message to print and we should
79 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
80 if (argumentMessage != null) {
83 // Validate that the arguments are sane
86 // Read the parameters
87 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
89 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup);
90 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
91 activator.initialize();
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 * Method to initiate a PdpUpdate.
111 * @param instance the instance id
112 * @return PdpUpdate the pdp update message
114 private PdpUpdate performPdpUpdate(final String instance) {
115 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
116 pdpUpdateMsg.setDescription("dummy pdp status for test");
117 pdpUpdateMsg.setPdpGroup(PDP_GROUP);
118 pdpUpdateMsg.setPdpSubgroup(PDP_SUBGROUP);
119 pdpUpdateMsg.setName(instance);
120 final ToscaPolicy toscaPolicy = new ToscaPolicy();
121 toscaPolicy.setType("apexpolicytype");
122 toscaPolicy.setVersion("1.0");
123 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
127 properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
128 StandardCharsets.UTF_8);
129 propertiesMap.put("content", properties);
130 } catch (final IOException e) {
131 propertiesMap.put("content", "");
133 toscaPolicy.setProperties(propertiesMap);
134 final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
135 toscaPolicies.add(toscaPolicy);
136 pdpUpdateMsg.setPolicies(toscaPolicies);
137 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
142 public void testPdpStateChangeMessageListener_passivetopassive() {
143 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
144 performPdpUpdate(pdpStatus.getName());
145 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
146 pdpStateChangeMsg.setState(PdpState.PASSIVE);
147 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
148 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
149 pdpStateChangeMsg.setName(pdpStatus.getName());
150 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
152 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
156 public void testPdpStateChangeMessageListener_activetoactive() {
157 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
158 performPdpUpdate(pdpStatus.getName());
159 pdpStatus.setState(PdpState.ACTIVE);
160 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
161 pdpStateChangeMsg.setState(PdpState.ACTIVE);
162 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
163 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
164 pdpStateChangeMsg.setName(pdpStatus.getName());
165 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
167 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());