Remove legacy operational policy from api config
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestLegacyOperationalPolicyProvider.java
index d812d4e..c91d8ad 100644 (file)
@@ -26,7 +26,6 @@ package org.onap.policy.api.main.rest.provider;
 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.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -35,7 +34,6 @@ import java.util.ArrayList;
 import java.util.Base64;
 import java.util.Collections;
 import java.util.List;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -73,9 +71,9 @@ public class TestLegacyOperationalPolicyProvider {
     private static StandardCoder standardCoder;
     private static StandardYamlCoder standardYamlCoder;
 
-    private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
+    private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.legacy.input.json";
     private static final String POLICY_RESOURCE_WITH_NO_VERSION =
-            "policies/vDNS.policy.operational.no.policyversion.json";
+        "policies/vDNS.policy.operational.no.policyversion.json";
     private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.yaml";
     private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
     private static final String POLICY_NAME = "operational.restart";
@@ -100,7 +98,8 @@ public class TestLegacyOperationalPolicyProvider {
         providerParams.setDatabaseUser("policy");
         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
         providerParams.setPersistenceUnit("ToscaConceptTest");
-        apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
+        apiParamGroup =
+            new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(), Collections.emptyList());
         ParameterService.register(apiParamGroup, true);
         operationalPolicyProvider = new LegacyOperationalPolicyProvider();
         policyTypeProvider = new PolicyTypeProvider();
@@ -124,14 +123,14 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
-        }).hasMessage("no policy found for policy: dummy:null");
+        }).hasMessage("service template not found in database");
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("service template not found in database");
 
         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
-                .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+            .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
         policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
         String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
@@ -140,23 +139,23 @@ public class TestLegacyOperationalPolicyProvider {
         assertNotNull(createdPolicy);
 
         LegacyOperationalPolicy firstVersion =
-                operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
+            operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1");
         assertNotNull(firstVersion);
         assertEquals("1", firstVersion.getPolicyVersion());
 
         LegacyOperationalPolicy latestVersion =
-                operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
+            operationalPolicyProvider.fetchOperationalPolicy("operational.restart", null);
         assertNotNull(latestVersion);
         assertEquals("1", latestVersion.getPolicyVersion());
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression");
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest");
             ;
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression");
 
         operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
         policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
@@ -170,7 +169,7 @@ public class TestLegacyOperationalPolicyProvider {
         }).hasMessage("could not find policy with ID dummy and type " + POLICY_TYPE_ID + " deployed in any pdp group");
 
         try (PolicyModelsProvider databaseProvider =
-                new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
+            new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
             assertEquals(0, databaseProvider.getPdpGroups("name").size());
             assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
 
@@ -190,7 +189,7 @@ public class TestLegacyOperationalPolicyProvider {
             pdpSubGroup.setDesiredInstanceCount(123);
             pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
             pdpSubGroup.getSupportedPolicyTypes()
-                    .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
+                .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
             pdpGroup.getPdpSubgroups().add(pdpSubGroup);
 
             Pdp pdp = new Pdp();
@@ -202,14 +201,14 @@ public class TestLegacyOperationalPolicyProvider {
             pdpSubGroup.getPdpInstances().add(pdp);
 
             // Create Pdp Groups
-            assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
-                    .getDesiredInstanceCount());
+            assertEquals(123,
+                databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
             assertEquals(1, databaseProvider.getPdpGroups("group").size());
 
             // Create Policy Type
             assertThatCode(() -> {
                 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
-                        .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
                 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
             }).doesNotThrowAnyException();
 
@@ -217,26 +216,26 @@ public class TestLegacyOperationalPolicyProvider {
             assertThatCode(() -> {
                 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
                 LegacyOperationalPolicy policyToCreate =
-                        standardCoder.decode(policyString, LegacyOperationalPolicy.class);
+                    standardCoder.decode(policyString, LegacyOperationalPolicy.class);
                 LegacyOperationalPolicy policyCreated =
-                        operationalPolicyProvider.createOperationalPolicy(policyToCreate);
+                    operationalPolicyProvider.createOperationalPolicy(policyToCreate);
                 assertEquals("operational.restart", policyCreated.getPolicyId());
                 assertEquals("1", policyCreated.getPolicyVersion());
-                assertFalse(policyCreated.getContent() == null);
+                assertNotNull(policyCreated.getContent());
             }).doesNotThrowAnyException();
 
             // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
             assertThatThrownBy(() -> {
                 operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME);
             }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
-                    + " deployed in any pdp group");
+                + " deployed in any pdp group");
 
             // Update pdpSubGroup
             pdpSubGroup.setPolicies(new ArrayList<>());
             pdpSubGroup.getPolicies()
-                    .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
+                .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
             assertEquals(1,
-                    databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
+                databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
 
             // Test fetchDeployedPolicies
             assertThatCode(() -> {
@@ -246,8 +245,7 @@ public class TestLegacyOperationalPolicyProvider {
             // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
             assertThatThrownBy(() -> {
                 operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION);
-            }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
-                    + " cannot be deleted as it is deployed in pdp groups");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -261,10 +259,10 @@ public class TestLegacyOperationalPolicyProvider {
             LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
             operationalPolicyProvider.createOperationalPolicy(policyToCreate);
         }).hasMessageContaining(
-                "no policy types are defined on the service template for the policies in the topology template");
+            "no policy types are defined on the service template for the policies in the topology template");
 
         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
-                .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+            .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
         policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
         String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
@@ -274,19 +272,26 @@ public class TestLegacyOperationalPolicyProvider {
         assertEquals("operational.restart", createdPolicy.getPolicyId());
         assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
 
-        assertThatThrownBy(() -> {
-            String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
-            LegacyOperationalPolicy badPolicyToCreate =
-                    standardCoder.decode(badPolicyString, LegacyOperationalPolicy.class);
-            operationalPolicyProvider.createOperationalPolicy(badPolicyToCreate);
-        }).hasMessage("mandatory field 'policy-version' is missing in the policy: operational.scaleout");
+        String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
+        LegacyOperationalPolicy defaultPolicyVersionPolicy =
+            standardCoder.decode(defaultPolicyVersionString, LegacyOperationalPolicy.class);
+        createdPolicy = operationalPolicyProvider.createOperationalPolicy(defaultPolicyVersionPolicy);
+        assertEquals("1", createdPolicy.getPolicyVersion());
+
+        assertThatCode(() -> {
+            String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+            LegacyOperationalPolicy duplicatePolicyToCreate =
+                standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
+            operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
+        }).doesNotThrowAnyException();
 
         assertThatThrownBy(() -> {
             String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
             LegacyOperationalPolicy duplicatePolicyToCreate =
-                    standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
+                standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class);
+            duplicatePolicyToCreate.setContent("some other content");
             operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate);
-        }).hasMessage("operational policy operational.restart:1 already exists; its latest version is 1");
+        }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity");
     }
 
     @Test
