2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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.assertTrue;
28 import java.io.ByteArrayOutputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.PrintStream;
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
39 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
40 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
41 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
42 import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler;
43 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
44 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
45 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.services.Registry;
48 import org.onap.policy.models.pdp.concepts.PdpStateChange;
49 import org.onap.policy.models.pdp.concepts.PdpStatus;
50 import org.onap.policy.models.pdp.concepts.PdpUpdate;
51 import org.onap.policy.models.pdp.enums.PdpState;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
55 * Class to perform unit test of {@link PdpStateChangeListener}.
57 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
59 public class TestPdpStateChangeListener {
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 ApexStarterActivator activator;
65 private ApexEngineHandler apexEngineHandler;
66 private PrintStream stdout = System.out;
69 * Method for setup before each test.
71 * @throws ApexStarterException if some error occurs while starting up the apex starter
72 * @throws FileNotFoundException if the file is missing
73 * @throws IOException if IO exception occurs
76 public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
77 pdpUpdateMessageListener = new PdpUpdateListener();
78 pdpStateChangeListener = new PdpStateChangeListener();
79 Registry.newRegistry();
80 final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
81 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
82 ApexStarterParameterGroup parameterGroup;
83 // The arguments return a string if there is a message to print and we should
85 final String argumentMessage = arguments.parse(apexStarterConfigParameters);
86 if (argumentMessage != null) {
89 // Validate that the arguments are sane
92 // Read the parameters
93 parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
95 activator = new ApexStarterActivator(parameterGroup);
96 Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
97 activator.initialize();
101 * Method for cleanup after each test.
103 * @throws Exception if an error occurs
106 public void teardown() throws Exception {
107 System.setOut(stdout);
109 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
110 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
111 apexEngineHandler.shutdown();
113 // clear the apex starter activator
114 if (activator != null && activator.isAlive()) {
115 activator.terminate();
120 public void testPdpStateChangeMessageListener_passivetopassive() {
121 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
122 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
123 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
124 PdpStateChange pdpStateChangeMsg =
125 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
126 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
128 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
132 public void testPdpStateChangeMessageListener_activetoactive() {
133 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
134 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
135 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
136 pdpStatus.setState(PdpState.ACTIVE);
137 PdpStateChange pdpStateChangeMsg =
138 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
139 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
141 assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
145 public void testPdpStateChangeMessageListener() throws InterruptedException, CoderException {
146 OutputStream outContent = new ByteArrayOutputStream();
147 System.setOut(new PrintStream(outContent));
148 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
149 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
150 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
151 PdpStateChange pdpStateChangeMsg =
152 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
153 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
154 assertTrue(outContent.toString().contains("State changed to active. No policies found."));
156 final ToscaPolicy toscaPolicy =
157 TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
158 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
159 toscaPolicies.add(toscaPolicy);
160 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
161 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
162 assertTrue(outContent.toString().contains("Apex engine started and policies are running."));
163 assertEquals(PdpState.ACTIVE, pdpStatus.getState());
167 public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException {
168 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
169 final ToscaPolicy toscaPolicy =
170 TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
171 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
172 toscaPolicies.add(toscaPolicy);
173 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
174 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
175 PdpStateChange pdpStateChangeMsg =
176 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
177 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
179 TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
180 OutputStream outContent = new ByteArrayOutputStream();
181 System.setOut(new PrintStream(outContent));
182 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
183 final String outString = outContent.toString();
184 assertTrue(outString.contains("Apex pdp state changed from Active to Passive."));
185 assertEquals(PdpState.PASSIVE, pdpStatus.getState());