2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.rest;
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;
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;
58 public class TestPdpGroupDeployProvider extends ProviderSuper {
60 private static final String EXPECTED_EXCEPTION = "expected exception";
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";
74 private PdpGroupDeployProvider prov;
78 public static void tearDownAfterClass() {
79 Registry.newRegistry();
83 * Configures mocks and objects.
85 * @throws Exception if an error occurs
89 public void setUp() throws Exception {
93 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList2.json"));
94 when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
96 prov = new PdpGroupDeployProvider();
100 * Tests updateGroupPolicies when policies are being added.
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));
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));
114 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
115 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
116 .thenReturn(loadPolicies("daoPolicyList.json"));
119 DeploymentGroups depgroups = toDeploymentGroups(groups);
120 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.POST);
122 prov.updateGroupPolicies(depgroups);
124 assertEquals(newgrp.toString(), dbgroup.toString());
125 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
129 * Tests updateGroupPolicies when policies are being deleted.
132 public void testUpdateGroupPoliciesDelete() throws Exception {
133 PdpGroups groups = loadPdpGroups("deployGroups.json");
134 PdpGroup newgrp = groups.getGroups().get(0);
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));
141 PdpGroup dbgroup = new PdpGroup(newgrp);
142 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
144 // policy that should be left
145 final ToscaConceptIdentifier policyId1 = policies.remove(0);
147 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
148 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
149 .thenReturn(loadPolicies("daoPolicyList.json"));
151 DeploymentGroups depgroups = toDeploymentGroups(groups);
152 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.DELETE);
154 prov.updateGroupPolicies(depgroups);
156 // only the first policy should remain
158 policies.add(policyId1);
160 assertEquals(newgrp.toString(), dbgroup.toString());
161 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
165 * Tests updateGroupPolicies when policies are being added and deleted in the same
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);
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));
179 // now make the subgrp reflect our final expectation
180 subgrp.getPolicies().remove(1);
181 subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
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)));
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)));
194 DeploymentGroup depgroup = new DeploymentGroup();
195 depgroup.setName(newgrp.getName());
196 depgroup.setDeploymentSubgroups(Arrays.asList(depsub1, depsub2));
198 DeploymentGroups depgroups = new DeploymentGroups();
199 depgroups.setGroups(Arrays.asList(depgroup));
201 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
202 .thenReturn(loadPolicies("daoPolicyList.json"))
203 .thenReturn(loadPolicies("createGroupNewPolicy2.json"));
205 prov.updateGroupPolicies(depgroups);
207 assertEquals(newgrp.toString(), dbgroup.toString());
208 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
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));
218 // something different in this subgroup
219 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
221 prov.updateGroupPolicies(toDeploymentGroups(groups));
223 assertEquals(newgrp.toString(), group.toString());
224 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
228 public void testUpdateGroupPolicies_EmptyRequest() throws Exception {
229 prov.updateGroupPolicies(toDeploymentGroups(loadPdpGroups("emptyGroups.json")));
231 // no groups, so no action should have been taken
232 assertNoGroupAction();
236 public void testUpdateGroupPolicies_InvalidRequest() throws Exception {
237 assertThatThrownBy(() -> prov.updateGroupPolicies(new DeploymentGroups())).isInstanceOf(PfModelException.class)
238 .hasMessageContaining("is null");
240 assertNoGroupAction();
244 public void testUpdateGroup_UnknownGroup() throws Exception {
245 PdpGroups groups = loadPdpGroups("deployGroups.json");
247 String groupName = groups.getGroups().get(0).getName();
250 when(dao.getPdpGroups(groupName)).thenReturn(Collections.emptyList());
252 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
253 .isInstanceOf(PfModelException.class).hasMessageContaining(groupName)
254 .hasMessageContaining("unknown group");
256 assertNoGroupAction();
260 public void testUpdateGroup() throws Exception {
261 PdpGroups groups = loadPdpGroups("deployGroups.json");
263 // DB group = new group
264 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
265 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
267 prov.updateGroupPolicies(toDeploymentGroups(groups));
269 assertNoGroupAction();
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));
278 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
279 .isInstanceOf(PfModelException.class).hasMessageContaining("pdpTypeB")
280 .hasMessageContaining("unknown subgroup");
283 assertNoGroupAction();
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));
293 // something different in this subgroup
294 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
296 prov.updateGroupPolicies(toDeploymentGroups(groups));
298 assertEquals(newgrp.toString(), group.toString());
299 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
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);
308 // group has no policies yet
309 group.getPdpSubgroups().get(0).getPolicies().clear();
310 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
313 when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());
315 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
316 .isInstanceOf(PfModelException.class)
317 .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
318 .hasMessageContaining("unknown policy");
320 assertNoGroupAction();
324 public void testUpdateSubGroup_Policies() throws Exception {
325 PdpGroups groups = loadPdpGroups("deployGroups.json");
326 PdpGroup newgrp = groups.getGroups().get(0);
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);
334 PdpGroup group = new PdpGroup(newgrp);
335 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
337 // add two new policies
338 ToscaConceptIdentifier policyId2 = new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION);
339 subgrp.getPolicies().add(policyId2);
341 ToscaConceptIdentifier policyId3 = new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION);
342 subgrp.getPolicies().add(policyId3);
344 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
345 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
346 .thenReturn(loadPolicies("daoPolicyList.json"));
348 prov.updateGroupPolicies(toDeploymentGroups(groups));
350 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
351 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
353 assertEquals(newgrp.toString(), group.toString());
355 // nothing is complete - notification should be empty
356 checkEmptyNotification();
358 // this requires a PDP UPDATE message
359 assertGroupUpdate(newgrp, subgrp);
363 public void testUpdateSubGroup_PolicyVersionPrefix() throws Exception {
364 PdpGroups groups = loadPdpGroups("deployGroups.json");
365 PdpGroup newgrp = groups.getGroups().get(0);
367 PdpGroup group = new PdpGroup(newgrp);
368 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
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");
376 prov.updateGroupPolicies(toDeploymentGroups(groups));
378 // restore full type before comparing
379 ident.setVersion(version);
381 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
382 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
384 assertEquals(newgrp.toString(), group.toString());
386 assertNoGroupAction();
390 public void testUpdateSubGroup_PolicyVersionPrefixMismatch() throws Exception {
391 PdpGroups groups = loadPdpGroups("deployGroups.json");
392 PdpGroup newgrp = groups.getGroups().get(0);
394 PdpGroup group = new PdpGroup(newgrp);
395 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
397 // use incorrect version prefix
398 newgrp.getPdpSubgroups().get(0).getPolicies().get(0).setVersion("9");
400 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
401 .isInstanceOf(PfModelException.class)
402 .hasMessageContaining("different version already deployed");
404 assertNoGroupAction();
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));
414 prov.updateGroupPolicies(toDeploymentGroups(dbgroups));
416 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
417 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
419 assertEquals(newgrp.toString(), group.toString());
422 checkEmptyNotification();
425 assertNoGroupAction();
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));
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");
439 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
441 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(dbgroups)))
442 .isInstanceOf(PfModelException.class)
443 .hasMessageContaining("different version already deployed");
445 assertNoGroupAction();
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));
455 final DeploymentGroups groups = toDeploymentGroups(dbgroups);
457 PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
459 // DB has no policies
460 dbsubgrp.getPolicies().clear();
462 // DB has a different supported type
463 dbsubgrp.getSupportedPolicyTypes().get(0).setName("some-other-type");
465 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
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");
471 assertNoGroupAction();
475 public void testDeployPolicies() throws PfModelException {
476 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
480 * Tests deployPolicies() when the policies are invalid.
483 public void testDeployPoliciesInvalidPolicies() throws Exception {
485 PdpDeployPolicies policies0 = loadFile("PapPoliciesList.json", PdpDeployPolicies.class);
486 assertThatCode(() -> prov.deployPolicies(policies0)).doesNotThrowAnyException();
489 PdpDeployPolicies policies = new PdpDeployPolicies();
490 assertThatThrownBy(() -> prov.deployPolicies(policies)).isInstanceOf(PfModelException.class)
491 .hasMessageContaining("policies");
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");
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\"");
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");
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");
518 * Tests deployPolicies() when the supported policy type uses a wild-card.
520 * @throws Exception if an error occurs
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());
528 policy1.setName("policy.some");
529 policy1.setVersion(POLICY1_VERSION);
530 policy1.setType("some.type");
531 policy1.setTypeVersion("100.2.3");
533 PdpDeployPolicies depreq = loadRequest();
534 depreq.getPolicies().get(0).setName("policy.some");
536 prov.deployPolicies(depreq);
538 assertGroup(getGroupUpdates(), GROUP1_NAME);
540 List<PdpUpdate> requests = getUpdateRequests(1);
541 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
543 // nothing is complete - notification should be empty
544 checkEmptyNotification();
548 public void testDeploySimplePolicies() throws Exception {
549 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
553 public void testDeploySimplePolicies_DaoEx() throws Exception {
554 PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
555 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
557 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
561 public void testDeploySimplePolicies_DaoPfRtEx() throws Exception {
562 PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
563 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
565 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
569 public void testDeploySimplePolicies_RuntimeEx() throws Exception {
570 RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION);
571 when(dao.getFilteredPolicyList(any())).thenThrow(exc);
573 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc);
577 public void testDeploySimplePolicies_NoGroups() throws Exception {
578 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json"));
580 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class)
581 .hasMessage("policy not supported by any PDP group: policyA 1.2.3");
585 public void testMakeUpdater() throws Exception {
587 * Each subgroup has a different PDP type and name.
589 * Type is not supported by the first subgroup.
591 * Second subgroup matches.
593 * Third subgroup already contains the policy.
595 * Last subgroup matches.
598 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json"));
600 prov.deployPolicies(loadRequest());
602 assertGroup(getGroupUpdates(), GROUP1_NAME);
604 List<PdpUpdate> requests = getUpdateRequests(2);
605 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
606 assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4);
608 // nothing is complete - notification should be empty
609 checkEmptyNotification();
613 public void testMakeUpdater_PolicyVersionMismatch() throws Exception {
615 // subgroup has a different version of the Policy
616 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json"));
618 PdpDeployPolicies req = loadRequest();
619 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
620 .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed");
622 verify(dao, never()).createPdpGroups(any());
623 verify(dao, never()).updatePdpGroups(any());
624 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
628 public void testMakeUpdater_NoPdps() throws Exception {
630 // subgroup has no PDPs
631 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json"));
633 PdpDeployPolicies req = loadRequest();
634 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
635 .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs");
637 verify(dao, never()).createPdpGroups(any());
638 verify(dao, never()).updatePdpGroups(any());
639 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
643 protected void assertUpdate(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
645 PdpUpdate update = updates.remove(0);
647 assertEquals(groupName, update.getPdpGroup());
648 assertEquals(pdpType, update.getPdpSubgroup());
649 assertEquals(pdpName, update.getName());
650 assertThat(update.getPoliciesToBeDeployed()).contains(policy1);
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());
659 private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception {
660 verify(dao, never()).createPdpGroups(any());
662 assertEquals(0, getStateChangeRequests(1).size());
664 List<PdpUpdate> pdpUpdates = getUpdateRequests(1);
665 assertEquals(1, pdpUpdates.size());
667 PdpUpdate pdpUpdate = pdpUpdates.get(0);
668 assertEquals("pdpA", pdpUpdate.getName());
669 assertEquals(group.getName(), pdpUpdate.getPdpGroup());
671 assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup());
673 List<ToscaConceptIdentifier> pdpPolicies =
674 pdpUpdate.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
675 .collect(Collectors.toList());
676 Collections.sort(pdpPolicies);
678 assertThat(subgrp.getPolicies()).containsAll(pdpPolicies);
680 List<PdpGroup> updates = getGroupUpdates();
681 assertEquals(Arrays.asList(group), updates);
685 * Loads a standard request.
687 * @return a standard request
689 protected PdpDeployPolicies loadRequest() {
690 return loadRequest("request.json");
694 * Loads a request from a JSON file.
696 * @param fileName name of the file from which to load
697 * @return the request that was loaded
699 protected PdpDeployPolicies loadRequest(String fileName) {
700 return loadFile(fileName, PdpDeployPolicies.class);
704 * Loads an empty request.
706 * @return an empty request
708 protected PdpDeployPolicies loadEmptyRequest() {
709 return loadRequest("emptyRequest.json");
712 private DeploymentGroups toDeploymentGroups(PdpGroups dbgroups) {
713 DeploymentGroups groups = new DeploymentGroups();
715 groups.setGroups(dbgroups.getGroups().stream().map(this::toDeploymentGroup).collect(Collectors.toList()));
720 private DeploymentGroup toDeploymentGroup(PdpGroup dbgroup) {
721 DeploymentGroup group = new DeploymentGroup();
723 group.setName(dbgroup.getName());
724 group.setDeploymentSubgroups(dbgroup.getPdpSubgroups().stream().map(this::toDeploymentSubGroup)
725 .collect(Collectors.toList()));
730 private DeploymentSubGroup toDeploymentSubGroup(PdpSubGroup dbsubgrp) {
731 DeploymentSubGroup subgrp = new DeploymentSubGroup();
733 subgrp.setAction(Action.PATCH);
734 subgrp.setPdpType(dbsubgrp.getPdpType());
735 subgrp.setPolicies(new ArrayList<>(dbsubgrp.getPolicies()));