2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2024 Nordix Foundation
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.sim.pdp.comm;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
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 org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.utils.services.Registry;
40 import org.onap.policy.models.pdp.concepts.PdpStateChange;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.models.pdp.concepts.PdpUpdate;
43 import org.onap.policy.models.pdp.enums.PdpState;
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.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 PdpStateChangeListener}.
54 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
56 class TestPdpStateChangeListener {
57 private static final String PDP_SUBGROUP = "pdpSubgroup";
58 private static final String PDP_GROUP = "pdpGroup";
59 private PdpUpdateListener pdpUpdateMessageListener;
60 private PdpStateChangeListener pdpStateChangeListener;
61 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
62 private static final String TOPIC = "my-topic";
63 private PdpSimulatorActivator activator;
66 * Method for setup before each test.
68 * @throws Exception if an error occurs
71 void setUp() throws Exception {
72 pdpUpdateMessageListener = new PdpUpdateListener();
73 pdpStateChangeListener = new PdpStateChangeListener();
74 Registry.newRegistry();
75 final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
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 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup);
91 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
92 activator.initialize();
96 * Method for cleanup after each test.
98 * @throws Exception if an error occurs
101 void teardown() throws Exception {
103 // clear the pdp simulator activator
104 if (activator != null && activator.isAlive()) {
105 activator.terminate();
110 * Method to initiate a PdpUpdate.
112 * @param instance the instance id
113 * @return PdpUpdate the pdp update message
115 private PdpUpdate performPdpUpdate(final String instance) {
116 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
117 pdpUpdateMsg.setDescription("dummy pdp status for test");
118 pdpUpdateMsg.setPdpGroup(PDP_GROUP);
119 pdpUpdateMsg.setPdpSubgroup(PDP_SUBGROUP);
120 pdpUpdateMsg.setName(instance);
121 final ToscaPolicy toscaPolicy = new ToscaPolicy();
122 toscaPolicy.setType("apexpolicytype");
123 toscaPolicy.setVersion("1.0");
124 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.setPoliciesToBeDeployed(toscaPolicies);
138 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
143 void testPdpStateChangeMessageListener_passivetopassive() {
144 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
145 performPdpUpdate(pdpStatus.getName());
146 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
147 pdpStateChangeMsg.setState(PdpState.PASSIVE);
148 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
149 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
150 pdpStateChangeMsg.setName(pdpStatus.getName());
151 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
153 assertEquals(pdpStateChangeMsg.getState(), pdpStatus.getState());
157 void testPdpStateChangeMessageListener_activetoactive() {
158 final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
159 performPdpUpdate(pdpStatus.getName());
160 pdpStatus.setState(PdpState.ACTIVE);
161 final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
162 pdpStateChangeMsg.setState(PdpState.ACTIVE);
163 pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
164 pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
165 pdpStateChangeMsg.setName(pdpStatus.getName());
166 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
168 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());