2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.Advice;
32 import com.att.research.xacml.api.AttributeAssignment;
33 import com.att.research.xacml.api.Decision;
34 import com.att.research.xacml.api.IdReference;
35 import com.att.research.xacml.api.Obligation;
36 import com.att.research.xacml.api.Response;
37 import com.att.research.xacml.api.XACML3;
38 import com.att.research.xacml.std.StdStatusCode;
39 import java.text.ParseException;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.HashMap;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.models.decisions.concepts.DecisionResponse;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 import org.onap.policy.pdp.xacml.application.common.TestUtilsCommon;
54 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
55 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
57 public class StdBaseTranslatorTest {
62 AttributeAssignment assignmentPolicyId;
63 AttributeAssignment assignmentPolicy;
64 AttributeAssignment assignmentBadPolicy;
65 AttributeAssignment assignmentWeight;
66 AttributeAssignment assignmentPolicyType;
67 AttributeAssignment assignmentUnknown;
69 Obligation obligation;
72 * beforeSetup - loads and creates objects used later by the tests.
73 * @throws CoderException CoderException
77 public void beforeSetup() throws CoderException {
78 policyJson = ResourceUtils.getResourceAsString("test.policy.json");
79 policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
81 assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
82 ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
83 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
87 assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
88 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
89 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
93 assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
94 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
95 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
99 assignmentWeight = TestUtilsCommon.createAttributeAssignment(
100 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
101 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
105 assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
106 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
107 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
111 assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
113 XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
117 obligation = TestUtilsCommon.createXacmlObligation(
118 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
119 Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType));
123 public void testTranslatorNormalFlow() throws Exception {
124 StdBaseTranslator translator = new MyStdBaseTranslator();
125 assertNotNull(translator);
126 assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
127 assertNull(translator.convertRequest(null));
129 assertThat(translator.generateAnyOfForPolicyType("foo.bar")).isNotNull();
130 assertThat(translator.generateAnyOfForPolicyType("foo.bar").getAllOf().size()).isEqualTo(1);
132 assertThat(translator.generateConditionForPolicyType("foo.bar")).isNotNull();
133 assertThat(translator.generateConditionForPolicyType("foo.bar").getExpression()).isNotNull();
136 // Test the addObligation method
138 PolicySetType policySet = new PolicySetType();
140 translator.addObligation(policySet, "policy.id", policyJson, 0, "foo.bar");
142 assertThat(policySet.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
143 assertThat(policySet.getObligationExpressions().getObligationExpression().get(0)
144 .getAttributeAssignmentExpression().size()).isEqualTo(4);
146 PolicyType policy = new PolicyType();
147 translator.addObligation(policy, null, policyJson, null, null);
149 assertThat(policy.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
150 assertThat(policy.getObligationExpressions().getObligationExpression().get(0)
151 .getAttributeAssignmentExpression().size()).isEqualTo(1);
153 RuleType rule = new RuleType();
154 translator.addObligation(rule, "policy.id", null, null, "foo.bar");
156 assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
157 assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
158 .getAttributeAssignmentExpression().size()).isEqualTo(2);
160 rule = new RuleType();
161 translator.addObligation(rule, null, null, null, null);
163 assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
164 assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
165 .getAttributeAssignmentExpression()).isEmpty();
168 // Should not throw an exception
170 translator.addObligation(new String(), "policy.id", policyJson, null, "foo.bar");
173 // Test the response conversion
175 Map<String, String> ids = new HashMap<>();
176 ids.put("onap.policies.Test", "1.0.0");
177 Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
179 Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK, null,
180 Decision.PERMIT, Arrays.asList(obligation), policyIds);
182 DecisionResponse decision = translator.convertResponse(xacmlResponse);
184 assertNotNull(decision);
186 assertThat(decision.getPolicies()).isNotNull();
187 assertThat(decision.getPolicies()).isEmpty();
191 public void testBadData() throws ToscaPolicyConversionException, ParseException {
192 TestTranslator translator = new TestTranslator();
194 assertThatThrownBy(() -> translator.convertPolicy(
195 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
196 .hasMessageContaining("missing metadata");
198 translator.metadata.put(StdBaseTranslator.POLICY_ID, "random.policy.id");
200 assertThatThrownBy(() -> translator.convertPolicy(
201 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
202 .hasMessageContaining("missing metadata");
204 translator.metadata.put(StdBaseTranslator.POLICY_VERSION, "1.0.0");
206 ToscaPolicy policy = new ToscaPolicy();
207 assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
209 Map<String, String> ids = new HashMap<>();
210 ids.put("onap.policies.Test", "1.0.0");
211 Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
213 Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK, null,
214 Decision.PERMIT, Arrays.asList(obligation), policyIds);
216 DecisionResponse decision = translator.convertResponse(xacmlResponse);
218 assertNotNull(decision);
220 assertThat(decision.getPolicies()).isNotNull();
221 assertThat(decision.getPolicies()).isEmpty();
223 Obligation badObligation = TestUtilsCommon.createXacmlObligation(
224 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
225 Arrays.asList(assignmentBadPolicy, assignmentUnknown));
227 xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE, null,
228 Decision.PERMIT, Arrays.asList(badObligation), policyIds);
230 decision = translator.convertResponse(xacmlResponse);
232 assertNotNull(decision);
234 xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
235 "Bad obligation", Decision.DENY, Arrays.asList(badObligation), policyIds);
237 decision = translator.convertResponse(xacmlResponse);
239 assertNotNull(decision);
240 assertThat(decision.getStatus()).isEqualTo("error");
241 assertThat(decision.getMessage()).isEqualTo("Bad obligation");
244 private class MyStdBaseTranslator extends StdBaseTranslator {
247 protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
251 protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
256 private class TestTranslator extends StdBaseTranslator {
257 public Map<String, String> metadata = new HashMap<>();
260 protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
264 protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
268 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
269 PolicyType xacmlPolicy = new PolicyType();
270 this.fillMetadataSection(xacmlPolicy, metadata);