Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestSessionData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021, 2023 Nordix Foundation.
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertNull;
32 import static org.junit.jupiter.api.Assertions.assertSame;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.ArgumentMatchers.anyBoolean;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.never;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41
42 import jakarta.ws.rs.core.Response.Status;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.Comparator;
47 import java.util.Iterator;
48 import java.util.List;
49 import org.apache.commons.lang3.tuple.Pair;
50 import org.junit.jupiter.api.BeforeEach;
51 import org.junit.jupiter.api.Test;
52 import org.mockito.ArgumentCaptor;
53 import org.onap.policy.models.base.PfModelException;
54 import org.onap.policy.models.pap.concepts.PolicyNotification;
55 import org.onap.policy.models.pdp.concepts.PdpGroup;
56 import org.onap.policy.models.pdp.concepts.PdpStateChange;
57 import org.onap.policy.models.pdp.concepts.PdpUpdate;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
61 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
63 import org.onap.policy.pap.main.notification.DeploymentStatus;
64 import org.onap.policy.pap.main.service.PolicyStatusService;
65
66 class TestSessionData extends ProviderSuper {
67     private static final String GROUP_NAME = "groupA";
68     private static final String PDP_TYPE = "MySubGroup";
69     private static final String PDP1 = "pdp_1";
70     private static final String PDP2 = "pdp_2";
71     private static final String PDP3 = "pdp_3";
72     private static final String POLICY_VERSION_PREFIX = "1.2.";
73     private static final String POLICY_NAME = "myPolicy";
74     private static final String POLICY_VERSION = POLICY_VERSION_PREFIX + "3";
75     private static final String POLICY_TYPE = "myType";
76     private static final String POLICY_TYPE_VERSION = "10.20.30";
77     private static final String EXPECTED_EXCEPTION = "expected exception";
78
79     private SessionData session;
80     private ToscaConceptIdentifierOptVersion ident;
81     private ToscaConceptIdentifier type;
82     private ToscaConceptIdentifier type2;
83     private PdpGroup group1;
84     private PdpGroup group2;
85
86     /**
87      * Initializes mocks and a session.
88      *
89      * @throws Exception if an error occurs
90      */
91     @Override
92     @BeforeEach
93     public void setUp() throws Exception {
94         super.setUp();
95
96         ident = new ToscaConceptIdentifierOptVersion(POLICY_NAME, POLICY_VERSION);
97         type = new ToscaConceptIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION);
98         type2 = new ToscaConceptIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION + "0");
99         group1 = loadGroup("group1.json");
100         group2 = loadGroup("group2.json");
101
102         session = new SessionData(DEFAULT_USER, toscaService, pdpGroupService, policyStatusService, policyAuditService);
103     }
104
105     @Test
106     void testGetPolicyType() throws Exception {
107         ToscaPolicyType policy1 = makePolicyType(POLICY_TYPE, POLICY_TYPE_VERSION);
108         when(toscaService.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenReturn(List.of(policy1));
109
110         assertSame(policy1, session.getPolicyType(type));
111
112         // retrieve a second time - should use cache
113         assertSame(policy1, session.getPolicyType(type));
114     }
115
116     @Test
117     void testGetPolicyType_NotFound() throws Exception {
118         when(toscaService.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
119
120         assertNull(session.getPolicyType(type));
121     }
122
123     @Test
124     void testGetPolicyType_DaoEx() throws Exception {
125         PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION);
126         when(toscaService.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenThrow(ex);
127
128         assertThatThrownBy(() -> session.getPolicyType(type)).isSameAs(ex);
129     }
130
131     @Test
132     void testGetPolicy_NullVersion() throws Exception {
133         ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
134         when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy1));
135
136         ident.setVersion(null);
137         assertSame(policy1, session.getPolicy(ident));
138
139         ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
140         assertEquals(POLICY_NAME, filter.getName());
141         assertEquals(ToscaTypedEntityFilter.LATEST_VERSION, filter.getVersion());
142         assertNull(filter.getVersionPrefix());
143
144         // retrieve a second time using full version - should use cache
145         assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
146         verify(toscaService).getFilteredPolicyList(any());
147     }
148
149     @Test
150     void testGetPolicy_MajorVersion() throws Exception {
151         ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
152         when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy1));
153
154         ident.setVersion("1");
155         assertSame(policy1, session.getPolicy(ident));
156
157         ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
158         assertEquals(POLICY_NAME, filter.getName());
159         assertEquals(ToscaTypedEntityFilter.LATEST_VERSION, filter.getVersion());
160         assertEquals("1.", filter.getVersionPrefix());
161
162         // retrieve a second time using full version - should use cache
163         assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
164         verify(toscaService).getFilteredPolicyList(any());
165     }
166
167     @Test
168     void testGetPolicy_MajorMinorVersion() throws Exception {
169         ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION);
170         when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy1));
171
172         ident.setVersion(POLICY_VERSION);
173         assertSame(policy1, session.getPolicy(ident));
174
175         ToscaTypedEntityFilter<ToscaPolicy> filter = getPolicyFilter();
176         assertEquals(POLICY_NAME, filter.getName());
177         assertEquals(POLICY_VERSION, filter.getVersion());
178         assertNull(filter.getVersionPrefix());
179
180         // retrieve a second time using full version - should use cache
181         assertSame(policy1, session.getPolicy(new ToscaConceptIdentifierOptVersion(policy1.getIdentifier())));
182         verify(toscaService).getFilteredPolicyList(any());
183     }
184
185     @Test
186     void testGetPolicy_NotFound() throws Exception {
187         when(toscaService.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());
188
189         assertNull(session.getPolicy(ident));
190     }
191
192     @Test
193     void testGetPolicy_DaoEx() throws Exception {
194         PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION);
195         when(toscaService.getFilteredPolicyList(any())).thenThrow(ex);
196
197         assertThatThrownBy(() -> session.getPolicy(ident)).isSameAs(ex);
198     }
199
200     @Test
201     void testIsVersionPrefix() {
202         assertTrue(SessionData.isVersionPrefix("1"));
203         assertTrue(SessionData.isVersionPrefix("12"));
204         assertTrue(SessionData.isVersionPrefix("1.2"));
205         assertTrue(SessionData.isVersionPrefix("1.23"));
206
207         assertFalse(SessionData.isVersionPrefix("1."));
208         assertFalse(SessionData.isVersionPrefix("1.2."));
209         assertFalse(SessionData.isVersionPrefix("1.2.3"));
210         assertFalse(SessionData.isVersionPrefix("1.2.3."));
211         assertFalse(SessionData.isVersionPrefix("1.2.3.4"));
212     }
213
214     @Test
215     void testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() {
216         // pre-load with a update and state-change for other PDPs
217         PdpUpdate update2 = makeUpdate(PDP2);
218         session.addUpdate(update2);
219
220         PdpStateChange change3 = makeStateChange(PDP3);
221         session.addStateChange(change3);
222
223         // add requests
224         PdpUpdate update = makeUpdate(PDP1);
225         PdpStateChange change = makeStateChange(PDP1);
226         session.addRequests(update, change);
227         verifyRequests(update, update2, change, change3);
228
229         /*
230          * repeat with a new pair
231          */
232         update = makeUpdate(PDP1);
233         change = makeStateChange(PDP1);
234         session.addRequests(update, change);
235         verifyRequests(update, update2, change, change3);
236
237         // just make an update this time
238         update = makeUpdate(PDP1);
239         session.addUpdate(update);
240         verifyRequests(update, update2, change, change3);
241     }
242
243     private void verifyRequests(PdpUpdate update, PdpUpdate update2, PdpStateChange change, PdpStateChange change3) {
244         List<Pair<PdpUpdate, PdpStateChange>> requests = sort(session.getPdpRequests(), this::compare);
245         assertEquals(3, requests.size());
246
247         System.out.println(requests);
248         System.out.println(update);
249
250         Iterator<Pair<PdpUpdate, PdpStateChange>> reqiter = requests.iterator();
251         Pair<PdpUpdate, PdpStateChange> pair = reqiter.next();
252         assertSame(update, pair.getLeft());
253         assertSame(change, pair.getRight());
254
255         pair = reqiter.next();
256         assertSame(update2, pair.getLeft());
257         assertSame(null, pair.getRight());
258
259         pair = reqiter.next();
260         assertSame(null, pair.getLeft());
261         assertSame(change3, pair.getRight());
262
263         // verify individual lists
264         List<PdpUpdate> updates = List.of(update, update2);
265         assertEquals(sort(updates, this::compare), sort(session.getPdpUpdates(), this::compare));
266
267         List<PdpStateChange> changes = List.of(change, change3);
268         assertEquals(sort(changes, this::compare), sort(session.getPdpStateChanges(), this::compare));
269     }
270
271     @Test
272     void testAddRequests_MismatchedNames() {
273         PdpUpdate update = makeUpdate(PDP1);
274         PdpStateChange change = makeStateChange(PDP2);
275         assertThatIllegalArgumentException().isThrownBy(() -> session.addRequests(update, change))
276                 .withMessage("PDP name mismatch pdp_1, pdp_2");
277     }
278
279     @Test
280     void testAddUpdate_testGetPdpUpdates() {
281         // several different updates, but one duplicate
282         PdpUpdate update1 = makeUpdate(PDP1);
283         session.addUpdate(update1);
284
285         PdpUpdate update2 = makeUpdate(PDP2);
286         session.addUpdate(update2);
287
288         PdpUpdate update3 = makeUpdate(PDP3);
289         session.addUpdate(update3);
290
291         List<PdpUpdate> lst = sort(getUpdateRequests(), this::compare);
292         assertEquals(List.of(update1, update2, update3).toString(), lst.toString());
293
294         // overwrite one
295         update2 = makeUpdate(PDP2);
296         session.addUpdate(update2);
297
298         lst = sort(getUpdateRequests(), this::compare);
299         assertEquals(List.of(update1, update2, update3).toString(), lst.toString());
300     }
301
302     @Test
303     void testAddStateChange_testGetPdpStateChanges() {
304         // several different changes, but one duplicate
305         PdpStateChange change1 = makeStateChange(PDP1);
306         session.addStateChange(change1);
307
308         PdpStateChange change2 = makeStateChange(PDP2);
309         session.addStateChange(change2);
310
311         PdpStateChange change3 = makeStateChange(PDP3);
312         session.addStateChange(change3);
313
314         List<PdpStateChange> lst = sort(getStateChangeRequests(), this::compare);
315         assertEquals(List.of(change1, change2, change3).toString(), lst.toString());
316
317         // overwrite one
318         change2 = makeStateChange(PDP2);
319         session.addStateChange(change2);
320
321         lst = sort(getStateChangeRequests(), this::compare);
322         assertEquals(List.of(change1, change2, change3).toString(), lst.toString());
323     }
324
325     private ToscaPolicyType makePolicyType(String name, String version) {
326         ToscaPolicyType type = new ToscaPolicyType();
327
328         type.setName(name);
329         type.setVersion(version);
330
331         return type;
332     }
333
334     private ToscaPolicy makePolicy(String name, String version) {
335         ToscaPolicy policy = new ToscaPolicy();
336
337         policy.setName(name);
338         policy.setVersion(version);
339
340         return policy;
341     }
342
343     @Test
344     void testCreate() throws Exception {
345         assertTrue(session.isUnchanged());
346
347         session.create(group1);
348         assertSame(group1, session.getGroup(group1.getName()));
349         assertFalse(session.isUnchanged());
350
351         // can add another
352         session.create(group2);
353         assertSame(group1, session.getGroup(group1.getName()));
354         assertSame(group2, session.getGroup(group2.getName()));
355         assertFalse(session.isUnchanged());
356
357         // cannot overwrite
358         assertThatIllegalStateException().isThrownBy(() -> session.create(group1))
359                 .withMessage("group already cached: groupA");
360     }
361
362     @Test
363     void testUpdate() {
364         assertTrue(session.isUnchanged());
365
366         // force the groups into the cache
367         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1, group2));
368         session.getActivePdpGroupsByPolicyType(type);
369
370         /*
371          * try group 1
372          */
373         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1));
374         PdpGroup newgrp = new PdpGroup(group1);
375         session.update(newgrp);
376         assertFalse(session.isUnchanged());
377
378         // repeat
379         newgrp = new PdpGroup(group1);
380         session.update(newgrp);
381         assertFalse(session.isUnchanged());
382
383         /*
384          * try group 2
385          */
386         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group2));
387         newgrp = new PdpGroup(group2);
388         session.update(newgrp);
389         assertFalse(session.isUnchanged());
390
391         // repeat
392         newgrp = new PdpGroup(group2);
393         session.update(newgrp);
394         assertFalse(session.isUnchanged());
395     }
396
397     @Test
398     void testUpdate_NotInCache() {
399         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1));
400
401         assertThatIllegalStateException().isThrownBy(() -> session.update(new PdpGroup(group1)))
402                 .withMessage("group not cached: groupA");
403     }
404
405     @Test
406     void testGetGroup() throws Exception {
407         when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(List.of(group1));
408
409         assertSame(group1, session.getGroup(GROUP_NAME));
410         verify(pdpGroupService).getPdpGroups(any(String.class));
411
412         // repeat
413         assertSame(group1, session.getGroup(GROUP_NAME));
414
415         // should not access dao again
416         verify(pdpGroupService, times(1)).getPdpGroups(any(String.class));
417     }
418
419     @Test
420     void testGetGroup_NotFound() throws Exception {
421         when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(Collections.emptyList());
422
423         assertNull(session.getGroup(GROUP_NAME));
424         verify(pdpGroupService).getPdpGroups(any(String.class));
425
426         // repeat
427         assertNull(session.getGroup(GROUP_NAME));
428
429         // SHOULD access dao again
430         verify(pdpGroupService, times(2)).getPdpGroups(GROUP_NAME);
431
432         // find it this time
433         when(pdpGroupService.getPdpGroups(GROUP_NAME)).thenReturn(List.of(group1));
434         assertSame(group1, session.getGroup(GROUP_NAME));
435         verify(pdpGroupService, times(3)).getPdpGroups(GROUP_NAME);
436     }
437
438     @Test
439     void testGetActivePdpGroupsByPolicyType() {
440         List<PdpGroup> groups = List.of(group1, group2);
441         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
442
443         // repeat
444         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
445         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
446         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
447
448         // only invoked once - should have used the cache for the rest
449         verify(pdpGroupService, times(1)).getFilteredPdpGroups(any());
450     }
451
452     @Test
453     void testAddGroup() {
454         List<PdpGroup> groups = List.of(group1, group2);
455         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
456
457         // query by each type
458         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
459         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2));
460
461         // invoked once for each type
462         verify(pdpGroupService, times(2)).getFilteredPdpGroups(any());
463
464         // repeat - should be no more invocations
465         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type));
466         assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2));
467         verify(pdpGroupService, times(2)).getFilteredPdpGroups(any());
468     }
469
470     @Test
471     void testUpdateDb() {
472         // force the groups into the cache
473         PdpGroup group3 = loadGroup("group3.json");
474         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1, group2, group3));
475         session.getActivePdpGroupsByPolicyType(type);
476
477         // create groups 4 & 5
478         PdpGroup group4 = loadGroup("group4.json");
479         session.create(group4);
480
481         PdpGroup group5 = loadGroup("group5.json");
482         session.create(group5);
483
484         // update group 1
485         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1));
486         PdpGroup newgrp1 = new PdpGroup(group1);
487         session.update(newgrp1);
488
489         // another update
490         newgrp1 = new PdpGroup(newgrp1);
491         session.update(newgrp1);
492
493         // update group 3
494         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group3));
495         PdpGroup newgrp3 = new PdpGroup(group3);
496         session.update(newgrp3);
497
498         // update group 5
499         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group5));
500         PdpGroup newgrp5 = new PdpGroup(group5);
501         session.update(newgrp5);
502
503         // push the changes to the DB
504         PolicyNotification notif = new PolicyNotification();
505         session.updateDb(notif);
506         assertThat(notif.getAdded()).isEmpty();
507
508         // expect one create for groups 4 & 5 (group5 replaced by newgrp5)
509         List<PdpGroup> creates = getGroupCreates();
510         assertEquals(2, creates.size());
511         assertSame(group4, creates.get(0));
512         assertSame(newgrp5, creates.get(1));
513
514         // expect one update for groups 1 & 3
515         List<PdpGroup> updates = getGroupUpdates();
516         assertEquals(2, updates.size());
517         assertSame(newgrp1, updates.get(0));
518         assertSame(newgrp3, updates.get(1));
519     }
520
521     @Test
522     void testUpdateDb_Empty() {
523         // force data into the cache
524         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(List.of(group1, group2));
525         session.getActivePdpGroupsByPolicyType(type);
526
527         PolicyNotification notif = new PolicyNotification();
528         session.updateDb(notif);
529         assertThat(notif.getAdded()).isEmpty();
530
531         verify(pdpGroupService, never()).createPdpGroups(any());
532         verify(pdpGroupService, never()).updatePdpGroups(any());
533     }
534
535     @Test
536     void testDeleteGroupFromDb() {
537         session.deleteGroupFromDb(group1);
538
539         verify(pdpGroupService).deletePdpGroup(group1.getName());
540     }
541
542     @Test
543     void testTrackDeploy() throws PfModelException {
544         testTrack(true);
545     }
546
547     @Test
548     void testTrackUndeploy() throws PfModelException {
549         testTrack(false);
550     }
551
552     protected void testTrack(boolean deploy) throws PfModelException {
553
554         DeploymentStatus status = mock(DeploymentStatus.class);
555
556         session =
557             new SessionData(DEFAULT_USER, toscaService, pdpGroupService, policyStatusService, policyAuditService) {
558                 @Override
559                 protected DeploymentStatus makeDeploymentStatus(PolicyStatusService policyStatusService) {
560                     return status;
561                 }
562             };
563
564         ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION);
565         policy.setType(POLICY_TYPE);
566         policy.setTypeVersion(POLICY_TYPE_VERSION);
567
568         when(toscaService.getFilteredPolicyList(any())).thenReturn(List.of(policy));
569
570         ToscaConceptIdentifier policyId = new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION);
571         List<String> pdps = List.of(PDP1, PDP2);
572
573         ToscaPolicy testPolicy = session.getPolicy(new ToscaConceptIdentifierOptVersion(policyId));
574
575         if (deploy) {
576             session.trackDeploy(testPolicy, pdps, GROUP_NAME, PDP_TYPE);
577             assertThat(session.getPoliciesToBeDeployed()).contains(testPolicy);
578         } else {
579             session.trackUndeploy(policyId, pdps, GROUP_NAME, PDP_TYPE);
580             assertThat(session.getPoliciesToBeUndeployed()).contains(policyId);
581         }
582
583         // should be called just once
584         verify(status).deleteDeployment(any(), anyBoolean());
585         verify(status, times(1)).deleteDeployment(policyId, !deploy);
586
587         // should be called for each PDP
588         verify(status, times(2)).deploy(any(), any(), any(), any(), any(), anyBoolean());
589         verify(status).deploy(PDP1, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
590         verify(status).deploy(PDP2, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy);
591     }
592
593     private PdpUpdate makeUpdate(String pdpName) {
594         PdpUpdate update = new PdpUpdate();
595
596         update.setName(pdpName);
597
598         return update;
599     }
600
601     private PdpStateChange makeStateChange(String pdpName) {
602         PdpStateChange change = new PdpStateChange();
603
604         change.setName(pdpName);
605
606         return change;
607     }
608
609     private ToscaTypedEntityFilter<ToscaPolicy> getPolicyFilter() throws Exception {
610         @SuppressWarnings("unchecked")
611         ArgumentCaptor<ToscaTypedEntityFilter<ToscaPolicy>> captor =
612                 ArgumentCaptor.forClass(ToscaTypedEntityFilter.class);
613         verify(toscaService).getFilteredPolicyList(captor.capture());
614
615         return captor.getValue();
616     }
617
618     private List<PdpUpdate> getUpdateRequests() {
619         return session.getPdpUpdates();
620     }
621
622     private List<PdpStateChange> getStateChangeRequests() {
623         return session.getPdpStateChanges();
624     }
625
626     private <T> List<T> sort(Collection<T> collection, Comparator<T> comparator) {
627         List<T> lst = new ArrayList<>(collection);
628         lst.sort(comparator);
629
630         return lst;
631     }
632
633     private int compare(Pair<PdpUpdate, PdpStateChange> left, Pair<PdpUpdate, PdpStateChange> right) {
634         return getName(left).compareTo(getName(right));
635     }
636
637     private int compare(PdpUpdate left, PdpUpdate right) {
638         return left.getName().compareTo(right.getName());
639     }
640
641     private int compare(PdpStateChange left, PdpStateChange right) {
642         return left.getName().compareTo(right.getName());
643     }
644
645     private String getName(Pair<PdpUpdate, PdpStateChange> pair) {
646         return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName());
647     }
648 }