a7391fab6c4f300b04773c7925136c93e1fecd73
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common.std;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30
31 import com.att.research.xacml.api.AttributeAssignment;
32 import com.att.research.xacml.api.Decision;
33 import com.att.research.xacml.api.IdReference;
34 import com.att.research.xacml.api.Obligation;
35 import com.att.research.xacml.api.Response;
36 import com.att.research.xacml.api.XACML3;
37 import com.att.research.xacml.std.StdStatusCode;
38 import java.text.ParseException;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.HashMap;
42 import java.util.Map;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.models.decisions.concepts.DecisionResponse;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.pdp.xacml.application.common.TestUtilsCommon;
53 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
54 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
55
56 public class StdBaseTranslatorTest {
57
58     String policyJson;
59     String policyBadJson;
60
61     AttributeAssignment assignmentPolicyId;
62     AttributeAssignment assignmentPolicy;
63     AttributeAssignment assignmentBadPolicy;
64     AttributeAssignment assignmentWeight;
65     AttributeAssignment assignmentPolicyType;
66     AttributeAssignment assignmentUnknown;
67
68     Obligation obligation;
69
70     /**
71      * beforeSetup - loads and creates objects used later by the tests.
72      * @throws CoderException CoderException
73      *
74      */
75     @Before
76     public void beforeSetup() throws CoderException {
77         policyJson = ResourceUtils.getResourceAsString("test.policy.json");
78         policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
79
80         assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
81                 ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
82                 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
83                 "policy.id"
84                 );
85
86         assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
87                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
88                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
89                 policyJson
90                 );
91
92         assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
93                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
94                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
95                 policyBadJson
96                 );
97
98         assignmentWeight = TestUtilsCommon.createAttributeAssignment(
99                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
100                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
101                 0
102                 );
103
104         assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
105                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
106                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
107                 "onap.policies.Test"
108                 );
109
110         assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
111                 "foo:bar",
112                 XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
113                 10.2
114                 );
115
116         obligation = TestUtilsCommon.createXacmlObligation(
117                 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
118                 Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType));
119     }
120
121     @Test
122     public void test() throws ParseException {
123         StdBaseTranslator translator = new MyStdBaseTranslator();
124         assertNotNull(translator);
125         assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
126         assertNull(translator.convertRequest(null));
127
128         assertThat(translator.generateAnyOfForPolicyType("foo.bar")).isNotNull();
129         assertThat(translator.generateAnyOfForPolicyType("foo.bar").getAllOf().size()).isEqualTo(1);
130
131         assertThat(translator.generateConditionForPolicyType("foo.bar")).isNotNull();
132         assertThat(translator.generateConditionForPolicyType("foo.bar").getExpression()).isNotNull();
133
134         //
135         // Test the addObligation method
136         //
137         PolicySetType policySet = new PolicySetType();
138
139         translator.addObligation(policySet, "policy.id", policyJson, 0, "foo.bar");
140
141         assertThat(policySet.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
142         assertThat(policySet.getObligationExpressions().getObligationExpression().get(0)
143                 .getAttributeAssignmentExpression().size()).isEqualTo(4);
144
145         PolicyType policy = new PolicyType();
146         translator.addObligation(policy, null, policyJson, null, null);
147
148         assertThat(policy.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
149         assertThat(policy.getObligationExpressions().getObligationExpression().get(0)
150                 .getAttributeAssignmentExpression().size()).isEqualTo(1);
151
152         RuleType rule = new RuleType();
153         translator.addObligation(rule, "policy.id", null, null, "foo.bar");
154
155         assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
156         assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
157                 .getAttributeAssignmentExpression().size()).isEqualTo(2);
158
159         rule = new RuleType();
160         translator.addObligation(rule, null, null, null, null);
161
162         assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
163         assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
164                 .getAttributeAssignmentExpression().size()).isEqualTo(0);
165
166         //
167         // Should not throw an exception
168         //
169         translator.addObligation(new String(), "policy.id", policyJson, null, "foo.bar");
170
171         //
172         // Test the response conversion
173         //
174         Map<String, String> ids = new HashMap<>();
175         ids.put("onap.policies.Test", "1.0.0");
176         Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
177
178         Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
179                 Decision.PERMIT, Arrays.asList(obligation), policyIds);
180
181         DecisionResponse decision = translator.convertResponse(xacmlResponse);
182
183         assertNotNull(decision);
184
185         assertThat(decision.getPolicies()).isNotNull();
186         assertThat(decision.getPolicies().size()).isEqualTo(0);
187     }
188
189     @Test
190     public void testBadData() throws ToscaPolicyConversionException, ParseException {
191         TestTranslator translator = new TestTranslator();
192
193         assertThatThrownBy(() -> translator.convertPolicy(
194                 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
195                     .hasMessageContaining("missing metadata");
196
197         translator.metadata.put(StdBaseTranslator.POLICY_ID, "random.policy.id");
198
199         assertThatThrownBy(() -> translator.convertPolicy(
200                 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
201                     .hasMessageContaining("missing metadata");
202
203         translator.metadata.put(StdBaseTranslator.POLICY_VERSION, "1.0.0");
204
205         ToscaPolicy policy = new ToscaPolicy();
206         assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
207
208         Map<String, String> ids = new HashMap<>();
209         ids.put("onap.policies.Test", "1.0.0");
210         Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
211
212         Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
213                 Decision.PERMIT, Arrays.asList(obligation), policyIds);
214
215         DecisionResponse decision = translator.convertResponse(xacmlResponse);
216
217         assertNotNull(decision);
218
219         assertThat(decision.getPolicies()).isNotNull();
220         assertThat(decision.getPolicies().size()).isEqualTo(0);
221
222         //
223         // This will need more work when I fix
224         // the convertResponse
225         //
226
227         Obligation badObligation = TestUtilsCommon.createXacmlObligation(
228                 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
229                 Arrays.asList(assignmentBadPolicy, assignmentUnknown));
230
231         xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE,
232                 Decision.PERMIT, Arrays.asList(badObligation), policyIds);
233
234         decision = translator.convertResponse(xacmlResponse);
235
236         assertNotNull(decision);
237
238         xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
239                 Decision.DENY, Arrays.asList(badObligation), policyIds);
240
241         decision = translator.convertResponse(xacmlResponse);
242
243         assertNotNull(decision);
244     }
245
246     private class MyStdBaseTranslator extends StdBaseTranslator {
247
248         @Override
249         protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
250         }
251
252     }
253
254     private class TestTranslator extends StdBaseTranslator {
255         public Map<String, String> metadata = new HashMap<>();
256
257         @Override
258         protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
259         }
260
261         @Override
262         public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
263             PolicyType xacmlPolicy = new PolicyType();
264             this.fillMetadataSection(xacmlPolicy, metadata);
265             return xacmlPolicy;
266         }
267     }
268
269 }