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;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertNotNull;
28 import com.att.research.xacml.api.AttributeAssignment;
29 import com.att.research.xacml.api.Obligation;
30 import com.att.research.xacml.api.XACML3;
31 import java.util.Arrays;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
37 public class OnapObligationTest {
42 AttributeAssignment assignmentPolicyId;
43 AttributeAssignment assignmentPolicy;
44 AttributeAssignment assignmentBadPolicy;
45 AttributeAssignment assignmentWeight;
46 AttributeAssignment assignmentPolicyType;
47 AttributeAssignment assignmentUnknown;
49 Obligation obligation;
52 * setup - create test data.
56 policyJson = ResourceUtils.getResourceAsString("test.policy.json");
57 policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
59 assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
60 ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
61 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
65 assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
66 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
67 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
71 assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
72 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
73 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
77 assignmentWeight = TestUtilsCommon.createAttributeAssignment(
78 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
79 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
83 assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
84 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
85 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
89 assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
91 XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
95 obligation = TestUtilsCommon.createXacmlObligation(
96 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
97 Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType,
102 public void testObligation() {
103 OnapObligation onapObligation = new OnapObligation(obligation);
104 assertNotNull(onapObligation);
105 assertThat(onapObligation.getPolicyId()).isEqualTo(assignmentPolicyId.getAttributeValue().getValue());
106 assertThat(onapObligation.getPolicyType()).isEqualTo(assignmentPolicyType.getAttributeValue().getValue());
107 assertThat(onapObligation.getPolicyContent()).isEqualTo(assignmentPolicy.getAttributeValue().getValue());
108 assertThat(onapObligation.getWeight()).isEqualTo(assignmentWeight.getAttributeValue().getValue());
112 public void testSimplePolicy() {
113 OnapObligation onapObligation = new OnapObligation("my.policy.id", policyJson);
114 assertNotNull(onapObligation);
115 assertThat(onapObligation.getPolicyId()).isEqualTo("my.policy.id");
116 assertThat(onapObligation.getPolicyContent()).isEqualTo(policyJson);
117 assertThat(onapObligation.getPolicyType()).isNull();
118 assertThat(onapObligation.getWeight()).isNull();
120 // Create an obligation from it
122 ObligationExpressionType newObligation = onapObligation.generateObligation();
123 assertNotNull(newObligation);
124 assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(2);
129 public void testWeightedPolicy() {
130 OnapObligation onapObligation = new OnapObligation("my.policy.id", policyJson, "onap.policies.Test", 5);
131 assertNotNull(onapObligation);
132 assertThat(onapObligation.getPolicyId()).isEqualTo("my.policy.id");
133 assertThat(onapObligation.getPolicyContent()).isEqualTo(policyJson);
134 assertThat(onapObligation.getPolicyType()).isEqualTo("onap.policies.Test");
135 assertThat(onapObligation.getWeight()).isEqualTo(5);
137 // Create an obligation from it
139 ObligationExpressionType newObligation = onapObligation.generateObligation();
140 assertNotNull(newObligation);
141 assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(4);