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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
/*
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
* Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* 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.pap.main.comm;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response.Status;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pap.concepts.PolicyStatus;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pap.main.comm.msgdata.Request;
import org.onap.policy.pap.main.comm.msgdata.RequestListener;
import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
import org.powermock.reflect.Whitebox;
@RunWith(MockitoJUnitRunner.class)
public class PdpModifyRequestMapTest extends CommonRequestBase {
private static final String MY_REASON = "my reason";
/**
* Used to capture input to dao.createPdpGroups().
*/
@Captor
private ArgumentCaptor<List<PdpGroup>> createCaptor;
/**
* Used to capture input to dao.updatePdpGroups().
*/
@Captor
private ArgumentCaptor<List<PdpGroup>> updateCaptor;
@Captor
private ArgumentCaptor<PolicyNotification> notificationCaptor;
/**
* Used to capture input to undeployer.undeploy().
*/
@Captor
private ArgumentCaptor<Collection<ToscaConceptIdentifier>> undeployCaptor;
@Mock
private PdpRequests requests;
@Mock
private PolicyUndeployer undeployer;
private MyMap map;
private PdpUpdate update;
private PdpStateChange change;
private PdpStatus response;
/**
* Sets up.
*
* @throws Exception if an error occurs
*/
@Before
@Override
public void setUp() throws Exception {
super.setUp();
response = new PdpStatus();
update = makeUpdate(PDP1, MY_GROUP, MY_SUBGROUP);
change = makeStateChange(PDP1, MY_STATE);
when(requests.getPdpName()).thenReturn(PDP1);
when(requests.isFirstInQueue(any())).thenReturn(true);
response.setName(MY_NAME);
response.setState(MY_STATE);
response.setPdpGroup(update.getPdpGroup());
response.setPdpSubgroup(update.getPdpSubgroup());
response.setPolicies(Collections.emptyList());
map = new MyMap(mapParams);
map.setPolicyUndeployer(undeployer);
}
@Test
public void testPdpModifyRequestMap() {
assertSame(mapParams, Whitebox.getInternalState(map, "params"));
assertSame(lock, Whitebox.getInternalState(map, "modifyLock"));
assertSame(daoFactory, Whitebox.getInternalState(map, "daoFactory"));
}
@Test
public void testIsEmpty() {
assertTrue(map.isEmpty());
map.addRequest(change);
assertFalse(map.isEmpty());
// indicate success
getListener(getSingletons(1).get(0)).success(PDP1);
assertTrue(map.isEmpty());
}
@Test
public void testStopPublishing() {
// try with non-existent PDP
map.stopPublishing(PDP1);
// now start a PDP and try it
map.addRequest(change);
map.stopPublishing(PDP1);
verify(requests).stopPublishing();
// try again - it shouldn't stop publishing again
map.stopPublishing(PDP1);
verify(requests, times(1)).stopPublishing();
}
@Test
public void testAddRequestPdpUpdatePdpStateChange_BothNull() {
// nulls should be ok
Assertions.assertThatCode(() -> map.addRequest(null, null)).doesNotThrowAnyException();
}
@Test
public void testAddRequestPdpUpdatePdpStateChange_NullUpdate() {
map.addRequest(null, change);
Request req = getSingletons(1).get(0);
assertSame(change, req.getMessage());
assertEquals("pdp_1 PdpStateChange", req.getName());
}
@Test
public void testAddRequestPdpUpdatePdpStateChange_NullStateChange() {
map.addRequest(update, null);
Request req = getSingletons(1).get(0);
assertSame(update, req.getMessage());
assertEquals("pdp_1 PdpUpdate", req.getName());
}
/**
* Tests addRequest() when two requests are provided and the second is an "activate"
* message.
*/
@Test
public void testAddRequestPdpUpdatePdpStateChange_BothProvided_Active() {
change.setState(PdpState.ACTIVE);
map.addRequest(update, change);
// should have only allocated one request structure
assertEquals(1, map.nalloc);
// both requests should have been added
List<Request> values = getSingletons(2);
// update should appear first
Request req = values.remove(0);
assertSame(update, req.getMessage());
assertEquals("pdp_1 PdpUpdate", req.getName());
req = values.remove(0);
assertSame(change, req.getMessage());
assertEquals("pdp_1 PdpStateChange", req.getName());
}
/**
* Tests addRequest() when two requests are provided and the second is "deactivate"
* message.
*/
@Test
public void testAddRequestPdpUpdatePdpStateChange_BothProvided_Passive() {
change.setState(PdpState.PASSIVE);
map.addRequest(update, change);
// should have only allocated one request structure
assertEquals(1, map.nalloc);
// both requests should have been added
List<Request> values = getSingletons(2);
// state-change should appear first
Request req = values.remove(0);
assertSame(change, req.getMessage());
assertEquals("pdp_1 PdpStateChange", req.getName());
req = values.remove(0);
assertSame(update, req.getMessage());
assertEquals("pdp_1 PdpUpdate", req.getName());
}
@Test
public void testAddRequestPdpUpdatePdpStateChange() {
// null should be ok
map.addRequest(null, null);
map.addRequest(change);
Request req = getSingletons(1).get(0);
assertSame(change, req.getMessage());
assertEquals("pdp_1 PdpStateChange", req.getName());
// broadcast should throw an exception
change.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(change))
.withMessageStartingWith("unexpected broadcast message: PdpStateChange");
}
@Test
public void testAddRequestPdpUpdate() {
// null should be ok
map.addRequest((PdpUpdate) null);
map.addRequest(update);
Request req = getSingletons(1).get(0);
assertSame(update, req.getMessage());
assertEquals("pdp_1 PdpUpdate", req.getName());
// broadcast should throw an exception
update.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(update))
.withMessageStartingWith("unexpected broadcast message: PdpUpdate");
}
@Test
public void testAddRequestPdpStateChange() {
// null should be ok
map.addRequest((PdpStateChange) null);
map.addRequest(change);
Request req = getSingletons(1).get(0);
assertSame(change, req.getMessage());
assertEquals("pdp_1 PdpStateChange", req.getName());
// broadcast should throw an exception
change.setName(null);
assertThatIllegalArgumentException().isThrownBy(() -> map.addRequest(change))
.withMessageStartingWith("unexpected broadcast message: PdpStateChange");
}
@Test
public void testAddSingleton() {
map.addRequest(change);
assertEquals(1, map.nalloc);
// should have one singleton
getSingletons(1);
// add another request with the same PDP
map.addRequest(makeStateChange(PDP1, MY_STATE));
assertEquals(1, map.nalloc);
// should now have another singleton
getSingletons(2);
// add another request with a different PDP
map.addRequest(makeStateChange(DIFFERENT, MY_STATE));
// should now have another allocation
assertEquals(2, map.nalloc);
// should now have another singleton
getSingletons(3);
}
@Test
public void testStartNextRequest_NoMore() {
map.addRequest(change);
// indicate success
getListener(getSingletons(1).get(0)).success(PDP1);
/*
* the above should have removed the requests so next time should allocate a new
* one
*/
map.addRequest(change);
assertEquals(2, map.nalloc);
}
@Test
public void testStartNextRequest_HaveMore() {
map.addRequest(update);
map.addRequest(change);
Request updateReq = getSingletons(2).get(0);
// indicate success with the update
when(requests.startNextRequest(updateReq)).thenReturn(true);
getListener(updateReq).success(PDP1);
// should have started the next request
verify(requests).startNextRequest(updateReq);
/*
* requests should still be there, so adding another request should not allocate a
* new one
*/
map.addRequest(update);
assertEquals(1, map.nalloc);
}
@Test
public void testRemovePdp() throws Exception {
map.addRequest(update);
// put the PDP in a group
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1)));
final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("MyPolicy", "10.20.30");
final ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "10.20.30");
// @formatter:off
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
when(dao.getGroupPolicyStatus(any())).thenReturn(Arrays.asList(
PdpPolicyStatus.builder()
.pdpGroup(MY_GROUP)
.pdpType(MY_SUBGROUP)
.pdpId(PDP1)
.policy(policy1)
.policyType(policyType)
.deploy(true)
.state(State.SUCCESS)
.build()));
// @formatter:on
// indicate retries exhausted
invokeLastRetryHandler(1);
// should have stopped publishing
verify(requests).stopPublishing();
// should have generated a notification; yes, it should go into the "added" set
verify(notifier).publish(notificationCaptor.capture());
assertThat(notificationCaptor.getValue().getDeleted()).isEmpty();
assertThat(notificationCaptor.getValue().getAdded()).hasSize(1);
PolicyStatus status = notificationCaptor.getValue().getAdded().get(0);
assertThat(status.getPolicy()).isEqualTo(policy1);
assertThat(status.getPolicyType()).isEqualTo(policyType);
assertThat(status.getFailureCount()).isZero();
assertThat(status.getIncompleteCount()).isZero();
assertThat(status.getSuccessCount()).isZero();
// should have removed from the group
List<PdpGroup> groups = getGroupUpdates();
assertEquals(1, groups.size());
assertSame(group, groups.get(0));
assertEquals(0, group.getPdpSubgroups().get(0).getCurrentInstanceCount());
}
@Test
public void testRemovePdp_NotInGroup() throws PfModelException {
map.addRequest(update);
// indicate retries exhausted
invokeLastRetryHandler(1);
// should have stopped publishing
verify(requests).stopPublishing();
// should not have done any updates
verify(dao, never()).updatePdpGroups(any());
// and no publishes
verify(notifier, never()).publish(any());
}
@Test
public void testRemovePdp_AlreadyRemovedFromMap() throws PfModelException {
map.addRequest(change);
map.stopPublishing(PDP1);
// put the PDP in a group
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1)));
invokeLastRetryHandler(1);
// should have stopped publishing a second time
verify(requests, times(2)).stopPublishing();
// should NOT have done any updates
verify(dao, never()).updatePdpGroups(any());
}
@Test
public void testRemovePdp_NoGroup() throws PfModelException {
map.addRequest(change);
invokeLastRetryHandler(1);
// should not have stopped publishing
verify(requests).stopPublishing();
// should not have done any updates
verify(dao, never()).updatePdpGroups(any());
}
@Test
public void testRemoveFromGroup() throws Exception {
map.addRequest(change);
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP + "a", PDP1 + "a"),
makeSubGroup(MY_SUBGROUP, PDP1), makeSubGroup(MY_SUBGROUP + "c", PDP1 + "c")));
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
invokeLastRetryHandler(1);
// verify that the PDP was removed from the subgroup
List<PdpGroup> groups = getGroupUpdates();
assertEquals(1, groups.size());
assertSame(group, groups.get(0));
List<PdpSubGroup> subgroups = group.getPdpSubgroups();
assertEquals(3, subgroups.size());
assertEquals("[pdp_1a]", getPdpNames(subgroups.get(0)));
assertEquals("[]", getPdpNames(subgroups.get(1)));
assertEquals("[pdp_1c]", getPdpNames(subgroups.get(2)));
}
@Test
public void testRemoveFromGroup_DaoEx() throws Exception {
map.addRequest(change);
when(dao.getFilteredPdpGroups(any())).thenThrow(makeException());
invokeLastRetryHandler(1);
// should still stop publishing
verify(requests).stopPublishing();
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
protected PfModelException makeException() {
return new PfModelException(Status.BAD_REQUEST, "expected exception");
}
@Test
public void testRemoveFromGroup_NoGroups() throws Exception {
map.addRequest(change);
invokeLastRetryHandler(1);
verify(dao, never()).updatePdpGroups(any());
}
@Test
public void testRemoveFromGroup_NoMatchingSubgroup() throws Exception {
map.addRequest(change);
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, DIFFERENT)));
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
invokeLastRetryHandler(1);
verify(dao, never()).updatePdpGroups(any());
}
@Test
public void testRemoveFromSubgroup() throws Exception {
map.addRequest(change);
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1, PDP1 + "x", PDP1 + "y")));
when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
invokeLastRetryHandler(1);
// verify that the PDP was removed from the subgroup
List<PdpGroup> groups = getGroupUpdates();
assertEquals(1, groups.size());
assertSame(group, groups.get(0));
PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
assertEquals(2, subgroup.getCurrentInstanceCount());
assertEquals("[pdp_1x, pdp_1y]", getPdpNames(subgroup));
}
@Test
public void testMakePdpRequests() {
// this should invoke the real method without throwing an exception
new PdpModifyRequestMap(mapParams).addRequest(change);
QueueToken<PdpMessage> token = queue.poll();
assertNotNull(token);
assertSame(change, token.get());
verify(dispatcher).register(eq(change.getRequestId()), any());
verify(timers).register(eq(change.getRequestId()), any());
}
@Test
public void testSingletonListenerFailure() throws Exception {
map.addRequest(change);
// invoke the method
invokeFailureHandler(1);
verify(undeployer, never()).undeploy(any(), any(), any());
verify(requests, never()).stopPublishing();
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
/**
* Tests Listener.failure() when something has to be undeployed.
*/
@Test
public void testSingletonListenerFailureUndeploy() throws Exception {
ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
ToscaPolicy policy = mock(ToscaPolicy.class);
when(policy.getIdentifier()).thenReturn(ident);
// add some policies to the update
update.setPolicies(Arrays.asList(policy));
map.addRequest(update);
/*
* Reconfigure the request when undeploy() is called. Also arrange for undeploy()
* to throw an exception.
*/
Request req = getSingletons(1).get(0);
doAnswer(ans -> {
PdpUpdate update2 = new PdpUpdate(update);
update2.setPolicies(Collections.emptyList());
assertTrue(req.reconfigure(update2));
throw makeException();
}).when(undeployer).undeploy(any(), any(), any());
// indicate that all policies failed (because response has no policies)
response.setName(PDP1);
req.setNotifier(notifier);
req.checkResponse(response);
// invoke the method
invokeFailureHandler(1);
verify(undeployer).undeploy(eq(MY_GROUP), eq(MY_SUBGROUP), undeployCaptor.capture());
assertEquals(Arrays.asList(ident).toString(), undeployCaptor.getValue().toString());
// no effect on the map
map.addRequest(update);
assertEquals(1, map.nalloc);
}
/**
* Tests Listener.failure() when something has to be undeployed, but the message
* remains unchanged.
*/
@Test
public void testSingletonListenerFailureUndeployMessageUnchanged() throws Exception {
ToscaConceptIdentifier ident = new ToscaConceptIdentifier("msg-unchanged", "8.7.6");
ToscaPolicy policy = mock(ToscaPolicy.class);
when(policy.getIdentifier()).thenReturn(ident);
// add some policies to the update
update.setPolicies(Arrays.asList(policy));
map.addRequest(update);
// indicate that all policies failed (because response has no policies)
response.setName(PDP1);
Request req = getSingletons(1).get(0);
req.setNotifier(notifier);
req.checkResponse(response);
// invoke the method
invokeFailureHandler(1);
verify(undeployer).undeploy(eq(MY_GROUP), eq(MY_SUBGROUP), undeployCaptor.capture());
assertEquals(Arrays.asList(ident).toString(), undeployCaptor.getValue().toString());
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
@Test
public void testSingletonListenerSuccess() throws Exception {
map.addRequest(change);
// invoke the method
invokeSuccessHandler(1);
verify(requests, never()).stopPublishing();
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
@Test
public void testRequestCompleted_LastRequest() throws Exception {
map.addRequest(change);
// invoke the method
invokeSuccessHandler(1);
verify(requests, never()).stopPublishing();
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
@Test
public void testRequestCompleted_NameMismatch() throws Exception {
// use a different name
when(requests.getPdpName()).thenReturn(DIFFERENT);
map.addRequest(change);
// put the PDP in a group
PdpGroup group = makeGroup(MY_GROUP);
group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1, DIFFERENT)));
// invoke the method - with a different name (i.e., PDP1 instead of DIFFERENT)
invokeSuccessHandler(1);
verify(requests, never()).stopPublishing();
// no effect on the map
map.addRequest(update);
assertEquals(1, map.nalloc);
// no updates
verify(dao, never()).updatePdpGroups(any());
}
@Test
public void testRequestCompleted_AlreadyStopped() throws Exception {
map.addRequest(change);
map.stopPublishing(PDP1);
// invoke the method
invokeSuccessHandler(1);
// should have called this a second time
verify(requests, times(2)).stopPublishing();
// requests should have been removed from the map so this should allocate another
map.addRequest(update);
assertEquals(2, map.nalloc);
}
@Test
public void testRequestCompleted_NotFirstInQueue() throws Exception {
map.addRequest(change);
when(requests.isFirstInQueue(any())).thenReturn(false);
// invoke the method
invokeSuccessHandler(1);
// should not have called this
verify(requests, never()).stopPublishing();
// no effect on the map
map.addRequest(update);
assertEquals(1, map.nalloc);
}
@Test
public void testSingletonListenerRetryCountExhausted() throws Exception {
map.addRequest(change);
// invoke the method
invokeLastRetryHandler(1);
verify(requests).stopPublishing();
}
/**
* Invokes the first request's listener.success() method.
*
* @param count expected number of requests
*/
private void invokeSuccessHandler(int count) {
getListener(getSingletons(count).get(0)).success(PDP1);
}
/**
* Invokes the first request's listener.failure() method.
*
* @param count expected number of requests
*/
private void invokeFailureHandler(int count) {
getListener(getSingletons(count).get(0)).failure(PDP1, MY_REASON);
}
/**
* Invokes the first request's listener.retryCountExhausted() method.
*
* @param count expected number of requests
*/
private void invokeLastRetryHandler(int count) {
getListener(getSingletons(count).get(0)).retryCountExhausted();
}
/**
* Gets the name of the PDPs contained within a subgroup.
*
* @param subgroup subgroup of interest
* @return the name of the PDPs contained within the subgroup
*/
private String getPdpNames(PdpSubGroup subgroup) {
return subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toList()).toString();
}
/**
* Gets the singleton requests added to {@link #requests}.
*
* @param count number of singletons expected
* @return the singleton requests
*/
private List<Request> getSingletons(int count) {
ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class);
verify(requests, times(count)).addSingleton(captor.capture());
return captor.getAllValues();
}
/**
* Gets the listener from a request.
*
* @param request request of interest
* @return the request's listener
*/
private RequestListener getListener(Request request) {
return Whitebox.getInternalState(request, "listener");
}
private PdpGroup makeGroup(String name) {
PdpGroup group = new PdpGroup();
group.setName(name);
return group;
}
private PdpSubGroup makeSubGroup(String pdpType, String... pdpNames) {
PdpSubGroup subgroup = new PdpSubGroup();
subgroup.setPdpType(pdpType);
subgroup.setPdpInstances(Arrays.asList(pdpNames).stream().map(this::makePdp).collect(Collectors.toList()));
return subgroup;
}
private Pdp makePdp(String pdpName) {
Pdp pdp = new Pdp();
pdp.setInstanceId(pdpName);
return pdp;
}
/**
* Gets the input to the method.
*
* @return the input that was passed to the dao.updatePdpGroups() method
* @throws Exception if an error occurred
*/
private List<PdpGroup> getGroupUpdates() throws Exception {
verify(dao).updatePdpGroups(updateCaptor.capture());
return copyList(updateCaptor.getValue());
}
/**
* Copies a list and sorts it by group name.
*
* @param source source list to copy
* @return a copy of the source list
*/
private List<PdpGroup> copyList(List<PdpGroup> source) {
List<PdpGroup> newlst = new ArrayList<>(source);
Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName()));
return newlst;
}
private class MyMap extends PdpModifyRequestMap {
/**
* Number of times requests were allocated.
*/
private int nalloc = 0;
public MyMap(PdpModifyRequestMapParams params) {
super(params);
}
@Override
protected PdpRequests makePdpRequests(String pdpName) {
++nalloc;
return requests;
}
}
}
|