2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
23 import static org.assertj.core.api.Assertions.assertThatCode;
25 import com.att.research.xacml.api.XACML3;
26 import java.io.IOException;
27 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
28 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
43 import org.junit.Test;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
46 public class ActionPolicyControllerTest {
49 public void testBasicConstructor() throws IOException {
50 ActionPolicyController controller = new ActionPolicyController();
51 final PolicyRestAdapter adapter = new PolicyRestAdapter();
53 // Cover the simple if instance branch
55 assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
59 public void testNoDescriptionNoTargetType() throws IOException {
61 // Do the test - with no description and
62 // no TargetType to cover those branches.
64 PolicyRestAdapter adapter = new PolicyRestAdapter();
65 adapter.setPolicyData(new PolicyType());
66 adapter.setPolicyName("name");
67 ActionPolicyController controller = new ActionPolicyController();
68 assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
72 public void testWithDescriptionEmptyTargetType() throws IOException {
74 // Create a simple PolicyType
76 PolicyType policy = new PolicyType();
77 policy.setTarget(new TargetType());
78 policy.setDescription("i am a description. @CreatedBy: policy designer");
80 // Now do the test - description and
81 // TargetType but its empty
83 PolicyRestAdapter adapter = new PolicyRestAdapter();
84 adapter.setPolicyData(policy);
85 adapter.setPolicyName("name");
86 ActionPolicyController controller = new ActionPolicyController();
87 assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
92 public void testWithTargetTypeWithAnyOf() throws IOException {
94 // Create TargetType with empty AnyOf
96 AttributeValueType value = new AttributeValueType();
97 value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
98 value.getContent().add(new String("value"));
99 AttributeDesignatorType designator = new AttributeDesignatorType();
100 designator.setAttributeId("foo:bar");
101 designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
102 designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION.stringValue());
104 MatchType match = new MatchType();
105 match.setMatchId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
106 match.setAttributeValue(value);
107 match.setAttributeDesignator(designator);
108 AllOfType allOf = new AllOfType();
109 allOf.getMatch().add(match);
110 AnyOfType anyOf = new AnyOfType();
111 anyOf.getAllOf().add(allOf);
112 TargetType target = new TargetType();
113 target.getAnyOf().add(anyOf);
115 // Create a simple Rule with NO obligations but a Condition
117 RuleType rule = new RuleType();
118 rule.setRuleId("id:rule");
119 rule.setEffect(EffectType.PERMIT);
121 AttributeValueType expressionValue = new AttributeValueType();
122 expressionValue.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
123 expressionValue.getContent().add(new String("a string value"));
125 designator = new AttributeDesignatorType();
126 designator.setAttributeId("foo:bar");
127 designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
128 designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION.stringValue());
130 ApplyType applyOneAndOnly = new ApplyType();
131 applyOneAndOnly.setDescription("apply this");
132 applyOneAndOnly.setFunctionId(XACML3.ID_FUNCTION_STRING_ONE_AND_ONLY.stringValue());
133 applyOneAndOnly.getExpression().add(new ObjectFactory().createAttributeDesignator(designator));
135 ApplyType applyOneAndOnly2 = new ApplyType();
136 applyOneAndOnly2.setDescription("apply this");
137 applyOneAndOnly2.setFunctionId(XACML3.ID_FUNCTION_STRING_ONE_AND_ONLY.stringValue());
138 applyOneAndOnly2.getExpression().add(new ObjectFactory().createAttributeValue(expressionValue));
140 ApplyType apply = new ApplyType();
141 apply.setDescription("apply this");
142 apply.setFunctionId(XACML3.ID_FUNCTION_STRING_EQUAL.stringValue());
143 apply.getExpression().add(new ObjectFactory().createApply(applyOneAndOnly));
144 apply.getExpression().add(new ObjectFactory().createApply(applyOneAndOnly2));
147 ConditionType condition = new ConditionType();
148 condition.setExpression(new ObjectFactory().createApply(apply));
149 rule.setCondition(condition);
151 // Create a simple Rule WITH obligations
153 AttributeValueType val = new AttributeValueType();
154 val.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
155 val.getContent().add(new String("obligation data"));
157 AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
158 assignment.setAttributeId("ob:id:1");
159 assignment.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue());
160 assignment.setExpression(new ObjectFactory().createAttributeValue(val));
162 AttributeValueType val2 = new AttributeValueType();
163 val2.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
164 val2.getContent().add(new String("iamperformer"));
166 AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType();
167 assignment2.setAttributeId("performer");
168 assignment2.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue());
169 assignment2.setExpression(new ObjectFactory().createAttributeValue(val2));
171 ObligationExpressionType ob = new ObligationExpressionType();
172 ob.setFulfillOn(EffectType.PERMIT);
173 ob.setObligationId("id:obligation");
174 ob.getAttributeAssignmentExpression().add(assignment);
175 ob.getAttributeAssignmentExpression().add(assignment2);
176 ObligationExpressionsType obs = new ObligationExpressionsType();
177 obs.getObligationExpression().add(ob);
178 RuleType obligationRule = new RuleType();
179 obligationRule.setRuleId("id:rule:obligations");
180 obligationRule.setEffect(EffectType.DENY);
181 obligationRule.setObligationExpressions(obs);
183 // Create a PolicyType
185 PolicyType policy = new PolicyType();
186 policy.setDescription("i am a description. @CreatedBy: policy designer");
187 policy.setTarget(target);
188 policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
189 policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(obligationRule);
191 // Add something the ActionPolicyController will skip over
192 // to catch that branch
194 policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(new VariableDefinitionType());
196 // Now do the test - description and
197 // TargetType but its empty
199 PolicyRestAdapter adapter = new PolicyRestAdapter();
200 adapter.setPolicyData(policy);
201 adapter.setPolicyName("name");
202 ActionPolicyController controller = new ActionPolicyController();
203 assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();