@@ -298,7 +303,7 @@ public class TestLegacyOperationalPolicyProvider {
         String legacyMinorPatchSuffix = ".0.0";
 
         try (PolicyModelsProvider databaseProvider =
-                new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
+            new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
             assertEquals(0, databaseProvider.getPdpGroups("name").size());
             assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
 
@@ -329,14 +334,14 @@ public class TestLegacyOperationalPolicyProvider {
             pdpSubGroup.getPdpInstances().add(pdp);
 
             // Create Pdp Groups
-            assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
-                    .getDesiredInstanceCount());
+            assertEquals(123,
+                databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
             assertEquals(1, databaseProvider.getPdpGroups("group").size());
 
             // Create Policy Type
             assertThatCode(() -> {
                 ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
-                        .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
                 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
             }).doesNotThrowAnyException();
 
@@ -344,9 +349,9 @@ public class TestLegacyOperationalPolicyProvider {
             assertThatCode(() -> {
                 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
                 LegacyOperationalPolicy policyToCreate =
-                        standardCoder.decode(policyString, LegacyOperationalPolicy.class);
+                    standardCoder.decode(policyString, LegacyOperationalPolicy.class);
                 LegacyOperationalPolicy createdPolicy =
-                        operationalPolicyProvider.createOperationalPolicy(policyToCreate);
+                    operationalPolicyProvider.createOperationalPolicy(policyToCreate);
                 assertNotNull(createdPolicy);
             }).doesNotThrowAnyException();
 
@@ -354,10 +359,10 @@ public class TestLegacyOperationalPolicyProvider {
             pdpSubGroup.setPolicies(new ArrayList<>());
             pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion + legacyMinorPatchSuffix));
             assertEquals(1,
-                    databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
+                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");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
         } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
@@ -368,15 +373,15 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
-                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+                .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
@@ -385,7 +390,7 @@ public class TestLegacyOperationalPolicyProvider {
             assertNotNull(createdPolicy);
 
             LegacyOperationalPolicy deletedPolicy =
-                    operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
+                operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
             assertNotNull(deletedPolicy);
             assertEquals("operational.restart", deletedPolicy.getPolicyId());
             assertTrue(deletedPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
@@ -393,7 +398,7 @@ public class TestLegacyOperationalPolicyProvider {
 
         assertThatThrownBy(() -> {
             operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1");
-        }).hasMessage("no policy found for policy: operational.restart:1");
+        }).hasMessage("no policies found");
 
         assertThatCode(() -> {
             policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");