diff options
Diffstat (limited to 'main/src/test/java/org/onap')
3 files changed, 322 insertions, 352 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java index a26213d7..739a5f38 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java @@ -32,8 +32,9 @@ import javax.ws.rs.core.Response; import org.junit.Test; import org.onap.policy.models.pap.concepts.PdpDeployPolicies; import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroups; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; /** @@ -41,7 +42,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOp */ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { - private static final String DEPLOY_GROUP_ENDPOINT = "pdps"; + private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch"; private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies"; @Test @@ -51,8 +52,8 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { } @Test - public void testDeployGroup() throws Exception { - Entity<PdpGroup> entgrp = makePdpGroupEntity(); + public void testUpdateGroupPolicies() throws Exception { + Entity<DeploymentGroups> entgrp = makeDeploymentGroupsEntity(); Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); Response rawresp = invocationBuilder.post(entgrp); @@ -88,16 +89,18 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp)); } - private Entity<PdpGroup> makePdpGroupEntity() { - PdpSubGroup subgrp = new PdpSubGroup(); + private Entity<DeploymentGroups> makeDeploymentGroupsEntity() { + DeploymentSubGroup subgrp = new DeploymentSubGroup(); subgrp.setPdpType("drools"); - PdpGroup group = new PdpGroup(); + DeploymentGroup group = new DeploymentGroup(); group.setName("drools-group"); - group.setDescription("my description"); - group.setPdpSubgroups(Arrays.asList(subgrp)); + group.setDeploymentSubgroups(Arrays.asList(subgrp)); - return Entity.entity(group, MediaType.APPLICATION_JSON); + DeploymentGroups groups = new DeploymentGroups(); + groups.setGroups(Arrays.asList(group)); + + return Entity.entity(groups, MediaType.APPLICATION_JSON); } private Entity<PdpDeployPolicies> makePdpPoliciesEntity() { diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java index a7fc11a8..daa0504a 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java @@ -20,12 +20,13 @@ package org.onap.policy.pap.main.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -33,7 +34,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.TreeMap; import java.util.TreeSet; import java.util.stream.Collectors; import javax.ws.rs.core.Response.Status; @@ -45,21 +45,26 @@ import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.pap.concepts.PdpDeployPolicies; +import org.onap.policy.models.pdp.concepts.DeploymentGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroups; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; public class TestPdpGroupDeployProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; private static final String POLICY2_NAME = "policyB"; + private static final String POLICY3_NAME = "policyC"; private static final String POLICY1_VERSION = "1.2.3"; + private static final String POLICY2_VERSION = "1.2.3"; + private static final String POLICY3_VERSION = "1.2.3"; private static final String GROUP1_NAME = "groupA"; private static final String PDP1_TYPE = "pdpTypeA"; private static final String PDP2_TYPE = "pdpTypeB"; @@ -80,479 +85,402 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { * * @throws Exception if an error occurs */ - @Override @Before public void setUp() throws Exception { super.setUp(); - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList2.json")); when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json"))); prov = new PdpGroupDeployProvider(); } + /** + * Tests updateGroupPolicies when policies are being added. + */ @Test - public void testCreateOrUpdateGroups() throws Exception { - prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json")); + public void testUpdateGroupPoliciesAdd() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - // no groups, so no action should have been taken - assertNoGroupAction(); - } + // add new policies + List<ToscaPolicyIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies(); + policies.add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION)); + policies.add(new ToscaPolicyIdentifier(POLICY3_NAME, POLICY3_VERSION)); - @Test - public void testCreateOrUpdateGroups_InvalidRequest() throws Exception { - assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class) - .hasMessageContaining("is null"); - - assertNoGroupAction(); - } + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("createGroupNewPolicy2.json")) + .thenReturn(loadPolicies("daoPolicyList.json")); - @Test - public void testCreateOrUpdate_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); + // add = POST + DeploymentGroups depgroups = toDeploymentGroups(groups); + depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.POST); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); + prov.updateGroupPolicies(depgroups); - assertNoGroupAction(); + assertEquals(newgrp.toString(), dbgroup.toString()); + assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0)); } + /** + * Tests updateGroupPolicies when policies are being deleted. + */ @Test - public void testAddGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup group = groups.getGroups().get(0); - group.setPdpGroupState(PdpState.PASSIVE); + public void testUpdateGroupPoliciesDelete() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); - prov.createOrUpdateGroups(groups); + // additional policies in the DB that will be removed + List<ToscaPolicyIdentifier> policies = newgrp.getPdpSubgroups().get(0).getPolicies(); + policies.add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION)); + policies.add(new ToscaPolicyIdentifier(POLICY3_NAME, POLICY3_VERSION)); - // should not have updated the state - assertEquals(PdpState.PASSIVE, group.getPdpGroupState()); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - assertSame(group, getGroupCreates().get(0)); - } + // policy that should be left + final ToscaPolicyIdentifier policyId1 = policies.remove(0); - @Test - public void testAddGroup_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("createGroupNewPolicy2.json")) + .thenReturn(loadPolicies("daoPolicyList.json")); - assertNoGroupAction(); - } + DeploymentGroups depgroups = toDeploymentGroups(groups); + depgroups.getGroups().get(0).getDeploymentSubgroups().get(0).setAction(Action.DELETE); - @Test - public void testValidateGroupOnly_NullState() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(null); - prov.createOrUpdateGroups(groups); - } + prov.updateGroupPolicies(depgroups); - @Test - public void testValidateGroupOnly_Active() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE); - prov.createOrUpdateGroups(groups); - } + // only the first policy should remain + policies.clear(); + policies.add(policyId1); - @Test - public void testValidateGroupOnly_Passive() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.PASSIVE); - prov.createOrUpdateGroups(groups); + assertEquals(newgrp.toString(), dbgroup.toString()); + assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0)); } + /** + * Tests updateGroupPolicies when policies are being added and deleted in the same subgroup. + */ @Test - public void testValidateGroupOnly_Invalid() { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); - } + public void testUpdateGroupPoliciesAddAndDelete() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - @Test - public void testUpdateGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + // put policy3 into db subgroup + subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY3_NAME, POLICY3_VERSION)); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - // DB group = new group - PdpGroup group = new PdpGroup(groups.getGroups().get(0)); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + // now make the subgrp reflect our final expectation + subgrp.getPolicies().remove(1); + subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION)); - prov.createOrUpdateGroups(groups); + // indicate policy2 being added and policy3 being deleted + DeploymentSubGroup depsub1 = new DeploymentSubGroup(); + depsub1.setAction(Action.POST); + depsub1.setPdpType(subgrp.getPdpType()); + depsub1.setPolicies(Arrays.asList(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION))); - assertNoGroupAction(); - } + DeploymentSubGroup depsub2 = new DeploymentSubGroup(); + depsub2.setAction(Action.DELETE); + depsub2.setPdpType(subgrp.getPdpType()); + depsub2.setPolicies(Arrays.asList(new ToscaPolicyIdentifier(POLICY3_NAME, POLICY3_VERSION))); - @Test - public void testUpdateGroup_PropertiesChanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + DeploymentGroup depgroup = new DeploymentGroup(); + depgroup.setName(newgrp.getName()); + depgroup.setDeploymentSubgroups(Arrays.asList(depsub1, depsub2)); - PdpGroup group = new PdpGroup(groups.getGroups().get(0)); - group.setProperties(new TreeMap<>()); + DeploymentGroups depgroups = new DeploymentGroups(); + depgroups.setGroups(Arrays.asList(depgroup)); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("createGroupNewPolicy2.json")); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); + prov.updateGroupPolicies(depgroups); - assertNoGroupAction(); + assertEquals(newgrp.toString(), dbgroup.toString()); + assertGroupUpdate(dbgroup, dbgroup.getPdpSubgroups().get(0)); } @Test - public void testUpdateGroup_NewDescription() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + public void testUpdateGroupPolicies() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); PdpGroup group = new PdpGroup(newgrp); - group.setDescription("old description"); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - prov.createOrUpdateGroups(groups); + // something different in this subgroup + group.getPdpSubgroups().get(0).getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION)); - assertGroupUpdateOnly(group); + prov.updateGroupPolicies(toDeploymentGroups(groups)); - assertEquals(group.getDescription(), "my description"); assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdate(group, group.getPdpSubgroups().get(0)); } @Test - public void testUpdateGroup_NewState() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - group.setPdpGroupState(PdpState.TEST); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - assertGroupUpdateOnly(group); + public void testUpdateGroupPolicies_EmptyRequest() throws Exception { + prov.updateGroupPolicies(toDeploymentGroups(loadPdpGroups("emptyGroups.json"))); - assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); - assertEquals(newgrp.toString(), group.toString()); + // no groups, so no action should have been taken + assertNoGroupAction(); } @Test - public void testUpdateGroup_UpdatedSubGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - // something different in this subgroup - group.getPdpSubgroups().get(0).setDesiredInstanceCount(10); - - prov.createOrUpdateGroups(groups); + public void testUpdateGroupPolicies_InvalidRequest() throws Exception { + assertThatThrownBy(() -> prov.updateGroupPolicies(new DeploymentGroups())).isInstanceOf(PfModelException.class) + .hasMessageContaining("is null"); - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); + assertNoGroupAction(); } @Test - public void testUpdateGroup_MultipleChanges() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - subgrp.setDesiredInstanceCount(30); - subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); - subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); + public void testUpdateGroup_UnknownGroup() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); - when(dao.getFilteredPolicyList(any())) - .thenReturn(loadPolicies("createGroupNewPolicy.json")) - .thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("createGroupNewPolicy.json")); + String groupName = groups.getGroups().get(0).getName(); - prov.createOrUpdateGroups(groups); + // group not found + when(dao.getPdpGroups(groupName)).thenReturn(Collections.emptyList()); - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups))) + .isInstanceOf(PfModelException.class).hasMessageContaining(groupName) + .hasMessageContaining("unknown group"); - assertEquals(newgrp.toString(), group.toString()); - - // this requires a PDP UPDATE message - assertGroupUpdate(group, subgrp); + assertNoGroupAction(); } @Test - public void testUpdateField_Unchanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); + public void testUpdateGroup() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); + + // DB group = new group + PdpGroup group = new PdpGroup(groups.getGroups().get(0)); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - prov.createOrUpdateGroups(groups); + prov.updateGroupPolicies(toDeploymentGroups(groups)); assertNoGroupAction(); } @Test - public void testUpdateField_WasNull() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); + public void testUpdateGroup_NewSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("deployGroups.json").getGroups().get(0); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - group.setDescription(null); + assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups))) + .isInstanceOf(PfModelException.class).hasMessageContaining("pdpTypeB") + .hasMessageContaining("unknown subgroup"); - prov.createOrUpdateGroups(groups); - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); + assertNoGroupAction(); } @Test - public void testUpdateField_NowNull() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + public void testUpdateGroup_UpdatedSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - newgrp.setDescription(null); + // something different in this subgroup + group.getPdpSubgroups().get(0).getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION)); - prov.createOrUpdateGroups(groups); + prov.updateGroupPolicies(toDeploymentGroups(groups)); assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); + assertGroupUpdate(group, group.getPdpSubgroups().get(0)); } @Test - public void testUpdateField_Changed() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + public void testUpdateSubGroup_Invalid() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.setDescription(group.getDescription() + "-changed"); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - /** - * Tests addSubgroup() when the new subgroup has a wild-card policy type. - * - * @throws Exception if an error occurs - */ - @Test - public void testAddSubGroupWildCardPolicyType() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json")); - when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); - - PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + // group has no policies yet + group.getPdpSubgroups().get(0).getPolicies().clear(); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - prov.createOrUpdateGroups(groups); - - PdpGroup newgrp = groups.getGroups().get(0); + // unknown policy + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1); - newsub.setCurrentInstanceCount(0); - newsub.setPdpInstances(new ArrayList<>(0)); + assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups))) + .isInstanceOf(PfModelException.class) + .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName()) + .hasMessageContaining("unknown policy"); - assertEquals(newgrp.toString(), group.toString()); + assertNoGroupAction(); } - /** - * Tests addSubgroup() when the new subgroup has a wild-card policy type, but the - * policy doesn't have a matching type. - * - * @throws PfModelException if an error occurs - */ @Test - public void testAddSubGroupWildCardPolicyTypeUnmatched() throws PfModelException { - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCardUnmatched.json")); - when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); - - PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + public void testUpdateSubGroup_Policies() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class); - } + // add a second subgroup, which will be left unchanged + PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); + PdpSubGroup subgrp2 = new PdpSubGroup(subgrp); + subgrp2.setPdpType(PDP2_TYPE); + newgrp.getPdpSubgroups().add(subgrp2); - @Test - public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type"); - } - - @Test - public void testAddSubGroup_ValidationPolicyTypeDaoEx() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + // add two new policies + ToscaPolicyIdentifier policyId2 = new ToscaPolicyIdentifier(POLICY2_NAME, POLICY2_VERSION); + subgrp.getPolicies().add(policyId2); - PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); - when(dao.getPolicyTypeList(any(), any())).thenThrow(exc); + ToscaPolicyIdentifier policyId3 = new ToscaPolicyIdentifier(POLICY3_NAME, POLICY3_VERSION); + subgrp.getPolicies().add(policyId3); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); - } + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("createGroupNewPolicy2.json")) + .thenReturn(loadPolicies("daoPolicyList.json")); - @Test - public void testAddSubGroup_ValidationPolicyNotFound() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + prov.updateGroupPolicies(toDeploymentGroups(groups)); - when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy"); - } + assertEquals(newgrp.toString(), group.toString()); - @Test - public void testAddSubGroup_ValidationPolicyDaoEx() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + // should have notified of added policy/PDPs + ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier, times(2)).addDeploymentData(captor.capture()); + assertDeploymentData(captor.getAllValues().get(0), policyId2, "[pdpA]"); + assertDeploymentData(captor.getAllValues().get(1), policyId3, "[pdpA]"); - PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(exc); + // should NOT have notified of any deleted policy/PDPs + verify(notifier, never()).addUndeploymentData(any()); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); + // this requires a PDP UPDATE message + assertGroupUpdate(newgrp, subgrp); } @Test - public void testUpdateSubGroup_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + public void testUpdateSubGroup_PolicyVersionPrefix() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - // change properties - newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); - - assertNoGroupAction(); - } + // use version prefix + PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); + ToscaPolicyIdentifier ident = subgrp.getPolicies().get(0); + String version = ident.getVersion(); + ident.setVersion("1"); - @Test - public void testUpdateSubGroup_SupportedPolicies() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + prov.updateGroupPolicies(toDeploymentGroups(groups)); - newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes() - .add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); + // restore full type before comparing + ident.setVersion(version); - prov.createOrUpdateGroups(groups); + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); + + assertNoGroupAction(); } @Test - public void testUpdateSubGroup_DesiredCount() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + public void testUpdateSubGroup_PolicyVersionPrefixMismatch() throws Exception { + PdpGroups groups = loadPdpGroups("deployGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20); + // use incorrect version prefix + newgrp.getPdpSubgroups().get(0).getPolicies().get(0).setVersion("9"); - prov.createOrUpdateGroups(groups); + assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(groups))) + .isInstanceOf(PfModelException.class) + .hasMessageContaining("different version already deployed"); - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); + assertNoGroupAction(); } @Test - public void testUpdateSubGroup_Policies() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsDelPolicy.json"); - PdpGroup newgrp = groups.getGroups().get(0); + public void testUpdateSubGroup_Unchanged() throws Exception { + PdpGroups dbgroups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = dbgroups.getGroups().get(0); PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - - // delete second policy - subgrp.setPolicies(subgrp.getPolicies().subList(0, 1)); - - // add new policy - ToscaPolicyIdentifier policyId2 = new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION); - subgrp.getPolicies().add(policyId2); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) - .thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("daoPolicyListDelPolicy.json")) - .thenReturn(loadPolicies("createGroupNewPolicy.json")); - - prov.createOrUpdateGroups(groups); + prov.updateGroupPolicies(toDeploymentGroups(dbgroups)); Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); assertEquals(newgrp.toString(), group.toString()); - // should have notified of added policy/PDPs - ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor, policyId2, "[pdpA]"); - - // should have notified of deleted policy/PDPs - captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addUndeploymentData(captor.capture()); - assertDeploymentData(captor, new ToscaPolicyIdentifier("ToBeDeleted", POLICY1_VERSION), "[pdpA]"); + // no notifications + verify(notifier, never()).addDeploymentData(any()); + verify(notifier, never()).addUndeploymentData(any()); - // this requires a PDP UPDATE message - assertGroupUpdate(group, subgrp); + // no group updates + assertNoGroupAction(); } @Test - public void testUpdateSubGroup_Unchanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); + public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception { + PdpGroups dbgroups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = dbgroups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + // arrange for DB policy version to be different + PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0); + dbsubgrp.getPolicies().get(0).setVersion("9.9.9"); - assertEquals(newgrp.toString(), group.toString()); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - // no notifications - verify(notifier, never()).addDeploymentData(any()); - verify(notifier, never()).addUndeploymentData(any()); + assertThatThrownBy(() -> prov.updateGroupPolicies(toDeploymentGroups(dbgroups))) + .isInstanceOf(PfModelException.class) + .hasMessageContaining("different version already deployed"); - // no group updates assertNoGroupAction(); } @Test - public void testValidateSubGroup_PropertiesMismatch() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + public void testUpdateSubGroup_UnsupportedType() throws Exception { + PdpGroups dbgroups = loadPdpGroups("deployGroups.json"); + PdpGroup newgrp = dbgroups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); + + final DeploymentGroups groups = toDeploymentGroups(dbgroups); + + PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0); + + // DB has no policies + dbsubgrp.getPolicies().clear(); - newgrp.setProperties(new TreeMap<>()); + // DB has a different supported type + dbsubgrp.getSupportedPolicyTypes().get(0).setName("some-other-type"); - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + + assertThatThrownBy(() -> prov.updateGroupPolicies(groups)) + .isInstanceOf(PfModelException.class) + .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName()) + .hasMessageContaining("not a supported policy for the subgroup"); assertNoGroupAction(); } @Test public void testDeployPolicies() throws PfModelException { - prov.deployPolicies(loadEmptyRequest()); + assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException(); } /** @@ -584,7 +512,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { // should have notified of added policy/PDPs ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB]"); + assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB]"); // no undeployment notifications verify(notifier, never()).addUndeploymentData(any()); @@ -592,7 +520,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { @Test public void testDeploySimplePolicies() throws Exception { - prov.deployPolicies(loadEmptyRequest()); + assertThatCode(() -> prov.deployPolicies(loadEmptyRequest())).doesNotThrowAnyException(); } @Test @@ -654,7 +582,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { // should have notified of added policy/PDPs ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); + assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB, pdpD]"); // no undeployment notifications verify(notifier, never()).addUndeploymentData(any()); @@ -729,17 +657,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(Arrays.asList(group), updates); } - private void assertGroupUpdateOnly(PdpGroup group) throws Exception { - verify(dao, never()).createPdpGroups(any()); - verify(reqmap, never()).addRequest(any(), any()); - - List<PdpGroup> updates = getGroupUpdates(); - assertEquals(Arrays.asList(group), updates); - } - - private void assertDeploymentData(ArgumentCaptor<PolicyPdpNotificationData> captor, ToscaPolicyIdentifier policyId, + private void assertDeploymentData(PolicyPdpNotificationData data, ToscaPolicyIdentifier policyId, String expectedPdps) { - PolicyPdpNotificationData data = captor.getValue(); assertEquals(policyId, data.getPolicyId()); assertEquals(policy1.getTypeIdentifier(), data.getPolicyType()); assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString()); @@ -772,4 +691,32 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { protected PdpDeployPolicies loadEmptyRequest() { return loadRequest("emptyRequest.json"); } + + private DeploymentGroups toDeploymentGroups(PdpGroups dbgroups) { + DeploymentGroups groups = new DeploymentGroups(); + + groups.setGroups(dbgroups.getGroups().stream().map(this::toDeploymentGroup).collect(Collectors.toList())); + + return groups; + } + + private DeploymentGroup toDeploymentGroup(PdpGroup dbgroup) { + DeploymentGroup group = new DeploymentGroup(); + + group.setName(dbgroup.getName()); + group.setDeploymentSubgroups(dbgroup.getPdpSubgroups().stream().map(this::toDeploymentSubGroup) + .collect(Collectors.toList())); + + return group; + } + + private DeploymentSubGroup toDeploymentSubGroup(PdpSubGroup dbsubgrp) { + DeploymentSubGroup subgrp = new DeploymentSubGroup(); + + subgrp.setAction(Action.PATCH); + subgrp.setPdpType(dbsubgrp.getPdpType()); + subgrp.setPolicies(new ArrayList<>(dbsubgrp.getPolicies())); + + return subgrp; + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java index 69165d1f..1f049709 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java @@ -36,7 +36,8 @@ import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.models.pap.concepts.PdpDeployPolicies; import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; -import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.DeploymentGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroups; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; @@ -46,10 +47,9 @@ import org.slf4j.LoggerFactory; public class PdpGroupDeployTest extends End2EndBase { private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployTest.class); - private static final String DEPLOY_GROUP_ENDPOINT = "pdps"; + private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch"; private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies"; private static final String DELETE_GROUP_ENDPOINT = "pdps/groups"; - private static final String CREATE_SUBGROUP = "pdpTypeA"; private static final String DEPLOY_SUBGROUP = "pdpTypeA"; /** @@ -82,27 +82,47 @@ public class PdpGroupDeployTest extends End2EndBase { public void tearDown() { // delete the group that was inserted try { - sendRequest(DELETE_GROUP_ENDPOINT + "/createGroups").delete(); + sendRequest(DELETE_GROUP_ENDPOINT + "/deployGroups").delete(); } catch (Exception e) { - logger.warn("cannot delete group: createGroups", e); + logger.warn("cannot delete group: deployGroups", e); } super.tearDown(); } @Test - public void testCreateGroups() throws Exception { + public void testUpdateGroupPolicies() throws Exception { - context.addPdp("pdpAA_1", CREATE_SUBGROUP); - context.addPdp("pdpAA_2", CREATE_SUBGROUP); - context.addPdp("pdpAB_1", "pdpTypeB"); + addGroups("deployGroups.json"); + + PdpStatus status11 = new PdpStatus(); + status11.setName("pdpAA_1"); + status11.setState(PdpState.ACTIVE); + status11.setPdpGroup("deployPolicies"); + status11.setPdpType(DEPLOY_SUBGROUP); + status11.setPdpSubgroup(DEPLOY_SUBGROUP); + + List<ToscaPolicyIdentifier> idents = Arrays.asList(new ToscaPolicyIdentifier("onap.restart.tca", "1.0.0")); + status11.setPolicies(idents); + + PdpStatus status12 = new PdpStatus(); + status12.setName("pdpAA_2"); + status12.setState(PdpState.ACTIVE); + status12.setPdpGroup("deployPolicies"); + status12.setPdpType(DEPLOY_SUBGROUP); + status12.setPdpSubgroup(DEPLOY_SUBGROUP); + status12.setPolicies(idents); + + context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11); + context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12); + context.addPdp("pdpAB_1", "pdpTypeA"); context.startThreads(); Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); - PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class); - Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON); + DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class); + Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON); Response rawresp = invocationBuilder.post(entity); PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); @@ -110,9 +130,8 @@ public class PdpGroupDeployTest extends End2EndBase { context.await(); - // none of the PDPs should have handled any requests - assertEquals(context.getPdps().size(), - context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count()); + // one of the PDPs should not have handled any requests + assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count()); // repeat - should be OK rawresp = invocationBuilder.post(entity); @@ -120,12 +139,13 @@ public class PdpGroupDeployTest extends End2EndBase { assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); assertNull(resp.getErrorDetails()); - // repeat with different properties - should fail - groups.getGroups().get(0).setProperties(null); + // repeat with unknown group - should fail + DeploymentGroup group = groups.getGroups().get(0); + group.setName("unknown-group"); rawresp = invocationBuilder.post(entity); resp = rawresp.readEntity(PdpGroupDeployResponse.class); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); - assertTrue(resp.getErrorDetails().contains("cannot change properties")); + assertTrue(resp.getErrorDetails().contains("unknown group")); } @Test |