2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.lifecycle;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.util.Collections;
33 import org.junit.jupiter.api.AfterAll;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.common.utils.logging.LoggerUtils;
37 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
38 import org.onap.policy.drools.system.PolicyEngineConstants;
39 import org.onap.policy.models.pdp.concepts.PdpStateChange;
40 import org.onap.policy.models.pdp.concepts.PdpUpdate;
41 import org.onap.policy.models.pdp.enums.PdpState;
44 * Lifecycle State Terminated Tests.
46 public class LifecycleStateTerminatedTest {
47 private LifecycleFsm fsm = new LifecycleFsm();
50 public static void setUp() {
51 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
52 LoggerUtils.setLevel("org.onap.policy.common.endpoints", "WARN");
56 public static void tearDown() {
57 SystemPersistenceConstants.getManager().setConfigurationDir(null);
61 void testConstructor() {
62 assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateTerminated(null));
64 LifecycleState state = new LifecycleStateTerminated(new LifecycleFsm());
65 assertNull(state.fsm.source);
66 assertNull(state.fsm.client);
67 assertNull(state.fsm.statusTask);
69 assertEquals(PdpState.TERMINATED, state.state());
70 assertEquals(PdpState.TERMINATED, state.fsm.state.state());
71 assertFalse(state.isAlive());
76 assertEquals(PdpState.TERMINATED, fsm.state.state());
77 assertFalse(fsm.isAlive());
82 private void simpleStart() {
83 assertTrue(fsm.start());
87 private void simpleStop() {
88 assertTrue(fsm.stop());
89 assertBasicTerminated();
94 assertBasicTerminated();
98 assertFalse(fsm.source.isAlive());
99 assertFalse(fsm.client.getSink().isAlive());
100 assertExtendedTerminated();
104 void doubleBounce() {
110 void testDoubleStartBounce() {
112 assertFalse(fsm.start());
113 assertBasicPassive();
118 void testShutdown() {
119 assertBasicTerminated();
121 assertBasicTerminated();
123 fsm = new LifecycleFsm();
128 assertBasicTerminated();
129 assertFalse(fsm.status());
130 assertBasicTerminated();
135 assertFalse(fsm.state.transitionToState(new LifecycleStateTerminated(fsm)));
136 assertEquals(PdpState.TERMINATED, fsm.state.state());
141 PdpUpdate update = new PdpUpdate();
142 update.setName(PolicyEngineConstants.getManager().getPdpName());
143 update.setPdpGroup("A");
144 update.setPdpSubgroup("a");
145 update.setPoliciesToBeDeployed(Collections.emptyList());
146 update.setPdpHeartbeatIntervalMs(4 * 600000L);
148 assertFalse(fsm.update(update));
150 assertEquals(PdpState.TERMINATED, fsm.state.state());
151 assertNotEquals((4 * 60000L) / 1000L, fsm.getStatusTimerSeconds());
155 void testStateChange() {
156 PdpStateChange change = new PdpStateChange();
157 change.setPdpGroup("A");
158 change.setPdpSubgroup("a");
159 change.setState(PdpState.ACTIVE);
160 change.setName("test");
162 fsm.stateChange(change);
164 assertEquals(PdpState.TERMINATED, fsm.state.state());
167 private void assertBasicTerminated() {
168 assertEquals(PdpState.TERMINATED, fsm.state.state());
169 assertFalse(fsm.isAlive());
170 assertFalse(fsm.state.isAlive());
173 private void assertExtendedTerminated() {
174 assertBasicTerminated();
175 assertTrue(fsm.statusTask.isCancelled());
176 assertTrue(fsm.statusTask.isDone());
177 assertFalse(fsm.scheduler.isShutdown());
180 private void assertBasicPassive() {
181 assertEquals(PdpState.PASSIVE, fsm.state.state());
182 assertNotNull(fsm.source);
183 assertNotNull(fsm.client);
184 assertNotNull(fsm.statusTask);
186 assertTrue(fsm.isAlive());
187 assertTrue(fsm.source.isAlive());
188 assertTrue(fsm.client.getSink().isAlive());
190 assertFalse(fsm.statusTask.isCancelled());
191 assertFalse(fsm.statusTask.isDone());