Merge "Enabling logback file to be loaded using oom configmap"
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStatePassiveTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. 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  * ============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.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.concurrent.TimeUnit;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.models.pdp.concepts.PdpStateChange;
43 import org.onap.policy.models.pdp.concepts.PdpStatus;
44 import org.onap.policy.models.pdp.concepts.PdpUpdate;
45 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
46 import org.onap.policy.models.pdp.enums.PdpMessageType;
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 Passive Tests.
53  */
54 public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
55
56     /**
57      * Start tests in the Passive state.
58      */
59     @Before
60     public void startPassive() {
61         /* start every test in passive mode */
62         fsm = makeFsmWithPseudoTime();
63         fsm.setStatusTimerSeconds(15L);
64         simpleStart();
65     }
66
67     @Test
68     public void constructor() {
69         assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null));
70         fsm.shutdown();
71     }
72
73     @Test
74     public void controller() {
75         fsm.start(controllerSupport.getController());
76         assertSame(controllerSupport.getController(),
77                         fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
78                                         ControllerSupport.POLICY_TYPE_VERSION)));
79
80         fsm.stop(controllerSupport.getController());
81         assertNull(fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
82                         ControllerSupport.POLICY_TYPE_VERSION)));
83
84         fsm.shutdown();
85     }
86
87     @Test
88     public void start() {
89         assertEquals(0, fsm.client.getSink().getRecentEvents().length);
90         assertFalse(fsm.start());
91         assertBasicPassive();
92
93         fsm.shutdown();
94     }
95
96     @Test
97     public void stop() {
98         simpleStop();
99         assertBasicTerminated();
100     }
101
102     private void simpleStart() {
103         assertTrue(fsm.start());
104         assertBasicPassive();
105     }
106
107     private void simpleStop() {
108         assertTrue(fsm.stop());
109         assertBasicTerminated();
110     }
111
112     @Test
113     public void shutdown() throws Exception {
114         simpleStop();
115
116         fsm.shutdown();
117         assertExtendedTerminated();
118     }
119
120     @Test
121     public void status() {
122         assertTrue(fsm.client.getSink().isAlive());
123         assertTrue(fsm.status());
124         assertSame(0, fsm.client.getSink().getRecentEvents().length);
125
126
127         fsm.start(controllerSupport.getController());
128         status(PdpState.PASSIVE, 1);
129         fsm.stop(controllerSupport.getController());
130         fsm.shutdown();
131     }
132
133     private void status(PdpState state, int initial) {
134         waitUntil(5, TimeUnit.SECONDS, isStatus(state, initial));
135         waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(state, initial + 1));
136         waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(state, initial + 2));
137         assertTrue(fsm.status());
138         waitUntil(200, TimeUnit.MILLISECONDS, isStatus(state, initial + 3));
139     }
140
141     @Test
142     public void update() throws IOException, CoderException {
143         controllerSupport.getController().getDrools().delete(ToscaPolicy.class);
144         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
145
146         PdpUpdate update = new PdpUpdate();
147         update.setName(NetworkUtil.getHostname());
148         update.setPdpGroup("Z");
149         update.setPdpSubgroup("z");
150         update.setPolicies(Collections.emptyList());
151
152         long interval = 2 * fsm.getStatusTimerSeconds();
153         update.setPdpHeartbeatIntervalMs(interval * 1000L);
154
155         assertTrue(fsm.update(update));
156
157         int qlength = fsm.client.getSink().getRecentEvents().length;
158         PdpStatus lastStatus = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[qlength - 1],
159                         PdpStatus.class);
160         assertEquals(update.getRequestId(), lastStatus.getRequestId());
161         assertEquals(update.getRequestId(), lastStatus.getResponse().getResponseTo());
162
163         assertEquals(PdpState.PASSIVE, fsm.state());
164         assertEquals(interval, fsm.getStatusTimerSeconds());
165         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
166         assertEquals("z", fsm.getSubgroup());
167         assertBasicPassive();
168
169         String rawPolicy = new String(
170                         Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
171         ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
172         update.setPolicies(Arrays.asList(toscaPolicy));
173
174         assertFalse(fsm.update(update));
175
176         assertEquals(PdpState.PASSIVE, fsm.state());
177         assertEquals(interval, fsm.getStatusTimerSeconds());
178         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
179         assertEquals("z", fsm.getSubgroup());
180         assertBasicPassive();
181
182         assertTrue(fsm.policyTypesMap.isEmpty());
183         assertTrue(fsm.policiesMap.isEmpty());
184
185         update.setPdpGroup(null);
186         update.setPdpSubgroup(null);
187
188         assertFalse(fsm.update(update));
189
190         assertEquals(PdpState.PASSIVE, fsm.state());
191         assertEquals(interval, fsm.getStatusTimerSeconds());
192         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
193         assertNull(fsm.getSubgroup());
194         assertBasicPassive();
195         assertTrue(fsm.policyTypesMap.isEmpty());
196         assertTrue(fsm.policiesMap.isEmpty());
197
198         update.setPdpGroup("A");
199         update.setPdpSubgroup("a");
200
201         assertFalse(fsm.update(update));
202
203         assertEquals(PdpState.PASSIVE, fsm.state());
204         assertEquals(interval, fsm.getStatusTimerSeconds());
205         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
206         assertEquals("a", fsm.getSubgroup());
207         assertBasicPassive();
208         assertTrue(fsm.policyTypesMap.isEmpty());
209         assertTrue(fsm.policiesMap.isEmpty());
210
211         fsm.start(controllerSupport.getController());
212         assertEquals(1, fsm.policyTypesMap.size());
213         assertTrue(fsm.policiesMap.isEmpty());
214
215         assertTrue(fsm.update(update));
216         assertEquals(1, fsm.policyTypesMap.size());
217         assertEquals(1, fsm.policiesMap.size());
218         assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy);
219         assertEquals(PdpState.PASSIVE, fsm.state());
220         assertEquals(interval, fsm.getStatusTimerSeconds());
221         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
222         assertEquals("a", fsm.getSubgroup());
223         assertBasicPassive();
224         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
225
226         update.setPdpGroup(null);
227         update.setPdpSubgroup(null);
228         update.setPolicies(Collections.emptyList());
229         assertTrue(fsm.update(update));
230         assertEquals(1, fsm.policyTypesMap.size());
231         assertEquals(0, fsm.policiesMap.size());
232         assertEquals(PdpState.PASSIVE, fsm.state());
233         assertEquals(interval, fsm.getStatusTimerSeconds());
234         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
235         assertNull(fsm.getSubgroup());
236         assertBasicPassive();
237         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
238
239         fsm.shutdown();
240     }
241
242     @Test
243     public void stateChange() throws CoderException, IOException {
244         /* no name */
245         PdpStateChange change = new PdpStateChange();
246         change.setPdpGroup("A");
247         change.setPdpSubgroup("a");
248         change.setState(PdpState.ACTIVE);
249
250         /* invalid name */
251         change.setName("test");
252         fsm.source.offer(new StandardCoder().encode(change));
253
254         assertEquals(PdpState.PASSIVE, fsm.state());
255         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
256         assertNull(fsm.getSubgroup());
257
258         PdpUpdate update = new PdpUpdate();
259         update.setName(NetworkUtil.getHostname());
260         update.setPdpGroup("A");
261         update.setPdpSubgroup("a");
262
263         String rawPolicy = new String(
264                         Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
265         ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
266         update.setPolicies(Arrays.asList(toscaPolicy));
267
268         controllerSupport.getController().start();
269         fsm.start(controllerSupport.getController());
270         assertEquals(1, fsm.policyTypesMap.size());
271         assertTrue(fsm.policiesMap.isEmpty());
272
273         assertTrue(fsm.update(update));
274         assertEquals(1, fsm.policyTypesMap.size());
275         assertEquals(1, fsm.policiesMap.size());
276         assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy);
277         assertEquals(PdpState.PASSIVE, fsm.state());
278         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
279         assertEquals("a", fsm.getSubgroup());
280         assertBasicPassive();
281         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
282
283         /* correct name */
284         change.setName(fsm.getName());
285         fsm.source.offer(new StandardCoder().encode(change));
286
287         assertEquals(PdpState.ACTIVE, fsm.state());
288         assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
289         assertEquals("a", fsm.getSubgroup());
290
291         waitUntil(5, TimeUnit.SECONDS, () -> controllerSupport.getController().getDrools().factCount("junits") == 1);
292
293         assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
294         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
295
296         fsm.shutdown();
297     }
298
299     private void assertBasicTerminated() {
300         assertEquals(PdpState.TERMINATED, fsm.state.state());
301         assertFalse(fsm.isAlive());
302         assertFalse(fsm.state.isAlive());
303     }
304
305     private void assertExtendedTerminated() throws Exception {
306         assertBasicTerminated();
307         assertTrue(fsm.statusTask.isCancelled());
308         assertTrue(fsm.statusTask.isDone());
309
310         // verify there are no outstanding tasks that might change the state
311         assertTrue(time.isEmpty());
312
313         assertFalse(fsm.client.getSink().isAlive());
314
315         String[] events = fsm.client.getSink().getRecentEvents();
316         PdpStatus status = new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
317         assertEquals("drools", status.getPdpType());
318         assertEquals(PdpState.TERMINATED, status.getState());
319         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
320         assertEquals(NetworkUtil.getHostname(), status.getName());
321         assertEquals(fsm.getName(), status.getName());
322         assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName());
323     }
324
325     private void assertBasicPassive() {
326         assertEquals(PdpState.PASSIVE, fsm.state.state());
327         assertNotNull(fsm.source);
328         assertNotNull(fsm.client);
329         assertNotNull(fsm.statusTask);
330
331         assertTrue(fsm.isAlive());
332         assertTrue(fsm.source.isAlive());
333         assertTrue(fsm.client.getSink().isAlive());
334
335         assertFalse(fsm.statusTask.isCancelled());
336         assertFalse(fsm.statusTask.isDone());
337     }
338 }