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.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
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;
57 public class TestPdpGroupDeployProvider extends ProviderSuper {
59 private static final String EXPECTED_EXCEPTION = "expected exception";
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";
73 private PdpGroupDeployProvider prov;
77 public static void tearDownAfterClass() {
78 Registry.newRegistry();
82 * Configures mocks and objects.
84 * @throws Exception if an error occurs
88 public void setUp() throws Exception {
92 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList2.json"));
93 when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
95 prov = new PdpGroupDeployProvider();
99 * Tests updateGroupPolicies when policies are being added.
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));
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));
113 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
114 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
115 .thenReturn(loadPolicies("daoPolicyList.json"));
118 DeploymentGroups depgroups = toDeploymentGroups(groups);
119 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.POST);
121 prov.updateGroupPolicies(depgroups);
123 assertEquals(newgrp.toString(), dbgroup.toString());
124 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
128 * Tests updateGroupPolicies when policies are being deleted.
131 public void testUpdateGroupPoliciesDelete() throws Exception {
132 PdpGroups groups = loadPdpGroups("deployGroups.json");
133 PdpGroup newgrp = groups.getGroups().get(0);
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));
140 PdpGroup dbgroup = new PdpGroup(newgrp);
141 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
143 // policy that should be left
144 final ToscaConceptIdentifier policyId1 = policies.remove(0);
146 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
147 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
148 .thenReturn(loadPolicies("daoPolicyList.json"));
150 DeploymentGroups depgroups = toDeploymentGroups(groups);
151 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.DELETE);
153 prov.updateGroupPolicies(depgroups);
155 // only the first policy should remain
157 policies.add(policyId1);
159 assertEquals(newgrp.toString(), dbgroup.toString());
160 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
164 * Tests updateGroupPolicies when policies are being added and deleted in the same
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);
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));
178 // now make the subgrp reflect our final expectation
179 subgrp.getPolicies().remove(1);
180 subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
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)));
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)));
193 DeploymentGroup depgroup = new DeploymentGroup();
194 depgroup.setName(newgrp.getName());
195 depgroup.setDeploymentSubgroups(Arrays.asList(depsub1, depsub2));
197 DeploymentGroups depgroups = new DeploymentGroups();
198 depgroups.setGroups(Arrays.asList(depgroup));
200 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
201 .thenReturn(loadPolicies("daoPolicyList.json"))
202 .thenReturn(loadPolicies("createGroupNewPolicy2.json"));
204 prov.updateGroupPolicies(depgroups);
206 assertEquals(newgrp.toString(), dbgroup.toString());
207 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
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));
217 // something different in this subgroup
218 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
220 prov.updateGroupPolicies(toDeploymentGroups(groups));
222 assertEquals(newgrp.toString(), group.toString());
223 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
227 public void testUpdateGroupPolicies_EmptyRequest() throws Exception {
228 prov.updateGroupPolicies(toDeploymentGroups(loadPdpGroups("emptyGroups.json")));
230 // no groups, so no action should have been taken
231 assertNoGroupAction();
235 public void testUpdateGroupPolicies_InvalidRequest() throws Exception {
236 assertThatThrownBy(() -> prov.updateGroupPolicies(new DeploymentGroups())).isInstanceOf(PfModelException.class)
237 .hasMessageContaining("is null");
239 assertNoGroupAction();
243 public void testUpdateGroup_UnknownGroup() throws Exception {
244 PdpGroups groups = loadPdpGroups("deployGroups.json");
246 String groupName = groups.getGroups().get(0).getName();
249 when(dao.getPdpGroups(groupName)).thenReturn(Collections.emptyList());
251 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
252 .isInstanceOf(PfModelException.class).hasMessageContaining(groupName)
253 .hasMessageContaining("unknown group");
255 assertNoGroupAction();
259 public void testUpdateGroup() throws Exception {
260 PdpGroups groups = loadPdpGroups("deployGroups.json");
262 // DB group = new group
263 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
264 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
266 prov.updateGroupPolicies(toDeploymentGroups(groups));
268 assertNoGroupAction();
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));
277 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
278 .isInstanceOf(PfModelException.class).hasMessageContaining("pdpTypeB")
279 .hasMessageContaining("unknown subgroup");
282 assertNoGroupAction();
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));
292 // something different in this subgroup
293 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
295 prov.updateGroupPolicies(toDeploymentGroups(groups));
297 assertEquals(newgrp.toString(), group.toString());
298 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
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);
307 // group has no policies yet
308 group.getPdpSubgroups().get(0).getPolicies().clear();
309 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
312 when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());
314 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
315 .isInstanceOf(PfModelException.class)
316 .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
317 .hasMessageContaining("unknown policy");
319 assertNoGroupAction();
323 public void testUpdateSubGroup_Policies() throws Exception {
324 PdpGroups groups = loadPdpGroups("deployGroups.json");
325 PdpGroup newgrp = groups.getGroups().get(0);
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);
333 PdpGroup group = new PdpGroup(newgrp);
334 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
336 // add two new policies
337 ToscaConceptIdentifier policyId2 = new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION);
338 subgrp.getPolicies().add(policyId2);
340 ToscaConceptIdentifier policyId3 = new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION);
341 subgrp.getPolicies().add(policyId3);
343 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
344 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
345 .thenReturn(loadPolicies("daoPolicyList.json"));
347 prov.updateGroupPolicies(toDeploymentGroups(groups));
349 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
350 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
352 assertEquals(newgrp.toString(), group.toString());
354 // nothing is complete - notification should be empty
355 checkEmptyNotification();
357 // this requires a PDP UPDATE message
358 assertGroupUpdate(newgrp, subgrp);
362 public void testUpdateSubGroup_PolicyVersionPrefix() throws Exception {
363 PdpGroups groups = loadPdpGroups("deployGroups.json");
364 PdpGroup newgrp = groups.getGroups().get(0);
366 PdpGroup group = new PdpGroup(newgrp);
367 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
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");
375 prov.updateGroupPolicies(toDeploymentGroups(groups));
377 // restore full type before comparing
378 ident.setVersion(version);
380 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
381 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
383 assertEquals(newgrp.toString(), group.toString());
385 assertNoGroupAction();
389 public void testUpdateSubGroup_PolicyVersionPrefixMismatch() throws Exception {
390 PdpGroups groups = loadPdpGroups("deployGroups.json");
391 PdpGroup newgrp = groups.getGroups().get(0);
393 PdpGroup group = new PdpGroup(newgrp);
394 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
396 // use incorrect version prefix
397 newgrp.getPdpSubgroups().get(0).getPolicies().get(0).setVersion("9");
399 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
400 .isInstanceOf(PfModelException.class)
401 .hasMessageContaining("different version already deployed");
403 assertNoGroupAction();
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));
413 prov.updateGroupPolicies(toDeploymentGroups(dbgroups));
415 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
416 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
418 assertEquals(newgrp.toString(), group.toString());
421 checkEmptyNotification();
424 assertNoGroupAction();
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));
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");
438 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
440 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(dbgroups)))
441 .isInstanceOf(PfModelException.class)
442 .hasMessageContaining("different version already deployed");
444 assertNoGroupAction();
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));
454 final DeploymentGroups groups = toDeploymentGroups(dbgroups);
456 PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
458 // DB has no policies
459 dbsubgrp.getPolicies().clear();
461 // DB has a different supported type
462 dbsubgrp.getSupportedPolicyTypes().get(0).setName("some-other-type");
464 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
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");
470 assertNoGroupAction();
474 public void testDeployPolicies() throws PfModelException {
475 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
479 * Tests deployPolicies() when the policies are invalid.
482 public void testDeployPoliciesInvalidPolicies() throws Exception {
484 PdpDeployPolicies policies0 = loadFile("PapPoliciesList.json", PdpDeployPolicies.class);
485 assertThatCode(() -> prov.deployPolicies(policies0)).doesNotThrowAnyException();
488 PdpDeployPolicies policies = new PdpDeployPolicies();
489 assertThatThrownBy(() -> prov.deployPolicies(policies)).isInstanceOf(PfModelException.class)
490 .hasMessageContaining("policies");
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");
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\"");
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");
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");
517 * Tests deployPolicies() when the supported policy type uses a wild-card.
519 * @throws Exception if an error occurs
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());
527 policy1.setName("policy.some");
528 policy1.setVersion(POLICY1_VERSION);
529 policy1.setType("some.type");
530 policy1.setTypeVersion("100.2.3");
532 PdpDeployPolicies depreq = loadRequest();
533 depreq.getPolicies().get(0).setName("policy.some");
535 prov.deployPolicies(depreq);
537 assertGroup(getGroupUpdates(), GROUP1_NAME);
539 List<PdpUpdate> requests = getUpdateRequests(1);
540 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
542 // nothing is complete - notification should be empty
543 checkEmptyNotification();
547 public void testDeploySimplePolicies() throws Exception {
548 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
552 public void testDeploySimplePolicies_DaoEx() throws Exception {
553 PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
554 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
556 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
560 public void testDeploySimplePolicies_DaoPfRtEx() throws Exception {
561 PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
562 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
564 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
568 public void testDeploySimplePolicies_RuntimeEx() throws Exception {
569 RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION);
570 when(dao.getFilteredPolicyList(any())).thenThrow(exc);
572 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc);
576 public void testDeploySimplePolicies_NoGroups() throws Exception {
577 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json"));
579 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class)
580 .hasMessage("policy not supported by any PDP group: policyA 1.2.3");
584 public void testMakeUpdater() throws Exception {
586 * Each subgroup has a different PDP type and name.
588 * Type is not supported by the first subgroup.
590 * Second subgroup matches.
592 * Third subgroup already contains the policy.
594 * Last subgroup matches.
597 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json"));
599 prov.deployPolicies(loadRequest());
601 assertGroup(getGroupUpdates(), GROUP1_NAME);
603 List<PdpUpdate> requests = getUpdateRequests(2);
604 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
605 assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4);
607 // nothing is complete - notification should be empty
608 checkEmptyNotification();
612 public void testMakeUpdater_PolicyVersionMismatch() throws Exception {
614 // subgroup has a different version of the Policy
615 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json"));
617 PdpDeployPolicies req = loadRequest();
618 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
619 .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed");
621 verify(dao, never()).createPdpGroups(any());
622 verify(dao, never()).updatePdpGroups(any());
623 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
627 public void testMakeUpdater_NoPdps() throws Exception {
629 // subgroup has no PDPs
630 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json"));
632 PdpDeployPolicies req = loadRequest();
633 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
634 .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs");
636 verify(dao, never()).createPdpGroups(any());
637 verify(dao, never()).updatePdpGroups(any());
638 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
642 protected void assertUpdate(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
644 PdpUpdate update = updates.remove(0);
646 assertEquals(groupName, update.getPdpGroup());
647 assertEquals(pdpType, update.getPdpSubgroup());
648 assertEquals(pdpName, update.getName());
649 assertThat(update.getPoliciesToBeDeployed()).contains(policy1);
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());
658 private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception {
659 verify(dao, never()).createPdpGroups(any());
661 assertEquals(0, getStateChangeRequests(1).size());
663 List<PdpUpdate> pdpUpdates = getUpdateRequests(1);
664 assertEquals(1, pdpUpdates.size());
666 PdpUpdate pdpUpdate = pdpUpdates.get(0);
667 assertEquals("pdpA", pdpUpdate.getName());
668 assertEquals(group.getName(), pdpUpdate.getPdpGroup());
670 assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup());
672 List<ToscaConceptIdentifier> pdpPolicies =
673 pdpUpdate.getPoliciesToBeDeployed().stream().map(ToscaPolicy::getIdentifier)
674 .collect(Collectors.toList());
675 Collections.sort(pdpPolicies);
677 assertThat(subgrp.getPolicies()).containsAll(pdpPolicies);
679 List<PdpGroup> updates = getGroupUpdates();
680 assertEquals(Arrays.asList(group), updates);
684 * Loads a standard request.
686 * @return a standard request
688 protected PdpDeployPolicies loadRequest() {
689 return loadRequest("request.json");
693 * Loads a request from a JSON file.
695 * @param fileName name of the file from which to load
696 * @return the request that was loaded
698 protected PdpDeployPolicies loadRequest(String fileName) {
699 return loadFile(fileName, PdpDeployPolicies.class);
703 * Loads an empty request.
705 * @return an empty request
707 protected PdpDeployPolicies loadEmptyRequest() {
708 return loadRequest("emptyRequest.json");
711 private DeploymentGroups toDeploymentGroups(PdpGroups dbgroups) {
712 DeploymentGroups groups = new DeploymentGroups();
714 groups.setGroups(dbgroups.getGroups().stream().map(this::toDeploymentGroup).collect(Collectors.toList()));
719 private DeploymentGroup toDeploymentGroup(PdpGroup dbgroup) {
720 DeploymentGroup group = new DeploymentGroup();
722 group.setName(dbgroup.getName());
723 group.setDeploymentSubgroups(dbgroup.getPdpSubgroups().stream().map(this::toDeploymentSubGroup)
724 .collect(Collectors.toList()));
729 private DeploymentSubGroup toDeploymentSubGroup(PdpSubGroup dbsubgrp) {
730 DeploymentSubGroup subgrp = new DeploymentSubGroup();
732 subgrp.setAction(Action.PATCH);
733 subgrp.setPdpType(dbsubgrp.getPdpType());
734 subgrp.setPolicies(new ArrayList<>(dbsubgrp.getPolicies()));