diff options
Diffstat (limited to 'main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java')
-rw-r--r-- | main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java | 103 |
1 files changed, 97 insertions, 6 deletions
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(() -> { |