2 * ============LICENSE_START=======================================================
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
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.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;
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;
56 * Lifecycle State Passive Tests.
58 public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
61 * Start tests in the Passive state.
64 public void startPassive() {
65 /* start every test in passive mode */
66 fsm = new LifecycleFsm();
67 fsm.setStatusTimerSeconds(15L);
70 assertEquals(0, fsm.client.getSink().getRecentEvents().length);
74 public void constructor() {
75 assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null));
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)));
86 fsm.stop(controllerSupport.getController());
87 assertNull(fsm.getController(
88 new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
89 ControllerSupport.POLICY_TYPE_VERSION)));
96 assertEquals(0, fsm.client.getSink().getRecentEvents().length);
97 assertFalse(fsm.start());
103 private Callable<Boolean> isStatus(PdpState state, int count) {
105 if (!fsm.client.getSink().isAlive()) {
109 if (fsm.client.getSink().getRecentEvents().length != count) {
113 String[] events = fsm.client.getSink().getRecentEvents();
115 new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
117 return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
124 assertBasicTerminated();
127 private void simpleStart() {
128 assertTrue(fsm.start());
129 assertBasicPassive();
132 private void simpleStop() {
133 assertTrue(fsm.stop());
134 assertBasicTerminated();
138 public void shutdown() throws CoderException {
142 assertExtendedTerminated();
146 public void status() {
147 status(PdpState.PASSIVE);
151 private void status(PdpState state) {
153 .atMost(5, TimeUnit.SECONDS)
154 .until(isStatus(state, 1));
157 .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
158 .until(isStatus(state, 2));
161 .atMost(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS)
162 .until(isStatus(state, 3));
164 assertTrue(fsm.status());
166 .atMost(200, TimeUnit.MILLISECONDS)
167 .until(isStatus(state, 4));
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());
178 long interval = 2 * fsm.getStatusTimerSeconds();
179 update.setPdpHeartbeatIntervalMs(interval * 1000L);
181 assertTrue(fsm.update(update));
183 assertEquals(PdpState.PASSIVE, fsm.state());
184 assertEquals(interval, fsm.getStatusTimerSeconds());
185 assertEquals("Z", fsm.getGroup());
186 assertEquals("z", fsm.getSubgroup());
187 assertBasicPassive();
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));
194 assertFalse(fsm.update(update));
196 assertEquals(PdpState.PASSIVE, fsm.state());
197 assertEquals(interval, fsm.getStatusTimerSeconds());
198 assertEquals("Z", fsm.getGroup());
199 assertEquals("z", fsm.getSubgroup());
200 assertBasicPassive();
202 assertTrue(fsm.policyTypesMap.isEmpty());
203 assertTrue(fsm.policiesMap.isEmpty());
205 fsm.start(controllerSupport.getController());
206 assertEquals(1, fsm.policyTypesMap.size());
207 assertTrue(fsm.policiesMap.isEmpty());
209 assertTrue(fsm.update(update));
210 assertEquals(1, fsm.policyTypesMap.size());
211 assertTrue(fsm.policiesMap.isEmpty());
217 public void stateChange() throws CoderException {
219 PdpStateChange change = new PdpStateChange();
220 change.setPdpGroup("A");
221 change.setPdpSubgroup("a");
222 change.setState(PdpState.ACTIVE);
225 change.setName("test");
226 fsm.source.offer(new StandardCoder().encode(change));
228 assertEquals(PdpState.PASSIVE, fsm.state());
229 assertNull(fsm.getGroup());
230 assertNull(fsm.getSubgroup());
233 change.setName(fsm.getName());
234 fsm.source.offer(new StandardCoder().encode(change));
236 assertEquals(PdpState.ACTIVE, fsm.state());
237 assertEquals("A", fsm.getGroup());
238 assertEquals("a", fsm.getSubgroup());
243 private void assertBasicTerminated() {
244 assertEquals(PdpState.TERMINATED, fsm.state.state());
245 assertFalse(fsm.isAlive());
246 assertFalse(fsm.state.isAlive());
249 private void assertExtendedTerminated() throws CoderException {
250 assertBasicTerminated();
251 assertTrue(fsm.statusTask.isCancelled());
252 assertTrue(fsm.statusTask.isDone());
254 String[] events = fsm.client.getSink().getRecentEvents();
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());
264 assertThatThrownBy( () -> await()
265 .atMost(2 * fsm.statusTimerSeconds, TimeUnit.SECONDS)
266 .until(isStatus(PdpState.TERMINATED, events.length))).isInstanceOf(ConditionTimeoutException.class);
269 private void assertBasicPassive() {
270 assertEquals(PdpState.PASSIVE, fsm.state.state());
271 assertNotNull(fsm.source);
272 assertNotNull(fsm.client);
273 assertNotNull(fsm.statusTask);
275 assertTrue(fsm.isAlive());
276 assertTrue(fsm.source.isAlive());
277 assertTrue(fsm.client.getSink().isAlive());
279 assertFalse(fsm.statusTask.isCancelled());
280 assertFalse(fsm.statusTask.isDone());