aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/ActionPolicyControllerTest.java
blob: e34cab45e518f7d3640db16dc04804f5e4165c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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();
    }
}