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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.xacml.application.common.std;
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;
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;
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;
56 public class StdBaseTranslatorTest {
61 AttributeAssignment assignmentPolicyId;
62 AttributeAssignment assignmentPolicy;
63 AttributeAssignment assignmentBadPolicy;
64 AttributeAssignment assignmentWeight;
65 AttributeAssignment assignmentPolicyType;
66 AttributeAssignment assignmentUnknown;
68 Obligation obligation;
71 * beforeSetup - loads and creates objects used later by the tests.
72 * @throws CoderException CoderException
76 public void beforeSetup() throws CoderException {
77 policyJson = ResourceUtils.getResourceAsString("test.policy.json");
78 policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
80 assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
81 ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
82 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
86 assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
87 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
88 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
92 assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
93 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
94 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
98 assignmentWeight = TestUtilsCommon.createAttributeAssignment(
99 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
100 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
104 assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
105 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
106 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
110 assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
112 XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
116 obligation = TestUtilsCommon.createXacmlObligation(
117 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
118 Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType));
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));
128 assertThat(translator.generateAnyOfForPolicyType("foo.bar")).isNotNull();
129 assertThat(translator.generateAnyOfForPolicyType("foo.bar").getAllOf().size()).isEqualTo(1);
131 assertThat(translator.generateConditionForPolicyType("foo.bar")).isNotNull();
132 assertThat(translator.generateConditionForPolicyType("foo.bar").getExpression()).isNotNull();
135 // Test the addObligation method
137 PolicySetType policySet = new PolicySetType();
139 translator.addObligation(policySet, "policy.id", policyJson, 0, "foo.bar");
141 assertThat(policySet.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
142 assertThat(policySet.getObligationExpressions().getObligationExpression().get(0)
143 .getAttributeAssignmentExpression().size()).isEqualTo(4);
145 PolicyType policy = new PolicyType();
146 translator.addObligation(policy, null, policyJson, null, null);
148 assertThat(policy.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
149 assertThat(policy.getObligationExpressions().getObligationExpression().get(0)
150 .getAttributeAssignmentExpression().size()).isEqualTo(1);
152 RuleType rule = new RuleType();
153 translator.addObligation(rule, "policy.id", null, null, "foo.bar");
155 assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
156 assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
157 .getAttributeAssignmentExpression().size()).isEqualTo(2);
159 rule = new RuleType();
160 translator.addObligation(rule, null, null, null, null);
162 assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
163 assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
164 .getAttributeAssignmentExpression().size()).isEqualTo(0);
167 // Should not throw an exception
169 translator.addObligation(new String(), "policy.id", policyJson, null, "foo.bar");
172 // Test the response conversion
174 Map<String, String> ids = new HashMap<>();
175 ids.put("onap.policies.Test", "1.0.0");
176 Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
178 Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
179 Decision.PERMIT, Arrays.asList(obligation), policyIds);
181 DecisionResponse decision = translator.convertResponse(xacmlResponse);
183 assertNotNull(decision);
185 assertThat(decision.getPolicies()).isNotNull();
186 assertThat(decision.getPolicies().size()).isEqualTo(0);
190 public void testBadData() throws ToscaPolicyConversionException, ParseException {
191 TestTranslator translator = new TestTranslator();
193 assertThatThrownBy(() -> translator.convertPolicy(
194 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
195 .hasMessageContaining("missing metadata");
197 translator.metadata.put(StdBaseTranslator.POLICY_ID, "random.policy.id");
199 assertThatThrownBy(() -> translator.convertPolicy(
200 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
201 .hasMessageContaining("missing metadata");
203 translator.metadata.put(StdBaseTranslator.POLICY_VERSION, "1.0.0");
205 ToscaPolicy policy = new ToscaPolicy();
206 assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
208 Map<String, String> ids = new HashMap<>();
209 ids.put("onap.policies.Test", "1.0.0");
210 Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
212 Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
213 Decision.PERMIT, Arrays.asList(obligation), policyIds);
215 DecisionResponse decision = translator.convertResponse(xacmlResponse);
217 assertNotNull(decision);
219 assertThat(decision.getPolicies()).isNotNull();
220 assertThat(decision.getPolicies().size()).isEqualTo(0);
223 // This will need more work when I fix
224 // the convertResponse
227 Obligation badObligation = TestUtilsCommon.createXacmlObligation(
228 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
229 Arrays.asList(assignmentBadPolicy, assignmentUnknown));
231 xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE,
232 Decision.PERMIT, Arrays.asList(badObligation), policyIds);
234 decision = translator.convertResponse(xacmlResponse);
236 assertNotNull(decision);
238 xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
239 Decision.DENY, Arrays.asList(badObligation), policyIds);
241 decision = translator.convertResponse(xacmlResponse);
243 assertNotNull(decision);
246 private class MyStdBaseTranslator extends StdBaseTranslator {
249 protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
254 private class TestTranslator extends StdBaseTranslator {
255 public Map<String, String> metadata = new HashMap<>();
258 protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
262 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
263 PolicyType xacmlPolicy = new PolicyType();
264 this.fillMetadataSection(xacmlPolicy, metadata);