Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / util / PolicyValidationTest.java
index 47b0f49..3efb825 100644 (file)
 
 package org.onap.policy.rest.util;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
 
+import java.io.IOException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 
 import org.junit.After;
@@ -33,6 +40,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.api.PolicyConfigType;
 import org.onap.policy.api.PolicyParameters;
+import org.onap.policy.common.utils.resources.TextFileUtils;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 
 public class PolicyValidationTest {
@@ -54,19 +62,7 @@ public class PolicyValidationTest {
         policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example");
         policyParameters.setOnapName("DCAE");
         policyParameters.setPriority("1");
-        String msJsonString = "{\"service\":\"TOSCA_namingJenny\",\"location\":\"Test  DictMSLoc\","
-                        + "\"uuid\":\"testDict  DCAEUIID\",\"policyName\":\"testModelValidation\","
-                        + "\"description\":\"test\",\"configName\":\"testDict  MSConfName\","
-                        + "\"templateVersion\":\"1607\",\"version\":\"gw12181031\",\"priority\":\"5\","
-                        + "\"policyScope\":\"resource=ResourcetypeVenktest1,service=ServiceName1707,type=Name1707,"
-                        + "closedLoopControlName=Retest_retest1\",\"riskType\":\"Test\",\"riskLevel\":\"3\","
-                        + "\"guard\":\"True\",\"content\":{\"police-instance-name\":\"testing\","
-                        + "\"naming-models\":[{\"naming-properties\":[{\"property-value\":\"test\","
-                        + "\"source-endpoint\":\"test\",\"property-name\":\"testPropertyname\","
-                        + "\"increment-sequence\":{\"scope\":\"VNF\",\"start-value\":\"1\",\"length\":\"3\","
-                        + "\"increment\":\"2\"},\"source-system\":\"TOSCA\"}],\"naming-type\":\"testNamingType\","
-                        + "\"naming-recipe\":\"testNamingRecipe\"}]}}";
-        ;
+        String msJsonString = TextFileUtils.getTextFileAsString("src/test/resources/policies/MicroServicePolicy.json");
         policyParameters.setConfigBody(msJsonString);
         policyParameters.setRequestID(UUID.randomUUID());
         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
@@ -80,10 +76,32 @@ public class PolicyValidationTest {
         PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
         PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
         PolicyValidation validation = new PolicyValidation();
-        StringBuilder responseString = validation.validatePolicy(policyData);
+        String responseString = validation.validatePolicy(policyData).toString();
 
-        assertNotSame("success", responseString.toString());
+        assertNotSame("success", responseString);
 
+        new PolicyValidation(null);
+        assertNull(PolicyValidation.getCommonClassDao());
+
+        policyData.setConfigPolicyType("ClosedLoop_Fault");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("success");
+
+        policyData.setConfigPolicyType("ClosedLoop_PM");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("success");
+
+        policyData.setConfigPolicyType("Enforcer Config");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("success");
+
+        policyData.setConfigPolicyType("Optimization");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("success");
+
+        policyData.setConfigPolicyType("Strange");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("success");
     }
 
     @Test
@@ -93,4 +111,234 @@ public class PolicyValidationTest {
         assertEquals("success", result);
     }
 
+    @Test
+    public void testPolicyHeadingValidation() throws IOException {
+        PolicyValidation validation = new PolicyValidation();
+        PolicyRestAdapter policyData = new PolicyRestAdapter();
+
+        String responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("PolicyName Should not be empty");
+
+        policyData.setPolicyName("%%%~~~%%%");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}");
+
+        policyData.setPolicyName("ALegalPolicyName");
+        policyData.setPolicyDescription("@CreatedBy:");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString)
+                        .contains("The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:");
+        policyData.setPolicyDescription("@CreatedBy:");
+
+        policyData.setPolicyDescription("A Legal Description");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString)
+                        .doesNotContain("The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:");
+    }
+
+    @Test
+    public void testPolicyAttributeValidation() throws IOException {
+        PolicyValidation validation = new PolicyValidation();
+        PolicyRestAdapter policyData = new PolicyRestAdapter();
+        policyData.setPolicyName("ALegalPolicyName");
+        policyData.setPolicyDescription("A Valid Description");
+
+        String responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("API");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("NOTAPI");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        List<Object> attributes = new ArrayList<>();
+        policyData.setAttributes(attributes);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        attributes.add(new String("hello"));
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+        attributes.clear();
+
+        Map<String, String> mapAttribute = new LinkedHashMap<>();
+        attributes.add(mapAttribute);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Component Attributes</b>:<i> has one missing Component Attribute value");
+
+        mapAttribute.put("key", "value");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
+                        responseString);
+
+        mapAttribute.put("key", "");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>Attributes or Component Attributes</b>:<i>null : value has spaces or invalid characters</i>"
+                        + "<br><b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
+                        responseString);
+        mapAttribute.clear();
+
+        responseString = validation.validatePolicy(policyData).toString();
+        mapAttribute.put("hello", "aaa");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>Component Attributes</b>:<i> has one missing Component Attribute key</i><br>"
+                        + "<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
+                        responseString);
+
+        policyData.setPolicyType("Config");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>RiskType</b>: Risk Type Should not be Empty</i><br>"
+                        + "<b>RiskLevel</b>: Risk Level Should not be Empty</i><br>"
+                        + "<b>Guard</b>: Guard Value Should not be Empty</i><br>", responseString);
+
+        policyData.setConfigPolicyType("Base");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("has one missing Attribute key");
+
+        policyData.setConfigPolicyType("BRMS_Param");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("has one missing Attribute key");
+
+        policyData.setConfigPolicyType("BRMS_Raw");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("has one missing Attribute key");
+
+        policyData.setConfigPolicyType(null);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Risk Level Should not be Empty");
+
+        mapAttribute.clear();
+        mapAttribute.put("value", "thevalue");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Risk Level Should not be Empty");
+
+        mapAttribute.put("value", "$$$%%%%");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Risk Level Should not be Empty");
+
+        policyData.setConfigPolicyType("Base");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("value has spaces or invalid characters");
+
+        policyData.setConfigPolicyType("BRMS_Param");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("value has spaces or invalid characters");
+
+        policyData.setConfigPolicyType("BRMS_Raw");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("value has spaces or invalid characters");
+
+        policyData.setConfigPolicyType(null);
+        policyData.setPolicyType(null);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("value has spaces or invalid characters");
+    }
+
+    @Test
+    public void testPolicySettingsValidation() throws IOException {
+        PolicyValidation validation = new PolicyValidation();
+        PolicyRestAdapter policyData = new PolicyRestAdapter();
+        policyData.setPolicyName("ALegalPolicyName");
+        policyData.setPolicyDescription("A Valid Description");
+
+        String responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("API");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("NOTAPI");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        List<Object> settings = new ArrayList<>();
+        policyData.setSettings(settings);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        settings.add("hello");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+        settings.clear();
+
+        Map<String, String> mapSetting = new LinkedHashMap<>();
+        settings.add(mapSetting);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Settings Attributes</b>:<i> has one missing Attribute key");
+
+        mapSetting.put("key", "value");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>", responseString);
+
+        mapSetting.put("key", "");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>", responseString);
+        mapSetting.clear();
+
+        mapSetting.put("value", "thevalue");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("has one missing Attribute key");
+
+        mapSetting.put("value", "$$$%%%");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("value has spaces or invalid characters");
+    }
+
+    @Test
+    public void testPolicyRuleAlgorithmsValidation() throws IOException {
+        PolicyValidation validation = new PolicyValidation();
+        PolicyRestAdapter policyData = new PolicyRestAdapter();
+        policyData.setPolicyName("ALegalPolicyName");
+        policyData.setPolicyDescription("A Valid Description");
+
+        String responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("API");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("NOTAPI");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        List<Object> ruleAlgorithmschoices = new ArrayList<>();
+        policyData.setRuleAlgorithmschoices(ruleAlgorithmschoices);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        ruleAlgorithmschoices.add("hello");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+        ruleAlgorithmschoices.clear();
+
+        Map<String, String> mapChoice = new LinkedHashMap<>();
+        ruleAlgorithmschoices.add(mapChoice);
+        assertNull(validation.validatePolicy(policyData));
+
+        mapChoice.clear();
+        mapChoice.put("id", "TheID");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Field 1 value is not selected");
+
+        mapChoice.put("dynamicRuleAlgorithmField1", "Field1");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Field 2 value is not selected");
+
+        mapChoice.put("dynamicRuleAlgorithmCombo", "Combo");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Field 3 value is empty");
+
+        mapChoice.put("dynamicRuleAlgorithmField2", "Field2");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        mapChoice.put("dynamicRuleAlgorithmField2", "%%%$$$");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Field 3 value has special characters");
+    }
 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use