PolicyValidation { public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException { try { boolean valid = true; StringBuilder responseString = new StringBuilder(); ObjectMapper mapper = new ObjectMapper(); if (policyData.getPolicyName() != null) { String policyNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getPolicyName()); if (!policyNameValidate.contains(SUCCESS)) { responseString.append("<b>PolicyName</b>:<i>" + policyNameValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>PolicyName</b>: PolicyName Should not be empty" + HTML_ITALICS_LNBREAK); valid = false; } if (policyData.getPolicyDescription() != null) { String descriptionValidate = PolicyUtils.descriptionValidator(policyData.getPolicyDescription()); if (!descriptionValidate.contains(SUCCESS)) { responseString.append("<b>Description</b>:<i>" + descriptionValidate + HTML_ITALICS_LNBREAK); valid = false; } } if (!"API".equals(policyData.getApiflag()) && policyData.getAttributes() != null && !policyData.getAttributes().isEmpty()) { for (Object attribute : policyData.getAttributes()) { if (attribute instanceof LinkedHashMap<?, ?>) { String attValue = null; String key = null; if (((LinkedHashMap<?, ?>) attribute).get("key") != null) { key = ((LinkedHashMap<?, ?>) attribute).get("key").toString(); if (!PolicyUtils.policySpecialCharWithDashValidator(key).contains(SUCCESS)) { responseString.append("<b>Attributes or Component Attributes</b>:<i>" + attValue + SPACESINVALIDCHARS); valid = false; } } else { if (CONFIG_POLICY.equals(policyData.getPolicyType())) { if ("Base".equals(policyData.getConfigPolicyType())) { responseString.append( "<b>Attributes</b>:<i> has one missing Attribute key</i><br>"); } if (BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())) { responseString.append( "<b>Rule Attributes</b>:<i> has one missing Attribute key</i><br>"); } } else { responseString.append(MISSING_COMPONENT_ATTRIBUTE_KEY); } valid = false; } if (((LinkedHashMap<?, ?>) attribute).get(VALUE) != null) { attValue = ((LinkedHashMap<?, ?>) attribute).get(VALUE).toString(); if (!PolicyUtils.policySpecialCharWithDashValidator(attValue).contains(SUCCESS)) { if (CONFIG_POLICY.equals(policyData.getPolicyType())) { if ("Base".equals(policyData.getConfigPolicyType())) { responseString.append("<b>Attributes</b>:<i>" + attValue + SPACESINVALIDCHARS); } if (BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())) { responseString.append( "<b>Rule Attributes</b>:<i>" + attValue + SPACESINVALIDCHARS); } } else { responseString.append( "<b>Component Attributes</b>:<i>" + attValue + SPACESINVALIDCHARS); } valid = false; } } else { if (CONFIG_POLICY.equals(policyData.getPolicyType())) { if ("Base".equals(policyData.getConfigPolicyType())) { responseString.append( "<b>Attributes</b>:<i> has one missing Attribute value</i><br>"); } if (BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())) { responseString.append(MISSING_ATTRIBUTE_VALUE); } } else { responseString.append(MISSING_COMPONENT_ATTRIBUTE_VALUE); } valid = false; } } } } if (!"API".equals(policyData.getApiflag()) && policyData.getSettings() != null && !policyData.getSettings().isEmpty()) { for (Object attribute : policyData.getSettings()) { if (attribute instanceof LinkedHashMap<?, ?>) { String value = null; if (((LinkedHashMap<?, ?>) attribute).get("key") == null) { responseString.append( "<b>Settings Attributes</b>:<i> has one missing Attribute key</i><br>"); valid = false; } if (((LinkedHashMap<?, ?>) attribute).get(VALUE) != null) { value = ((LinkedHashMap<?, ?>) attribute).get(VALUE).toString(); if (!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)) { responseString.append("<b>Settings Attributes</b>:<i>" + value + SPACESINVALIDCHARS); valid = false; } } else { responseString.append( "<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>"); valid = false; } } } } if (!"API".equals(policyData.getApiflag()) && policyData.getRuleAlgorithmschoices() != null && !policyData.getRuleAlgorithmschoices().isEmpty()) { for (Object attribute : policyData.getRuleAlgorithmschoices()) { if (attribute instanceof LinkedHashMap<?, ?>) { String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString(); if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1") == null) { responseString.append(RULE_ALGORITHMS + label + " : Field 1 value is not selected</i><br>"); valid = false; } if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo") == null) { responseString.append(RULE_ALGORITHMS + label + " : Field 2 value is not selected</i><br>"); valid = false; } if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") != null) { String value = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") .toString(); if (!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)) { responseString.append(RULE_ALGORITHMS + label + " : Field 3 value has special characters</i><br>"); valid = false; } } else { responseString.append(RULE_ALGORITHMS + label + " : Field 3 value is empty</i><br>"); valid = false; } } } } if (CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())) { if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) || MICROSERVICES.equals(policyData.getConfigPolicyType()) || OPTIMIZATION.equals(policyData.getConfigPolicyType())) { if (!Strings.isNullOrEmpty(policyData.getOnapName())) { String onapNameValidate = PolicyUtils .policySpecialCharWithDashValidator(policyData.getOnapName()); if (!onapNameValidate.contains(SUCCESS)) { responseString.append("<b>OnapName</b>:<i>" + onapNameValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>Onap Name</b>: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK); valid = false; } } if (!Strings.isNullOrEmpty(policyData.getRiskType())) { String riskTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getRiskType()); if (!riskTypeValidate.contains(SUCCESS)) { responseString.append("<b>RiskType</b>:<i>" + riskTypeValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>RiskType</b>: Risk Type Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } if (!Strings.isNullOrEmpty(policyData.getRiskLevel())) { String validateRiskLevel = PolicyUtils.policySpecialCharValidator(policyData.getRiskLevel()); if (!validateRiskLevel.contains(SUCCESS)) { responseString.append("<b>RiskLevel</b>:<i>" + validateRiskLevel + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>RiskLevel</b>: Risk Level Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } if (!Strings.isNullOrEmpty(policyData.getGuard())) { String validateGuard = PolicyUtils.policySpecialCharValidator(policyData.getGuard()); if (!validateGuard.contains(SUCCESS)) { responseString.append("<b>Guard</b>:<i>" + validateGuard + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>Guard</b>: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } if ("Base".equalsIgnoreCase(policyData.getConfigPolicyType())) { if (!Strings.isNullOrEmpty(policyData.getConfigName())) { String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); if (!configNameValidate.contains(SUCCESS)) { responseString.append("ConfigName:" + configNameValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("Config Name: Config Name Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } if (!Strings.isNullOrEmpty(policyData.getConfigType())) { String configTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigType()); if (!configTypeValidate.contains(SUCCESS)) { responseString.append("ConfigType:" + configTypeValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("Config Type: Config Type Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } if (!Strings.isNullOrEmpty(policyData.getConfigBodyData())) { String configBodyData = policyData.getConfigBodyData(); String configType = policyData.getConfigType(); if (configType != null) { if ("JSON".equals(configType)) { if (!PolicyUtils.isJSONValid(configBodyData)) { responseString.append( "Config Body: JSON Content is not valid" + HTML_ITALICS_LNBREAK); valid = false; } } else if ("XML".equals(configType)) { if (!PolicyUtils.isXMLValid(configBodyData)) { responseString.append("Config Body: XML Content data is not valid" + HTML_ITALICS_LNBREAK); valid = false; } } else if ("PROPERTIES".equals(configType)) { if (!PolicyUtils.isPropValid(configBodyData)) { responseString.append( "Config Body: Property data is not valid" + HTML_ITALICS_LNBREAK); valid = false; } } } } else { responseString.append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } } if (FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())) { if (policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()) { String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); if (!configNameValidate.contains(SUCCESS)) { responseString.append("<b>ConfigName</b>:<i>" + configNameValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>Config Name</b>:<i> Config Name is required" + HTML_ITALICS_LNBREAK); valid = false; } if (policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()) { responseString.append( "<b>Security Zone</b>:<i> Security Zone is required" + HTML_ITALICS_LNBREAK); valid = false; } } if (BRMSPARAM.equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())) { responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required" + HTML_ITALICS_LNBREAK); valid = false; } if (BRMSRAW.equalsIgnoreCase(policyData.getConfigPolicyType())) { if (policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()) { String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData()); if (message.contains("[ERR")) { responseString.append("<b>Raw Rule Validate</b>:<i>Raw Rule has error" + message + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required" + HTML_ITALICS_LNBREAK); valid = false; } } if (CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())) { try { if (Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName") .toString())) { responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK); valid = false; } } catch (Exception e) { LOGGER.error("ERROR in ClosedLoop_PM PolicyName", e); responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK); valid = false; } if (policyData.getJsonBody() != null) { ClosedLoopPMBody pmBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopPMBody.class); if (pmBody.getEmailAddress() != null) { String result = emailValidation(pmBody.getEmailAddress(), responseString.toString()); if (result != SUCCESS) { responseString.append(result + HTML_ITALICS_LNBREAK); valid = false; } } if (!pmBody.isGamma() && !pmBody.isMcr() && !pmBody.isTrinity() && !pmBody.isvDNS() && !pmBody.isvUSP()) { responseString.append(SELECT_AT_LEAST_ONE_D2_VIRTUALIZED_SERVICES + HTML_ITALICS_LNBREAK); valid = false; } if (pmBody.getGeoLink() != null && !pmBody.getGeoLink().isEmpty()) { String result = PolicyUtils.policySpecialCharValidator(pmBody.getGeoLink()); if (!result.contains(SUCCESS)) { responseString.append("<b>GeoLink</b>:<i>" + result + HTML_ITALICS_LNBREAK); valid = false; } } if (pmBody.getAttributes() != null && !pmBody.getAttributes().isEmpty()) { for (Entry<String, String> entry : pmBody.getAttributes().entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (!key.contains("Message")) { String attributeValidate = PolicyUtils.policySpecialCharValidator(value); if (!attributeValidate.contains(SUCCESS)) { responseString.append("<b>Attributes</b>:<i>" + key + " : value has spaces or invalid characters" + HTML_ITALICS_LNBREAK); valid = false; } } } } } else { responseString.append( "<b>D2/Virtualized Services</b>:<i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK); valid = false; } } if (CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())) { if (policyData.getJsonBody() != null) { if ("API".equals(policyData.getApiflag())) { JSONObject json = new JSONObject(policyData.getJsonBody()); if (!json.isNull("conditions")) { String apiCondition = (String) json.get("conditions"); if (Strings.isNullOrEmpty(apiCondition)) { responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK); return responseString; } } else { responseString.append("<b>Conditions</b>:" + " <i>There were no conditions provided in configBody json" + HTML_ITALICS_LNBREAK); return responseString; } } else { if (policyData.getTrapDatas().getTrap1() != null) { if (policyData.getClearTimeOut() == null) { responseString.append("<b>Trigger Clear TimeOut</b>: " + "<i>Trigger Clear TimeOut is required when at " + "least One Trigger Signature is enabled</i><br>"); valid = false; } if (policyData.getTrapMaxAge() == null) { responseString.append("<b>Trap Max Age</b>: <i>Trap Max Age is required when at " + "least One Trigger Signature is enabled</i><br>"); valid = false; } } if (policyData.getFaultDatas().getTrap1() != null && policyData.getVerificationclearTimeOut() == null) { responseString.append( "<b>Fault Clear TimeOut</b>: <i>Fault Clear TimeOut is required when at" + "least One Fault Signature is enabled</i><br>"); valid = false; } } ClosedLoopFaultBody faultBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopFaultBody.class); if (faultBody.getEmailAddress() != null && !faultBody.getEmailAddress().isEmpty()) { String result = emailValidation(faultBody.getEmailAddress(), responseString.toString()); if (!SUCCESS.equals(result)) { responseString.append(result + HTML_ITALICS_LNBREAK); valid = false; } } if (!(faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP())) { responseString.append(SELECT_AT_LEAST_ONE_D2_VIRTUALIZED_SERVICES + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getActions() == null || faultBody.getActions().isEmpty()) { responseString.append( "<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getClosedLoopPolicyStatus() == null || faultBody.getClosedLoopPolicyStatus().isEmpty()) { responseString.append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getConditions() == null) { responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getGeoLink() != null && !faultBody.getGeoLink().isEmpty()) { String result = PolicyUtils.policySpecialCharWithSpaceValidator(faultBody.getGeoLink()); if (!result.contains(SUCCESS)) { responseString.append("<b>GeoLink</b>:<i>" + result + HTML_ITALICS_LNBREAK); valid = false; } } if (faultBody.getAgingWindow() == 0) { responseString.append( "<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getTimeInterval() == 0) { responseString.append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getRetrys() == 0) { responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getTimeOutvPRO() == 0) { responseString.append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getTimeOutRuby() == 0) { responseString.append( "<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK); valid = false; } if (faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()) { responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append( "<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK); responseString.append( "<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK); responseString.append( "<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK); responseString.append( "<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK); responseString.append( "<b>Conditions</b>: <i>Select Atleast one Condition" + HTML_ITALICS_LNBREAK); responseString.append("<b>PEP Name</b>: <i>PEP Name is required" + HTML_ITALICS_LNBREAK); responseString.append("<b>PEP Action</b>: <i>PEP Action is required" + HTML_ITALICS_LNBREAK); responseString.append( "<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK); responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK); responseString.append( "<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK); responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK); responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK); valid = false; } } if (MICROSERVICES.equals(policyData.getConfigPolicyType())) { boolean tmpValid = validateMsModel(policyData, responseString); if (!tmpValid) { valid = false; } } if (OPTIMIZATION.equals(policyData.getConfigPolicyType())) { boolean tmpValid = validateOptimization(policyData, responseString); if (!tmpValid) { valid = false; } } } if ((DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType())) || (DECISION_POLICY_MS.equalsIgnoreCase(policyData.getPolicyType()))) { if (!RAW.equalsIgnoreCase(policyData.getRuleProvider())) { if (!Strings.isNullOrEmpty(policyData.getOnapName())) { String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName()); if (!onapNameValidate.contains(SUCCESS)) { responseString.append("OnapName:" + onapNameValidate + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("Onap Name: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK); valid = false; } } if (RAW.equalsIgnoreCase(policyData.getRuleProvider())) { Object policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(StringEscapeUtils .unescapeXml(policyData.getRawXacmlPolicy()).getBytes(StandardCharsets.UTF_8))); if (!(policy instanceof PolicySetType || policy instanceof PolicyType)) { responseString.append("Raw XACML: The XACML Content is not valid" + HTML_ITALICS_LNBREAK); valid = false; } } if (DECISION_MS_MODEL.equals(policyData.getRuleProvider())) { LOGGER.info("Validating Decision MS Policy - "); boolean tmpValid = validateMsModel(policyData, responseString); if (!tmpValid) { valid = false; } } if ("Rainy_Day".equals(policyData.getRuleProvider())) { if (policyData.getRainyday() == null) { responseString.append("<b> Rainy Day Parameters are Required </b><br>"); valid = false; } else { if (Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())) { responseString.append("Rainy Day <b>Service Type</b> is Required<br>"); valid = false; } if (Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())) { responseString.append("Rainy Day <b>VNF Type</b> is Required<br>"); valid = false; } if (Strings.isNullOrEmpty(policyData.getRainyday().getBbid())) { responseString.append("Rainy Day <b>Building Block ID</b> is Required<br>"); valid = false; } if (Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())) { responseString.append("Rainy Day <b>Work Step</b> is Required<br>"); valid = false; } if (!policyData.getRainyday().getTreatmentTableChoices().isEmpty() && policyData.getRainyday().getTreatmentTableChoices() != null) { for (Object treatmentMap : policyData.getRainyday().getTreatmentTableChoices()) { String errorCode = null; String treatment = null; if (treatmentMap instanceof LinkedHashMap<?, ?>) { if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("errorcode")) { errorCode = ((LinkedHashMap<?, ?>) treatmentMap).get("errorcode").toString(); } if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("treatment")) { treatment = ((LinkedHashMap<?, ?>) treatmentMap).get("treatment").toString(); } } if (Strings.isNullOrEmpty(errorCode) && Strings.isNullOrEmpty(treatment)) { responseString.append("Rainy Day <b>Error Code</b> and " + "<b>Desired Treatment</b> cannot be empty<br>"); valid = false; break; } if (Strings.isNullOrEmpty(errorCode)) { responseString.append("Rainy Day <b>Error Code</b> is Required " + "for each Desired Treatment<br>"); valid = false; break; } if (Strings.isNullOrEmpty(treatment)) { responseString.append("Rainy Day <b>Desired Treatment" + "</b> is Required for each Error Code<br>"); valid = false; break; } } } else { responseString.append("Rainy Day <b>Desired Automated Treatments</b> are Required<br>"); valid = false; } } } if ("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider()) || "GUARD_MIN_MAX".equals(policyData.getRuleProvider())) { if (policyData.getYamlparams() == null) { responseString.append("<b> Guard Params are Required </b>" + HTML_ITALICS_LNBREAK); valid = false; } else { if (Strings.isNullOrEmpty(policyData.getYamlparams().getActor())) { responseString.append("Guard Params <b>Actor</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())) { responseString.append("Guard Params <b>Recipe</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())) { responseString.append("Guard Params <b>Guard Active Start</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())) { responseString.append( "Guard Params <b>Guard Active End</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } if ("GUARD_YAML".equals(policyData.getRuleProvider())) { if (Strings.isNullOrEmpty(policyData.getYamlparams().getLimit())) { responseString.append(" Guard Params <b>Limit</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())) { responseString.append( " Guard Params <b>Limit</b> Should be Integer " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())) { responseString.append( "Guard Params <b>Time Window</b> is Required" + HTML_ITALICS_LNBREAK); valid = false; } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())) { responseString.append(" Guard Params <b>Time Window</b> Should be Integer " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())) { responseString.append( "Guard Params <b>Time Units</b> is Required" + HTML_ITALICS_LNBREAK); valid = false; } } else if ("GUARD_MIN_MAX".equals(policyData.getRuleProvider())) { if (Strings.isNullOrEmpty(policyData.getYamlparams().getMin())) { responseString.append(" Guard Params <b>Min</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMin())) { responseString.append( " Guard Params <b>Min</b> Should be Integer " + HTML_ITALICS_LNBREAK); valid = false; } if (Strings.isNullOrEmpty(policyData.getYamlparams().getMax())) { responseString.append(" Guard Params <b>Max</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMax())) { responseString.append( " Guard Params <b>Max</b> Should be Integer " + HTML_ITALICS_LNBREAK); valid = false; } } else if ("GUARD_BL_YAML".equals(policyData.getRuleProvider()) && "Use Manual Entry".equals(policyData.getBlackListEntryType())) { if (policyData.getYamlparams().getBlackList() == null || policyData.getYamlparams().getBlackList().isEmpty()) { responseString.append( " Guard Params <b>BlackList</b> is Required " + HTML_ITALICS_LNBREAK); valid = false; } else { for (String blackList : policyData.getYamlparams().getBlackList()) { if (blackList == null || !(SUCCESS .equals(PolicyUtils.policySpecialCharValidator(blackList)))) { responseString.append(" Guard Params <b>BlackList</b> Should be valid String" + HTML_ITALICS_LNBREAK); valid = false; break; } } } } } } } if (ACTION_POLICY.equalsIgnoreCase(policyData.getPolicyType())) { if (!Strings.isNullOrEmpty(policyData.getActionPerformer())) { String actionPerformer = PolicyUtils.policySpecialCharValidator(policyData.getActionPerformer()); if (!actionPerformer.contains(SUCCESS)) { responseString.append("<b>ActionPerformer</b>:<i>" + actionPerformer + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>ActionPerformer</b>:<i> ActionPerformer Should not be empty" + HTML_ITALICS_LNBREAK); valid = false; } if (!Strings.isNullOrEmpty(policyData.getActionAttributeValue())) { String actionAttribute = PolicyUtils .policySpecialCharValidator(policyData.getActionAttributeValue()); if (!actionAttribute.contains(SUCCESS)) { responseString.append("<b>ActionAttribute</b>:<i>" + actionAttribute + HTML_ITALICS_LNBREAK); valid = false; } } else { responseString.append("<b>ActionAttribute</b>:<i> ActionAttribute Should not be empty" + HTML_ITALICS_LNBREAK); valid = false; } } if (CONFIG_POLICY.equals(policyData.getPolicyType())) { String value = ""; if (valid) { if (commonClassDao != null) { List<Object> spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType()); if (!spData.isEmpty()) { SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) spData.get(0); value = "<b>Message</b>:<i>" + safePolicyWarningData.getMessage() + "</i>"; } } responseString.append(SUCCESS + "@#" + value); } } else { if (valid) { responseString.append(SUCCESS); } } return responseString; } catch (Exception e) { LOGGER.error("Exception Occured during Policy Validation" + e); return null; } } @Autowired  PolicyValidation(CommonClassDao commonClassDao);  PolicyValidation(); StringBuilder validatePolicy(PolicyRestAdapter policyData);  }
@Test public void testPolicyConfigBaseValidation() 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.setPolicyType("Config"); policyData.setConfigPolicyType("Base"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Onap Name Should not be empty"); policyData.setOnapName("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>OnapName</b>:<i>The Value in Required Field"); policyData.setOnapName("AValidOnapName"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Risk Level Should not be Empty"); policyData.setRiskType("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>RiskType</b>:<i>The Value in Required Field"); policyData.setRiskType("AValidRiskType"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Risk Level Should not be Empty"); policyData.setRiskLevel("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>RiskLevel</b>:<i>The Value in Required Field"); policyData.setRiskLevel("AValidRiskLevel"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Guard Value Should not be Empty"); policyData.setGuard("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Guard</b>:<i>The Value in Required Field"); policyData.setGuard("AValidGuard"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Name Should not be Empty"); policyData.setConfigName("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ConfigName:The Value in Required Field"); policyData.setConfigName("AValidConfigName"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Type Should not be Empty"); policyData.setConfigType("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ConfigType:The Value in Required Field"); policyData.setConfigType("AValidConfigType"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Body Should not be Empty"); policyData.setConfigBodyData(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Body Should not be Empty"); policyData.setConfigBodyData("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setConfigType(null); policyData.setConfigBodyData("ValidConfigBodyData"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Type Should not be Empty"); policyData.setConfigType("JSON"); policyData.setConfigBodyData("{"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Body: JSON Content is not valid"); policyData.setConfigBodyData("ValidConfigBodyData"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setConfigType("XML"); policyData.setConfigBodyData("{"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Body: XML Content data is not valid"); policyData.setConfigBodyData("<tag>value</tag>"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setConfigType("PROPERTIES"); policyData.setConfigBodyData("{"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Body: Property data is not valid"); policyData.setConfigBodyData("propertyName=PropertyValue"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); }
@Test public void testPolicyConfigFirewallValidation() 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.setOnapName("AValidOnapName"); policyData.setRiskType("AValidRiskType"); policyData.setRiskLevel("AValidRiskLevel"); policyData.setGuard("AValidGuard"); assertEquals("success", responseString); policyData.setPolicyType("Config"); policyData.setConfigPolicyType("Firewall Config"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Name is required"); policyData.setConfigName(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Config Name is required"); policyData.setConfigName("%%%$$$$"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>ConfigName</b>:<i>The Value in Required Field"); policyData.setConfigName("AValidConfigName"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Security Zone is required"); policyData.setSecurityZone(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Security Zone is required"); policyData.setSecurityZone("AValidSeurityZone"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); }
@Test public void testPolicyConfigBRMSValidation() 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.setOnapName("AValidOnapName"); policyData.setRiskType("AValidRiskType"); policyData.setRiskLevel("AValidRiskLevel"); policyData.setGuard("AValidGuard"); assertEquals("success", responseString); policyData.setPolicyType("Config"); policyData.setConfigPolicyType("BRMS_Param"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("BRMS Template is required"); policyData.setRuleName(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("BRMS Template is required"); policyData.setRuleName("AValidRuleName"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setRuleName(null); policyData.setConfigPolicyType("BRMS_Raw"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Raw Rule is required"); policyData.setConfigBodyData(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Raw Rule is required"); policyData.setConfigBodyData("InvalidConfigBodyData"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("expecting one of the following tokens"); policyData.setConfigBodyData("import org.onap.policy.test.DummyTestSomething;"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); }
@Test public void testPolicyConfigCloseLoopPmValidation() 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.setOnapName("AValidOnapName"); policyData.setRiskType("AValidRiskType"); policyData.setRiskLevel("AValidRiskLevel"); policyData.setGuard("AValidGuard"); assertEquals("success", responseString); policyData.setPolicyType("Config"); policyData.setConfigPolicyType("ClosedLoop_PM"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ServiceType PolicyName is required"); Map<String, String> serviceTypePolicyName = null; policyData.setServiceTypePolicyName(serviceTypePolicyName); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ServiceType PolicyName is required"); serviceTypePolicyName = new LinkedHashMap<>(); policyData.setServiceTypePolicyName(serviceTypePolicyName); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ServiceType PolicyName is required"); serviceTypePolicyName.put("AKey", "AValue"); policyData.setServiceTypePolicyName(serviceTypePolicyName); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ServiceType PolicyName is required"); serviceTypePolicyName.clear(); serviceTypePolicyName.put("serviceTypePolicyName", ""); policyData.setServiceTypePolicyName(serviceTypePolicyName); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("ServiceType PolicyName is required"); serviceTypePolicyName.put("serviceTypePolicyName", "AValidserviceTypePolicyName"); policyData.setServiceTypePolicyName(serviceTypePolicyName); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody(""); assertNull(validation.validatePolicy(policyData)); policyData.setJsonBody("InvalidConfigBodyData"); assertNull(validation.validatePolicy(policyData)); policyData.setJsonBody("{}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"gamma\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"gamma\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"mcr\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"mcr\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"trinity\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"vDNS\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"vDNS\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"vUSP\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"vUSP\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Email Address is not Valid"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"%%%$$$\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Email Address is not Valid"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"dorothy@emealdcity.oz\"}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"geoLink\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"geoLink\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"geoLink\": \"$$$%%%\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("The Value in Required Field"); policyData.setJsonBody("{\"trinity\": true, \"geoLink\": \"AValidGeoLink\"}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"attributes\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"attributes\": {}}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"attributes\": {\"an0\":\"av0\"}}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setJsonBody("{\"trinity\": true, \"attributes\": {\"an0\":\"$$$%%%\"}}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("value has spaces or invalid characters"); policyData.setJsonBody("{\"trinity\": true, \"attributes\": {\"Message\":\"$$$%%%\"}}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); }
@Test public void testPolicyConfigCloseLoopFaultValidation() 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.setOnapName("AValidOnapName"); policyData.setRiskType("AValidRiskType"); policyData.setRiskLevel("AValidRiskLevel"); policyData.setGuard("AValidGuard"); assertEquals("success", responseString); policyData.setPolicyType("Config"); policyData.setConfigPolicyType("ClosedLoop_Fault"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody(""); assertNull(validation.validatePolicy(policyData)); policyData.setJsonBody("InvalidConfigBodyData"); assertNull(validation.validatePolicy(policyData)); policyData.setJsonBody("{}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("There were no conditions provided in configBody json"); policyData.setJsonBody("{\"conditions\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("There were no conditions provided in configBody json"); policyData.setJsonBody("{\"conditions\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select At least one Condition"); policyData.setJsonBody("{\"conditions\": \"AValidCondition\"}"); assertNull(validation.validatePolicy(policyData)); policyData.setApiflag("NOAPI"); policyData.setJsonBody(""); assertNull(validation.validatePolicy(policyData)); policyData.setJsonBody("InvalidConfigBodyData"); assertNull(validation.validatePolicy(policyData)); ClosedLoopFaultTrapDatas trapDatas = new ClosedLoopFaultTrapDatas(); policyData.setTrapDatas(trapDatas); ClosedLoopFaultTrapDatas faultDatas = new ClosedLoopFaultTrapDatas(); policyData.setFaultDatas(faultDatas); policyData.setJsonBody("{}"); assertThat(responseString).contains("Select At least one Condition"); List<Object> trap1 = new ArrayList<>(); trapDatas.setTrap1(trap1); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Trigger Clear TimeOut is required"); policyData.setClearTimeOut("AValidClearTimeout"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Trap Max Age is required"); policyData.setTrapMaxAge("AValidTrapMaxAge"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); trapDatas.setTrap1(null); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); faultDatas.setTrap1(null); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); faultDatas.setTrap1(trap1); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Fault Clear TimeOut is required when"); policyData.setVerificationclearTimeOut("AValidVerificationClearTimeout"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"gamma\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"gamma\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"mcr\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"mcr\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"trinity\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"vDNS\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"vDNS\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"vUSP\": false}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select at least one D2"); policyData.setJsonBody("{\"vUSP\": true}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"%%%$$$\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Email Address is not Valid"); policyData.setJsonBody("{\"trinity\": true, \"emailAddress\": \"dorothy@emealdcity.oz\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("vPRO Actions is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": \"$$$%%%\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Policy Status is required"); assertThat(responseString).contains("Vnf Type is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": \"ValidActions\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Policy Status is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": \"ValidActions\", \"closedLoopPolicyStatus\": null}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Policy Status is required"); policyData.setJsonBody("{\"trinity\": true, \"actions\": \"ValidActions\", \"closedLoopPolicyStatus\": \"\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Policy Status is required"); policyData.setJsonBody( "{\"trinity\": true, \"actions\": \"ValidActions\", \"closedLoopPolicyStatus\": \"$$$%%%\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select At least one Condition"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select At least one Condition"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": null" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Select At least one Condition"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Aging Window is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"geoLink\": null" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Aging Window is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"geoLink\": \"\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Aging Window is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"geoLink\": \"%%%$$$\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("GeoLink</b>:<i>The Value in Required Field"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"geoLink\": \"AValidGeoLink\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Aging Window is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Time Interval is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Number of Retries is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("APP-C Timeout is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1," + "\"timeOutvPRO\": -1" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("TimeOutRuby is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1," + "\"timeOutvPRO\": -1," + "\"timeOutRuby\": -1" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Vnf Type is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1," + "\"timeOutvPRO\": -1," + "\"timeOutRuby\": -1," + "\"vnfType\": null" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Vnf Type is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1," + "\"timeOutvPRO\": -1," + "\"timeOutRuby\": -1," + "\"vnfType\": \"\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Vnf Type is required"); policyData.setJsonBody("{" + "\"trinity\": true," + "\"actions\": \"ValidActions\"," + "\"closedLoopPolicyStatus\": \"AValidStatus\"," + "\"conditions\": \"SEND\"," + "\"agingWindow\": -1," + "\"timeInterval\": -1," + "\"retrys\": -1," + "\"timeOutvPRO\": -1," + "\"timeOutRuby\": -1," + "\"vnfType\": \"AValid VNF Type\"" + "}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); }
@Test public void testPolicyConfigMicroServiceValidation() throws IOException { PolicyValidation validation = new PolicyValidation(commonClassDao); PolicyRestAdapter policyData = new PolicyRestAdapter(); policyData.setPolicyName("ALegalPolicyName"); policyData.setPolicyDescription("A Valid Description"); String responseString = validation.validatePolicy(policyData).toString(); assertEquals("success", responseString); policyData.setApiflag("NOAPI"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success", responseString); policyData.setOnapName("AValidOnapName"); policyData.setRiskType("AValidRiskType"); policyData.setRiskLevel("AValidRiskLevel"); policyData.setGuard("AValidGuard"); assertEquals("success", responseString); policyData.setPolicyType("Config"); policyData.setConfigPolicyType("Micro Service"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Micro Service Model is required"); policyData.setServiceType(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Micro Service Model is required"); policyData.setServiceType("ServiceType"); assertNull(validation.validatePolicy(policyData)); policyData.setPolicyJSON(""); assertNull(validation.validatePolicy(policyData)); String msJsonContentString0 = TextFileUtils .getTextFileAsString("src/test/resources/policies/MicroServicePolicyContent0.json"); policyData.setPolicyJSON(new ObjectMapper().readTree(msJsonContentString0)); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Micro Service Version is required"); String msJsonContentString1 = TextFileUtils .getTextFileAsString("src/test/resources/policies/MicroServicePolicyContent1.json"); policyData.setPolicyJSON(new ObjectMapper().readTree(msJsonContentString1)); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Micro Service Version is required"); policyData.setServiceType("ServiceType-vServiceVersion"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("The model name, ServiceType of version, ServiceVersion was not found"); policyData.setServiceType("ServiceType"); policyData.setRuleProvider("MicroService_Model"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).doesNotContain("Priority is required"); policyData.setVersion("ServiceVersion"); Mockito.when(commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", "ServiceType:ServiceVersion")).thenReturn(null); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("The model name, ServiceType of version, ServiceVersion was not found"); List<Object> msModelsList = new ArrayList<>(); MicroServiceModels msModels = new MicroServiceModels(); msModelsList.add(msModels); Mockito.when(commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", "ServiceType:ServiceVersion")).thenReturn(msModelsList); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setAnnotation("SomeAnntation"); assertNull(validation.validatePolicy(policyData)); msModels.setAnnotation("annotation0Key=annotation0Value"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setAnnotation("annotation0Key=range::10-100"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("annotation0Key is required for the MicroService model"); msModels.setAnnotation("police-instance-name=range::10-100"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Model Range:police-instance-name must be between 10 - 100"); msModels.setAnnotation("police-instance-range=range::12-100"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Model Range:police-instance-range must be between 12 - 100"); msModels.setAnnotation("police-instance-range=range::0-10"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("Model Range:police-instance-range must be between 0 - 10"); msModels.setAnnotation("police-instance-range=range::10-100"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setAnnotation(null); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setRuleProvider("Not_MicroService_Model"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> location is required for this model"); policyData.setLocation("AValidLocation"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> configName is required for this model"); policyData.setConfigName("AValidConfigname"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> uuid is required for this model"); policyData.setUuid("AValidUUID"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> policyScope is required for this model"); policyData.setPolicyScope("AValidPolicyScope"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Priority</b>:<i> Priority is required"); policyData.setPriority("AValidPriority"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); policyData.setApiflag("API"); assertNull(validation.validatePolicy(policyData)); msModels.setSubAttributes("{}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setSubAttributes("{\"subAttrName0\": \"subAttrValue0\"}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setSubAttributes("{\"subAttrName0\": 10}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setSubAttributes("{\"subAttrName0\": {}}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setSubAttributes("{\"subAttrName0\": []}"); responseString = validation.validatePolicy(policyData).toString(); assertEquals("success@#", responseString); msModels.setSubAttributes("{\"subAttrName0\": \"subAttrValue0-required-true\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setSubAttributes("{\"subAttrName0\": \":subAttrValue0-required-true\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setSubAttributes("{\"subAttrName0\": \"UnknownType:subAttrValue0-required-true\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setSubAttributes("{\"subAttrName0\": \"string:subAttrValue0-required-true\"}"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setRefAttributes("refAttrName0="); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setRefAttributes("refAttrName0=refAttrValue0"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setRefAttributes("refAttrName0=refAttrValue0-required-true"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setAttributes(""); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setAttributes("modelAttrName0=modelAttrValue0"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required"); msModels.setRefAttributes("refAttrName0=refAttrValue0-required-true,"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).contains("refAttrName0 is required"); msModels.setRefAttributes("police-instance-range=refAttrValue0-required-true,"); responseString = validation.validatePolicy(policyData).toString(); assertThat(responseString).doesNotContain("police-instance-range is required"); }
@Test public void microServicePolicyTests() throws Exception { PolicyParameters policyParameters = new PolicyParameters(); policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); policyParameters.setPolicyName("Test.junitPolicy"); policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example"); policyParameters.setOnapName("DCAE"); policyParameters.setPriority("1"); String msJsonString = TextFileUtils.getTextFileAsString("src/test/resources/policies/MicroServicePolicy.json"); policyParameters.setConfigBody(msJsonString); policyParameters.setRequestID(UUID.randomUUID()); SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy"); Date date = dateformat3.parse("15/10/2016"); policyParameters.setTtlDate(date); policyParameters.setGuard(true); policyParameters.setRiskLevel("5"); policyParameters.setRiskType("TEST"); policyParameters.setRequestID(UUID.randomUUID()); PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper(); PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters); PolicyValidation validation = new PolicyValidation(); String responseString = validation.validatePolicy(policyData).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 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"); }