FirewallConfigPolicy extends Policy { @Override public boolean prepareToSave() throws PAPException { if (isPreparedToSave()) { return true; } int version = 0; String policyID = policyAdapter.getPolicyID(); version = policyAdapter.getHighestVersion(); if ("Config".equals(policyAdapter.getPolicyType())) { PolicyType policyConfig = new PolicyType(); policyConfig.setVersion(Integer.toString(version)); policyConfig.setPolicyId(policyID); policyConfig.setTarget(new TargetType()); policyAdapter.setData(policyConfig); } policyName = policyAdapter.getNewFileName(); String scope = policyName.substring(0, policyName.indexOf('.')); String dbPolicyName = policyName.substring(policyName.indexOf('.') + 1).replace(".xml", ""); int oldversion = Integer.parseInt(dbPolicyName.substring(dbPolicyName.lastIndexOf('.') + 1)); dbPolicyName = dbPolicyName.substring(0, dbPolicyName.lastIndexOf('.') + 1); if (oldversion > 1) { oldversion = oldversion - 1; dbPolicyName = dbPolicyName + oldversion + ".xml"; } String createPolicyQuery = "SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName"; SimpleBindings params = new SimpleBindings(); params.put("scope", scope); params.put("policyName", dbPolicyName); List<?> createPolicyQueryList = commonClassDao.getDataByQuery(createPolicyQuery, params); if (!createPolicyQueryList.isEmpty()) { PolicyEntity entitydata = (PolicyEntity) createPolicyQueryList.get(0); policyAdapter.setPrevJsonBody(entitydata.getConfigurationData().getConfigBody()); } if (policyAdapter.getData() != null) { String jsonBody = policyAdapter.getJsonBody(); saveConfigurations(policyName, jsonBody); if (!policyName.endsWith(".xml")) { policyName = policyName + ".xml"; } PolicyType configPolicy = (PolicyType) policyAdapter.getData(); configPolicy.setDescription(policyAdapter.getPolicyDescription()); configPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); AllOfType allOfOne = new AllOfType(); String fileName = policyAdapter.getNewFileName(); String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length()); if ((name == null) || (name.equals(""))) { name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length()); } allOfOne.getMatch().add(createMatch("PolicyName", name)); AllOfType allOf = new AllOfType(); allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName())); allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType())); allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel()))); allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard())); allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate())); AnyOfType anyOf = new AnyOfType(); anyOf.getAllOf().add(allOfOne); anyOf.getAllOf().add(allOf); TargetType target = new TargetType(); target.getAnyOf().add(anyOf); configPolicy.setTarget(target); RuleType rule = new RuleType(); rule.setRuleId(policyAdapter.getRuleID()); rule.setEffect(EffectType.PERMIT); AllOfType allOfInRule = new AllOfType(); MatchType accessMatch = new MatchType(); AttributeValueType accessAttributeValue = new AttributeValueType(); accessAttributeValue.setDataType(STRING_DATATYPE); accessAttributeValue.getContent().add("ACCESS"); accessMatch.setAttributeValue(accessAttributeValue); AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType(); URI accessURI = null; try { accessURI = new URI(ACTION_ID); } catch (URISyntaxException e) { PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "FirewallConfigPolicy", "Exception creating ACCESS URI"); } accessAttributeDesignator.setCategory(CATEGORY_ACTION); accessAttributeDesignator.setDataType(STRING_DATATYPE); accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue()); accessMatch.setAttributeDesignator(accessAttributeDesignator); accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); MatchType configMatch = new MatchType(); AttributeValueType configAttributeValue = new AttributeValueType(); configAttributeValue.setDataType(STRING_DATATYPE); configAttributeValue.getContent().add("Config"); configMatch.setAttributeValue(configAttributeValue); AttributeDesignatorType configAttributeDesignator = new AttributeDesignatorType(); URI configURI = null; try { configURI = new URI(RESOURCE_ID); } catch (URISyntaxException e) { PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "FirewallConfigPolicy", "Exception creating Config URI"); } configAttributeDesignator.setCategory(CATEGORY_RESOURCE); configAttributeDesignator.setDataType(STRING_DATATYPE); configAttributeDesignator.setAttributeId(new IdentifierImpl(configURI).stringValue()); configMatch.setAttributeDesignator(configAttributeDesignator); configMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); allOfInRule.getMatch().add(accessMatch); allOfInRule.getMatch().add(configMatch); AnyOfType anyOfInRule = new AnyOfType(); anyOfInRule.getAllOf().add(allOfInRule); TargetType targetInRule = new TargetType(); targetInRule.getAnyOf().add(anyOfInRule); rule.setTarget(targetInRule); rule.setAdviceExpressions(getAdviceExpressions(version, policyName)); configPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); policyAdapter.setPolicyData(configPolicy); } else { PolicyLogger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName()); } setPreparedToSave(true); return true; } FirewallConfigPolicy(); @Autowired  FirewallConfigPolicy(CommonClassDao commonClassDao);  FirewallConfigPolicy(PolicyRestAdapter policyAdapter); @Override Map<String, String> savePolicies(); @Override boolean prepareToSave(); @Override Object getCorrectPolicyDataObject();  }
@Test public void testPrepareToSave() { logger.debug("test prepareToSave Policy: enter"); boolean response = false; try { when(mockFWConfig.prepareToSave()).thenReturn(true); response = mockFWConfig.prepareToSave(); } catch (Exception e) { logger.error("Exception Occured" + e); } assertTrue(response); }