Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateTerminatedTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.lifecycle;
23
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;
31
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;
42
43 /**
44  * Lifecycle State Terminated Tests.
45  */
46 public class LifecycleStateTerminatedTest {
47     private LifecycleFsm fsm = new LifecycleFsm();
48
49     @BeforeAll
50     public static void setUp() {
51         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
52         LoggerUtils.setLevel("org.onap.policy.common.endpoints", "WARN");
53     }
54
55     @AfterAll
56     public static void tearDown() {
57         SystemPersistenceConstants.getManager().setConfigurationDir(null);
58     }
59
60     @Test
61     void testConstructor() {
62         assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateTerminated(null));
63
64         LifecycleState state = new LifecycleStateTerminated(new LifecycleFsm());
65         assertNull(state.fsm.source);
66         assertNull(state.fsm.client);
67         assertNull(state.fsm.statusTask);
68
69         assertEquals(PdpState.TERMINATED, state.state());
70         assertEquals(PdpState.TERMINATED, state.fsm.state.state());
71         assertFalse(state.isAlive());
72     }
73
74     @Test
75     void testStop() {
76         assertEquals(PdpState.TERMINATED, fsm.state.state());
77         assertFalse(fsm.isAlive());
78
79         simpleStop();
80     }
81
82     private void simpleStart() {
83         assertTrue(fsm.start());
84         assertBasicPassive();
85     }
86
87     private void simpleStop() {
88         assertTrue(fsm.stop());
89         assertBasicTerminated();
90     }
91
92     @Test
93     void testBounce() {
94         assertBasicTerminated();
95         simpleStart();
96         simpleStop();
97
98         assertFalse(fsm.source.isAlive());
99         assertFalse(fsm.client.getSink().isAlive());
100         assertExtendedTerminated();
101     }
102
103     @Test
104     void doubleBounce() {
105         testBounce();
106         testBounce();
107     }
108
109     @Test
110     void testDoubleStartBounce() {
111         simpleStart();
112         assertFalse(fsm.start());
113         assertBasicPassive();
114         simpleStop();
115     }
116
117     @Test
118     void testShutdown() {
119         assertBasicTerminated();
120         fsm.shutdown();
121         assertBasicTerminated();
122
123         fsm = new LifecycleFsm();
124     }
125
126     @Test
127     void testStatus() {
128         assertBasicTerminated();
129         assertFalse(fsm.status());
130         assertBasicTerminated();
131     }
132
133     @Test
134     void changeState() {
135         assertFalse(fsm.state.transitionToState(new LifecycleStateTerminated(fsm)));
136         assertEquals(PdpState.TERMINATED, fsm.state.state());
137     }
138
139     @Test
140     void testUpdate() {
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);
147
148         assertFalse(fsm.update(update));
149
150         assertEquals(PdpState.TERMINATED, fsm.state.state());
151         assertNotEquals((4 * 60000L) / 1000L, fsm.getStatusTimerSeconds());
152     }
153
154     @Test
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");
161
162         fsm.stateChange(change);
163
164         assertEquals(PdpState.TERMINATED, fsm.state.state());
165     }
166
167     private void assertBasicTerminated() {
168         assertEquals(PdpState.TERMINATED, fsm.state.state());
169         assertFalse(fsm.isAlive());
170         assertFalse(fsm.state.isAlive());
171     }
172
173     private void assertExtendedTerminated() {
174         assertBasicTerminated();
175         assertTrue(fsm.statusTask.isCancelled());
176         assertTrue(fsm.statusTask.isDone());
177         assertFalse(fsm.scheduler.isShutdown());
178     }
179
180     private void assertBasicPassive() {
181         assertEquals(PdpState.PASSIVE, fsm.state.state());
182         assertNotNull(fsm.source);
183         assertNotNull(fsm.client);
184         assertNotNull(fsm.statusTask);
185
186         assertTrue(fsm.isAlive());
187         assertTrue(fsm.source.isAlive());
188         assertTrue(fsm.client.getSink().isAlive());
189
190         assertFalse(fsm.statusTask.isCancelled());
191         assertFalse(fsm.statusTask.isDone());
192     }
193 }