aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java
blob: 6ff989cdc8c3281ee2dca42dc8bdd5529cc8d28e (plain)
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
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
 * Modifications Copyright (C) 2022 Bell Canada. 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.pap.main.comm;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
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.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
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.base.PfModelRuntimeException;
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.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.onap.policy.pap.main.service.PdpGroupService;
import org.onap.policy.pap.main.service.PolicyStatusService;
import org.powermock.reflect.Whitebox;

@RunWith(MockitoJUnitRunner.class)
public class PdpModifyRequestMapTest extends CommonRequestBase {
    private static final String MY_REASON = "my reason";
    private static final int EXPIRED_SECONDS = 100;

    /**
     * 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;

    /**
     * Used to capture input to undeployer.undeploy().
     */
    @Captor
    private ArgumentCaptor<Collection<ToscaConceptIdentifier>> undeployCaptor;

    @Mock
    private PdpRequests requests;

    @Mock
    private PolicyUndeployer undeployer;

    @Mock
    private PdpStatusMessageHandler responseHandler;

    @Mock
    private PdpGroupService pdpGroupService;

    @Mock
    private PolicyStatusService policyStatusService;

    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);
    }

    @Test
    public void testPdpModifyRequestMap() {
        assertSame(mapParams, Whitebox.getInternalState(map, "params"));
        assertSame(lock, Whitebox.getInternalState(map, "modifyLock"));
    }

    @Test
    public void testIsEmpty() {
        assertTrue(map.isEmpty());

        map.addRequest(change);
        assertFalse(map.isEmpty());

        // indicate success
        getListener(getSingletons(1).get(0)).success(PDP1, response);

        assertTrue(map.isEmpty());
        verify(responseHandler, never()).handlePdpStatus(response);
    }

    @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, response);

        verify(responseHandler, never()).handlePdpStatus(response);

        /*
         * 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, response);

        // should be called for the update
        verify(responseHandler).handlePdpStatus(response);

        // 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 testRemoveExpiredPdps() throws Exception {
        PdpGroup group1 = makeGroup(MY_GROUP);
        group1.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1)));

        PdpGroup group2 = makeGroup(MY_GROUP2);
        group2.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP2, PDP3), makeSubGroup(MY_SUBGROUP2, PDP4)));

        // expire all items in group2's first subgroup
        Instant expired = Instant.now().minusSeconds(EXPIRED_SECONDS);
        group2.getPdpSubgroups().get(0).getPdpInstances().forEach(pdp -> pdp.setLastUpdate(expired));

        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1, group2));

        // run it
        map.removeExpiredPdps();

        // should have removed from the group
        List<PdpGroup> groups = getGroupUpdates();
        assertThat(groups).hasSize(1);
        assertThat(groups.get(0)).isSameAs(group2);
        assertThat(group2.getPdpSubgroups()).hasSize(2);

        final Iterator<PdpSubGroup> iter = group2.getPdpSubgroups().iterator();

        PdpSubGroup subgrp = iter.next();
        assertThat(subgrp.getPdpInstances()).isEmpty();
        assertThat(subgrp.getCurrentInstanceCount()).isZero();

        subgrp = iter.next();
        assertThat(subgrp.getPdpInstances()).hasSize(1);
        assertThat(subgrp.getCurrentInstanceCount()).isEqualTo(1);
        assertThat(subgrp.getPdpInstances().get(0).getInstanceId()).isEqualTo(PDP4);
    }

    @Test
    public void testRemoveExpiredPdps_NothingExpired() throws Exception {
        PdpGroup group1 = makeGroup(MY_GROUP);
        group1.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1)));

        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1));

        // run it
        map.removeExpiredPdps();

        verify(pdpGroupService, never()).updatePdpGroups(any());
        verify(publisher, never()).enqueue(any());
    }

    @Test
    public void testRemoveExpiredPdps_DaoEx() throws Exception {
        when(pdpGroupService.getFilteredPdpGroups(any())).thenThrow(makeRuntimeException());

        assertThatCode(map::removeExpiredPdps).doesNotThrowAnyException();
    }

    @Test
    public void testRemoveExpiredPdps_DaoRtEx() throws Exception {
        when(pdpGroupService.getFilteredPdpGroups(any())).thenThrow(makeRuntimeException());

        assertThatCode(map::removeExpiredPdps).doesNotThrowAnyException();
    }

    @Test
    public void testRemoveFromSubgroup() throws Exception {
        PdpGroup group = makeGroup(MY_GROUP);
        group.setPdpSubgroups(List.of(makeSubGroup(MY_SUBGROUP, PDP1, PDP2, PDP3)));

        // expire pdp1 and pdp3
        Instant expired = Instant.now().minusSeconds(EXPIRED_SECONDS);
        List<Pdp> pdps = group.getPdpSubgroups().get(0).getPdpInstances();
        pdps.get(0).setLastUpdate(expired);
        pdps.get(2).setLastUpdate(expired);
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group));

        // run it
        map.removeExpiredPdps();

        // should have removed from the group
        List<PdpGroup> groups = getGroupUpdates();
        assertThat(groups).hasSize(1);
        assertThat(groups.get(0)).isSameAs(group);
        assertThat(group.getPdpSubgroups()).hasSize(1);
        assertThat(group.getPdpSubgroups().get(0).getCurrentInstanceCount()).isEqualTo(1);

        pdps = group.getPdpSubgroups().get(0).getPdpInstances();
        assertThat(pdps).hasSize(1);
        assertThat(pdps.get(0).getInstanceId()).isEqualTo(PDP2);
    }

    protected PfModelException makeException() {
        return new PfModelException(Status.BAD_REQUEST, "expected exception");
    }

    protected PfModelRuntimeException makeRuntimeException() {
        return new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception");
    }

    @Test
    public void testMakePdpRequests() {
        // this should invoke the real method without throwing an exception
        PdpModifyRequestMap reqMap =
            new PdpModifyRequestMap(pdpGroupService, policyStatusService, responseHandler, undeployer, notifier);
        reqMap.initialize(mapParams);
        reqMap.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.setPoliciesToBeDeployed(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.setPoliciesToBeDeployed(Collections.emptyList());
            update2.setPoliciesToBeUndeployed(Arrays.asList(policy.getIdentifier()));
            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.setPoliciesToBeDeployed(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(pdpGroupService, 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 {
        final var request = map.addRequest(change);

        // invoke the method
        invokeLastRetryHandler(1, request);

        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, response);
    }

    /**
     * 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
     * @param request request whose count was exhausted
     */
    private void invokeLastRetryHandler(int count, Request request) {
        getListener(getSingletons(count).get(0)).retryCountExhausted(request);
    }

    /**
     * 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.setCurrentInstanceCount(pdpNames.length);
        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);
        pdp.setLastUpdate(Instant.now());

        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(pdpGroupService).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(pdpGroupService, policyStatusService, responseHandler, undeployer, notifier);
            super.initialize(params);;
        }

        @Override
        protected PdpRequests makePdpRequests(String pdpName) {
            ++nalloc;
            return requests;
        }
    }
}