1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* =============LICENSE_END========================================================
*/
package org.onap.policy.drools.lifecycle;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.drools.persistence.SystemPersistence;
import org.onap.policy.drools.utils.logging.LoggerUtil;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.enums.PdpMessageType;
import org.onap.policy.models.pdp.enums.PdpState;
/**
* Lifecycle State Active Test.
*/
public class LifecycleStateActiveTest {
private LifecycleFsm fsm;
@BeforeClass
public static void setUp() {
SystemPersistence.manager.setConfigurationDir("src/test/resources");
LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN");
}
@AfterClass
public static void tearDown() {
SystemPersistence.manager.setConfigurationDir(null);
}
/**
* Start tests in the Active state.
*/
@Before
public void startActive() throws CoderException {
fsm = new LifecycleFsm();
fsm.setStatusTimerSeconds(15);
assertTrue(fsm.start());
goActive();
assertActive();
}
private void goActive() throws CoderException {
PdpStateChange change = new PdpStateChange();
change.setPdpGroup("A");
change.setPdpSubgroup("a");
change.setState(PdpState.ACTIVE);
change.setName("test");
fsm.source.offer(new StandardCoder().encode(change));
}
@Test
public void constructor() {
assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateActive(null));
fsm.shutdown();
}
@Test
public void start() {
assertActive();
assertFalse(fsm.start());
assertActive();
fsm.shutdown();
}
private void assertActive() {
assertEquals(PdpState.ACTIVE, fsm.state());
assertEquals("A", fsm.getPdpGroup());
assertEquals("a", fsm.getPdpSubgroup());
assertTrue(fsm.isAlive());
await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
}
@Test
public void stop() {
assertTrue(fsm.stop());
assertBasicTerminated();
fsm.shutdown();
}
private void assertBasicTerminated() {
assertEquals(PdpState.TERMINATED, fsm.state());
assertFalse(fsm.isAlive());
assertFalse(fsm.state.isAlive());
await().atMost(1, TimeUnit.SECONDS).until(isStatus(PdpState.TERMINATED));
}
@Test
public void shutdown() {
fsm.shutdown();
assertBasicTerminated();
assertTrue(fsm.statusTask.isCancelled());
assertTrue(fsm.statusTask.isDone());
}
private Callable<Boolean> isStatus(PdpState state) {
return () -> {
if (fsm.client.getSink().getRecentEvents().length == 0) {
return false;
}
List<String> events = Arrays.asList(fsm.client.getSink().getRecentEvents());
PdpStatus status =
new StandardCoder().decode(events.get(events.size() - 1), PdpStatus.class);
return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
};
}
@Test
public void status() {
await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
int preCount = fsm.client.getSink().getRecentEvents().length;
assertTrue(fsm.status());
assertEquals(preCount + 1, fsm.client.getSink().getRecentEvents().length);
fsm.shutdown();
}
@Test
public void stateChange() throws CoderException {
assertActive();
/* dup */
PdpStateChange change = new PdpStateChange();
change.setPdpGroup("B");
change.setPdpSubgroup("b");
change.setState(PdpState.ACTIVE);
change.setName("test");
fsm.source.offer(new StandardCoder().encode(change));
assertEquals(PdpState.ACTIVE, fsm.state());
assertEquals("B", fsm.getPdpGroup());
assertEquals("b", fsm.getPdpSubgroup());
change.setState(PdpState.SAFE);
fsm.source.offer(new StandardCoder().encode(change));
assertEquals(PdpState.ACTIVE, fsm.state());
change.setState(PdpState.TERMINATED);
fsm.source.offer(new StandardCoder().encode(change));
assertEquals(PdpState.ACTIVE, fsm.state());
change.setState(PdpState.PASSIVE);
fsm.source.offer(new StandardCoder().encode(change));
assertEquals(PdpState.PASSIVE, fsm.state());
await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.PASSIVE));
fsm.shutdown();
}
}
|