CreatePolicyController extends RestrictedBaseController { public void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) { attributeList = new ArrayList<>(); if (! (policyAdapter.getPolicyData() instanceof PolicyType)) { return; } Object policyData = policyAdapter.getPolicyData(); PolicyType policy = (PolicyType) policyData; policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName()); policyAdapter.setConfigType(entity.getConfigurationData().getConfigType()); policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody()); String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf('_') + 1); policyAdapter.setPolicyName(policyNameValue); String description = ""; try { description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:")); } catch (Exception e) { policyLogger.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e); description = policy.getDescription(); } policyAdapter.setPolicyDescription(description); TargetType target = policy.getTarget(); if (target != null) { for (AnyOfType anyOf : target.getAnyOf()) { int index = 0; for (AllOfType allOf : anyOf.getAllOf()) { for (MatchType match : allOf.getMatch()) { String value = (String) match.getAttributeValue().getContent().get(0); String attributeId = match.getAttributeDesignator().getAttributeId(); policyAdapter.setupUsingAttribute(attributeId, value); if (index >= 7) { Map<String, String> attribute = new HashMap<>(); attribute.put("key", attributeId); attribute.put("value", value); attributeList.add(attribute); } index++; } } } policyAdapter.setAttributes(attributeList); } List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition(); for (Object object : ruleList) { if (object instanceof RuleType) { policyAdapter.setRuleID(((RuleType) object).getRuleId()); } } }  void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity);  }
@Test public void testEasyStuff() { CreatePolicyController controller = new CreatePolicyController(); PolicyRestAdapter adapter = new PolicyRestAdapter(); controller.prePopulateBaseConfigPolicyData(adapter, null); VariableDefinitionType var = new VariableDefinitionType(); PolicyType policy = new PolicyType(); policy.setDescription("i am a description. @CreatedBy: policy designer"); policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(var); adapter.setPolicyData(policy); adapter.setPolicyName("name"); controller.prePopulateBaseConfigPolicyData(adapter, entity); }
@Test public void testBadDescription() { PolicyRestAdapter adapter = new PolicyRestAdapter(); VariableDefinitionType var = new VariableDefinitionType(); PolicyType policy = new PolicyType(); policy.setDescription("i am a description"); policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(var); adapter.setPolicyData(policy); adapter.setPolicyName("name"); CreatePolicyController controller = new CreatePolicyController(); controller.prePopulateBaseConfigPolicyData(adapter, entity); }
@Test public void testExpectedPolicyContents() { AllOfType allOf = new AllOfType(); allOf.getMatch().add(createMatchType("ONAPName", "ONAPName")); allOf.getMatch().add(createMatchType("RiskType", "RiskType")); allOf.getMatch().add(createMatchType("RiskLevel", "RiskLevel")); allOf.getMatch().add(createMatchType("guard", "guard")); allOf.getMatch().add(createMatchType("TTLDate", "TTLDate")); allOf.getMatch().add(createMatchType("ConfigName", "ConfigName")); allOf.getMatch().add(createMatchType("NA", "TTLDate")); allOf.getMatch().add(createMatchType("custom", "custom")); allOf.getMatch().add(createMatchType("custom", "custom")); allOf.getMatch().add(createMatchType("custom", "custom")); AnyOfType anyOf = new AnyOfType(); anyOf.getAllOf().add(allOf); TargetType target = new TargetType(); target.getAnyOf().add(anyOf); RuleType rule = new RuleType(); PolicyType policy = new PolicyType(); policy.setDescription("i am a description. @CreatedBy: policy designer"); policy.setTarget(target); policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); PolicyRestAdapter adapter = new PolicyRestAdapter(); adapter.setPolicyData(policy); adapter.setPolicyName("name"); CreatePolicyController controller = new CreatePolicyController(); controller.prePopulateBaseConfigPolicyData(adapter, entity); }