Add coverage to a few Controllers
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / ActionPolicyControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controller;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24
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;
45
46 public class ActionPolicyControllerTest {
47
48     @Test
49     public void testBasicConstructor() throws IOException {
50         ActionPolicyController controller = new ActionPolicyController();
51         final PolicyRestAdapter adapter = new PolicyRestAdapter();
52         //
53         // Cover the simple if instance branch
54         //
55         assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
56     }
57
58     @Test
59     public void testNoDescriptionNoTargetType() throws IOException {
60         //
61         // Do the test - with no description and
62         // no TargetType to cover those branches.
63         //
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();
69     }
70
71     @Test
72     public void testWithDescriptionEmptyTargetType() throws IOException {
73         //
74         // Create a simple PolicyType
75         //
76         PolicyType policy = new PolicyType();
77         policy.setTarget(new TargetType());
78         policy.setDescription("i am a description. @CreatedBy: policy designer");
79         //
80         // Now do the test - description and
81         // TargetType but its empty
82         //
83         PolicyRestAdapter adapter = new PolicyRestAdapter();
84         adapter.setPolicyData(policy);
85         adapter.setPolicyName("name");
86         ActionPolicyController controller = new ActionPolicyController();
87         assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
88     }
89
90
91     @Test
92     public void testWithTargetTypeWithAnyOf() throws IOException {
93         //
94         // Create TargetType with empty AnyOf
95         //
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());
103
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);
114         //
115         // Create a simple Rule with NO obligations but a Condition
116         //
117         RuleType rule = new RuleType();
118         rule.setRuleId("id:rule");
119         rule.setEffect(EffectType.PERMIT);
120
121         AttributeValueType expressionValue = new AttributeValueType();
122         expressionValue.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
123         expressionValue.getContent().add(new String("a string value"));
124
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());
129
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));
134
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));
139
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));
145
146
147         ConditionType condition = new ConditionType();
148         condition.setExpression(new ObjectFactory().createApply(apply));
149         rule.setCondition(condition);
150         //
151         // Create a simple Rule WITH obligations
152         //
153         AttributeValueType val = new AttributeValueType();
154         val.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
155         val.getContent().add(new String("obligation data"));
156
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));
161
162         AttributeValueType val2 = new AttributeValueType();
163         val2.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
164         val2.getContent().add(new String("iamperformer"));
165
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));
170
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);
182         //
183         // Create a PolicyType
184         //
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);
190         //
191         // Add something the ActionPolicyController will skip over
192         // to catch that branch
193         //
194         policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(new VariableDefinitionType());
195         //
196         // Now do the test - description and
197         // TargetType but its empty
198         //
199         PolicyRestAdapter adapter = new PolicyRestAdapter();
200         adapter.setPolicyData(policy);
201         adapter.setPolicyName("name");
202         ActionPolicyController controller = new ActionPolicyController();
203         assertThatCode(() -> controller.prePopulateActionPolicyData(adapter)).doesNotThrowAnyException();
204     }
205 }