Merge "Apply policy/models native drools policies changes"
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestLegacyGuardPolicyProvider.java
index 1f0077d..dfe29cf 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP Policy API
  * ================================================================================
  * Copyright (C) 2019 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.
@@ -32,6 +33,7 @@ import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Base64;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -41,6 +43,7 @@ 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.coder.StandardYamlCoder;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.Pdp;
@@ -55,11 +58,12 @@ 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.LegacyGuardPolicyContent;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 
 /**
- * This class performs unit test of {@link LegacyGuardPolicyProvider}
+ * This class performs unit test of {@link LegacyGuardPolicyProvider}.
  *
  * @author Chenfei Gao (cgao@research.att.com)
  */
@@ -70,16 +74,18 @@ public class TestLegacyGuardPolicyProvider {
     private static PolicyModelsProviderParameters providerParams;
     private static ApiParameterGroup apiParamGroup;
     private static StandardCoder standardCoder;
+    private static StandardYamlCoder standardYamlCoder;
 
     private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
     private static final String POLICY_RESOURCE_VER1 = "policies/vDNS.policy.guard.frequency.input.ver1.json";
     private static final String POLICY_RESOURCE_VER2 = "policies/vDNS.policy.guard.frequency.input.ver2.json";
+    private static final String POLICY_RESOURCE_WITH_NO_VERSION =
+            "policies/vDNS.policy.guard.frequency.no.policyversion.json";
     private static final String POLICY_TYPE_RESOURCE =
-            "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.json";
+            "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml";
     private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
     private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.guard.FrequencyLimiter";
     private static final String POLICY_TYPE_VERSION = "1.0.0";
-    private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0";
     private static final String POLICY_NAME = "guard.frequency.scaleout";
     private static final String POLICY_VERSION = "1";
     private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
@@ -91,15 +97,16 @@ public class TestLegacyGuardPolicyProvider {
      */
     @Before
     public void setupParameters() throws PfModelException {
-
         standardCoder = new StandardCoder();
+        standardYamlCoder = new StandardYamlCoder();
         providerParams = new PolicyModelsProviderParameters();
         providerParams.setDatabaseDriver("org.h2.Driver");
         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
         providerParams.setDatabaseUser("policy");
         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
         providerParams.setPersistenceUnit("ToscaConceptTest");
-        apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
+        apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams,
+                Collections.emptyList(), Collections.emptyList());
         ParameterService.register(apiParamGroup, true);
         guardPolicyProvider = new LegacyGuardPolicyProvider();
         policyTypeProvider = new PolicyTypeProvider();
@@ -112,13 +119,11 @@ public class TestLegacyGuardPolicyProvider {
      */
     @After
     public void tearDown() throws PfModelException {
-
         guardPolicyProvider.close();
         policyTypeProvider.close();
         ParameterService.deregister(apiParamGroup);
     }
 
-
     @Test
     public void testFetchGuardPolicy() {
 
@@ -128,12 +133,11 @@ public class TestLegacyGuardPolicyProvider {
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
-            String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
-            ToscaServiceTemplate policyTypeServiceTemplate =
-                    standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER1);
@@ -163,11 +167,11 @@ public class TestLegacyGuardPolicyProvider {
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "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(() -> {
             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest");
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression");
 
         assertThatCode(() -> {
             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
@@ -203,8 +207,8 @@ public class TestLegacyGuardPolicyProvider {
             pdpSubGroup.setPdpType("type");
             pdpSubGroup.setDesiredInstanceCount(123);
             pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
-            pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(
-                    POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
+            pdpSubGroup.getSupportedPolicyTypes()
+                    .add(new ToscaPolicyTypeIdentifier(POLICY_TYPE_NAME, POLICY_TYPE_VERSION));
             pdpGroup.getPdpSubgroups().add(pdpSubGroup);
 
             Pdp pdp = new Pdp();
@@ -222,9 +226,8 @@ public class TestLegacyGuardPolicyProvider {
 
             // Create Policy Type
             assertThatCode(() -> {
-                String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
-                ToscaServiceTemplate policyTypeServiceTemplate =
-                        standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+                ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                        .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
                 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
             }).doesNotThrowAnyException();
 
@@ -233,70 +236,81 @@ public class TestLegacyGuardPolicyProvider {
                 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
                 LegacyGuardPolicyInput policyToCreate =
                         standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
-                Map<String, LegacyGuardPolicyOutput> policyCreated = guardPolicyProvider
-                        .createGuardPolicy(policyToCreate);
+                Map<String, LegacyGuardPolicyOutput> policyCreated =
+                        guardPolicyProvider.createGuardPolicy(policyToCreate);
                 assertFalse(policyCreated.isEmpty());
             }).doesNotThrowAnyException();
 
             // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true
-            assertThatThrownBy(
-                () -> {
-                    guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
-                }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type "
-                    + POLICY_TYPE_ID + " deployed in any pdp group");
-
+            assertThatThrownBy(() -> {
+                guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
+            }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + POLICY_TYPE_ID
+                    + " deployed in any pdp group");
 
             // Update pdpSubGroup
             pdpSubGroup.setPolicies(new ArrayList<>());
-            pdpSubGroup.getPolicies().add(
-                    new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
-            assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
-                    .getPolicies().size());
+            pdpSubGroup.getPolicies()
+                    .add(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX));
+            assertEquals(1,
+                    databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
 
             // Test fetchDeployedPolicies
-            assertThatCode(
-                () -> {
-                    guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
-                }).doesNotThrowAnyException();
+            assertThatCode(() -> {
+                guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME);
+            }).doesNotThrowAnyException();
 
             // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
-            assertThatThrownBy(
-                () -> {
-                    guardPolicyProvider.deleteGuardPolicy(
-                            POLICY_NAME, POLICY_VERSION);
-                }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION
-                    + " cannot be deleted as it is deployed in pdp groups");
-        }
-        catch (Exception exc) {
+            assertThatThrownBy(() -> {
+                guardPolicyProvider.deleteGuardPolicy(POLICY_NAME, POLICY_VERSION);
+            }).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 testCreateGuardPolicy() {
-
+    public void testCreateGuardPolicy() throws Exception {
         assertThatThrownBy(() -> {
             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
             guardPolicyProvider.createGuardPolicy(policyToCreate);
-        }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
+        }).hasMessageContaining(
+                "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);
+        policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
+
+        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());
+        assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
+        assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
+                createdPolicy.get("guard.frequency.scaleout").getType());
+        assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
+
+        String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION);
+        LegacyGuardPolicyInput defaultVersionPolicy =
+                standardCoder.decode(defaultPolicyVersionString, LegacyGuardPolicyInput.class);
+        createdPolicy = guardPolicyProvider.createGuardPolicy(defaultVersionPolicy);
+        assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout.noversion").getVersion());
 
         assertThatCode(() -> {
-            String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
-            ToscaServiceTemplate policyTypeServiceTemplate =
-                    standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
-            policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
-            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());
-            assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
-            assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
-                    createdPolicy.get("guard.frequency.scaleout").getType());
-            assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
+            String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+            LegacyGuardPolicyInput duplicatePolicyToCreate =
+                    standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class);
+            guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate);
         }).doesNotThrowAnyException();
+
+        assertThatThrownBy(() -> {
+            String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+            LegacyGuardPolicyInput duplicatePolicyToCreate =
+                    standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class);
+            duplicatePolicyToCreate.setContent(new LegacyGuardPolicyContent());
+            guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate);
+        }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity");
     }
 
     @Test
@@ -327,8 +341,7 @@ public class TestLegacyGuardPolicyProvider {
             pdpSubGroup.setPdpType("type");
             pdpSubGroup.setDesiredInstanceCount(123);
             pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
-            pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(
-                    policyTypeId, policyTypeVersion));
+            pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
             pdpGroup.getPdpSubgroups().add(pdpSubGroup);
 
             Pdp pdp = new Pdp();
@@ -346,17 +359,16 @@ public class TestLegacyGuardPolicyProvider {
 
             // Create Policy Type
             assertThatCode(() -> {
-                String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
-                ToscaServiceTemplate policyTypeServiceTemplate =
-                    standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+                ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                        .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
                 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
             }).doesNotThrowAnyException();
 
             // Create Policy
             assertThatCode(() -> {
                 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
-                LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString,
-                        LegacyGuardPolicyInput.class);
+                LegacyGuardPolicyInput policyToCreate =
+                        standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
                 Map<String, LegacyGuardPolicyOutput> createdPolicy =
                         guardPolicyProvider.createGuardPolicy(policyToCreate);
                 assertNotNull(createdPolicy);
@@ -366,14 +378,12 @@ public class TestLegacyGuardPolicyProvider {
             // 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());
+            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) {
+                guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
+            }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
+        } catch (Exception exc) {
             fail("Test should not throw an exception");
         }
     }
@@ -382,16 +392,15 @@ public class TestLegacyGuardPolicyProvider {
     public void testDeleteGuardPolicy() {
         assertThatThrownBy(() -> {
             guardPolicyProvider.deleteGuardPolicy("dummy", null);
-        }).hasMessage("legacy policy version is not an integer");
+        }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> {
             guardPolicyProvider.deleteGuardPolicy("dummy", "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");
 
         assertThatCode(() -> {
-            String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
-            ToscaServiceTemplate policyTypeServiceTemplate =
-                    standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
+            ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
+                    .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
 
             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
@@ -400,8 +409,8 @@ public class TestLegacyGuardPolicyProvider {
             assertNotNull(createdPolicy);
             assertFalse(createdPolicy.isEmpty());
 
-            Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
-                    .deleteGuardPolicy("guard.frequency.scaleout", "1");
+            Map<String, LegacyGuardPolicyOutput> deletedPolicy =
+                    guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
             assertNotNull(deletedPolicy);
             assertFalse(deletedPolicy.isEmpty());
             assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));