2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
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.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.TreeSet;
39 import java.util.stream.Collectors;
40 import javax.ws.rs.core.Response.Status;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.ArgumentCaptor;
45 import org.onap.policy.common.utils.services.Registry;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
49 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
50 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
51 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
52 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
53 import org.onap.policy.models.pdp.concepts.PdpGroup;
54 import org.onap.policy.models.pdp.concepts.PdpGroups;
55 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
56 import org.onap.policy.models.pdp.concepts.PdpUpdate;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
59 import org.onap.policy.pap.main.notification.PolicyPdpNotificationData;
61 public class TestPdpGroupDeployProvider extends ProviderSuper {
62 private static final String EXPECTED_EXCEPTION = "expected exception";
64 private static final String POLICY2_NAME = "policyB";
65 private static final String POLICY3_NAME = "policyC";
66 private static final String POLICY1_VERSION = "1.2.3";
67 private static final String POLICY2_VERSION = "1.2.3";
68 private static final String POLICY3_VERSION = "1.2.3";
69 private static final String GROUP1_NAME = "groupA";
70 private static final String PDP1_TYPE = "pdpTypeA";
71 private static final String PDP2_TYPE = "pdpTypeB";
72 private static final String PDP4_TYPE = "pdpTypeD";
73 private static final String PDP2 = "pdpB";
74 private static final String PDP4 = "pdpD";
76 private PdpGroupDeployProvider prov;
80 public static void tearDownAfterClass() {
81 Registry.newRegistry();
85 * Configures mocks and objects.
87 * @throws Exception if an error occurs
91 public void setUp() throws Exception {
95 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList2.json"));
96 when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json")));
98 prov = new PdpGroupDeployProvider();
102 * Tests updateGroupPolicies when policies are being added.
105 public void testUpdateGroupPoliciesAdd() throws Exception {
106 PdpGroups groups = loadPdpGroups("deployGroups.json");
107 PdpGroup newgrp = groups.getGroups().get(0);
108 PdpGroup dbgroup = new PdpGroup(newgrp);
109 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
112 List<ToscaConceptIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies();
113 policies.add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
114 policies.add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
116 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
117 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
118 .thenReturn(loadPolicies("daoPolicyList.json"));
121 DeploymentGroups depgroups = toDeploymentGroups(groups);
122 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.POST);
124 prov.updateGroupPolicies(depgroups);
126 assertEquals(newgrp.toString(), dbgroup.toString());
127 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
131 * Tests updateGroupPolicies when policies are being deleted.
134 public void testUpdateGroupPoliciesDelete() throws Exception {
135 PdpGroups groups = loadPdpGroups("deployGroups.json");
136 PdpGroup newgrp = groups.getGroups().get(0);
138 // additional policies in the DB that will be removed
139 List<ToscaConceptIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies();
140 policies.add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
141 policies.add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
143 PdpGroup dbgroup = new PdpGroup(newgrp);
144 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
146 // policy that should be left
147 final ToscaConceptIdentifier policyId1 = policies.remove(0);
149 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
150 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
151 .thenReturn(loadPolicies("daoPolicyList.json"));
153 DeploymentGroups depgroups = toDeploymentGroups(groups);
154 depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.DELETE);
156 prov.updateGroupPolicies(depgroups);
158 // only the first policy should remain
160 policies.add(policyId1);
162 assertEquals(newgrp.toString(), dbgroup.toString());
163 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
167 * Tests updateGroupPolicies when policies are being added and deleted in the same subgroup.
170 public void testUpdateGroupPoliciesAddAndDelete() throws Exception {
171 PdpGroups groups = loadPdpGroups("deployGroups.json");
172 PdpGroup newgrp = groups.getGroups().get(0);
173 PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
175 // put policy3 into db subgroup
176 subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION));
177 PdpGroup dbgroup = new PdpGroup(newgrp);
178 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
180 // now make the subgrp reflect our final expectation
181 subgrp.getPolicies().remove(1);
182 subgrp.getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
184 // indicate policy2 being added and policy3 being deleted
185 DeploymentSubGroup depsub1 = new DeploymentSubGroup();
186 depsub1.setAction(Action.POST);
187 depsub1.setPdpType(subgrp.getPdpType());
188 depsub1.setPolicies(Arrays.asList(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION)));
190 DeploymentSubGroup depsub2 = new DeploymentSubGroup();
191 depsub2.setAction(Action.DELETE);
192 depsub2.setPdpType(subgrp.getPdpType());
193 depsub2.setPolicies(Arrays.asList(new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION)));
195 DeploymentGroup depgroup = new DeploymentGroup();
196 depgroup.setName(newgrp.getName());
197 depgroup.setDeploymentSubgroups(Arrays.asList(depsub1, depsub2));
199 DeploymentGroups depgroups = new DeploymentGroups();
200 depgroups.setGroups(Arrays.asList(depgroup));
202 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
203 .thenReturn(loadPolicies("daoPolicyList.json"))
204 .thenReturn(loadPolicies("createGroupNewPolicy2.json"));
206 prov.updateGroupPolicies(depgroups);
208 assertEquals(newgrp.toString(), dbgroup.toString());
209 assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0));
213 public void testUpdateGroupPolicies() throws Exception {
214 PdpGroups groups = loadPdpGroups("deployGroups.json");
215 PdpGroup newgrp = groups.getGroups().get(0);
216 PdpGroup group = new PdpGroup(newgrp);
217 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
219 // something different in this subgroup
220 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
222 prov.updateGroupPolicies(toDeploymentGroups(groups));
224 assertEquals(newgrp.toString(), group.toString());
225 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
229 public void testUpdateGroupPolicies_EmptyRequest() throws Exception {
230 prov.updateGroupPolicies(toDeploymentGroups(loadPdpGroups("emptyGroups.json")));
232 // no groups, so no action should have been taken
233 assertNoGroupAction();
237 public void testUpdateGroupPolicies_InvalidRequest() throws Exception {
238 assertThatThrownBy(() -> prov.updateGroupPolicies(new DeploymentGroups())).isInstanceOf(PfModelException.class)
239 .hasMessageContaining("is null");
241 assertNoGroupAction();
245 public void testUpdateGroup_UnknownGroup() throws Exception {
246 PdpGroups groups = loadPdpGroups("deployGroups.json");
248 String groupName = groups.getGroups().get(0).getName();
251 when(dao.getPdpGroups(groupName)).thenReturn(Collections.emptyList());
253 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
254 .isInstanceOf(PfModelException.class).hasMessageContaining(groupName)
255 .hasMessageContaining("unknown group");
257 assertNoGroupAction();
261 public void testUpdateGroup() throws Exception {
262 PdpGroups groups = loadPdpGroups("deployGroups.json");
264 // DB group = new group
265 PdpGroup group = new PdpGroup(groups.getGroups().get(0));
266 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
268 prov.updateGroupPolicies(toDeploymentGroups(groups));
270 assertNoGroupAction();
274 public void testUpdateGroup_NewSubGroup() throws Exception {
275 PdpGroups groups = loadPdpGroups("createGroupsNewSub.json");
276 PdpGroup group = loadPdpGroups("deployGroups.json").getGroups().get(0);
277 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
279 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
280 .isInstanceOf(PfModelException.class).hasMessageContaining("pdpTypeB")
281 .hasMessageContaining("unknown subgroup");
284 assertNoGroupAction();
288 public void testUpdateGroup_UpdatedSubGroup() throws Exception {
289 PdpGroups groups = loadPdpGroups("deployGroups.json");
290 PdpGroup newgrp = groups.getGroups().get(0);
291 PdpGroup group = new PdpGroup(newgrp);
292 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
294 // something different in this subgroup
295 group.getPdpSubgroups().get(0).getPolicies().add(new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION));
297 prov.updateGroupPolicies(toDeploymentGroups(groups));
299 assertEquals(newgrp.toString(), group.toString());
300 assertGroupUpdate(group, group.getPdpSubgroups().get(0));
304 public void testUpdateSubGroup_Invalid() throws Exception {
305 PdpGroups groups = loadPdpGroups("deployGroups.json");
306 PdpGroup newgrp = groups.getGroups().get(0);
307 PdpGroup group = new PdpGroup(newgrp);
309 // group has no policies yet
310 group.getPdpSubgroups().get(0).getPolicies().clear();
311 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
314 when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList());
316 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
317 .isInstanceOf(PfModelException.class)
318 .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
319 .hasMessageContaining("unknown policy");
321 assertNoGroupAction();
325 public void testUpdateSubGroup_Policies() throws Exception {
326 PdpGroups groups = loadPdpGroups("deployGroups.json");
327 PdpGroup newgrp = groups.getGroups().get(0);
329 // add a second subgroup, which will be left unchanged
330 PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
331 PdpSubGroup subgrp2 = new PdpSubGroup(subgrp);
332 subgrp2.setPdpType(PDP2_TYPE);
333 newgrp.getPdpSubgroups().add(subgrp2);
335 PdpGroup group = new PdpGroup(newgrp);
336 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
338 // add two new policies
339 ToscaConceptIdentifier policyId2 = new ToscaConceptIdentifier(POLICY2_NAME, POLICY2_VERSION);
340 subgrp.getPolicies().add(policyId2);
342 ToscaConceptIdentifier policyId3 = new ToscaConceptIdentifier(POLICY3_NAME, POLICY3_VERSION);
343 subgrp.getPolicies().add(policyId3);
345 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json"))
346 .thenReturn(loadPolicies("createGroupNewPolicy2.json"))
347 .thenReturn(loadPolicies("daoPolicyList.json"));
349 prov.updateGroupPolicies(toDeploymentGroups(groups));
351 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
352 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
354 assertEquals(newgrp.toString(), group.toString());
356 // should have notified of added policy/PDPs
357 ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
358 verify(notifier, times(2)).addDeploymentData(captor.capture());
359 assertDeploymentData(captor.getAllValues().get(0), policyId2, "[pdpA]");
360 assertDeploymentData(captor.getAllValues().get(1), policyId3, "[pdpA]");
362 // should NOT have notified of any deleted policy/PDPs
363 verify(notifier, never()).addUndeploymentData(any());
365 // this requires a PDP UPDATE message
366 assertGroupUpdate(newgrp, subgrp);
370 public void testUpdateSubGroup_PolicyVersionPrefix() throws Exception {
371 PdpGroups groups = loadPdpGroups("deployGroups.json");
372 PdpGroup newgrp = groups.getGroups().get(0);
374 PdpGroup group = new PdpGroup(newgrp);
375 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
377 // use version prefix
378 PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0);
379 ToscaConceptIdentifier ident = subgrp.getPolicies().get(0);
380 String version = ident.getVersion();
381 ident.setVersion("1");
383 prov.updateGroupPolicies(toDeploymentGroups(groups));
385 // restore full type before comparing
386 ident.setVersion(version);
388 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
389 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
391 assertEquals(newgrp.toString(), group.toString());
393 assertNoGroupAction();
397 public void testUpdateSubGroup_PolicyVersionPrefixMismatch() throws Exception {
398 PdpGroups groups = loadPdpGroups("deployGroups.json");
399 PdpGroup newgrp = groups.getGroups().get(0);
401 PdpGroup group = new PdpGroup(newgrp);
402 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
404 // use incorrect version prefix
405 newgrp.getPdpSubgroups().get(0).getPolicies().get(0).setVersion("9");
407 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups)))
408 .isInstanceOf(PfModelException.class)
409 .hasMessageContaining("different version already deployed");
411 assertNoGroupAction();
415 public void testUpdateSubGroup_Unchanged() throws Exception {
416 PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
417 PdpGroup newgrp = dbgroups.getGroups().get(0);
418 PdpGroup group = new PdpGroup(newgrp);
419 when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group));
421 prov.updateGroupPolicies(toDeploymentGroups(dbgroups));
423 Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies());
424 Collections.sort(group.getPdpSubgroups().get(0).getPolicies());
426 assertEquals(newgrp.toString(), group.toString());
429 verify(notifier, never()).addDeploymentData(any());
430 verify(notifier, never()).addUndeploymentData(any());
433 assertNoGroupAction();
437 public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception {
438 PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
439 PdpGroup newgrp = dbgroups.getGroups().get(0);
440 PdpGroup dbgroup = new PdpGroup(newgrp);
441 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
443 // arrange for DB policy version to be different
444 PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
445 dbsubgrp.getPolicies().get(0).setVersion("9.9.9");
447 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
449 assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(dbgroups)))
450 .isInstanceOf(PfModelException.class)
451 .hasMessageContaining("different version already deployed");
453 assertNoGroupAction();
457 public void testUpdateSubGroup_UnsupportedType() throws Exception {
458 PdpGroups dbgroups = loadPdpGroups("deployGroups.json");
459 PdpGroup newgrp = dbgroups.getGroups().get(0);
460 PdpGroup dbgroup = new PdpGroup(newgrp);
461 when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup));
463 final DeploymentGroups groups = toDeploymentGroups(dbgroups);
465 PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0);
467 // DB has no policies
468 dbsubgrp.getPolicies().clear();
470 // DB has a different supported type
471 dbsubgrp.getSupportedPolicyTypes().get(0).setName("some-other-type");
473 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json"));
475 assertThatThrownBy(() -> prov.updateGroupPolicies(groups))
476 .isInstanceOf(PfModelException.class)
477 .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName())
478 .hasMessageContaining("not a supported policy for the subgroup");
480 assertNoGroupAction();
484 public void testDeployPolicies() throws PfModelException {
485 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
489 * Tests deployPolicies() when the supported policy type uses a wild-card.
491 * @throws Exception if an error occurs
494 public void testDeployPoliciesWildCard() throws Exception {
495 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("deployPoliciesWildCard.json"));
496 when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json"));
497 when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList());
499 policy1.setName("policy.some");
500 policy1.setVersion(POLICY1_VERSION);
501 policy1.setType("some.type");
502 policy1.setTypeVersion("100.2.3");
504 PdpDeployPolicies depreq = loadRequest();
505 depreq.getPolicies().get(0).setName("policy.some");
507 prov.deployPolicies(depreq);
509 assertGroup(getGroupUpdates(), GROUP1_NAME);
511 List<PdpUpdate> requests = getUpdateRequests(1);
512 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
514 // should have notified of added policy/PDPs
515 ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
516 verify(notifier).addDeploymentData(captor.capture());
517 assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB]");
519 // no undeployment notifications
520 verify(notifier, never()).addUndeploymentData(any());
524 public void testDeploySimplePolicies() throws Exception {
525 assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException();
529 public void testDeploySimplePolicies_DaoEx() throws Exception {
530 PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
531 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
533 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
537 public void testDeploySimplePolicies_DaoPfRtEx() throws Exception {
538 PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
539 when(dao.getFilteredPdpGroups(any())).thenThrow(exc);
541 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc);
545 public void testDeploySimplePolicies_RuntimeEx() throws Exception {
546 RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION);
547 when(dao.getFilteredPolicyList(any())).thenThrow(exc);
549 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc);
553 public void testDeploySimplePolicies_NoGroups() throws Exception {
554 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json"));
556 assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class)
557 .hasMessage("policy not supported by any PDP group: policyA 1.2.3");
561 public void testMakeUpdater() throws Exception {
563 * Each subgroup has a different PDP type and name.
565 * Type is not supported by the first subgroup.
567 * Second subgroup matches.
569 * Third subgroup already contains the policy.
571 * Last subgroup matches.
574 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json"));
576 prov.deployPolicies(loadRequest());
578 assertGroup(getGroupUpdates(), GROUP1_NAME);
580 List<PdpUpdate> requests = getUpdateRequests(2);
581 assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2);
582 assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4);
584 // should have notified of added policy/PDPs
585 ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
586 verify(notifier).addDeploymentData(captor.capture());
587 assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB, pdpD]");
589 // no undeployment notifications
590 verify(notifier, never()).addUndeploymentData(any());
594 public void testMakeUpdater_PolicyVersionMismatch() throws Exception {
596 // subgroup has a different version of the Policy
597 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json"));
599 PdpDeployPolicies req = loadRequest();
600 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
601 .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed");
603 verify(dao, never()).createPdpGroups(any());
604 verify(dao, never()).updatePdpGroups(any());
605 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
609 public void testMakeUpdater_NoPdps() throws Exception {
611 // subgroup has no PDPs
612 when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json"));
614 PdpDeployPolicies req = loadRequest();
615 assertThatThrownBy(() -> prov.deployPolicies(req)).isInstanceOf(PfModelRuntimeException.class)
616 .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs");
618 verify(dao, never()).createPdpGroups(any());
619 verify(dao, never()).updatePdpGroups(any());
620 verify(reqmap, never()).addRequest(any(PdpUpdate.class));
624 protected void assertUpdate(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
626 PdpUpdate update = updates.remove(0);
628 assertEquals(groupName, update.getPdpGroup());
629 assertEquals(pdpType, update.getPdpSubgroup());
630 assertEquals(pdpName, update.getName());
631 assertTrue(update.getPolicies().contains(policy1));
634 private void assertNoGroupAction() throws Exception {
635 verify(dao, never()).createPdpGroups(any());
636 verify(dao, never()).updatePdpGroups(any());
637 verify(reqmap, never()).addRequest(any(), any());
640 private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception {
641 verify(dao, never()).createPdpGroups(any());
643 assertEquals(0, getStateChangeRequests(1).size());
645 List<PdpUpdate> pdpUpdates = getUpdateRequests(1);
646 assertEquals(1, pdpUpdates.size());
648 PdpUpdate pdpUpdate = pdpUpdates.get(0);
649 assertEquals("pdpA", pdpUpdate.getName());
650 assertEquals(group.getName(), pdpUpdate.getPdpGroup());
652 assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup());
654 List<ToscaConceptIdentifier> pdpPolicies =
655 pdpUpdate.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
656 Collections.sort(pdpPolicies);
658 assertEquals(subgrp.getPolicies().toString(), pdpPolicies.toString());
660 List<PdpGroup> updates = getGroupUpdates();
661 assertEquals(Arrays.asList(group), updates);
664 private void assertDeploymentData(PolicyPdpNotificationData data, ToscaConceptIdentifier policyId,
665 String expectedPdps) {
666 assertEquals(policyId, data.getPolicyId());
667 assertEquals(policy1.getTypeIdentifier(), data.getPolicyType());
668 assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString());
672 * Loads a standard request.
674 * @return a standard request
676 protected PdpDeployPolicies loadRequest() {
677 return loadRequest("request.json");
681 * Loads a request from a JSON file.
683 * @param fileName name of the file from which to load
684 * @return the request that was loaded
686 protected PdpDeployPolicies loadRequest(String fileName) {
687 return loadFile(fileName, PdpDeployPolicies.class);
691 * Loads an empty request.
693 * @return an empty request
695 protected PdpDeployPolicies loadEmptyRequest() {
696 return loadRequest("emptyRequest.json");
699 private DeploymentGroups toDeploymentGroups(PdpGroups dbgroups) {
700 DeploymentGroups groups = new DeploymentGroups();
702 groups.setGroups(dbgroups.getGroups().stream().map(this::toDeploymentGroup).collect(Collectors.toList()));
707 private DeploymentGroup toDeploymentGroup(PdpGroup dbgroup) {
708 DeploymentGroup group = new DeploymentGroup();
710 group.setName(dbgroup.getName());
711 group.setDeploymentSubgroups(dbgroup.getPdpSubgroups().stream().map(this::toDeploymentSubGroup)
712 .collect(Collectors.toList()));
717 private DeploymentSubGroup toDeploymentSubGroup(PdpSubGroup dbsubgrp) {
718 DeploymentSubGroup subgrp = new DeploymentSubGroup();
720 subgrp.setAction(Action.PATCH);
721 subgrp.setPdpType(dbsubgrp.getPdpType());
722 subgrp.setPolicies(new ArrayList<>(dbsubgrp.getPolicies()));