ec3da9bc337d5a5cf605b6628ebf73d749be59ee
[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.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.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.handler.PdpMessageHandler;
47 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
48 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
49 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.common.utils.cmd.CommandLineException;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.services.Registry;
53 import org.onap.policy.models.pdp.concepts.PdpStateChange;
54 import org.onap.policy.models.pdp.concepts.PdpStatus;
55 import org.onap.policy.models.pdp.concepts.PdpUpdate;
56 import org.onap.policy.models.pdp.enums.PdpState;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
58
59 /**
60  * Class to perform unit test of {@link PdpUpdateListener}.
61  *
62  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
63  */
64 public class TestPdpUpdateListener {
65     private PdpUpdateListener pdpUpdateMessageListener;
66     private PdpStateChangeListener pdpStateChangeListener;
67     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
68     private static final String TOPIC = "my-topic";
69     private ApexStarterActivator activator;
70     private ApexEngineHandler apexEngineHandler;
71     private PrintStream stdout = System.out;
72
73     /**
74      * Method for setup before each test.
75      *
76      * @throws ApexStarterException if some error occurs while starting up the apex starter
77      * @throws FileNotFoundException if the file is missing
78      * @throws IOException if IO exception occurs
79      * @throws CommandLineException if any parsing of args has errors
80      */
81     @Before
82     public void setUp() throws ApexStarterException, FileNotFoundException, IOException, CommandLineException {
83         Registry.newRegistry();
84         final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"};
85         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
86         ApexStarterParameterGroup parameterGroup;
87         // The arguments return a string if there is a message to print and we should
88         // exit
89         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
90         if (argumentMessage != null) {
91             return;
92         }
93         // Validate that the arguments are sane
94         arguments.validate();
95
96         // Read the parameters
97         parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
98
99         activator = new ApexStarterActivator(parameterGroup);
100         Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
101         activator.initialize();
102         pdpUpdateMessageListener = new PdpUpdateListener();
103         pdpStateChangeListener = new PdpStateChangeListener();
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 testPdpUpdateMssageListener() throws CoderException {
127         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
128         final ToscaPolicy toscaPolicy =
129             TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json");
130         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
131         toscaPolicies.add(toscaPolicy);
132         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
133                 new LinkedList<>());
134         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
135         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
136         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
137         assertEquals(pdpStatus.getPolicies(),
138                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
139     }
140
141     @Test
142     public void testPdpUpdateMssageListener_success() throws InterruptedException, CoderException {
143         OutputStream outContent = new ByteArrayOutputStream();
144         System.setOut(new PrintStream(outContent));
145         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
146         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
147             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
148                     new ArrayList<>()));
149         PdpStateChange pdpStateChangeMsg =
150             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
151         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
152         final ToscaPolicy toscaPolicy =
153             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
154         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
155         toscaPolicies.add(toscaPolicy);
156         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
157                 new LinkedList<>());
158         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
159         final String outString = outContent.toString();
160         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
161         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
162         assertEquals(pdpStatus.getPolicies(),
163                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
164         assertThat(outString).contains("Apex engine started and policies are running.");
165     }
166
167     @Test
168     public void testPdpUpdateMssageListener_undeploy() throws InterruptedException, CoderException {
169         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
170         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
171             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
172                     new ArrayList<>()));
173         PdpStateChange pdpStateChangeMsg =
174             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
175         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
176         final ToscaPolicy toscaPolicy =
177             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
178         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
179         toscaPolicies.add(toscaPolicy);
180         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
181                 new ArrayList<>());
182         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
183         OutputStream outContent = new ByteArrayOutputStream();
184         System.setOut(new PrintStream(outContent));
185         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
186             TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
187                     toscaPolicies.stream().map(ToscaPolicy::getIdentifier)
188                     .collect(Collectors.toList())));
189         final String outString = outContent.toString();
190         assertThat(outString).contains("Pdp update successful. No policies are running.");
191     }
192
193     @Test
194     public void testPdpUpdateMssageListener_multi_policy_duplicate()
195         throws InterruptedException, ApexStarterException, CoderException {
196         OutputStream outContent = new ByteArrayOutputStream();
197         System.setOut(new PrintStream(outContent));
198         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
199         final ToscaPolicy toscaPolicy =
200             TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json");
201         final ToscaPolicy toscaPolicy2 =
202             TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json");
203         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
204         toscaPolicies.add(toscaPolicy);
205         toscaPolicies.add(toscaPolicy2);
206         final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
207                 new LinkedList<>());
208         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
209         PdpStateChange pdpStateChangeMsg =
210             TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
211         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
212         final String outString = outContent.toString();
213         assertTrue(outString.contains(
214             "Apex engine started. But, only the following polices are running - apex_policy_name:1.0  . "
215             + "Other policies failed execution. Please see the logs for more details."));
216     }
217 }