diff options
author | Tej, Tarun <tt3868@att.com> | 2017-09-15 00:52:13 -0400 |
---|---|---|
committer | Tej, Tarun <tt3868@att.com> | 2017-09-15 01:37:52 -0400 |
commit | e0385921034ae9ce860038ea65d2d13259f7cc4c (patch) | |
tree | fa9461d587584fca27fc9a4a81614c91dcac6231 /ONAP-PAP-REST/src | |
parent | e9e7bef3c130f7525a5ad250f365ee910bebe85a (diff) |
Adding Junits for policy engine
Adding Junits to Policy Engine Utils project, removed duplicates, fixed
few defects along the way and fixes to projects.
Issue-Id: POLICY-52
Change-Id: Ia0598300f9bd60f5372b9ef7d8984657478a1611
Signed-off-by: Tej, Tarun <tt3868@att.com>
Diffstat (limited to 'ONAP-PAP-REST/src')
5 files changed, 21 insertions, 123 deletions
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java index 63a62d23d..adcffe939 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java @@ -24,18 +24,23 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.StringReader; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; -import java.util.Scanner; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; +import org.apache.commons.io.FilenameUtils; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.eelf.PolicyLogger; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.utils.PolicyUtils; + +import com.att.research.xacml.api.pap.PAPException; +import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType; @@ -51,21 +56,6 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; -import org.apache.commons.io.FilenameUtils; -import org.onap.policy.common.logging.eelf.MessageCodes; -import org.onap.policy.common.logging.eelf.PolicyLogger; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; -import org.onap.policy.rest.adapter.PolicyRestAdapter; -import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.XMLReader; - -import com.att.research.xacml.api.pap.PAPException; -import com.att.research.xacml.std.IdentifierImpl; - public class ConfigPolicy extends Policy { /** @@ -148,15 +138,15 @@ public class ConfigPolicy extends Policy { String id = policyAdapter.getConfigType(); if (id != null) { if (id.equals(JSON_CONFIG)) { - if (!isJSONValid(configBodyData)) { + if (!PolicyUtils.isJSONValid(configBodyData)) { isValidForm = false; } } else if (id.equals(XML_CONFIG)) { - if (!isXMLValid(configBodyData)) { + if (!PolicyUtils.isXMLValid(configBodyData)) { isValidForm = false; } } else if (id.equals(PROPERTIES_CONFIG)) { - if (!isPropValid(configBodyData)||configBodyData.equals("")) { + if (!PolicyUtils.isPropValid(configBodyData)||configBodyData.equals("")) { isValidForm = false; } } else if (id.equals(OTHER_CONFIG)) { @@ -169,68 +159,6 @@ public class ConfigPolicy extends Policy { } - // Validation for XML. - private boolean isXMLValid(String data) { - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setValidating(false); - factory.setNamespaceAware(true); - try { - SAXParser parser = factory.newSAXParser(); - XMLReader reader = parser.getXMLReader(); - reader.setErrorHandler(new XMLErrorHandler()); - reader.parse(new InputSource(new StringReader(data))); - } catch (ParserConfigurationException | SAXException | IOException e) { - LOGGER.debug(e); - return false; - } - return true; - - } - - // Validation for Properties file. - public boolean isPropValid(String prop) { - - Scanner scanner = new Scanner(prop); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - line.replaceAll("\\s+", ""); - if (line.startsWith("#")) { - continue; - } else { - if (line.contains("=")) { - String[] parts = line.split("="); - if (parts.length < 2) { - scanner.close(); - return false; - } - } else { - scanner.close(); - return false; - } - } - } - scanner.close(); - return true; - - } - - public class XMLErrorHandler implements ErrorHandler { - - public void warning(SAXParseException e) throws SAXException { - System.out.println(e.getMessage()); - } - - public void error(SAXParseException e) throws SAXException { - System.out.println(e.getMessage()); - } - - public void fatalError(SAXParseException e) throws SAXException { - System.out.println(e.getMessage()); - } - - } - @Override public Map<String, String> savePolicies() throws PAPException { diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java index 6e43c967c..eae3e79a9 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java @@ -285,7 +285,7 @@ public class DecisionPolicy extends Policy { activeTimeRange.put("end", yamlParams.get("guardActiveEnd")); String blackListString = yamlParams.get("blackList"); List<String> blackList = null; - if(blackListString!=null){ + if(blackListString!=null && !blackListString.trim().isEmpty()){ if (blackListString.contains(",")){ blackList = Arrays.asList(blackListString.split(",")); } @@ -303,6 +303,9 @@ public class DecisionPolicy extends Policy { templateFile = new File(classLoader.getResource(XACML_BLGUARD_TEMPLATE).getFile()); xacmlTemplatePath = templateFile.toPath(); cons.setActive_time_range(activeTimeRange); + if(blackList==null || blackList.isEmpty()){ + throw new BuilderException("blackList is required"); + } cons.setBlacklist(blackList); break; default: diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java index 169e6e14e..20c76f3be 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java @@ -20,23 +20,17 @@ package org.onap.policy.pap.xacml.rest.components; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; -import javax.json.Json; -import javax.json.JsonReader; - import org.apache.commons.io.FilenameUtils; -import org.json.JSONObject; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -202,33 +196,6 @@ public abstract class Policy { return dynamicMatch; } - //validation for numeric - protected boolean isNumeric(String str){ - for (char c : str.toCharArray()){ - if (!Character.isDigit(c)) return false; - } - return true; - } - - // Validation for json. - protected static boolean isJSONValid(String data) { - JsonReader jsonReader = null; - try { - new JSONObject(data); - InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); - jsonReader = Json.createReader(stream); - LOGGER.info("Json Value is: " + jsonReader.read().toString() ); - } catch (Exception e) { - LOGGER.error("Exception Occured while reading json"+e); - return false; - }finally{ - if(jsonReader != null){ - jsonReader.close(); - } - } - return true; - } - // the Policy Name as Unique One throws error @SuppressWarnings("static-access") protected Path getNextFilename(Path parent, String policyType, String polcyFileName, Integer version) { diff --git a/ONAP-PAP-REST/src/main/resources/Decision_GuardBLPolicyTemplate.xml b/ONAP-PAP-REST/src/main/resources/Decision_GuardBLPolicyTemplate.xml index 19e567ddd..a8e1b9480 100644 --- a/ONAP-PAP-REST/src/main/resources/Decision_GuardBLPolicyTemplate.xml +++ b/ONAP-PAP-REST/src/main/resources/Decision_GuardBLPolicyTemplate.xml @@ -15,11 +15,11 @@ <AttributeDesignator Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" AttributeId="ONAPName" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> - <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${actor}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">(?i)${actor}</AttributeValue> <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="actor" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> - <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${recipe}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">(?i)${recipe}</AttributeValue> <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="recipe" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> diff --git a/ONAP-PAP-REST/src/main/resources/Decision_GuardPolicyTemplate.xml b/ONAP-PAP-REST/src/main/resources/Decision_GuardPolicyTemplate.xml index ed5b27b7d..6dee0cb97 100644 --- a/ONAP-PAP-REST/src/main/resources/Decision_GuardPolicyTemplate.xml +++ b/ONAP-PAP-REST/src/main/resources/Decision_GuardPolicyTemplate.xml @@ -15,11 +15,11 @@ <AttributeDesignator Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" AttributeId="ONAPName" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> - <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${actor}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">(?i)${actor}</AttributeValue> <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="actor" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> - <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${recipe}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">(?i)${recipe}</AttributeValue> <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="recipe" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> </Match> <Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-regexp-match"> |