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