3716e043f1d80651457d44ce53cad05bf5f2c8f1
[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-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
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.assertTrue;
28
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.LinkedList;
36 import java.util.List;
37 import java.util.stream.Collectors;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
42 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
43 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
44 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
45 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
46 import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler;
47 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
48 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
49 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
50 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
51 import org.onap.policy.common.utils.cmd.CommandLineException;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.services.Registry;
54 import org.onap.policy.models.pdp.concepts.PdpStateChange;
55 import org.onap.policy.models.pdp.concepts.PdpStatus;
56 import org.onap.policy.models.pdp.concepts.PdpUpdate;
57 import org.onap.policy.models.pdp.enums.PdpState;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
59
60 /**
61  * Class to perform unit test of {@link PdpUpdateListener}.
62  *
63  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
64  */
65 public class TestPdpUpdateListener {
66     private PdpUpdateListener pdpUpdateMessageListener;
67     private PdpStateChangeListener pdpStateChangeListener;
68     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
69     private static final String TOPIC = "my-topic";
70     private ApexStarterActivator activator;
71     private ApexEngineHandler apexEngineHandler;
72     private PrintStream stdout = System.out;
73
74     /**
75      * Method for setup before each test.
76      *
77      * @throws ApexStarterException if some error occurs while starting up the apex starter
78      * @throws FileNotFoundException if the file is missing
79      * @throws IOException if IO exception occurs
80      * @throws CommandLineException if any parsing of args has errors
81      */
82     @Before
83     public void setUp() throws ApexStarterException, FileNotFoundException, IOException, CommandLineException {
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         activator.initialize();
103         pdpUpdateMessageListener = new PdpUpdateListener();
104         pdpStateChangeListener = new PdpStateChangeListener();
105         Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER,
106                 new ApexPolicyStatisticsManager());
107     }
108
109     /**
110      * Method for cleanup after each test.
111      *
112      * @throws Exception if an error occurs
113      */
114     @After
115     public void teardown() throws Exception {
116         System.setOut(stdout);
117         apexEngineHandler =
118             Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
119         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
120             apexEngineHandler.shutdown();
121         }
122         // clear the apex starter activator
123         if (activator != null && activator.isAlive()) {
124             activator.terminate();
125         }
126     }
127
128     @Test
129     public void testPdpUpdateMssageListener() throws CoderException {
130         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
131         final ToscaPolicy toscaPolicy =
132             TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
133         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
134         toscaPolicies.add(toscaPolicy);
135         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
136                 new LinkedList<>());
137         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
138         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
139         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
140         assertEquals(pdpStatus.getPolicies(),
141                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
142     }
143
144     @Test
145     public void testPdpUpdateMssageListener_success() 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<>(),
151                     new ArrayList<>()));
152         PdpStateChange pdpStateChangeMsg =
153             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
154         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
155         final ToscaPolicy toscaPolicy =
156             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
157         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
158         toscaPolicies.add(toscaPolicy);
159         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
160                 new LinkedList<>());
161         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
162         final String outString = outContent.toString();
163         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
164         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
165         assertEquals(pdpStatus.getPolicies(),
166                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
167         assertThat(outString).contains("Apex engine started. Deployed policies are: apex_policy_name:1.0");
168     }
169
170     @Test
171     public void testPdpUpdateMssageListener_undeploy() throws InterruptedException, CoderException {
172         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
173         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
174             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
175                     new ArrayList<>()));
176         PdpStateChange pdpStateChangeMsg =
177             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
178         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
179         final ToscaPolicy toscaPolicy =
180             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
181         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
182         toscaPolicies.add(toscaPolicy);
183         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
184                 new ArrayList<>());
185         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
186         OutputStream outContent = new ByteArrayOutputStream();
187         System.setOut(new PrintStream(outContent));
188         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
189             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
190                     toscaPolicies.stream().map(ToscaPolicy::getIdentifier)
191                     .collect(Collectors.toList())));
192         final String outString = outContent.toString();
193         assertThat(outString).contains("Pdp update successful. No policies are running.");
194     }
195
196     @Test
197     public void testPdpUpdateMssageListener_multi_policy_duplicate()
198         throws InterruptedException, ApexStarterException, CoderException {
199         OutputStream outContent = new ByteArrayOutputStream();
200         System.setOut(new PrintStream(outContent));
201         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
202         final ToscaPolicy toscaPolicy =
203             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
204         final ToscaPolicy toscaPolicy2 =
205             TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json");
206         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
207         toscaPolicies.add(toscaPolicy);
208         toscaPolicies.add(toscaPolicy2);
209         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
210                 new LinkedList<>());
211         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
212         PdpStateChange pdpStateChangeMsg =
213             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
214         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
215         final String outString = outContent.toString();
216         assertTrue(outString.contains(
217             "Apex engine started. But, only the following polices are running - apex_policy_name:1.0  . "
218             + "Other policies failed execution. Please see the logs for more details."));
219     }
220 }