aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java
blob: ec79c03a99516d31a1650cd599a8fbee8b55226d (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 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.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
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.Comparator;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
import org.onap.policy.pap.main.notification.DeploymentStatus;
import org.onap.policy.pap.main.service.PolicyStatusService;

public class TestSessionData extends ProviderSuper {
    private static final String GROUP_NAME = "groupA";
    private static final String PDP_TYPE = "MySubGroup";
    private static final String PDP1 = "pdp_1";
    private static final String PDP2 = "pdp_2";
    private static final String PDP3 = "pdp_3";
    private static final String POLICY_VERSION_PREFIX = "1.2.";
    private static final String POLICY_NAME = "myPolicy";
    private static final String POLICY_VERSION = POLICY_VERSION_PREFIX + "3";
    private static final String POLICY_TYPE = "myType";
    private static final String POLICY_TYPE_VERSION = "10.20.30";
    private static final String EXPECTED_EXCEPTION = "expected exception";

    private SessionData session;
    private ToscaConceptIdentifierOptVersion ident;
    private ToscaConceptIdentifier type;
    private ToscaConceptIdentifier type2;
    private PdpGroup group1;
    private PdpGroup group2;

    /**
     * Initializes mocks and a session.
     *
     * @throws Exception if an error occurs
     */
    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();

        ident = new ToscaConceptIdentifierOptVersion(POLICY_NAME, POLICY_VERSION);
        type = new ToscaConceptIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION);
        type2 = new ToscaConceptIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION + "0");
        group1 = loadGroup("group1.json");
        group2 = loadGroup("group2.json");

        session = new SessionData(DEFAULT_USER, toscaService, pdpGroupService, policyStatusService, policyAuditService);
    }

    @Test
    public void testGetPolicyType() throws Exception {
        ToscaPolicyType policy1 = makePolicyType(POLICY_TYPE, POLICY_TYPE_VERSION);
        when(toscaService.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenReturn(Arrays.asList(policy1));

        assertSame(policy1, session.getPolicyType(type));

        // retrieve a second time - should use cache
        assertSame(policy1, session.getPolicyType(type));
    }

    @Test
    public void testGetPolicyType_NotFound() throws Exception {
        when(toscaService.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());

        assertNull(session.getPolicyType(type));
    }

    @Test
    public void testGetPolicyType_DaoEx() throws Exception {
        PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION);
        when(toscaService.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenThrow(ex);

        assertThatThrownBy(() -> session.getPolicyType(type)).isSameAs(ex);
    }

    @Test
    public void testGetPolicy_NullVersion() throws Exception {
        ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
        when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));

        ident.setVersion(null);
        assertSame(policy1, session.getPolicy(ident));

        ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
        assertEquals(POLICY_NAME, filter.getName());
        assertEquals(ToscaTypedEntityFilter.LATEST_VERSION, filter.getVersion());
        assertEquals(null, filter.getVersionPrefix());

        // retrieve a second time using full version - should use cache
        assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
        verify(toscaService).getFilteredPolicyList(any());
    }

    @Test
    public void testGetPolicy_MajorVersion() throws Exception {
        ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
        when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));

        ident.setVersion("1");
        assertSame(policy1, session.getPolicy(ident));

        ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
        assertEquals(POLICY_NAME, filter.getName());
        assertEquals(ToscaTypedEntityFilter.LATEST_VERSION, filter.getVersion());
        assertEquals("1.", filter.getVersionPrefix());

        // retrieve a second time using full version - should use cache
        assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
        verify(toscaService).getFilteredPolicyList(any());
    }

    @Test
    public void testGetPolicy_MajorMinorVersion() throws Exception {
        ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
        when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));

        ident.setVersion(POLICY_VERSION);
        assertSame(policy1, session.getPolicy(ident));

        ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
        assertEquals(POLICY_NAME, filter.getName());
        assertEquals(POLICY_VERSION, filter.getVersion());
        assertEquals(null, filter.getVersionPrefix());

        // retrieve a second time using full version - should use cache
        assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
        verify(toscaService).getFilteredPolicyList(any());
    }

    @Test
    public void testGetPolicy_NotFound() throws Exception {
        when(toscaService.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());

        assertNull(session.getPolicy(ident));
    }

    @Test
    public void testGetPolicy_DaoEx() throws Exception {
        PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION);
        when(toscaService.getFilteredPolicyList(any())).thenThrow(ex);

        assertThatThrownBy(() -> session.getPolicy(ident)).isSameAs(ex);
    }

    @Test
    public void testIsVersionPrefix() {
        assertTrue(SessionData.isVersionPrefix("1"));
        assertTrue(SessionData.isVersionPrefix("12"));
        assertTrue(SessionData.isVersionPrefix("1.2"));
        assertTrue(SessionData.isVersionPrefix("1.23"));

        assertFalse(SessionData.isVersionPrefix("1."));
        assertFalse(SessionData.isVersionPrefix("1.2."));
        assertFalse(SessionData.isVersionPrefix("1.2.3"));
        assertFalse(SessionData.isVersionPrefix("1.2.3."));
        assertFalse(SessionData.isVersionPrefix("1.2.3.4"));
    }

    @Test
    public void testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() {
        // pre-load with a update and state-change for other PDPs
        PdpUpdate update2 = makeUpdate(PDP2);
        session.addUpdate(update2);

        PdpStateChange change3 = makeStateChange(PDP3);
        session.addStateChange(change3);

        // add requests
        PdpUpdate update = makeUpdate(PDP1);
        PdpStateChange change = makeStateChange(PDP1);
        session.addRequests(update, change);
        verifyRequests(update, update2, change, change3);

        /*
         * repeat with a new pair
         */
        update = makeUpdate(PDP1);
        change = makeStateChange(PDP1);
        session.addRequests(update, change);
        verifyRequests(update, update2, change, change3);

        // just make an update this time
        update = makeUpdate(PDP1);
        session.addUpdate(update);
        verifyRequests(update, update2, change, change3);
    }

    private void verifyRequests(PdpUpdate update, PdpUpdate update2, PdpStateChange change, PdpStateChange change3) {
        List<Pair<PdpUpdate, PdpStateChange>> requests = sort(session.getPdpRequests(), this::compare);
        assertEquals(3, requests.size());

        System.out.println(requests);
        System.out.println(update);

        Iterator<Pair<PdpUpdate, PdpStateChange>> reqiter = requests.iterator();
        Pair<PdpUpdate, PdpStateChange> pair = reqiter.next();
        assertSame(update, pair.getLeft());
        assertSame(change, pair.getRight());

        pair = reqiter.next();
        assertSame(update2, pair.getLeft());
        assertSame(null, pair.getRight());

        pair = reqiter.next();
        assertSame(null, pair.getLeft());
        assertSame(change3, pair.getRight());

        // verify individual lists
        List<PdpUpdate> updates = Arrays.asList(update, update2);
        assertEquals(sort(updates, this::compare), sort(session.getPdpUpdates(), this::compare));

        List<PdpStateChange> changes = Arrays.asList(change, change3);
        assertEquals(sort(changes, this::compare), sort(session.getPdpStateChanges(), this::compare));
    }

    @Test
    public void testAddRequests_MismatchedNames() {
        PdpUpdate update = makeUpdate(PDP1);
        PdpStateChange change = makeStateChange(PDP2);
        assertThatIllegalArgumentException().isThrownBy(() -> session.addRequests(update, change))
                .withMessage("PDP name mismatch pdp_1, pdp_2");
    }

    @Test
    public void testAddUpdate_testGetPdpUpdates() {
        // several different updates, but one duplicate
        PdpUpdate update1 = makeUpdate(PDP1);
        session.addUpdate(update1);

        PdpUpdate update2 = makeUpdate(PDP2);
        session.addUpdate(update2);

        PdpUpdate update3 = makeUpdate(PDP3);
        session.addUpdate(update3);

        List<PdpUpdate> lst = sort(getUpdateRequests(), this::compare);
        assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString());

        // overwrite one
        update2 = makeUpdate(PDP2);
        session.addUpdate(update2);

        lst = sort(getUpdateRequests(), this::compare);
        assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString());
    }

    @Test
    public void testAddStateChange_testGetPdpStateChanges() {
        // several different changes, but one duplicate
        PdpStateChange change1 = makeStateChange(PDP1);
        session.addStateChange(change1);

        PdpStateChange change2 = makeStateChange(PDP2);
        session.addStateChange(change2);

        PdpStateChange change3 = makeStateChange(PDP3);
        session.addStateChange(change3);

        List<PdpStateChange> lst = sort(getStateChangeRequests(), this::compare);
        assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString());

        // overwrite one
        change2 = makeStateChange(PDP2);
        session.addStateChange(change2);

        lst = sort(getStateChangeRequests(), this::compare);
        assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString());
    }

    private ToscaPolicyType makePolicyType(String name, String version) {
        ToscaPolicyType type = new ToscaPolicyType();

        type.setName(name);
        type.setVersion(version);

        return type;
    }

    private ToscaPolicy makePolicy(String name, String version) {
        ToscaPolicy policy = new ToscaPolicy();

        policy.setName(name);
        policy.setVersion(version);

        return policy;
    }

    @Test
    public void testCreate() throws Exception {
        assertTrue(session.isUnchanged());

        session.create(group1);
        assertSame(group1, session.getGroup(group1.getName()));
        assertFalse(session.isUnchanged());

        // can add another
        session.create(group2);
        assertSame(group1, session.getGroup(group1.getName()));
        assertSame(group2, session.getGroup(group2.getName()));
        assertFalse(session.isUnchanged());

        // cannot overwrite
        assertThatIllegalStateException().isThrownBy(() -> session.create(group1))
                .withMessage("group already cached: groupA");
    }

    @Test
    public void testUpdate() throws Exception {
        assertTrue(session.isUnchanged());

        // force the groups into the cache
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2));
        session.getActivePdpGroupsByPolicyType(type);

        /*
         * try group 1
         */
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1));
        PdpGroup newgrp = new PdpGroup(group1);
        session.update(newgrp);
        assertFalse(session.isUnchanged());

        // repeat
        newgrp = new PdpGroup(group1);
        session.update(newgrp);
        assertFalse(session.isUnchanged());

        /*
         * try group 2
         */
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group2));
        newgrp = new PdpGroup(group2);
        session.update(newgrp);
        assertFalse(session.isUnchanged());

        // repeat
        newgrp = new PdpGroup(group2);
        session.update(newgrp);
        assertFalse(session.isUnchanged());
    }

    @Test
    public void testUpdate_NotInCache() throws Exception {
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1));

        assertThatIllegalStateException().isThrownBy(() -> session.update(new PdpGroup(group1)))
                .withMessage("group not cached: groupA");
    }

    @Test
    public void testGetGroup() throws Exception {
        when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1));

        assertSame(group1, session.getGroup(GROUP_NAME));
        verify(pdpGroupService).getPdpGroups(any(String.class));

        // repeat
        assertSame(group1, session.getGroup(GROUP_NAME));

        // should not access dao again
        verify(pdpGroupService, times(1)).getPdpGroups(any(String.class));
    }

    @Test
    public void testGetGroup_NotFound() throws Exception {
        when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(Collections.emptyList());

        assertNull(session.getGroup(GROUP_NAME));
        verify(pdpGroupService).getPdpGroups(any(String.class));

        // repeat
        assertNull(session.getGroup(GROUP_NAME));

        // SHOULD access dao again
        verify(pdpGroupService, times(2)).getPdpGroups(GROUP_NAME);

        // find it this time
        when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1));
        assertSame(group1, session.getGroup(GROUP_NAME));
        verify(pdpGroupService, times(3)).getPdpGroups(GROUP_NAME);
    }

    @Test
    public void testGetActivePdpGroupsByPolicyType() throws Exception {
        List<PdpGroup> groups = Arrays.asList(group1, group2);
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);

        // repeat
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));

        // only invoked once - should have used the cache for the rest
        verify(pdpGroupService, times(1)).getFilteredPdpGroups(any());
    }

    @Test
    public void testAddGroup() throws Exception {
        List<PdpGroup> groups = Arrays.asList(group1, group2);
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);

        // query by each type
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2));

        // invoked once for each type
        verify(pdpGroupService, times(2)).getFilteredPdpGroups(any());

        // repeat - should be no more invocations
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
        assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2));
        verify(pdpGroupService, times(2)).getFilteredPdpGroups(any());
    }

    @Test
    public void testUpdateDb() throws Exception {
        // force the groups into the cache
        PdpGroup group3 = loadGroup("group3.json");
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2, group3));
        session.getActivePdpGroupsByPolicyType(type);

        // create groups 4 & 5
        PdpGroup group4 = loadGroup("group4.json");
        session.create(group4);

        PdpGroup group5 = loadGroup("group5.json");
        session.create(group5);

        // update group 1
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1));
        PdpGroup newgrp1 = new PdpGroup(group1);
        session.update(newgrp1);

        // another update
        newgrp1 = new PdpGroup(newgrp1);
        session.update(newgrp1);

        // update group 3
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group3));
        PdpGroup newgrp3 = new PdpGroup(group3);
        session.update(newgrp3);

        // update group 5
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group5));
        PdpGroup newgrp5 = new PdpGroup(group5);
        session.update(newgrp5);

        // push the changes to the DB
        PolicyNotification notif = new PolicyNotification();
        session.updateDb(notif);
        assertThat(notif.getAdded()).isEmpty();

        // expect one create for groups 4 & 5 (group5 replaced by newgrp5)
        List<PdpGroup> creates = getGroupCreates();
        assertEquals(2, creates.size());
        assertSame(group4, creates.get(0));
        assertSame(newgrp5, creates.get(1));

        // expect one update for groups 1 & 3
        List<PdpGroup> updates = getGroupUpdates();
        assertEquals(2, updates.size());
        assertSame(newgrp1, updates.get(0));
        assertSame(newgrp3, updates.get(1));
    }

    @Test
    public void testUpdateDb_Empty() throws Exception {
        // force data into the cache
        when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2));
        session.getActivePdpGroupsByPolicyType(type);

        PolicyNotification notif = new PolicyNotification();
        session.updateDb(notif);
        assertThat(notif.getAdded()).isEmpty();

        verify(pdpGroupService, never()).createPdpGroups(any());
        verify(pdpGroupService, never()).updatePdpGroups(any());
    }

    @Test
    public void testDeleteGroupFromDb() throws Exception {
        session.deleteGroupFromDb(group1);

        verify(pdpGroupService).deletePdpGroup(group1.getName());
    }

    @Test
    public void testTrackDeploy() throws PfModelException {
        testTrack(true);
    }

    @Test
    public void testTrackUndeploy() throws PfModelException {
        testTrack(false);
    }

    protected void testTrack(boolean deploy) throws PfModelException {

        DeploymentStatus status = mock(DeploymentStatus.class);

        session =
            new SessionData(DEFAULT_USER, toscaService, pdpGroupService, policyStatusService, policyAuditService) {
                @Override
                protected DeploymentStatus makeDeploymentStatus(PolicyStatusService policyStatusService) {
                    return status;
                }
            };

        ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION);
        policy.setType(POLICY_TYPE);
        policy.setTypeVersion(POLICY_TYPE_VERSION);

        when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy));

        ToscaConceptIdentifier policyId = new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION);
        List<String> pdps = Arrays.asList(PDP1, PDP2);

        ToscaPolicy testPolicy = session.getPolicy(new ToscaConceptIdentifierOptVersion(policyId));

        if (deploy) {
            session.trackDeploy(testPolicy, pdps, GROUP_NAME, PDP_TYPE);
            assertThat(session.getPoliciesToBeDeployed()).contains(testPolicy);
        } else {
            session.trackUndeploy(policyId, pdps, GROUP_NAME, PDP_TYPE);
            assertThat(session.getPoliciesToBeUndeployed()).contains(policyId);
        }

        // should be called just once
        verify(status).deleteDeployment(any(), anyBoolean());
        verify(status, times(1)).deleteDeployment(policyId, !deploy);

        // should be called for each PDP
        verify(status, times(2)).deploy(any(), any(), any(), any(), any(), anyBoolean());
        verify(status).deploy(PDP1, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
        verify(status).deploy(PDP2, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
    }

    private PdpUpdate makeUpdate(String pdpName) {
        PdpUpdate update = new PdpUpdate();

        update.setName(pdpName);

        return update;
    }

    private PdpStateChange makeStateChange(String pdpName) {
        PdpStateChange change = new PdpStateChange();

        change.setName(pdpName);

        return change;
    }

    private ToscaTypedEntityFilter<ToscaPolicy> getPolicyFilter() throws Exception {
        @SuppressWarnings("unchecked")
        ArgumentCaptor<ToscaTypedEntityFilter<ToscaPolicy>> captor =
                ArgumentCaptor.forClass(ToscaTypedEntityFilter.class);
        verify(toscaService).getFilteredPolicyList(captor.capture());

        return captor.getValue();
    }

    private List<PdpUpdate> getUpdateRequests() {
        return session.getPdpUpdates();
    }

    private List<PdpStateChange> getStateChangeRequests() {
        return session.getPdpStateChanges();
    }

    private <T> List<T> sort(Collection<T> collection, Comparator<T> comparator) {
        List<T> lst = new ArrayList<>(collection);
        Collections.sort(lst, comparator);

        return lst;
    }

    private int compare(Pair<PdpUpdate, PdpStateChange> left, Pair<PdpUpdate, PdpStateChange> right) {
        return getName(left).compareTo(getName(right));
    }

    private int compare(PdpUpdate left, PdpUpdate right) {
        return left.getName().compareTo(right.getName());
    }

    private int compare(PdpStateChange left, PdpStateChange right) {
        return left.getName().compareTo(right.getName());
    }

    private String getName(Pair<PdpUpdate, PdpStateChange> pair) {
        return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName());
    }
}