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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/*
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2018 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.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.drools.pooling.state;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.drools.pooling.message.BucketAssignments;
import org.onap.policy.drools.pooling.message.Identification;
import org.onap.policy.drools.pooling.message.Leader;
import org.onap.policy.drools.pooling.message.Message;
import org.onap.policy.drools.pooling.message.Offline;
import org.onap.policy.drools.utils.Pair;
public class QueryStateTest extends SupportBasicStateTester {
private QueryState state;
/**
* Setup.
*/
@Before
public void setUp() throws Exception {
super.setUp();
state = new QueryState(mgr);
}
@Test
public void testGetFilter() {
Map<String, Object> filter = state.getFilter();
FilterUtilsTest utils = new FilterUtilsTest();
utils.checkArray(FilterUtils.CLASS_OR, 2, filter);
utils.checkEquals(FilterUtils.MSG_CHANNEL, Message.ADMIN, utils.getItem(filter, 0));
utils.checkEquals(FilterUtils.MSG_CHANNEL, MY_HOST, utils.getItem(filter, 1));
}
@Test
public void testGoQuery() {
assertNull(state.goQuery());
}
@Test
public void testStart() {
state.start();
Pair<Long, StateTimerTask> timer = onceTasks.remove();
assertEquals(STD_IDENTIFICATION_MS, timer.first().longValue());
assertNotNull(timer.second());
}
@Test
public void testProcessIdentification_SameSource() {
String[] arr = {HOST2, PREV_HOST, MY_HOST};
BucketAssignments asgn = new BucketAssignments(arr);
assertNull(state.process(new Identification(MY_HOST, asgn)));
// info should be unchanged
assertEquals(MY_HOST, state.getLeader());
verify(mgr, never()).startDistributing(asgn);
}
@Test
public void testProcessIdentification_DiffSource() {
String[] arr = {HOST2, PREV_HOST, MY_HOST};
BucketAssignments asgn = new BucketAssignments(arr);
assertNull(state.process(new Identification(HOST2, asgn)));
// leader should be unchanged
assertEquals(MY_HOST, state.getLeader());
// should have picked up the assignments
verify(mgr).startDistributing(asgn);
}
@Test
public void testProcessLeader_Invalid() {
Leader msg = new Leader(PREV_HOST, null);
// should stay in the same state, and not start distributing
assertNull(state.process(msg));
verify(mgr, never()).startDistributing(any());
verify(mgr, never()).goActive();
verify(mgr, never()).goInactive();
// info should be unchanged
assertEquals(MY_HOST, state.getLeader());
assertEquals(ASGN3, state.getAssignments());
}
@Test
public void testProcessLeader_SameLeader() {
String[] arr = {HOST2, PREV_HOST, MY_HOST};
BucketAssignments asgn = new BucketAssignments(arr);
// identify a leader that's better than my host
assertEquals(null, state.process(new Identification(PREV_HOST, asgn)));
// now send a Leader message for that leader
Leader msg = new Leader(PREV_HOST, asgn);
State next = mock(State.class);
when(mgr.goActive()).thenReturn(next);
// should go Active and start distributing
assertEquals(next, state.process(msg));
verify(mgr, never()).goInactive();
// Ident msg + Leader msg = times(2)
verify(mgr, times(2)).startDistributing(asgn);
}
@Test
public void testProcessLeader_BetterLeaderWithAssignment() {
String[] arr = {HOST2, PREV_HOST, MY_HOST};
BucketAssignments asgn = new BucketAssignments(arr);
Leader msg = new Leader(PREV_HOST, asgn);
State next = mock(State.class);
when(mgr.goActive()).thenReturn(next);
// should go Active and start distributing
assertEquals(next, state.process(msg));
verify(mgr).startDistributing(asgn);
verify(mgr, never()).goInactive();
}
@Test
public void testProcessLeader_BetterLeaderWithoutAssignment() {
String[] arr = {HOST2, PREV_HOST, HOST1};
BucketAssignments asgn = new BucketAssignments(arr);
Leader msg = new Leader(PREV_HOST, asgn);
State next = mock(State.class);
when(mgr.goInactive()).thenReturn(next);
// should go Inactive, but start distributing
assertEquals(next, state.process(msg));
verify(mgr).startDistributing(asgn);
verify(mgr, never()).goActive();
}
@Test
public void testProcessLeader_NotABetterLeader() {
// no assignments yet
mgr.startDistributing(null);
state = new QueryState(mgr);
BucketAssignments asgn = new BucketAssignments(new String[] {HOST1, HOST2});
Leader msg = new Leader(HOST1, asgn);
State next = mock(State.class);
when(mgr.goInactive()).thenReturn(next);
// should stay in the same state
assertNull(state.process(msg));
verify(mgr, never()).goActive();
verify(mgr, never()).goInactive();
// should have started distributing
verify(mgr).startDistributing(asgn);
// this host should still be the leader
assertEquals(MY_HOST, state.getLeader());
// new assignments
assertEquals(asgn, state.getAssignments());
}
@Test
public void testProcessOffline_NullHost() {
assertNull(state.process(new Offline()));
assertEquals(MY_HOST, state.getLeader());
}
@Test
public void testProcessOffline_SameHost() {
assertNull(state.process(new Offline(MY_HOST)));
assertEquals(MY_HOST, state.getLeader());
}
@Test
public void testProcessOffline_DiffHost() {
BucketAssignments asgn = new BucketAssignments(new String[] {PREV_HOST, HOST1});
mgr.startDistributing(asgn);
state = new QueryState(mgr);
// tell it that the hosts are alive
state.process(new Identification(PREV_HOST, asgn));
state.process(new Identification(HOST1, asgn));
// #2 goes offline
assertNull(state.process(new Offline(HOST1)));
// #1 should still be the leader
assertEquals(PREV_HOST, state.getLeader());
// #1 goes offline
assertNull(state.process(new Offline(PREV_HOST)));
// this should still be the leader now
assertEquals(MY_HOST, state.getLeader());
}
@Test
public void testQueryState() {
/*
* Prove the state is attached to the manager by invoking getHost(), which
* delegates to the manager.
*/
assertEquals(MY_HOST, state.getHost());
}
@Test
public void testAwaitIdentification_MissingSelfIdent() {
state.start();
Pair<Long, StateTimerTask> timer = onceTasks.remove();
assertEquals(STD_IDENTIFICATION_MS, timer.first().longValue());
assertNotNull(timer.second());
// should published an Offline message and go inactive
State next = mock(State.class);
when(mgr.goStart()).thenReturn(next);
assertEquals(next, timer.second().fire());
// should continue distributing
verify(mgr, never()).startDistributing(null);
Offline msg = captureAdminMessage(Offline.class);
assertEquals(MY_HOST, msg.getSource());
}
@Test
public void testAwaitIdentification_Leader() {
state.start();
state.process(new Identification(MY_HOST, null));
Pair<Long, StateTimerTask> timer = onceTasks.remove();
assertEquals(STD_IDENTIFICATION_MS, timer.first().longValue());
assertNotNull(timer.second());
State next = mock(State.class);
when(mgr.goActive()).thenReturn(next);
assertEquals(next, timer.second().fire());
// should have published a Leader message
Leader msg = captureAdminMessage(Leader.class);
assertEquals(MY_HOST, msg.getSource());
assertTrue(msg.getAssignments().hasAssignment(MY_HOST));
}
@Test
public void testAwaitIdentification_HasAssignment() {
// not the leader, but has an assignment
BucketAssignments asgn = new BucketAssignments(new String[] {PREV_HOST, MY_HOST, HOST2});
mgr.startDistributing(asgn);
state = new QueryState(mgr);
state.start();
state.process(new Identification(MY_HOST, null));
// tell it the leader is still active
state.process(new Identification(PREV_HOST, asgn));
Pair<Long, StateTimerTask> timer = onceTasks.remove();
assertEquals(STD_IDENTIFICATION_MS, timer.first().longValue());
assertNotNull(timer.second());
// set up active state, as that's what it should return
State next = mock(State.class);
when(mgr.goActive()).thenReturn(next);
assertEquals(next, timer.second().fire());
// should NOT have published a Leader message
assertTrue(admin.isEmpty());
// should have gone active with the current assignments
verify(mgr).goActive();
}
@Test
public void testAwaitIdentification_NoAssignment() {
// not the leader and no assignment
BucketAssignments asgn = new BucketAssignments(new String[] {HOST1, HOST2});
mgr.startDistributing(asgn);
state = new QueryState(mgr);
state.start();
state.process(new Identification(MY_HOST, null));
// tell it the leader is still active
state.process(new Identification(PREV_HOST, asgn));
Pair<Long, StateTimerTask> timer = onceTasks.remove();
assertEquals(STD_IDENTIFICATION_MS, timer.first().longValue());
assertNotNull(timer.second());
// set up inactive state, as that's what it should return
State next = mock(State.class);
when(mgr.goInactive()).thenReturn(next);
assertEquals(next, timer.second().fire());
// should NOT have published a Leader message
assertTrue(admin.isEmpty());
}
@Test
public void testRecordInfo_NullSource() {
state.setAssignments(ASGN3);
state.setLeader(MY_HOST);
BucketAssignments asgn = new BucketAssignments(new String[] {PREV_HOST, MY_HOST, HOST2});
state.process(new Identification(null, asgn));
// leader unchanged
assertEquals(MY_HOST, state.getLeader());
// assignments still updated
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_SourcePreceedsMyHost() {
state.setAssignments(ASGN3);
state.setLeader(MY_HOST);
BucketAssignments asgn = new BucketAssignments(new String[] {PREV_HOST, MY_HOST, HOST2});
state.process(new Identification(PREV_HOST, asgn));
// new leader
assertEquals(PREV_HOST, state.getLeader());
// assignments still updated
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_SourceFollowsMyHost() {
mgr.startDistributing(null);
state.setLeader(MY_HOST);
BucketAssignments asgn = new BucketAssignments(new String[] {HOST1, HOST2});
state.process(new Identification(HOST1, asgn));
// leader unchanged
assertEquals(MY_HOST, state.getLeader());
// assignments still updated
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_NewIsNull() {
state.setAssignments(ASGN3);
state.process(new Identification(HOST1, null));
assertEquals(ASGN3, state.getAssignments());
}
@Test
public void testRecordInfo_NewIsEmpty() {
state.setAssignments(ASGN3);
state.process(new Identification(PREV_HOST, new BucketAssignments()));
assertEquals(ASGN3, state.getAssignments());
}
@Test
public void testRecordInfo_OldIsNull() {
mgr.startDistributing(null);
BucketAssignments asgn = new BucketAssignments(new String[] {HOST1, HOST2});
state.process(new Identification(HOST1, asgn));
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_OldIsEmpty() {
state.setAssignments(new BucketAssignments());
BucketAssignments asgn = new BucketAssignments(new String[] {HOST1, HOST2});
state.process(new Identification(HOST1, asgn));
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_NewLeaderPreceedsOld() {
state.setAssignments(ASGN3);
state.setLeader(MY_HOST);
BucketAssignments asgn = new BucketAssignments(new String[] {PREV_HOST, MY_HOST, HOST2});
state.process(new Identification(HOST3, asgn));
assertEquals(asgn, state.getAssignments());
}
@Test
public void testRecordInfo_NewLeaderSucceedsOld() {
state.setAssignments(ASGN3);
state.setLeader(MY_HOST);
BucketAssignments asgn = new BucketAssignments(new String[] {HOST2, HOST3});
state.process(new Identification(HOST3, asgn));
// should be unchanged
assertEquals(ASGN3, state.getAssignments());
}
}
|