summaryrefslogtreecommitdiffstats
path: root/ONAP-REST
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-11-27 11:10:04 +0000
committerliamfallon <liam.fallon@est.tech>2019-11-27 11:10:10 +0000
commit35f44d97d569ef4da46d17c30cae71e75bcfe0c9 (patch)
treebe681ec3af44c4f7f8f9eb7676bb05e8784e0dfa /ONAP-REST
parent9bad71c7150ea6f94b2a08b68f356b4ee96c3d66 (diff)
Unit/SONAR/Checkstyle in ONAP-REST
Util package of ONAP-REST, with JUnit added and SONAR/Checkstyle issues addressed. Coverage increased from 18% to 31% in large PolicyValidation class. Issue-ID: POLICY-2131 Change-Id: Ie059db312918021683fe27cc2aaeed86b04046f2 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'ONAP-REST')
-rw-r--r--ONAP-REST/src/main/java/org/onap/policy/rest/util/MsModelUtils.java1
-rw-r--r--ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java5
-rw-r--r--ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java278
-rw-r--r--ONAP-REST/src/test/resources/policies/MicroServicePolicy.json38
4 files changed, 305 insertions, 17 deletions
diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/MsModelUtils.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/MsModelUtils.java
index 5b596be45..b18572574 100644
--- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/MsModelUtils.java
+++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/MsModelUtils.java
@@ -202,7 +202,6 @@ public class MsModelUtils {
processXmiEpackage(file);
}
return classMap;
-
}
/**
diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
index 6bab919ad..adb8995c5 100644
--- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
+++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
@@ -47,6 +47,8 @@ import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
+import lombok.Getter;
+
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
@@ -113,6 +115,7 @@ public class PolicyValidation {
private static Map<String, String> jsonRequestMap = new HashMap<>();
private static List<String> modelRequiredFieldsList = new ArrayList<>();
+ @Getter
private static CommonClassDao commonClassDao;
private Set<String> allReqTrueKeys = new HashSet<>();
@@ -230,7 +233,7 @@ public class PolicyValidation {
// Decision Policy Attributes Validation
if (!"API".equals(policyData.getApiflag()) && policyData.getSettings() != null
&& !policyData.getSettings().isEmpty()) {
- for (Object attribute : policyData.getAttributes()) {
+ for (Object attribute : policyData.getSettings()) {
if (attribute instanceof LinkedHashMap<?, ?>) {
String value = null;
if (((LinkedHashMap<?, ?>) attribute).get("key") == null) {
diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
index 47b0f49b5..3efb825b4 100644
--- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
+++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
@@ -21,11 +21,18 @@
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");
+ }
}
diff --git a/ONAP-REST/src/test/resources/policies/MicroServicePolicy.json b/ONAP-REST/src/test/resources/policies/MicroServicePolicy.json
new file mode 100644
index 000000000..b9c06e536
--- /dev/null
+++ b/ONAP-REST/src/test/resources/policies/MicroServicePolicy.json
@@ -0,0 +1,38 @@
+{
+ "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"
+ }
+ ]
+ }
+}