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.assertTrue;
29 import java.io.ByteArrayOutputStream;
30 import java.io.OutputStream;
31 import java.io.PrintStream;
32 import java.util.ArrayList;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.stream.Collectors;
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.handler.PdpMessageHandler;
46 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
47 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
48 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
49 import org.onap.policy.common.utils.cmd.CommandLineException;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.services.Registry;
52 import org.onap.policy.models.pdp.concepts.PdpStateChange;
53 import org.onap.policy.models.pdp.concepts.PdpStatus;
54 import org.onap.policy.models.pdp.concepts.PdpUpdate;
55 import org.onap.policy.models.pdp.enums.PdpState;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
59 * Class to perform unit test of {@link PdpUpdateListener}.
61 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
63 class TestPdpUpdateListener {
64 private PdpUpdateListener pdpUpdateMessageListener;
65 private PdpStateChangeListener pdpStateChangeListener;
66 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
67 private static final String TOPIC = "my-topic";
68 private ApexStarterActivator activator;
69 private ApexEngineHandler apexEngineHandler;
70 private final PrintStream stdout = System.out;
73 * Method for setup before each test.
75 * @throws ApexStarterException if some error occurs while starting up the apex starter
76 * @throws CommandLineException if any parsing of args has errors
79 void setUp() throws ApexStarterException, CommandLineException {
80 Registry.newRegistry();
81 final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
82 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
83 ApexStarterParameterGroup parameterGroup;
84 // The arguments return a string if there is a message to print and we should
86 final String argumentMessage = arguments.parse(apexStarterConfigParameters);
87 if (argumentMessage != null) {
90 // Validate that the arguments are sane
93 // Read the parameters
94 parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
96 activator = new ApexStarterActivator(parameterGroup);
97 Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
98 activator.initialize();
99 pdpUpdateMessageListener = new PdpUpdateListener();
100 pdpStateChangeListener = new PdpStateChangeListener();
101 Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER,
102 new ApexPolicyStatisticsManager());
106 * Method for cleanup after each test.
108 * @throws Exception if an error occurs
111 void teardown() throws Exception {
112 System.setOut(stdout);
114 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
115 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
116 apexEngineHandler.shutdown();
118 // clear the apex starter activator
119 if (activator != null && activator.isAlive()) {
120 activator.terminate();
125 void testPdpUpdateMessageListener() throws CoderException {
126 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
127 final ToscaPolicy toscaPolicy =
128 TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
129 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
130 toscaPolicies.add(toscaPolicy);
131 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
133 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
134 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
135 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
136 assertEquals(pdpStatus.getPolicies(),
137 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
141 void testPdpUpdateMessageListener_success() throws CoderException {
142 OutputStream outContent = new ByteArrayOutputStream();
143 System.setOut(new PrintStream(outContent));
144 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
145 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
146 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
148 PdpStateChange pdpStateChangeMsg =
149 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
150 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
151 final ToscaPolicy toscaPolicy =
152 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
153 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
154 toscaPolicies.add(toscaPolicy);
155 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
157 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
158 final String outString = outContent.toString();
159 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
160 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
161 assertEquals(pdpStatus.getPolicies(),
162 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
163 assertThat(outString).contains("Apex engine started. Deployed policies are: apex_policy_name:1.0");
167 void testPdpUpdateMessageListener_undeploy() throws CoderException {
168 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
169 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
170 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
172 PdpStateChange pdpStateChangeMsg =
173 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
174 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
175 final ToscaPolicy toscaPolicy =
176 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
177 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
178 toscaPolicies.add(toscaPolicy);
179 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
181 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
182 OutputStream outContent = new ByteArrayOutputStream();
183 System.setOut(new PrintStream(outContent));
184 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
185 TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
186 toscaPolicies.stream().map(ToscaPolicy::getIdentifier)
187 .collect(Collectors.toList())));
188 final String outString = outContent.toString();
189 assertThat(outString).contains("Pdp update successful. No policies are running.");
193 void testPdpUpdateMessageListener_multi_policy_duplicate() throws CoderException {
194 OutputStream outContent = new ByteArrayOutputStream();
195 System.setOut(new PrintStream(outContent));
196 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
197 final ToscaPolicy toscaPolicy =
198 TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
199 final ToscaPolicy toscaPolicy2 =
200 TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json");
201 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
202 toscaPolicies.add(toscaPolicy);
203 toscaPolicies.add(toscaPolicy2);
204 final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
206 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
207 PdpStateChange pdpStateChangeMsg =
208 TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
209 pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
210 final String outString = outContent.toString();
211 assertTrue(outString.contains(
212 "Apex engine started. But, only the following polices are running - apex_policy_name:1.0 . "
213 + "Other policies failed execution. Please see the logs for more details."));