10f82633f6b0697b02ddfa26ab117d29cae735eb
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.services.onappf.comm;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.PrintStream;
35 import java.util.ArrayList;
36 import java.util.List;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
41 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
42 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
43 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
44 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
45 import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler;
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;
57
58 /**
59  * Class to perform unit test of {@link PdpStateChangeListener}.
60  *
61  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
62  */
63 public class TestPdpStateChangeListener {
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 PrintStream stdout = System.out;
71
72     /**
73      * Method for setup before each test.
74      *
75      * @throws ApexStarterException if some error occurs while starting up the apex starter
76      * @throws FileNotFoundException if the file is missing
77      * @throws IOException if IO exception occurs
78      * @throws CommandLineException if any parsing of args has errors
79      */
80     @Before
81     public void setUp() throws ApexStarterException, FileNotFoundException, IOException, CommandLineException {
82         pdpUpdateMessageListener = new PdpUpdateListener();
83         pdpStateChangeListener = new PdpStateChangeListener();
84         Registry.newRegistry();
85         final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
86         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
87         ApexStarterParameterGroup parameterGroup;
88         // The arguments return a string if there is a message to print and we should
89         // exit
90         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
91         if (argumentMessage != null) {
92             return;
93         }
94         // Validate that the arguments are sane
95         arguments.validate();
96
97         // Read the parameters
98         parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
99
100         activator = new ApexStarterActivator(parameterGroup);
101         Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
102         Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager());
103         activator.initialize();
104     }
105
106     /**
107      * Method for cleanup after each test.
108      *
109      * @throws Exception if an error occurs
110      */
111     @After
112     public void teardown() throws Exception {
113         System.setOut(stdout);
114         apexEngineHandler =
115             Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
116         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
117             apexEngineHandler.shutdown();
118         }
119         // clear the apex starter activator
120         if (activator != null && activator.isAlive()) {
121             activator.terminate();
122         }
123     }
124
125     @Test
126     public void testPdpStateChangeMessageListener_passivetopassive() {
127         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
128         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
129             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
130                     new ArrayList<>()));
131         PdpStateChange pdpStateChangeMsg =
132             TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
133         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
134
135         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
136     }
137
138     @Test
139     public void testPdpStateChangeMessageListener_activetoactive() {
140         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
141         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
142             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
143                     new ArrayList<>()));
144         pdpStatus.setState(PdpState.ACTIVE);
145         PdpStateChange pdpStateChangeMsg =
146             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
147         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
148
149         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
150     }
151
152     @Test
153     public void testPdpStateChangeMessageListener() throws InterruptedException, CoderException {
154         OutputStream outContent = new ByteArrayOutputStream();
155         System.setOut(new PrintStream(outContent));
156         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
157         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
158             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
159                     new ArrayList<>()));
160         PdpStateChange pdpStateChangeMsg =
161             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
162         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
163         assertTrue(outContent.toString().contains("State changed to active. No policies found."));
164
165         final ToscaPolicy toscaPolicy =
166             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
167         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
168         toscaPolicies.add(toscaPolicy);
169         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
170                 new ArrayList<>());
171         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
172         assertThat(outContent.toString()).contains("Apex engine started and policies are running.");
173         assertEquals(PdpState.ACTIVE, pdpStatus.getState());
174
175         final ApexPolicyStatisticsManager policyCounterManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
176         assertNotNull(policyCounterManager);
177         assertEquals(policyCounterManager.getPolicyDeployCount(),
178                 policyCounterManager.getPolicyDeploySuccessCount() + policyCounterManager.getPolicyDeployFailCount());
179
180         apexEngineHandler =
181                 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
182         assertNotNull(apexEngineHandler);
183         assertTrue(apexEngineHandler.getEngineStats().size() > 0);
184     }
185
186     @Test
187     public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException {
188         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
189         final ToscaPolicy toscaPolicy =
190             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
191         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
192         toscaPolicies.add(toscaPolicy);
193         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
194                 new ArrayList<>());
195         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
196         PdpStateChange pdpStateChangeMsg =
197             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
198         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
199         pdpStateChangeMsg =
200             TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
201         OutputStream outContent = new ByteArrayOutputStream();
202         System.setOut(new PrintStream(outContent));
203         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
204         final String outString = outContent.toString();
205         assertTrue(outString.contains("Apex pdp state changed from Active to Passive."));
206         assertEquals(PdpState.PASSIVE, pdpStatus.getState());
207     }
208 }