diff options
author | Bilal A <bilal@research.att.com> | 2019-07-31 13:49:11 -0400 |
---|---|---|
committer | Bilal A <bilal@research.att.com> | 2019-07-31 13:49:11 -0400 |
commit | 47d51f34eddb4a49403e4eddb7a7343ab3c7b6f8 (patch) | |
tree | 8b4639ac462372db5ff07e98902c4c01057ea09e /main | |
parent | 466090990a3fa02a431ab23dc5261553028042a1 (diff) |
Policy API JUnit Tests
1) Adds tests for testing LegacyGuardPolicyProvider and
LegacyOperationalPolicyProvider
2) Fixes issue in LegacyGuardPolicyProvider and
LegacyOperationalPolicyProvider to handle
policy version number properly while deleting policies.
Issue-ID: POLICY-1771
Signed-off-by: Bilal A <bilal@research.att.com>
Change-Id: I01d48b5352e7e6da403faf1e4997d3550024755f
Diffstat (limited to 'main')
4 files changed, 205 insertions, 15 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java index 2f450866..d0032b03 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java @@ -25,7 +25,9 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; import java.util.List; import java.util.Map; + import javax.ws.rs.core.Response; + import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -41,6 +43,7 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; public class LegacyGuardPolicyProvider extends CommonModelProvider { private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; + private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; /** @@ -108,7 +111,7 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException { List<ToscaPolicyIdentifier> policies = new ArrayList<>(); - policies.add(new ToscaPolicyIdentifier(policyId, policyVersion)); + policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build(); List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java index 965ecdce..ac8fa269 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java @@ -24,7 +24,9 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; import java.util.List; + import javax.ws.rs.core.Response; + import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -39,6 +41,7 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; public class LegacyOperationalPolicyProvider extends CommonModelProvider { private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; + private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; /** @@ -105,7 +108,7 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException { List<ToscaPolicyIdentifier> policies = new ArrayList<>(); - policies.add(new ToscaPolicyIdentifier(policyId, policyVersion)); + policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build(); List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java index e892d417..6091e1d2 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java @@ -28,18 +28,32 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; import java.util.Base64; +import java.util.List; import java.util.Map; -import org.junit.AfterClass; -import org.junit.BeforeClass; + +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; @@ -70,8 +84,8 @@ public class TestLegacyGuardPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @BeforeClass - public static void setupParameters() throws PfModelException { + @Before + public void setupParameters() throws PfModelException { standardCoder = new StandardCoder(); providerParams = new PolicyModelsProviderParameters(); @@ -91,8 +105,8 @@ public class TestLegacyGuardPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @AfterClass - public static void tearDown() throws PfModelException { + @After + public void tearDown() throws PfModelException { guardPolicyProvider.close(); policyTypeProvider.close(); @@ -185,8 +199,86 @@ public class TestLegacyGuardPolicyProvider { } @Test - public void testDeleteGuardPolicy() { + public void testDeleteGuardPolicyException() { + String policyId = "guard.frequency.scaleout"; + String policyVersion = "1"; + String policyTypeVersion = "1.0.0"; + String policyTypeId = "onap.policies.controlloop.guard.FrequencyLimiter"; + String legacyMinorPatchSuffix = ".0.0"; + + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) { + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List<PdpGroup> groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier( + policyTypeId, policyTypeVersion)); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + // Create Pdp Groups + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + // Create Policy Type + assertThatCode(() -> { + String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE); + ToscaServiceTemplate policyTypeServiceTemplate = + standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + }).doesNotThrowAnyException(); + + // Create Policy + assertThatCode(() -> { + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, + LegacyGuardPolicyInput.class); + Map<String, LegacyGuardPolicyOutput> createdPolicy = + guardPolicyProvider.createGuardPolicy(policyToCreate); + assertNotNull(createdPolicy); + assertFalse(createdPolicy.isEmpty()); + }).doesNotThrowAnyException(); + + // Update pdpSubGroup + pdpSubGroup.setPolicies(new ArrayList<>()); + pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix)); + assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getPolicies().size()); + assertThatThrownBy(() -> { + guardPolicyProvider + .deleteGuardPolicy("guard.frequency.scaleout", "1"); + }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups"); + } + catch (Exception exc) { + fail("Test should not throw an exception"); + } + } + @Test + public void testDeleteGuardPolicy() { assertThatThrownBy(() -> { guardPolicyProvider.deleteGuardPolicy("dummy", null); }).hasMessage("legacy policy version is not an integer"); @@ -216,6 +308,7 @@ public class TestLegacyGuardPolicyProvider { deletedPolicy.get("guard.frequency.scaleout").getType()); assertEquals("1", deletedPolicy.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString()); + }).doesNotThrowAnyException(); assertThatThrownBy(() -> { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java index 4b7827a9..2915f129 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java @@ -27,17 +27,31 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; import java.util.Base64; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import java.util.List; + +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -64,8 +78,8 @@ public class TestLegacyOperationalPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @BeforeClass - public static void setupParameters() throws PfModelException { + @Before + public void setupParameters() throws PfModelException { standardCoder = new StandardCoder(); providerParams = new PolicyModelsProviderParameters(); @@ -85,8 +99,8 @@ public class TestLegacyOperationalPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @AfterClass - public static void tearDown() throws PfModelException { + @After + public void tearDown() throws PfModelException { operationalPolicyProvider.close(); policyTypeProvider.close(); @@ -174,6 +188,83 @@ public class TestLegacyOperationalPolicyProvider { } @Test + public void testDeleteOperationalPolicyException() { + String policyId = "operational.restart"; + String policyVersion = "1"; + String policyTypeVersion = "1.0.0"; + String policyTypeId = "onap.policies.controlloop.Operational"; + String legacyMinorPatchSuffix = ".0.0"; + + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) { + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List<PdpGroup> groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier( + policyTypeId, policyTypeVersion)); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + // Create Pdp Groups + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + // Create Policy Type + assertThatCode(() -> { + String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE); + ToscaServiceTemplate policyTypeServiceTemplate = + standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + }).doesNotThrowAnyException(); + + // Create Policy + assertThatCode(() -> { + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy policyToCreate = + standardCoder.decode(policyString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy createdPolicy = + operationalPolicyProvider.createOperationalPolicy(policyToCreate); + assertNotNull(createdPolicy); + }).doesNotThrowAnyException(); + + // Update pdpSubGroup + pdpSubGroup.setPolicies(new ArrayList<>()); + pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix)); + assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getPolicies().size()); + assertThatThrownBy(() -> { + operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion); + }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups"); + } + catch (Exception exc) { + fail("Test should not throw an exception"); + } + } + + @Test public void testDeleteOperationalPolicy() { assertThatThrownBy(() -> { |