deploy and undeploy as audits.
[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 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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.concurrent.Callable;
39 import java.util.concurrent.TimeUnit;
40 import org.awaitility.core.ConditionTimeoutException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.onap.policy.models.pdp.concepts.PdpStateChange;
47 import org.onap.policy.models.pdp.concepts.PdpStatus;
48 import org.onap.policy.models.pdp.concepts.PdpUpdate;
49 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
50 import org.onap.policy.models.pdp.enums.PdpMessageType;
51 import org.onap.policy.models.pdp.enums.PdpState;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
54
55 /**
56  * Lifecycle State Passive Tests.
57  */
58 public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
59
60     /**
61      * Start tests in the Passive state.
62      */
63     @Before
64     public void startPassive() {
65         /* start every test in passive mode */
66         fsm = new LifecycleFsm();
67         fsm.setStatusTimerSeconds(15L);
68         simpleStart();
69
70         assertEquals(0, fsm.client.getSink().getRecentEvents().length);
71     }
72
73     @Test
74     public void constructor() {
75         assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null));
76         fsm.shutdown();
77     }
78
79     @Test
80     public void controller() {
81         fsm.start(controllerSupport.getController());
82         assertSame(controllerSupport.getController(),
83             fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
84                 ControllerSupport.POLICY_TYPE_VERSION)));
85
86         fsm.stop(controllerSupport.getController());
87         assertNull(fsm.getController(
88             new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
89                                            ControllerSupport.POLICY_TYPE_VERSION)));
90
91         fsm.shutdown();
92     }
93
94     @Test
95     public void start() {
96         assertEquals(0, fsm.client.getSink().getRecentEvents().length);
97         assertFalse(fsm.start());
98         assertBasicPassive();
99
100         fsm.shutdown();
101     }
102
103     private Callable<Boolean> isStatus(PdpState state, int count) {
104         return () -> {
105             if (!fsm.client.getSink().isAlive()) {
106                 return false;
107             }
108
109             if (fsm.client.getSink().getRecentEvents().length != count) {
110                 return false;
111             }
112
113             String[] events = fsm.client.getSink().getRecentEvents();
114             PdpStatus status =
115                 new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
116
117             return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
118         };
119     }
120
121     @Test
122     public void stop() {
123         simpleStop();
124         assertBasicTerminated();
125     }
126
127     private void simpleStart() {
128         assertTrue(fsm.start());
129         assertBasicPassive();
130     }
131
132     private void simpleStop() {
133         assertTrue(fsm.stop());
134         assertBasicTerminated();
135     }
136
137     @Test
138     public void shutdown() throws CoderException {
139         simpleStop();
140
141         fsm.shutdown();
142         assertExtendedTerminated();
143     }
144
145     @Test
146     public void status() {
147         status(PdpState.PASSIVE);
148         fsm.shutdown();
149     }
150
151     private void status(PdpState state) {
152         await()
153             .atMost(5, TimeUnit.SECONDS)
154             .until(isStatus(state, 1));
155
156         await()
157             .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
158             .until(isStatus(state, 2));
159
160         await()
161             .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
162             .until(isStatus(state, 3));
163
164         assertTrue(fsm.status());
165         await()
166             .atMost(200, TimeUnit.MILLISECONDS)
167             .until(isStatus(state, 4));
168     }
169
170     @Test
171     public void update() throws IOException, CoderException {
172         PdpUpdate update = new PdpUpdate();
173         update.setName(NetworkUtil.getHostname());
174         update.setPdpGroup("Z");
175         update.setPdpSubgroup("z");
176         update.setPolicies(Collections.emptyList());
177
178         long interval = 2 * fsm.getStatusTimerSeconds();
179         update.setPdpHeartbeatIntervalMs(interval * 1000L);
180
181         assertTrue(fsm.update(update));
182
183         assertEquals(PdpState.PASSIVE, fsm.state());
184         assertEquals(interval, fsm.getStatusTimerSeconds());
185         assertEquals("Z", fsm.getGroup());
186         assertEquals("z", fsm.getSubgroup());
187         assertBasicPassive();
188
189         String rawPolicy =
190             new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
191         ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
192         update.setPolicies(Arrays.asList(toscaPolicy));
193
194         assertFalse(fsm.update(update));
195
196         assertEquals(PdpState.PASSIVE, fsm.state());
197         assertEquals(interval, fsm.getStatusTimerSeconds());
198         assertEquals("Z", fsm.getGroup());
199         assertEquals("z", fsm.getSubgroup());
200         assertBasicPassive();
201
202         assertTrue(fsm.policyTypesMap.isEmpty());
203         assertTrue(fsm.policiesMap.isEmpty());
204
205         fsm.start(controllerSupport.getController());
206         assertEquals(1, fsm.policyTypesMap.size());
207         assertTrue(fsm.policiesMap.isEmpty());
208
209         assertTrue(fsm.update(update));
210         assertEquals(1, fsm.policyTypesMap.size());
211         assertTrue(fsm.policiesMap.isEmpty());
212
213         fsm.shutdown();
214     }
215
216     @Test
217     public void stateChange() throws CoderException {
218         /* no name */
219         PdpStateChange change = new PdpStateChange();
220         change.setPdpGroup("A");
221         change.setPdpSubgroup("a");
222         change.setState(PdpState.ACTIVE);
223
224         /* invalid name */
225         change.setName("test");
226         fsm.source.offer(new StandardCoder().encode(change));
227
228         assertEquals(PdpState.PASSIVE, fsm.state());
229         assertNull(fsm.getGroup());
230         assertNull(fsm.getSubgroup());
231
232         /* correct name */
233         change.setName(fsm.getName());
234         fsm.source.offer(new StandardCoder().encode(change));
235
236         assertEquals(PdpState.ACTIVE, fsm.state());
237         assertEquals("A", fsm.getGroup());
238         assertEquals("a", fsm.getSubgroup());
239
240         fsm.shutdown();
241     }
242
243     private void assertBasicTerminated() {
244         assertEquals(PdpState.TERMINATED, fsm.state.state());
245         assertFalse(fsm.isAlive());
246         assertFalse(fsm.state.isAlive());
247     }
248
249     private void assertExtendedTerminated() throws CoderException {
250         assertBasicTerminated();
251         assertTrue(fsm.statusTask.isCancelled());
252         assertTrue(fsm.statusTask.isDone());
253
254         String[] events = fsm.client.getSink().getRecentEvents();
255         PdpStatus status =
256             new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
257         assertEquals("drools", status.getPdpType());
258         assertEquals(PdpState.TERMINATED, status.getState());
259         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
260         assertEquals(NetworkUtil.getHostname(), status.getName());
261         assertEquals(fsm.getName(), status.getName());
262         assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName());
263
264         assertThatThrownBy( () -> await()
265             .atMost(2 * fsm.statusTimerSeconds, TimeUnit.SECONDS)
266             .until(isStatus(PdpState.TERMINATED, events.length))).isInstanceOf(ConditionTimeoutException.class);
267     }
268
269     private void assertBasicPassive() {
270         assertEquals(PdpState.PASSIVE, fsm.state.state());
271         assertNotNull(fsm.source);
272         assertNotNull(fsm.client);
273         assertNotNull(fsm.statusTask);
274
275         assertTrue(fsm.isAlive());
276         assertTrue(fsm.source.isAlive());
277         assertTrue(fsm.client.getSink().isAlive());
278
279         assertFalse(fsm.statusTask.isCancelled());
280         assertFalse(fsm.statusTask.isDone());
281     }
282 }