2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.lifecycle;
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;
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;
52 * Lifecycle State Passive Tests.
54 public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
57 * Start tests in the Passive state.
60 public void startPassive() {
61 /* start every test in passive mode */
62 fsm = makeFsmWithPseudoTime();
63 fsm.setStatusTimerSeconds(15L);
68 public void constructor() {
69 assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null));
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)));
80 fsm.stop(controllerSupport.getController());
81 assertNull(fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
82 ControllerSupport.POLICY_TYPE_VERSION)));
89 assertEquals(0, fsm.client.getSink().getRecentEvents().length);
90 assertFalse(fsm.start());
99 assertBasicTerminated();
102 private void simpleStart() {
103 assertTrue(fsm.start());
104 assertBasicPassive();
107 private void simpleStop() {
108 assertTrue(fsm.stop());
109 assertBasicTerminated();
113 public void shutdown() throws Exception {
117 assertExtendedTerminated();
121 public void status() {
122 assertTrue(fsm.client.getSink().isAlive());
123 assertTrue(fsm.status());
124 assertSame(0, fsm.client.getSink().getRecentEvents().length);
127 fsm.start(controllerSupport.getController());
128 status(PdpState.PASSIVE, 1);
129 fsm.stop(controllerSupport.getController());
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));
142 public void update() throws IOException, CoderException {
143 controllerSupport.getController().getDrools().delete(ToscaPolicy.class);
144 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
146 PdpUpdate update = new PdpUpdate();
147 update.setName(NetworkUtil.getHostname());
148 update.setPdpGroup("Z");
149 update.setPdpSubgroup("z");
150 update.setPolicies(Collections.emptyList());
152 long interval = 2 * fsm.getStatusTimerSeconds();
153 update.setPdpHeartbeatIntervalMs(interval * 1000L);
155 assertTrue(fsm.update(update));
157 int qlength = fsm.client.getSink().getRecentEvents().length;
158 PdpStatus lastStatus = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[qlength - 1],
160 assertEquals(update.getRequestId(), lastStatus.getRequestId());
161 assertEquals(update.getRequestId(), lastStatus.getResponse().getResponseTo());
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();
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));
174 assertFalse(fsm.update(update));
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();
182 assertTrue(fsm.policyTypesMap.isEmpty());
183 assertTrue(fsm.policiesMap.isEmpty());
185 update.setPdpGroup(null);
186 update.setPdpSubgroup(null);
188 assertFalse(fsm.update(update));
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());
198 update.setPdpGroup("A");
199 update.setPdpSubgroup("a");
201 assertFalse(fsm.update(update));
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());
211 fsm.start(controllerSupport.getController());
212 assertEquals(1, fsm.policyTypesMap.size());
213 assertTrue(fsm.policiesMap.isEmpty());
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"));
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"));
243 public void stateChange() throws CoderException, IOException {
245 PdpStateChange change = new PdpStateChange();
246 change.setPdpGroup("A");
247 change.setPdpSubgroup("a");
248 change.setState(PdpState.ACTIVE);
251 change.setName("test");
252 fsm.source.offer(new StandardCoder().encode(change));
254 assertEquals(PdpState.PASSIVE, fsm.state());
255 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
256 assertNull(fsm.getSubgroup());
258 PdpUpdate update = new PdpUpdate();
259 update.setName(NetworkUtil.getHostname());
260 update.setPdpGroup("A");
261 update.setPdpSubgroup("a");
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));
268 controllerSupport.getController().start();
269 fsm.start(controllerSupport.getController());
270 assertEquals(1, fsm.policyTypesMap.size());
271 assertTrue(fsm.policiesMap.isEmpty());
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"));
284 change.setName(fsm.getName());
285 fsm.source.offer(new StandardCoder().encode(change));
287 assertEquals(PdpState.ACTIVE, fsm.state());
288 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
289 assertEquals("a", fsm.getSubgroup());
291 waitUntil(5, TimeUnit.SECONDS, () -> controllerSupport.getController().getDrools().factCount("junits") == 1);
293 assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
294 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
299 private void assertBasicTerminated() {
300 assertEquals(PdpState.TERMINATED, fsm.state.state());
301 assertFalse(fsm.isAlive());
302 assertFalse(fsm.state.isAlive());
305 private void assertExtendedTerminated() throws Exception {
306 assertBasicTerminated();
307 assertTrue(fsm.statusTask.isCancelled());
308 assertTrue(fsm.statusTask.isDone());
310 // verify there are no outstanding tasks that might change the state
311 assertTrue(time.isEmpty());
313 assertFalse(fsm.client.getSink().isAlive());
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());
325 private void assertBasicPassive() {
326 assertEquals(PdpState.PASSIVE, fsm.state.state());
327 assertNotNull(fsm.source);
328 assertNotNull(fsm.client);
329 assertNotNull(fsm.statusTask);
331 assertTrue(fsm.isAlive());
332 assertTrue(fsm.source.isAlive());
333 assertTrue(fsm.client.getSink().isAlive());
335 assertFalse(fsm.statusTask.isCancelled());
336 assertFalse(fsm.statusTask.isDone());