From e0385921034ae9ce860038ea65d2d13259f7cc4c Mon Sep 17 00:00:00 2001 From: "Tej, Tarun" Date: Fri, 15 Sep 2017 00:52:13 -0400 Subject: 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 --- .../pap/xacml/rest/components/ConfigPolicy.java | 98 +++------------------- .../pap/xacml/rest/components/DecisionPolicy.java | 5 +- .../policy/pap/xacml/rest/components/Policy.java | 33 -------- 3 files changed, 17 insertions(+), 119 deletions(-) (limited to 'ONAP-PAP-REST/src/main/java') 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 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 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) { -- cgit 1.2.3-korg