2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2024 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
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.apex.services.onappf.comm;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import java.io.ByteArrayOutputStream;
32 import java.io.OutputStream;
33 import java.io.PrintStream;
34 import java.util.ArrayList;
35 import java.util.List;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
40 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
41 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
42 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
43 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
44 import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler;
45 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
46 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
47 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
48 import org.onap.policy.common.utils.cmd.CommandLineException;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.pdp.concepts.PdpStateChange;
52 import org.onap.policy.models.pdp.concepts.PdpStatus;
53 import org.onap.policy.models.pdp.concepts.PdpUpdate;
54 import org.onap.policy.models.pdp.enums.PdpState;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
58 * Class to perform unit test of {@link PdpStateChangeListener}.
60 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
62 class TestPdpStateChangeListener {
63 private PdpUpdateListener pdpUpdateMessageListener;
64 private PdpStateChangeListener pdpStateChangeListener;
65 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
66 private static final String TOPIC = "my-topic";
67 private ApexStarterActivator activator;
68 private ApexEngineHandler apexEngineHandler;
69 private final PrintStream stdout = System.out;
72 * Method for setup before each test.
74 * @throws ApexStarterException if some error occurs while starting up the apex starter
75 * @throws CommandLineException if any parsing of args has errors
78 void setUp() throws ApexStarterException, CommandLineException {
79 pdpUpdateMessageListener = new PdpUpdateListener();
80 pdpStateChangeListener = new PdpStateChangeListener();
81 Registry.newRegistry();
82 final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
83 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
84 ApexStarterParameterGroup parameterGroup;
85 // The arguments return a string if there is a message to print and we should
87 final String argumentMessage = arguments.parse(apexStarterConfigParameters);
88 if (argumentMessage != null) {
91 // Validate that the arguments are sane
94 // Read the parameters
95 parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
97 activator = new ApexStarterActivator(parameterGroup);
98 Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
99 Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager());
100 activator.initialize();
104 * Method for cleanup after each test.
106 * @throws Exception if an error occurs
109 void teardown() throws Exception {
110 System.setOut(stdout);
112 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
113 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
114 apexEngineHandler.shutdown();
116 // clear the apex starter activator
117 if (activator != null && activator.isAlive()) {
118 activator.terminate();
123 void testPdpStateChangeMessageListener_passiveToPassive() {
124 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
125 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
126 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
128 PdpStateChange pdpStateChangeMsg =
129 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
130 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
132 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
136 void testPdpStateChangeMessageListener_activeToActive() {
137 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
138 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
139 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
141 pdpStatus.setState(PdpState.ACTIVE);
142 PdpStateChange pdpStateChangeMsg =
143 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
144 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
146 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
150 void testPdpStateChangeMessageListener() throws CoderException {
151 OutputStream outContent = new ByteArrayOutputStream();
152 System.setOut(new PrintStream(outContent));
153 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
154 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
155 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
157 PdpStateChange pdpStateChangeMsg =
158 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
159 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
160 assertTrue(outContent.toString().contains("State changed to active. No policies found."));
162 final ToscaPolicy toscaPolicy =
163 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
164 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
165 toscaPolicies.add(toscaPolicy);
166 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
168 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
169 assertThat(outContent.toString()).contains("Apex engine started. Deployed policies are: apex_policy_name:1.0");
170 assertEquals(PdpState.ACTIVE, pdpStatus.getState());
172 final ApexPolicyStatisticsManager policyCounterManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
173 assertNotNull(policyCounterManager);
174 assertEquals(policyCounterManager.getPolicyDeployCount(),
175 policyCounterManager.getPolicyDeploySuccessCount() + policyCounterManager.getPolicyDeployFailCount());
178 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
179 assertNotNull(apexEngineHandler);
180 assertFalse(apexEngineHandler.getEngineStats().isEmpty());
184 void testPdpStateChangeMessageListener_activeToPassive() throws CoderException {
185 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
186 final ToscaPolicy toscaPolicy =
187 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
188 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
189 toscaPolicies.add(toscaPolicy);
190 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
192 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
193 PdpStateChange pdpStateChangeMsg =
194 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
195 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
197 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
198 OutputStream outContent = new ByteArrayOutputStream();
199 System.setOut(new PrintStream(outContent));
200 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
201 final String outString = outContent.toString();
202 assertTrue(outString.contains("Apex pdp state changed from Active to Passive."));
203 assertEquals(PdpState.PASSIVE, pdpStatus.getState());