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) 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.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.handler.PdpMessageHandler;
44 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
45 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
46 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.services.Registry;
49 import org.onap.policy.models.pdp.concepts.PdpStateChange;
50 import org.onap.policy.models.pdp.concepts.PdpStatus;
51 import org.onap.policy.models.pdp.concepts.PdpUpdate;
52 import org.onap.policy.models.pdp.enums.PdpState;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
56 * Class to perform unit test of {@link PdpUpdateListener}.
58 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
60 public class TestPdpUpdateListener {
61 private PdpUpdateListener pdpUpdateMessageListener;
62 private PdpStateChangeListener pdpStateChangeListener;
63 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
64 private static final String TOPIC = "my-topic";
65 private ApexStarterActivator activator;
66 private ApexEngineHandler apexEngineHandler;
67 private PrintStream stdout = System.out;
70 * Method for setup before each test.
72 * @throws ApexStarterException if some error occurs while starting up the apex starter
73 * @throws FileNotFoundException if the file is missing
74 * @throws IOException if IO exception occurs
77 public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
78 Registry.newRegistry();
79 final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
80 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
81 ApexStarterParameterGroup parameterGroup;
82 // The arguments return a string if there is a message to print and we should
84 final String argumentMessage = arguments.parse(apexStarterConfigParameters);
85 if (argumentMessage != null) {
88 // Validate that the arguments are sane
91 // Read the parameters
92 parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
94 activator = new ApexStarterActivator(parameterGroup);
95 Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
96 activator.initialize();
97 pdpUpdateMessageListener = new PdpUpdateListener();
98 pdpStateChangeListener = new PdpStateChangeListener();
102 * Method for cleanup after each test.
104 * @throws Exception if an error occurs
107 public void teardown() throws Exception {
108 System.setOut(stdout);
110 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
111 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
112 apexEngineHandler.shutdown();
114 // clear the apex starter activator
115 if (activator != null && activator.isAlive()) {
116 activator.terminate();
121 public void testPdpUpdateMssageListener() throws CoderException {
122 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
123 final ToscaPolicy toscaPolicy =
124 TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
125 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
126 toscaPolicies.add(toscaPolicy);
127 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
128 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
129 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
130 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
131 assertEquals(pdpStatus.getPolicies(),
132 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
136 public void testPdpUpdateMssageListener_success() throws InterruptedException, CoderException {
137 OutputStream outContent = new ByteArrayOutputStream();
138 System.setOut(new PrintStream(outContent));
139 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
140 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
141 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
142 PdpStateChange pdpStateChangeMsg =
143 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
144 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
145 final ToscaPolicy toscaPolicy =
146 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
147 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
148 toscaPolicies.add(toscaPolicy);
149 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
150 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
151 final String outString = outContent.toString();
152 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
153 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
154 assertEquals(pdpStatus.getPolicies(),
155 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
156 assertTrue(outString.contains("Apex engine started and policies are running."));
160 public void testPdpUpdateMssageListener_undeploy() throws InterruptedException, CoderException {
161 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
162 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
163 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
164 PdpStateChange pdpStateChangeMsg =
165 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
166 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
167 final ToscaPolicy toscaPolicy =
168 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
169 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
170 toscaPolicies.add(toscaPolicy);
171 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
172 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
173 OutputStream outContent = new ByteArrayOutputStream();
174 System.setOut(new PrintStream(outContent));
175 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
176 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
177 final String outString = outContent.toString();
178 assertTrue(outString.contains("Pdp update successful. No policies are running."));
183 public void testPdpUpdateMssageListener_multi_policy_duplicate()
184 throws InterruptedException, ApexStarterException, CoderException {
185 OutputStream outContent = new ByteArrayOutputStream();
186 System.setOut(new PrintStream(outContent));
187 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
188 final ToscaPolicy toscaPolicy =
189 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
190 final ToscaPolicy toscaPolicy2 =
191 TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json");
192 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
193 toscaPolicies.add(toscaPolicy);
194 toscaPolicies.add(toscaPolicy2);
195 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
196 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
197 PdpStateChange pdpStateChangeMsg =
198 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
199 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
200 final String outString = outContent.toString();
201 assertTrue(outString.contains(
202 "Apex engine started. But, only the following polices are running - apex_policy_name:1.0 . "
203 + "Other policies failed execution. Please see the logs for more details."));