2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2020 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.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import java.io.ByteArrayOutputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.io.PrintStream;
34 import java.util.ArrayList;
35 import java.util.List;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.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.coder.CoderException;
49 import org.onap.policy.common.utils.services.Registry;
50 import org.onap.policy.models.pdp.concepts.PdpStateChange;
51 import org.onap.policy.models.pdp.concepts.PdpStatus;
52 import org.onap.policy.models.pdp.concepts.PdpUpdate;
53 import org.onap.policy.models.pdp.enums.PdpState;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57 * Class to perform unit test of {@link PdpStateChangeListener}.
59 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
61 public class TestPdpStateChangeListener {
62 private PdpUpdateListener pdpUpdateMessageListener;
63 private PdpStateChangeListener pdpStateChangeListener;
64 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
65 private static final String TOPIC = "my-topic";
66 private ApexStarterActivator activator;
67 private ApexEngineHandler apexEngineHandler;
68 private PrintStream stdout = System.out;
71 * Method for setup before each test.
73 * @throws ApexStarterException if some error occurs while starting up the apex starter
74 * @throws FileNotFoundException if the file is missing
75 * @throws IOException if IO exception occurs
78 public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
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 public 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 public 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<ToscaPolicy>()));
127 PdpStateChange pdpStateChangeMsg =
128 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
129 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
131 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
135 public void testPdpStateChangeMessageListener_activetoactive() {
136 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
137 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
138 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
139 pdpStatus.setState(PdpState.ACTIVE);
140 PdpStateChange pdpStateChangeMsg =
141 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
142 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
144 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
148 public void testPdpStateChangeMessageListener() throws InterruptedException, CoderException {
149 OutputStream outContent = new ByteArrayOutputStream();
150 System.setOut(new PrintStream(outContent));
151 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
152 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
153 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
154 PdpStateChange pdpStateChangeMsg =
155 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
156 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
157 assertTrue(outContent.toString().contains("State changed to active. No policies found."));
159 final ToscaPolicy toscaPolicy =
160 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
161 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
162 toscaPolicies.add(toscaPolicy);
163 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
164 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
165 assertTrue(outContent.toString().contains("Apex engine started and policies are running."));
166 assertEquals(PdpState.ACTIVE, pdpStatus.getState());
168 final ApexPolicyStatisticsManager policyCounterManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
169 assertNotNull(policyCounterManager);
170 assertEquals(policyCounterManager.getPolicyDeployCount(),
171 policyCounterManager.getPolicyDeploySuccessCount() + policyCounterManager.getPolicyDeployFailCount());
174 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
175 assertNotNull(apexEngineHandler);
176 assertTrue(apexEngineHandler.getEngineStats().size() > 0);
180 public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException {
181 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
182 final ToscaPolicy toscaPolicy =
183 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
184 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
185 toscaPolicies.add(toscaPolicy);
186 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
187 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
188 PdpStateChange pdpStateChangeMsg =
189 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
190 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
192 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
193 OutputStream outContent = new ByteArrayOutputStream();
194 System.setOut(new PrintStream(outContent));
195 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
196 final String outString = outContent.toString();
197 assertTrue(outString.contains("Apex pdp state changed from Active to Passive."));
198 assertEquals(PdpState.PASSIVE, pdpStatus.getState());