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