Changed identifiers to concept identifiers
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupDeployProvider.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.TreeSet;
39 import java.util.stream.Collectors;
40 import javax.ws.rs.core.Response.Status;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.ArgumentCaptor;
45 import org.onap.policy.common.utils.services.Registry;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
49 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
50 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
51 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
52 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
53 import org.onap.policy.models.pdp.concepts.PdpGroup;
54 import org.onap.policy.models.pdp.concepts.PdpGroups;
55 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
56 import org.onap.policy.models.pdp.concepts.PdpUpdate;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
59 import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
60
61 public class TestPdpGroupDeployProvider extends ProviderSuper {
62     private static final String EXPECTED_EXCEPTION = "expected exception";
63
64     private static final String POLICY2_NAME = "policyB";
65     private static final String POLICY3_NAME = "policyC";
66     private static final String POLICY1_VERSION = "1.2.3";
67     private static final String POLICY2_VERSION = "1.2.3";
68     private static final String POLICY3_VERSION = "1.2.3";
69     private static final String GROUP1_NAME = "groupA";
70     private static final String PDP1_TYPE = "pdpTypeA";
71     private static final String PDP2_TYPE = "pdpTypeB";
72     private static final String PDP4_TYPE = "pdpTypeD";
73     private static final String PDP2 = "pdpB";
74     private static final String PDP4 = "pdpD";
75
76     private PdpGroupDeployProvider prov;
77
78
79     @AfterClass
80     public static void tearDownAfterClass() {
81         Registry.newRegistry();
82     }
83
84     /**
85      * Configures mocks and objects.
86      *
87      * @throws Exception if an error occurs
88      */
89     @Override
90     @Before
91     public void setUp() throws Exception {
92
93         super.setUp();
94
95         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList2.json"));
96         when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
97
98         prov = new PdpGroupDeployProvider();
99     }
100
101     /**
102      * Tests updateGroupPolicies when policies are being added.
103      */
104     @Test
105     public void testUpdateGroupPoliciesAdd() throws Exception {
106         PdpGroups groups = loadPdpGroups("deployGroups.json");
107         PdpGroup newgrp = groups.getGroups().get(0);
108         PdpGroup dbgroup = new PdpGroup(newgrp);
109         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
110
111         // add new policies
112         List<ToscaConceptIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies();
113         policies.add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
114         policies.add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
115
116         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
117                         .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
118                         .thenReturn(loadPolicies("daoPolicyList.json"));
119
120         // add = POST
121         DeploymentGroups depgroups = toDeploymentGroups(groups);
122         depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.POST);
123
124         prov.updateGroupPolicies(depgroups);
125
126         assertEquals(newgrp.toString(), dbgroup.toString());
127         assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
128     }
129
130     /**
131      * Tests updateGroupPolicies when policies are being deleted.
132      */
133     @Test
134     public void testUpdateGroupPoliciesDelete() throws Exception {
135         PdpGroups groups = loadPdpGroups("deployGroups.json");
136         PdpGroup newgrp = groups.getGroups().get(0);
137
138         // additional policies in the DB that will be removed
139         List<ToscaConceptIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies();
140         policies.add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
141         policies.add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
142
143         PdpGroup dbgroup = new PdpGroup(newgrp);
144         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
145
146         // policy that should be left
147         final ToscaConceptIdentifier policyId1 = policies.remove(0);
148
149         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
150                         .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
151                         .thenReturn(loadPolicies("daoPolicyList.json"));
152
153         DeploymentGroups depgroups = toDeploymentGroups(groups);
154         depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.DELETE);
155
156         prov.updateGroupPolicies(depgroups);
157
158         // only the first policy should remain
159         policies.clear();
160         policies.add(policyId1);
161
162         assertEquals(newgrp.toString(), dbgroup.toString());
163         assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
164     }
165
166     /**
167      * Tests updateGroupPolicies when policies are being added and deleted in the same subgroup.
168      */
169     @Test
170     public void testUpdateGroupPoliciesAddAndDelete() throws Exception {
171         PdpGroups groups = loadPdpGroups("deployGroups.json");
172         PdpGroup newgrp = groups.getGroups().get(0);
173         PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
174
175         // put policy3 into db subgroup
176         subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
177         PdpGroup dbgroup = new PdpGroup(newgrp);
178         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
179
180         // now make the subgrp reflect our final expectation
181         subgrp.getPolicies().remove(1);
182         subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
183
184         // indicate policy2 being added and policy3 being deleted
185         DeploymentSubGroup depsub1 = new DeploymentSubGroup();
186         depsub1.setAction(Action.POST);
187         depsub1.setPdpType(subgrp.getPdpType());
188         depsub1.setPolicies(Arrays.asList(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION)));
189
190         DeploymentSubGroup depsub2 = new DeploymentSubGroup();
191         depsub2.setAction(Action.DELETE);
192         depsub2.setPdpType(subgrp.getPdpType());
193         depsub2.setPolicies(Arrays.asList(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION)));
194
195         DeploymentGroup depgroup = new DeploymentGroup();
196         depgroup.setName(newgrp.getName());
197         depgroup.setDeploymentSubgroups(Arrays.asList(depsub1, depsub2));
198
199         DeploymentGroups depgroups = new DeploymentGroups();
200         depgroups.setGroups(Arrays.asList(depgroup));
201
202         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
203                         .thenReturn(loadPolicies("daoPolicyList.json"))
204                         .thenReturn(loadPolicies("createGroupNewPolicy2.json"));
205
206         prov.updateGroupPolicies(depgroups);
207
208         assertEquals(newgrp.toString(), dbgroup.toString());
209         assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
210     }
211
212     @Test
213     public void testUpdateGroupPolicies() throws Exception {
214         PdpGroups groups = loadPdpGroups("deployGroups.json");
215         PdpGroup newgrp = groups.getGroups().get(0);
216         PdpGroup group = new PdpGroup(newgrp);
217         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
218
219         // something different in this subgroup
220         group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
221
222         prov.updateGroupPolicies(toDeploymentGroups(groups));
223
224         assertEquals(newgrp.toString(), group.toString());
225         assertGroupUpdate(group, group.getPdpSubgroups().get(0));
226     }
227
228     @Test
229     public void testUpdateGroupPolicies_EmptyRequest() throws Exception {
230         prov.updateGroupPolicies(toDeploymentGroups(loadPdpGroups("emptyGroups.json")));
231
232         // no groups, so no action should have been taken
233         assertNoGroupAction();
234     }
235
236     @Test
237     public void testUpdateGroupPolicies_InvalidRequest() throws Exception {
238         assertThatThrownBy(() -> prov.updateGroupPolicies(new DeploymentGroups())).isInstanceOf(PfModelException.class)
239                         .hasMessageContaining("is null");
240
241         assertNoGroupAction();
242     }
243
244     @Test
245     public void testUpdateGroup_UnknownGroup() throws Exception {
246         PdpGroups groups = loadPdpGroups("deployGroups.json");
247
248         String groupName = groups.getGroups().get(0).getName();
249
250         // group not found
251         when(dao.getPdpGroups(groupName)).thenReturn(Collections.emptyList());
252
253         assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
254                         .isInstanceOf(PfModelException.class).hasMessageContaining(groupName)
255                         .hasMessageContaining("unknown group");
256
257         assertNoGroupAction();
258     }
259
260     @Test
261     public void testUpdateGroup() throws Exception {
262         PdpGroups groups = loadPdpGroups("deployGroups.json");
263
264         // DB group = new group
265         PdpGroup group = new PdpGroup(groups.getGroups().get(0));
266         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
267
268         prov.updateGroupPolicies(toDeploymentGroups(groups));
269
270         assertNoGroupAction();
271     }
272
273     @Test
274     public void testUpdateGroup_NewSubGroup() throws Exception {
275         PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
276         PdpGroup group = loadPdpGroups("deployGroups.json").getGroups().get(0);
277         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
278
279         assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
280                         .isInstanceOf(PfModelException.class).hasMessageContaining("pdpTypeB")
281                         .hasMessageContaining("unknown subgroup");
282
283
284         assertNoGroupAction();
285     }
286
287     @Test
288     public void testUpdateGroup_UpdatedSubGroup() throws Exception {
289         PdpGroups groups = loadPdpGroups("deployGroups.json");
290         PdpGroup newgrp = groups.getGroups().get(0);
291         PdpGroup group = new PdpGroup(newgrp);
292         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
293
294         // something different in this subgroup
295         group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
296
297         prov.updateGroupPolicies(toDeploymentGroups(groups));
298
299         assertEquals(newgrp.toString(), group.toString());
300         assertGroupUpdate(group, group.getPdpSubgroups().get(0));
301     }
302
303     @Test
304     public void testUpdateSubGroup_Invalid() throws Exception {
305         PdpGroups groups = loadPdpGroups("deployGroups.json");
306         PdpGroup newgrp = groups.getGroups().get(0);
307         PdpGroup group = new PdpGroup(newgrp);
308
309         // group has no policies yet
310         group.getPdpSubgroups().get(0).getPolicies().clear();
311         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
312
313         // unknown policy
314         when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());
315
316         assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
317                         .isInstanceOf(PfModelException.class)
318                         .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
319                         .hasMessageContaining("unknown policy");
320
321         assertNoGroupAction();
322     }
323
324     @Test
325     public void testUpdateSubGroup_Policies() throws Exception {
326         PdpGroups groups = loadPdpGroups("deployGroups.json");
327         PdpGroup newgrp = groups.getGroups().get(0);
328
329         // add a second subgroup, which will be left unchanged
330         PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
331         PdpSubGroup subgrp2 = new PdpSubGroup(subgrp);
332         subgrp2.setPdpType(PDP2_TYPE);
333         newgrp.getPdpSubgroups().add(subgrp2);
334
335         PdpGroup group = new PdpGroup(newgrp);
336         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
337
338         // add two new policies
339         ToscaConceptIdentifier policyId2 = new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION);
340         subgrp.getPolicies().add(policyId2);
341
342         ToscaConceptIdentifier policyId3 = new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION);
343         subgrp.getPolicies().add(policyId3);
344
345         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
346                         .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
347                         .thenReturn(loadPolicies("daoPolicyList.json"));
348
349         prov.updateGroupPolicies(toDeploymentGroups(groups));
350
351         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
352         Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
353
354         assertEquals(newgrp.toString(), group.toString());
355
356         // should have notified of added policy/PDPs
357         ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
358         verify(notifier, times(2)).addDeploymentData(captor.capture());
359         assertDeploymentData(captor.getAllValues().get(0), policyId2, "[pdpA]");
360         assertDeploymentData(captor.getAllValues().get(1), policyId3, "[pdpA]");
361
362         // should NOT have notified of any deleted policy/PDPs
363         verify(notifier, never()).addUndeploymentData(any());
364
365         // this requires a PDP UPDATE message
366         assertGroupUpdate(newgrp, subgrp);
367     }
368
369     @Test
370     public void testUpdateSubGroup_PolicyVersionPrefix() throws Exception {
371         PdpGroups groups = loadPdpGroups("deployGroups.json");
372         PdpGroup newgrp = groups.getGroups().get(0);
373
374         PdpGroup group = new PdpGroup(newgrp);
375         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
376
377         // use version prefix
378         PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
379         ToscaConceptIdentifier ident = subgrp.getPolicies().get(0);
380         String version = ident.getVersion();
381         ident.setVersion("1");
382
383         prov.updateGroupPolicies(toDeploymentGroups(groups));
384
385         // restore full type before comparing
386         ident.setVersion(version);
387
388         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
389         Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
390
391         assertEquals(newgrp.toString(), group.toString());
392
393         assertNoGroupAction();
394     }
395
396     @Test
397     public void testUpdateSubGroup_PolicyVersionPrefixMismatch() throws Exception {
398         PdpGroups groups = loadPdpGroups("deployGroups.json");
399         PdpGroup newgrp = groups.getGroups().get(0);
400
401         PdpGroup group = new PdpGroup(newgrp);
402         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
403
404         // use incorrect version prefix
405         newgrp.getPdpSubgroups().get(0).getPolicies().get(0).setVersion("9");
406
407         assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
408                         .isInstanceOf(PfModelException.class)
409                         .hasMessageContaining("different version already deployed");
410
411         assertNoGroupAction();
412     }
413
414     @Test
415     public void testUpdateSubGroup_Unchanged() throws Exception {
416         PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
417         PdpGroup newgrp = dbgroups.getGroups().get(0);
418         PdpGroup group = new PdpGroup(newgrp);
419         when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
420
421         prov.updateGroupPolicies(toDeploymentGroups(dbgroups));
422
423         Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
424         Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
425
426         assertEquals(newgrp.toString(), group.toString());
427
428         // no notifications
429         verify(notifier, never()).addDeploymentData(any());
430         verify(notifier, never()).addUndeploymentData(any());
431
432         // no group updates
433         assertNoGroupAction();
434     }
435
436     @Test
437     public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception {
438         PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
439         PdpGroup newgrp = dbgroups.getGroups().get(0);
440         PdpGroup dbgroup = new PdpGroup(newgrp);
441         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
442
443         // arrange for DB policy version to be different
444         PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
445         dbsubgrp.getPolicies().get(0).setVersion("9.9.9");
446
447         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
448
449         assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(dbgroups)))
450                         .isInstanceOf(PfModelException.class)
451                         .hasMessageContaining("different version already deployed");
452
453         assertNoGroupAction();
454     }
455
456     @Test
457     public void testUpdateSubGroup_UnsupportedType() throws Exception {
458         PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
459         PdpGroup newgrp = dbgroups.getGroups().get(0);
460         PdpGroup dbgroup = new PdpGroup(newgrp);
461         when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
462
463         final DeploymentGroups groups = toDeploymentGroups(dbgroups);
464
465         PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
466
467         // DB has no policies
468         dbsubgrp.getPolicies().clear();
469
470         // DB has a different supported type
471         dbsubgrp.getSupportedPolicyTypes().get(0).setName("some-other-type");
472
473         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
474
475         assertThatThrownBy(() -> prov.updateGroupPolicies(groups))
476                         .isInstanceOf(PfModelException.class)
477                         .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
478                         .hasMessageContaining("not a supported policy for the subgroup");
479
480         assertNoGroupAction();
481     }
482
483     @Test
484     public void testDeployPolicies() throws PfModelException {
485         assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
486     }
487
488     /**
489      * Tests deployPolicies() when the supported policy type uses a wild-card.
490      *
491      * @throws Exception if an error occurs
492      */
493     @Test
494     public void testDeployPoliciesWildCard() throws Exception {
495         when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("deployPoliciesWildCard.json"));
496         when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json"));
497         when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
498
499         policy1.setName("policy.some");
500         policy1.setVersion(POLICY1_VERSION);
501         policy1.setType("some.type");
502         policy1.setTypeVersion("100.2.3");
503
504         PdpDeployPolicies depreq = loadRequest();
505         depreq.getPolicies().get(0).setName("policy.some");
506
507         prov.deployPolicies(depreq);
508
509         assertGroup(getGroupUpdates(), GROUP1_NAME);
510
511         List<PdpUpdate> requests = getUpdateRequests(1);
512         assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
513
514         // should have notified of added policy/PDPs
515         ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
516         verify(notifier).addDeploymentData(captor.capture());
517         assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB]");
518
519         // no undeployment notifications
520         verify(notifier, never()).addUndeploymentData(any());
521     }
522
523     @Test
524     public void testDeploySimplePolicies() throws Exception {
525         assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
526     }
527
528     @Test
529     public void testDeploySimplePolicies_DaoEx() throws Exception {
530         PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
531         when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
532
533         assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
534     }
535
536     @Test
537     public void testDeploySimplePolicies_DaoPfRtEx() throws Exception {
538         PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
539         when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
540
541         assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
542     }
543
544     @Test
545     public void testDeploySimplePolicies_RuntimeEx() throws Exception {
546         RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION);
547         when(dao.getFilteredPolicyList(any())).thenThrow(exc);
548
549         assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc);
550     }
551
552     @Test
553     public void testDeploySimplePolicies_NoGroups() throws Exception {
554         when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json"));
555
556         assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class)
557                         .hasMessage("policy not supported by any PDP group: policyA 1.2.3");
558     }
559
560     @Test
561     public void testMakeUpdater() throws Exception {
562         /*
563          * Each subgroup has a different PDP type and name.
564          *
565          * Type is not supported by the first subgroup.
566          *
567          * Second subgroup matches.
568          *
569          * Third subgroup already contains the policy.
570          *
571          * Last subgroup matches.
572          */
573
574         when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json"));
575
576         prov.deployPolicies(loadRequest());
577
578         assertGroup(getGroupUpdates(), GROUP1_NAME);
579
580         List<PdpUpdate> requests = getUpdateRequests(2);
581         assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
582         assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4);
583
584         // should have notified of added policy/PDPs
585         ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
586         verify(notifier).addDeploymentData(captor.capture());
587         assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB, pdpD]");
588
589         // no undeployment notifications
590         verify(notifier, never()).addUndeploymentData(any());
591     }
592
593     @Test
594     public void testMakeUpdater_PolicyVersionMismatch() throws Exception {
595
596         // subgroup has a different version of the Policy
597         when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json"));
598
599         PdpDeployPolicies req = loadRequest();
600         assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
601                         .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed");
602
603         verify(dao, never()).createPdpGroups(any());
604         verify(dao, never()).updatePdpGroups(any());
605         verify(reqmap, never()).addRequest(any(PdpUpdate.class));
606     }
607
608     @Test
609     public void testMakeUpdater_NoPdps() throws Exception {
610
611         // subgroup has no PDPs
612         when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json"));
613
614         PdpDeployPolicies req = loadRequest();
615         assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
616                         .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs");
617
618         verify(dao, never()).createPdpGroups(any());
619         verify(dao, never()).updatePdpGroups(any());
620         verify(reqmap, never()).addRequest(any(PdpUpdate.class));
621     }
622
623
624     protected void assertUpdate(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
625
626         PdpUpdate update = updates.remove(0);
627
628         assertEquals(groupName, update.getPdpGroup());
629         assertEquals(pdpType, update.getPdpSubgroup());
630         assertEquals(pdpName, update.getName());
631         assertTrue(update.getPolicies().contains(policy1));
632     }
633
634     private void assertNoGroupAction() throws Exception {
635         verify(dao, never()).createPdpGroups(any());
636         verify(dao, never()).updatePdpGroups(any());
637         verify(reqmap, never()).addRequest(any(), any());
638     }
639
640     private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception {
641         verify(dao, never()).createPdpGroups(any());
642
643         assertEquals(0, getStateChangeRequests(1).size());
644
645         List<PdpUpdate> pdpUpdates = getUpdateRequests(1);
646         assertEquals(1, pdpUpdates.size());
647
648         PdpUpdate pdpUpdate = pdpUpdates.get(0);
649         assertEquals("pdpA", pdpUpdate.getName());
650         assertEquals(group.getName(), pdpUpdate.getPdpGroup());
651
652         assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup());
653
654         List<ToscaConceptIdentifier> pdpPolicies =
655                         pdpUpdate.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
656         Collections.sort(pdpPolicies);
657
658         assertEquals(subgrp.getPolicies().toString(), pdpPolicies.toString());
659
660         List<PdpGroup> updates = getGroupUpdates();
661         assertEquals(Arrays.asList(group), updates);
662     }
663
664     private void assertDeploymentData(PolicyPdpNotificationData data, ToscaConceptIdentifier policyId,
665                     String expectedPdps) {
666         assertEquals(policyId, data.getPolicyId());
667         assertEquals(policy1.getTypeIdentifier(), data.getPolicyType());
668         assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString());
669     }
670
671     /**
672      * Loads a standard request.
673      *
674      * @return a standard request
675      */
676     protected PdpDeployPolicies loadRequest() {
677         return loadRequest("request.json");
678     }
679
680     /**
681      * Loads a request from a JSON file.
682      *
683      * @param fileName name of the file from which to load
684      * @return the request that was loaded
685      */
686     protected PdpDeployPolicies loadRequest(String fileName) {
687         return loadFile(fileName, PdpDeployPolicies.class);
688     }
689
690     /**
691      * Loads an empty request.
692      *
693      * @return an empty request
694      */
695     protected PdpDeployPolicies loadEmptyRequest() {
696         return loadRequest("emptyRequest.json");
697     }
698
699     private DeploymentGroups toDeploymentGroups(PdpGroups dbgroups) {
700         DeploymentGroups groups = new DeploymentGroups();
701
702         groups.setGroups(dbgroups.getGroups().stream().map(this::toDeploymentGroup).collect(Collectors.toList()));
703
704         return groups;
705     }
706
707     private DeploymentGroup toDeploymentGroup(PdpGroup dbgroup) {
708         DeploymentGroup group = new DeploymentGroup();
709
710         group.setName(dbgroup.getName());
711         group.setDeploymentSubgroups(dbgroup.getPdpSubgroups().stream().map(this::toDeploymentSubGroup)
712                         .collect(Collectors.toList()));
713
714         return group;
715     }
716
717     private DeploymentSubGroup toDeploymentSubGroup(PdpSubGroup dbsubgrp) {
718         DeploymentSubGroup subgrp = new DeploymentSubGroup();
719
720         subgrp.setAction(Action.PATCH);
721         subgrp.setPdpType(dbsubgrp.getPdpType());
722         subgrp.setPolicies(new ArrayList<>(dbsubgrp.getPolicies()));
723
724         return subgrp;
725     }
726 }