diff options
7 files changed, 427 insertions, 67 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/ActionPolicyController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/ActionPolicyController.java index c83cb777d..98a479f81 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/ActionPolicyController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/ActionPolicyController.java @@ -27,22 +27,18 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; - import javax.xml.bind.JAXBElement; - import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType; 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.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.adapter.PolicyRestAdapter; @@ -113,19 +109,20 @@ public class ActionPolicyController extends RestrictedBaseController { return; } // Under the obligationExpressions we have obligationExpression. - List<ObligationExpressionType> obligationList = obligations.getObligationExpression(); - if (obligationList == null) { - return; - } - for (ObligationExpressionType obligation : obligationList) { + // NOTE: getObligationExpression() will never return NULL. + // + for (ObligationExpressionType obligation : obligations.getObligationExpression()) { policyAdapter.setActionAttributeValue(obligation.getObligationId()); // Under the obligationExpression we have attributeAssignmentExpression. - List<AttributeAssignmentExpressionType> attributeAssignmentExpressionList = - obligation.getAttributeAssignmentExpression(); - if (attributeAssignmentExpressionList == null) { - continue; - } - for (AttributeAssignmentExpressionType attributeAssignmentExpression : attributeAssignmentExpressionList) { + // + // NOTE: obligation.getAttributeAssignmentExpression() will NEVER be null + // It will always return a list. + // + for (AttributeAssignmentExpressionType attributeAssignmentExpression : + obligation.getAttributeAssignmentExpression()) { + // + // + // String attributeID = attributeAssignmentExpression.getAttributeId(); AttributeValueType attributeValue = (AttributeValueType) attributeAssignmentExpression.getExpression().getValue(); @@ -169,37 +166,44 @@ public class ActionPolicyController extends RestrictedBaseController { private void setPolicyAdapterAttributes(PolicyRestAdapter policyAdapter, List<AnyOfType> anyOfList) { List<Object> attributeList = new ArrayList<>(); - if (anyOfList == null) { - return; - } + // + // NOTE: If using xacml3 code and doing a getAnyOf(), the anyOfList will + // NEVER be null as that code will create it if it is null. + // + // Remove the null check as its impossible to cover it. + // // under target we have AnyOFType for (AnyOfType anyOf : anyOfList) { // Under AntOfType we have AllOfType + // + // NOTE: This will NEVER be null as the method call in the + // previous line getAllOf() will never return a null. It + // always creates it if its empty. + // List<AllOfType> allOfList = anyOf.getAllOf(); - if (allOfList == null) { - continue; - } // Under AllOfType we have Match. for (AllOfType allOfType : allOfList) { - List<MatchType> matchList = allOfType.getMatch(); - if (matchList != null) { - // - // Under the match we have attributeValue and - // attributeDesignator. So,finally down to the actual attribute. - // - // Component attributes are saved under Target here we are fetching them back. - // One row is default so we are not adding dynamic component at index 0. - matchList.forEach(match -> { - AttributeValueType attributeValue = match.getAttributeValue(); - String value = (String) attributeValue.getContent().get(0); - AttributeDesignatorType designator = match.getAttributeDesignator(); - String attributeId = designator.getAttributeId(); - Map<String, String> attribute = new HashMap<>(); - attribute.put("key", attributeId); - attribute.put("value", value); - attributeList.add(attribute); - }); - } + // + // NOTE: allOfType.getMatch() will NEVER be null as the method + // call getMatch will always return something. If its + // not there it will create it. + // + // + // Under the match we have attributeValue and + // attributeDesignator. So,finally down to the actual attribute. + // + // Component attributes are saved under Target here we are fetching them back. + // One row is default so we are not adding dynamic component at index 0. + allOfType.getMatch().forEach(match -> { + AttributeValueType attributeValue = match.getAttributeValue(); + String value = (String) attributeValue.getContent().get(0); + AttributeDesignatorType designator = match.getAttributeDesignator(); + String attributeId = designator.getAttributeId(); + Map<String, String> attribute = new HashMap<>(); + attribute.put("key", attributeId); + attribute.put("value", value); + attributeList.add(attribute); + }); policyAdapter.setAttributes(attributeList); } } @@ -214,7 +218,8 @@ public class ActionPolicyController extends RestrictedBaseController { LOGGER.debug("Prepopulating rule algoirthm: " + index); } // Check to see if Attribute Value exists, if yes then it is not a compound rule - if (jaxbElement.getValue() instanceof AttributeValueType) { + if (jaxbElement.getValue() instanceof AttributeValueType + || jaxbElement.getValue() instanceof AttributeDesignatorType) { prePopulateRuleAlgorithms(index, actionApply, jaxbActionTypes); ruleAlgorithmTracker.addLast(index); isCompoundRule = false; @@ -282,11 +287,18 @@ public class ActionPolicyController extends RestrictedBaseController { String attributeValue = (String) actionConditionAttributeValue.getContent().get(0); ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_2, attributeValue); - ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue(); - List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression(); - AttributeDesignatorType attributeDesignator = - (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue(); - ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_1, attributeDesignator.getAttributeId()); + // + // This is making a BIG assumption here that there exists an innerApply. This IF + // statement was added to support JUnit code coverage. For lack of any example of what + // this policy should actually look like. + // + if (jaxbActionTypes.size() > 1) { + ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue(); + List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression(); + AttributeDesignatorType attributeDesignator = + (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue(); + ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_1, attributeDesignator.getAttributeId()); + } } ruleAlgorithmList.add(ruleMap); } diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AdminTabController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AdminTabController.java index 0ab266071..e9190968d 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AdminTabController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/AdminTabController.java @@ -26,7 +26,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; -import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; @@ -108,6 +107,8 @@ public class AdminTabController extends RestrictedBaseController { method = {org.springframework.web.bind.annotation.RequestMethod.POST}) public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setCharacterEncoding(CHARACTER_ENCODING); + request.setCharacterEncoding(CHARACTER_ENCODING); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -123,20 +124,14 @@ public class AdminTabController extends RestrictedBaseController { globalRole.setRole("super-admin"); commonClassDao.update(globalRole); - response.setCharacterEncoding(CHARACTER_ENCODING); response.setContentType("application / json"); - request.setCharacterEncoding(CHARACTER_ENCODING); String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)); response.getWriter().write(new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}").toString()); - - return null; } catch (Exception e) { LOGGER.error("Exception Occured" + e); - response.setCharacterEncoding(CHARACTER_ENCODING); - request.setCharacterEncoding(CHARACTER_ENCODING); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreatePolicyController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreatePolicyController.java index af4f8cfc3..be9910c24 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreatePolicyController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreatePolicyController.java @@ -80,26 +80,28 @@ public class CreatePolicyController extends RestrictedBaseController { policyAdapter.setPolicyDescription(description); // Get the target data under policy. TargetType target = policy.getTarget(); - if (target != null && target.getAnyOf() != null) { + // + // NOTE: target.getAnyOf() will NEVER return null + // + if (target != null) { // Under target we have AnyOFType List<AnyOfType> anyOfList = target.getAnyOf(); Iterator<AnyOfType> iterAnyOf = anyOfList.iterator(); while (iterAnyOf.hasNext()) { AnyOfType anyOf = iterAnyOf.next(); // Under AnyOFType we have AllOFType + // + // NOTE: anyOf.getAllOf() will NEVER return null + // List<AllOfType> allOfList = anyOf.getAllOf(); - if (allOfList == null) { - continue; - } Iterator<AllOfType> iterAllOf = allOfList.iterator(); int index = 0; while (iterAllOf.hasNext()) { AllOfType allOf = iterAllOf.next(); // Under AllOFType we have Match + // NOTE: allOf.getMatch() will NEVER be NULL + // List<MatchType> matchList = allOf.getMatch(); - if (matchList == null) { - continue; - } Iterator<MatchType> iterMatch = matchList.iterator(); while (iterMatch.hasNext()) { MatchType match = iterMatch.next(); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/ActionPolicyControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/ActionPolicyControllerTest.java new file mode 100644 index 000000000..e34cab45e --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/ActionPolicyControllerTest.java @@ -0,0 +1,205 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controller; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import com.att.research.xacml.api.XACML3; +import java.io.IOException; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType; +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 oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType; +import org.junit.Test; +import org.onap.policy.rest.adapter.PolicyRestAdapter; + +public class ActionPolicyControllerTest { + + @Test + public void testBasicConstructor() throws IOException { + ActionPolicyController controller = new ActionPolicyController(); + final PolicyRestAdapter adapter = new PolicyRestAdapter(); + // + // Cover the simple if instance branch + // + assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException(); + } + + @Test + public void testNoDescriptionNoTargetType() throws IOException { + // + // Do the test - with no description and + // no TargetType to cover those branches. + // + PolicyRestAdapter adapter = new PolicyRestAdapter(); + adapter.setPolicyData(new PolicyType()); + adapter.setPolicyName("name"); + ActionPolicyController controller = new ActionPolicyController(); + assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException(); + } + + @Test + public void testWithDescriptionEmptyTargetType() throws IOException { + // + // Create a simple PolicyType + // + PolicyType policy = new PolicyType(); + policy.setTarget(new TargetType()); + policy.setDescription("i am a description. @CreatedBy: policy designer"); + // + // Now do the test - description and + // TargetType but its empty + // + PolicyRestAdapter adapter = new PolicyRestAdapter(); + adapter.setPolicyData(policy); + adapter.setPolicyName("name"); + ActionPolicyController controller = new ActionPolicyController(); + assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException(); + } + + + @Test + public void testWithTargetTypeWithAnyOf() throws IOException { + // + // Create TargetType with empty AnyOf + // + AttributeValueType value = new AttributeValueType(); + value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + value.getContent().add(new String("value")); + AttributeDesignatorType designator = new AttributeDesignatorType(); + designator.setAttributeId("foo:bar"); + designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION.stringValue()); + + MatchType match = new MatchType(); + match.setMatchId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue()); + match.setAttributeValue(value); + match.setAttributeDesignator(designator); + AllOfType allOf = new AllOfType(); + allOf.getMatch().add(match); + AnyOfType anyOf = new AnyOfType(); + anyOf.getAllOf().add(allOf); + TargetType target = new TargetType(); + target.getAnyOf().add(anyOf); + // + // Create a simple Rule with NO obligations but a Condition + // + RuleType rule = new RuleType(); + rule.setRuleId("id:rule"); + rule.setEffect(EffectType.PERMIT); + + AttributeValueType expressionValue = new AttributeValueType(); + expressionValue.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + expressionValue.getContent().add(new String("a string value")); + + designator = new AttributeDesignatorType(); + designator.setAttributeId("foo:bar"); + designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION.stringValue()); + + ApplyType applyOneAndOnly = new ApplyType(); + applyOneAndOnly.setDescription("apply this"); + applyOneAndOnly.setFunctionId(XACML3.ID_FUNCTION_STRING_ONE_AND_ONLY.stringValue()); + applyOneAndOnly.getExpression().add(new ObjectFactory().createAttributeDesignator(designator)); + + ApplyType applyOneAndOnly2 = new ApplyType(); + applyOneAndOnly2.setDescription("apply this"); + applyOneAndOnly2.setFunctionId(XACML3.ID_FUNCTION_STRING_ONE_AND_ONLY.stringValue()); + applyOneAndOnly2.getExpression().add(new ObjectFactory().createAttributeValue(expressionValue)); + + ApplyType apply = new ApplyType(); + apply.setDescription("apply this"); + apply.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue()); + apply.getExpression().add(new ObjectFactory().createApply(applyOneAndOnly)); + apply.getExpression().add(new ObjectFactory().createApply(applyOneAndOnly2)); + + + ConditionType condition = new ConditionType(); + condition.setExpression(new ObjectFactory().createApply(apply)); + rule.setCondition(condition); + // + // Create a simple Rule WITH obligations + // + AttributeValueType val = new AttributeValueType(); + val.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + val.getContent().add(new String("obligation data")); + + AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType(); + assignment.setAttributeId("ob:id:1"); + assignment.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue()); + assignment.setExpression(new ObjectFactory().createAttributeValue(val)); + + AttributeValueType val2 = new AttributeValueType(); + val2.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + val2.getContent().add(new String("iamperformer")); + + AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType(); + assignment2.setAttributeId("performer"); + assignment2.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue()); + assignment2.setExpression(new ObjectFactory().createAttributeValue(val2)); + + ObligationExpressionType ob = new ObligationExpressionType(); + ob.setFulfillOn(EffectType.PERMIT); + ob.setObligationId("id:obligation"); + ob.getAttributeAssignmentExpression().add(assignment); + ob.getAttributeAssignmentExpression().add(assignment2); + ObligationExpressionsType obs = new ObligationExpressionsType(); + obs.getObligationExpression().add(ob); + RuleType obligationRule = new RuleType(); + obligationRule.setRuleId("id:rule:obligations"); + obligationRule.setEffect(EffectType.DENY); + obligationRule.setObligationExpressions(obs); + // + // Create a PolicyType + // + PolicyType policy = new PolicyType(); + policy.setDescription("i am a description. @CreatedBy: policy designer"); + policy.setTarget(target); + policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); + policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(obligationRule); + // + // Add something the ActionPolicyController will skip over + // to catch that branch + // + policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(new VariableDefinitionType()); + // + // Now do the test - description and + // TargetType but its empty + // + PolicyRestAdapter adapter = new PolicyRestAdapter(); + adapter.setPolicyData(policy); + adapter.setPolicyName("name"); + ActionPolicyController controller = new ActionPolicyController(); + assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException(); + } +} diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java index 68da8e714..85337f18b 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AdminTabControllerTest.java @@ -22,6 +22,7 @@ package org.onap.policy.controller; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -54,6 +55,11 @@ public class AdminTabControllerTest { private HttpServletRequest request; private MockHttpServletResponse response; + /** + * Before. + * + * @throws Exception Exception + */ @Before public void setUp() throws Exception { @@ -82,6 +88,7 @@ public class AdminTabControllerTest { @Test public void testGetAdminRole() { AdminTabController admin = new AdminTabController(); + assertNotNull(AdminTabController.getCommonClassDao()); try { admin.getAdminTabEntityData(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("lockdowndata")); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java index 7d2d5f8c2..64c3b04ff 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java @@ -45,7 +45,7 @@ import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) public class AutoPushControllerTest { - private PolicyController controller = new PolicyController();; + private PolicyController controller = new PolicyController(); private AutoPushController apController = new AutoPushController(); @Rule @@ -93,22 +93,22 @@ public class AutoPushControllerTest { Mockito.when(UserUtils.getUserSession(Mockito.any())).thenReturn(user); // Mock policy controller - PolicyController pController = Mockito.mock(PolicyController.class); - PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(pController); - Mockito.when(pController.getRoles(Mockito.any())).thenReturn(null); + PolicyController policyController = Mockito.mock(PolicyController.class); + PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(policyController); + Mockito.when(policyController.getRoles(Mockito.any())).thenReturn(null); // Test group container MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); apController.getPolicyGroupContainerData(request, response); - assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); + assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); // Test push apController.pushPolicyToPDPGroup(request, response); - assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); + assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); // Test remove apController.removePDPGroup(request, response); - assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); + assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreatePolicyControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreatePolicyControllerTest.java new file mode 100644 index 000000000..6a431a0af --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/CreatePolicyControllerTest.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controller; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import com.att.research.xacml.api.XACML3; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType; +import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType; +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 oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.jpa.ConfigurationDataEntity; +import org.onap.policy.rest.jpa.PolicyEntity; + +public class CreatePolicyControllerTest { + + private PolicyEntity entity; + + /** + * before - sets up the mocked PolicyEntity. + */ + @Before + public void before() { + // + // PolicyEntity + // + ConfigurationDataEntity dataEntity = mock(ConfigurationDataEntity.class); + when(dataEntity.getConfigType()).thenReturn("configtype"); + entity = mock(PolicyEntity.class); + when(entity.getConfigurationData()).thenReturn(dataEntity); + } + + @Test + public void testEasyStuff() { + CreatePolicyController controller = new CreatePolicyController(); + PolicyRestAdapter adapter = new PolicyRestAdapter(); + controller.prePopulateBaseConfigPolicyData(adapter, null); + // + // Create a simple PolicyType + // + 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(); + // + // Create a simple PolicyType + // + 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); + } + + private MatchType createMatchType(String strValue, String id) { + AttributeValueType value = new AttributeValueType(); + value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue()); + value.getContent().add(new String(strValue)); + AttributeDesignatorType designator = new AttributeDesignatorType(); + designator.setAttributeId(id); + + MatchType match = new MatchType(); + match.setAttributeValue(value); + match.setAttributeDesignator(designator); + + return match; + } + +} |