Address sonar issues in drools-pdp
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateActiveTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * =============LICENSE_END========================================================
19  */
20
21 package org.onap.policy.drools.lifecycle;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Objects;
38 import java.util.concurrent.TimeUnit;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.pdp.concepts.PdpUpdate;
47 import org.onap.policy.models.pdp.enums.PdpState;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
50
51 /**
52  * Lifecycle State Active Test.
53  */
54 public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
55
56     /**
57      * Start tests in the Active state.
58      */
59     @Before
60     public void startActive() throws CoderException {
61         fsm = makeFsmWithPseudoTime();
62
63         fsm.setStatusTimerSeconds(15);
64         assertTrue(fsm.start());
65
66         goActive();
67         assertActive();
68     }
69
70     private void goActive() throws CoderException {
71         PdpStateChange change = new PdpStateChange();
72         change.setPdpGroup("A");
73         change.setPdpSubgroup("a");
74         change.setState(PdpState.ACTIVE);
75         change.setName(fsm.getName());
76
77         fsm.setSubGroupAction("a");
78         fsm.source.offer(new StandardCoder().encode(change));
79         controllerSupport.getController().start();
80     }
81
82     @Test
83     public void constructor() {
84         assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateActive(null));
85         fsm.shutdown();
86     }
87
88     @Test
89     public void testStart() {
90         assertActive();
91         assertFalse(fsm.start());
92         assertActive();
93
94         fsm.shutdown();
95     }
96
97     private void assertActive() {
98         assertEquals(PdpState.ACTIVE, fsm.state());
99         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
100         assertEquals("a", fsm.getSubgroup());
101         assertTrue(fsm.isAlive());
102         waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
103     }
104
105     @Test
106     public void testStop() {
107         assertTrue(fsm.stop());
108         assertBasicTerminated();
109
110         fsm.shutdown();
111     }
112
113     private void assertBasicTerminated() {
114         assertEquals(PdpState.TERMINATED, fsm.state());
115         assertFalse(fsm.isAlive());
116         assertFalse(fsm.state.isAlive());
117         waitUntil(1, TimeUnit.SECONDS, isStatus(PdpState.TERMINATED));
118     }
119
120     @Test
121     public void testShutdown() {
122         fsm.shutdown();
123
124         assertBasicTerminated();
125
126         assertTrue(fsm.statusTask.isCancelled());
127         assertTrue(fsm.statusTask.isDone());
128     }
129
130     @Test
131     public void testStatus() {
132         waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
133         int preCount = fsm.client.getSink().getRecentEvents().length;
134
135         assertTrue(fsm.status());
136         assertEquals(preCount + 1, fsm.client.getSink().getRecentEvents().length);
137
138         fsm.start(controllerSupport.getController());
139         assertTrue(fsm.status());
140         assertEquals(preCount + 2, fsm.client.getSink().getRecentEvents().length);
141
142         fsm.stop(controllerSupport.getController());
143         fsm.shutdown();
144     }
145
146     @Test
147     public void testStateChange() throws CoderException {
148         assertActive();
149
150         /* no name and mismatching group info */
151         PdpStateChange change = new PdpStateChange();
152         change.setPdpGroup("B");
153         change.setPdpSubgroup("b");
154         change.setState(PdpState.ACTIVE);
155
156         fsm.source.offer(new StandardCoder().encode(change));
157         assertEquals(PdpState.ACTIVE, fsm.state());
158         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
159         assertNotEquals("b", fsm.getSubgroup());
160
161         change.setName(fsm.getName());
162         fsm.source.offer(new StandardCoder().encode(change));
163         assertEquals(PdpState.ACTIVE, fsm.state());
164         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
165         assertEquals("a", fsm.getSubgroup());
166
167         change.setState(PdpState.SAFE);
168         fsm.source.offer(new StandardCoder().encode(change));
169         assertEquals(PdpState.ACTIVE, fsm.state());
170
171         change.setState(PdpState.TERMINATED);
172         fsm.source.offer(new StandardCoder().encode(change));
173         assertEquals(PdpState.ACTIVE, fsm.state());
174
175         change.setState(PdpState.PASSIVE);
176         fsm.source.offer(new StandardCoder().encode(change));
177         assertEquals(PdpState.PASSIVE, fsm.state());
178         waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE));
179
180         fsm.shutdown();
181     }
182
183     @Test
184     public void testUpdate() throws IOException, CoderException {
185
186         // TODO: extract repeated similar assertion blocks into their own helper methods
187
188         PdpUpdate update = new PdpUpdate();
189         update.setName(NetworkUtil.getHostname());
190         update.setPdpGroup("W");
191         update.setPdpSubgroup("w");
192         update.setPolicies(Collections.emptyList());
193
194         fsm.start(controllerSupport.getController());
195         assertTrue(fsm.update(update));
196
197         assertEquals(PdpState.ACTIVE, fsm.state());
198         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
199         assertEquals("w", fsm.getSubgroup());
200
201         String restartV1 =
202             new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
203         ToscaPolicy toscaPolicyRestartV1 = new StandardCoder().decode(restartV1, ToscaPolicy.class);
204         update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
205
206         int qlength = fsm.client.getSink().getRecentEvents().length;
207
208         // update with an operational.restart policy
209
210         assertTrue(fsm.update(update));
211         assertEquals(qlength + 1, fsm.client.getSink().getRecentEvents().length);
212         assertEquals(4, fsm.policyTypesMap.size());
213         assertNotNull(fsm.getPolicyTypesMap().get(
214                 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
215         assertNotNull(fsm.getPolicyTypesMap().get(
216                 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
217         assertNotNull(fsm.getPolicyTypesMap().get(
218                 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
219         assertNotNull(fsm.getPolicyTypesMap().get(
220                 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
221                 "1.0.0")));
222         PdpStatus cachedStatus = new StandardCoder()
223                                     .decode(fsm.client.getSink().getRecentEvents()[qlength], PdpStatus.class);
224         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
225
226         List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
227         assertEquals(1, factPolicies.size());
228         assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
229         assertEquals(1, fsm.policiesMap.size());
230
231         // dup update with the same operational.restart policy - nothing changes
232
233         assertTrue(fsm.update(update));
234         assertEquals(qlength + 2, fsm.client.getSink().getRecentEvents().length);
235         assertEquals(4, fsm.policyTypesMap.size());
236         cachedStatus = new StandardCoder()
237             .decode(fsm.client.getSink().getRecentEvents()[qlength + 1], PdpStatus.class);
238         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
239
240
241         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
242         assertEquals(1, factPolicies.size());
243         assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
244         assertEquals(1, fsm.policiesMap.size());
245
246         // undeploy operational.restart policy
247
248         update.setPolicies(Collections.emptyList());
249         assertTrue(fsm.update(update));
250         assertEquals(qlength + 3, fsm.client.getSink().getRecentEvents().length);
251         assertEquals(4, fsm.policyTypesMap.size());
252         cachedStatus = new StandardCoder()
253             .decode(fsm.client.getSink().getRecentEvents()[qlength + 2], PdpStatus.class);
254         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
255
256         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
257         assertEquals(0, factPolicies.size());
258         assertEquals(0, fsm.policiesMap.size());
259
260         // redeploy operational.restart policy
261
262         update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
263         assertTrue(fsm.update(update));
264         assertEquals(qlength + 4, fsm.client.getSink().getRecentEvents().length);
265         assertEquals(4, fsm.policyTypesMap.size());
266         cachedStatus = new StandardCoder()
267             .decode(fsm.client.getSink().getRecentEvents()[qlength + 3], PdpStatus.class);
268         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
269
270         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
271         assertEquals(1, factPolicies.size());
272         assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
273         assertEquals(1, fsm.policiesMap.size());
274
275         // deploy a new version of the operational.restart policy
276
277         String restartV2 =
278             new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.v2.json")));
279         ToscaPolicy toscaPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
280         update.setPolicies(Arrays.asList(toscaPolicyRestartV2));
281         assertTrue(fsm.update(update));
282         assertEquals(qlength + 5, fsm.client.getSink().getRecentEvents().length);
283         assertEquals(4, fsm.policyTypesMap.size());
284         cachedStatus = new StandardCoder()
285             .decode(fsm.client.getSink().getRecentEvents()[qlength + 4], PdpStatus.class);
286         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
287
288         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
289         assertEquals(1, factPolicies.size());
290         assertNotEquals(toscaPolicyRestartV1, factPolicies.get(0));
291         assertEquals(toscaPolicyRestartV2, factPolicies.get(0));
292         assertEquals(1, fsm.policiesMap.size());
293
294         // deploy another policy : firewall
295
296         String firewall =
297             new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-firewall.json")));
298         ToscaPolicy toscaPolicyFirewall = new StandardCoder().decode(firewall, ToscaPolicy.class);
299         update.setPolicies(Arrays.asList(toscaPolicyRestartV2, toscaPolicyFirewall));
300         assertTrue(fsm.update(update));
301         assertEquals(qlength + 6, fsm.client.getSink().getRecentEvents().length);
302         assertEquals(4, fsm.policyTypesMap.size());
303         cachedStatus = new StandardCoder()
304             .decode(fsm.client.getSink().getRecentEvents()[qlength + 5], PdpStatus.class);
305         assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
306
307         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
308         assertEquals(2, factPolicies.size());
309         assertTrue(factPolicies.stream().noneMatch((ff) -> Objects.equals(toscaPolicyRestartV1, ff)));
310         assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyRestartV2, ff)));
311         assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyFirewall, ff)));
312         assertEquals(2, fsm.policiesMap.size());
313
314         long originalInterval = fsm.getStatusTimerSeconds();
315         long interval = 10 * originalInterval;
316         update.setPdpHeartbeatIntervalMs(interval * 1000L);
317
318         assertTrue(fsm.update(update));
319
320         assertEquals(PdpState.ACTIVE, fsm.state());
321         assertEquals(interval, fsm.getStatusTimerSeconds());
322
323         assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
324
325         fsm.shutdown();
326     }
327
328 }