diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2019-10-11 14:03:19 -0400 |
---|---|---|
committer | Pamela Dragosh <pdragosh@research.att.com> | 2019-10-11 14:50:49 -0400 |
commit | 4e6615528e03f1a6f7808e28481bd55fe39ed572 (patch) | |
tree | 2f46083b28015778d879d57934eca4b0aaac161b /POLICY-SDK-APP/src/test/java | |
parent | f36e53a3637e1204a42491ec0eeed7b3c763f681 (diff) |
Add coverage to a few Controllers
Cleaned up parts of the code that were useless.
The coverage for 2 classes are now over 80%. The
CreatePolicyController coverage is now 100%
I have no idea what kind of policy the ActionPolicyController
is expecting, seems bizarre so will have to leave it alone
and move on.
Issue-ID: POLICY-2133
Change-Id: I80629d3b6f2aba301483f3fa8c3cad16cc01ffbe
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'POLICY-SDK-APP/src/test/java')
4 files changed, 358 insertions, 7 deletions
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; + } + +} |