Add changes for safe delete in policy-models
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestPolicyProvider.java
index 5d0b412..d9b01fd 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy API
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ 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.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.common.utils.coder.StandardYamlCoder;
 import org.onap.policy.common.utils.resources.ResourceUtils;
@@ -76,19 +77,19 @@ public class TestPolicyProvider {
     private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json";
     private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION =
             "policies/vCPE.policy.bad.policytypeversion.json";
+    private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json";
+    private static final String POLICY_RESOURCE_WITH_DIFFERENT_FIELDS =
+            "policies/vCPE.policy.different.policy.fields.json";
     private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json";
 
-    // @formatter:off
-    private String[] toscaPolicyTypeResourceNames = {
-        "policytypes/onap.policies.optimization.AffinityPolicy.yaml",
-        "policytypes/onap.policies.optimization.DistancePolicy.yaml",
-        "policytypes/onap.policies.optimization.HpaPolicy.yaml",
-        "policytypes/onap.policies.optimization.QueryPolicy.yaml",
-        "policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
-        "policytypes/onap.policies.optimization.Vim_fit.yaml",
-        "policytypes/onap.policies.optimization.VnfPolicy.yaml"
-    };
-    // @formatter:on
+    public static final String POLICY_TYPE_RESOURCE_OPERATIONAL =
+            "policytypes/onap.policies.controlloop.Operational.yaml";
+    public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
+            "policytypes/onap.policies.controlloop.operational.Common.yaml";
+    public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
+            "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
+    private static final String POLICY_RESOURCE_OPERATIONAL = "policies/vCPE.policy.operational.input.tosca.json";
+    public static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools";
 
     /**
      * Initializes parameters.
@@ -130,15 +131,15 @@ public class TestPolicyProvider {
 
         assertThatThrownBy(() -> {
             policyProvider.fetchPolicies("dummy", "1.0.0", null, null);
-        }).hasMessage("policy with ID null:null and type dummy:1.0.0 does not exist");
+        }).hasMessage("service template not found in database");
 
         assertThatThrownBy(() -> {
             policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", null);
-        }).hasMessage("policy with ID dummy:null and type dummy:1.0.0 does not exist");
+        }).hasMessage("service template not found in database");
 
         assertThatThrownBy(() -> {
             policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0");
-        }).hasMessage("policy with ID dummy:1.0.0 and type dummy:1.0.0 does not exist");
+        }).hasMessage("service template not found in database");
     }
 
     @Test
@@ -146,10 +147,9 @@ public class TestPolicyProvider {
 
         assertThatThrownBy(() -> {
             policyProvider.fetchLatestPolicies("dummy", "dummy", "dummy");
-        }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist");
+        }).hasMessage("service template not found in database");
     }
 
-
     @Test
     public void testFetchDeployedPolicies() {
         String policyId = "onap.restart.tca";
@@ -200,8 +200,8 @@ public class TestPolicyProvider {
 
             // Create Policy Type
             assertThatCode(() -> {
-                ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
-                        ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+                ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                        .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
                 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
             }).doesNotThrowAnyException();
 
@@ -218,10 +218,9 @@ public class TestPolicyProvider {
             // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
             assertThatThrownBy(() -> {
                 policyProvider.fetchDeployedPolicies(policyTypeId, policyTypeVersion, policyId);
-            })  .hasMessage("could not find policy with ID " + policyId + " and type " + policyTypeId + ":"
+            }).hasMessage("could not find policy with ID " + policyId + " and type " + policyTypeId + ":"
                     + policyTypeVersion + " deployed in any pdp group");
 
-
             // Update pdpSubGroup
             pdpSubGroup.setPolicies(new ArrayList<>());
             pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion));
@@ -237,25 +236,24 @@ public class TestPolicyProvider {
             assertThatThrownBy(() -> {
                 policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
                         "1.0.0");
-            })  .hasMessageContaining("policy with ID " + policyId + ":" + policyVersion
-                    + " 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");
         }
     }
 
     @Test
-    public void testCreatePolicy() {
+    public void testCreatePolicy() throws Exception {
 
         assertThatThrownBy(() -> {
             policyProvider.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate());
-        }).hasMessage("policy type with ID dummy:1.0.0 does not exist");
+        }).hasMessage("topology template not specified on service template");
 
-        assertThatCode(() -> {
-            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
-                    ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
-            policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-        }).doesNotThrowAnyException();
+        ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        assertThatCode(() -> policyTypeProvider.createPolicyType(policyTypeServiceTemplate)).doesNotThrowAnyException();
 
         assertThatThrownBy(() -> {
             String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID);
@@ -263,7 +261,8 @@ public class TestPolicyProvider {
                     standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
             policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
                     badPolicyServiceTemplate);
-        }).hasMessage("policy type id does not match");
+        }).hasMessageContaining(
+                "policy type onap.policies.monitoring.cdap.tca.hi.lo.appxxx:0.0.0 referenced in policy not found");
 
         assertThatThrownBy(() -> {
             String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION);
@@ -271,41 +270,133 @@ public class TestPolicyProvider {
                     standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
             policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
                     badPolicyServiceTemplate);
-        }).hasMessage("policy type version does not match");
+        }).hasMessageContaining(
+                "policy type onap.policies.monitoring.cdap.tca.hi.lo.app:2.0.0 referenced in policy not found");
 
-        assertThatCode(() -> {
-            String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
-            ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
-            ToscaServiceTemplate serviceTemplate = policyProvider
-                    .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
-            assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
-        }).doesNotThrowAnyException();
+        assertThatThrownBy(() -> {
+            String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION);
+            ToscaServiceTemplate badPolicyServiceTemplate =
+                    standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+            policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+                    badPolicyServiceTemplate);
+        }).hasMessageContaining("key version is a null version");
+
+        String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+        ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+        ToscaServiceTemplate serviceTemplate = policyProvider
+                .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+        assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+
+        assertThatThrownBy(() -> {
+            String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DIFFERENT_FIELDS);
+            ToscaServiceTemplate badPolicyServiceTemplate =
+                    standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+            policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+                    badPolicyServiceTemplate);
+        }).hasMessageContaining("entity in incoming fragment does not equal existing entity");
+    }
+
+    @Test
+    public void testCreateOperationalDroolsPolicy() throws CoderException, PfModelException {
+        ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL), ToscaServiceTemplate.class);
+
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
+
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_OPERATIONAL);
+        ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+        ToscaServiceTemplate serviceTemplate =
+                policyProvider.createPolicy(POLICY_TYPE_OPERATIONAL_DROOLS, "1.0.0", policyServiceTemplate);
+        assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
     }
 
     @Test
     public void testSimpleCreatePolicy() throws Exception {
 
-        String errorMessage = "policy type onap.policies.optimization.AffinityPolicy:0.0.0 for "
-            + "policy OSDF_CASABLANCA.Affinity_vCPE_1:1.0.0 does not exist";
         assertThatThrownBy(() -> {
             String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
             ToscaServiceTemplate multiPoliciesServiceTemplate =
                     standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
             policyProvider.createPolicies(multiPoliciesServiceTemplate);
-        }).hasMessage(errorMessage);
+        }).hasMessageContaining(
+                "no policy types are defined on the service template for the policies in the topology template");
 
         // Create required policy types
-        for (String policyTypeName : toscaPolicyTypeResourceNames) {
-            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
-                    ResourceUtils.getResourceAsString(policyTypeName), ToscaServiceTemplate.class);
-            policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-        }
+        ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.Optimization.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Resource.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils
+                        .getResourceAsString("policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils
+                        .getResourceAsString("policytypes/onap.policies.optimization.resource.DistancePolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.Vim_fit.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.HpaPolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Service.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils
+                        .getResourceAsString("policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.service.QueryPolicy.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        policyTypeServiceTemplate = standardYamlCoder.decode(
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"),
+                ToscaServiceTemplate.class);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
         // Create multiple policies in one call
         String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
         ToscaServiceTemplate multiPoliciesServiceTemplate =
                 standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
-        policyProvider.createPolicies(multiPoliciesServiceTemplate);
+
+        assertThatCode(() -> {
+            policyProvider.createPolicies(multiPoliciesServiceTemplate);
+            policyProvider.createPolicies(multiPoliciesServiceTemplate);
+        }).doesNotThrowAnyException();
     }
 
     @Test
@@ -313,11 +404,11 @@ public class TestPolicyProvider {
 
         assertThatThrownBy(() -> {
             policyProvider.deletePolicy("dummy", "1.0.0", "dummy", "1.0.0");
-        }).hasMessage("policy with ID dummy:1.0.0 and type dummy:1.0.0 does not exist");
+        }).hasMessage("service template not found in database");
 
         assertThatCode(() -> {
-            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
-                    ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
         }).doesNotThrowAnyException();
 
@@ -335,12 +426,9 @@ public class TestPolicyProvider {
             assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
         }).doesNotThrowAnyException();
 
-        String exceptionMessage = "policy with ID onap.restart.tca:1.0.0 and type "
-                + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist";
         assertThatThrownBy(() -> {
             policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
                     "1.0.0");
-        }).hasMessage(exceptionMessage);
+        }).hasMessageContaining("no policies found");
     }
-
 }