c02d7fb35792356181a31c75de95350a3ad92177
[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;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertNotNull;
27
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;
36
37 public class OnapObligationTest {
38
39     String policyJson;
40     String policyBadJson;
41
42     AttributeAssignment assignmentPolicyId;
43     AttributeAssignment assignmentPolicy;
44     AttributeAssignment assignmentBadPolicy;
45     AttributeAssignment assignmentWeight;
46     AttributeAssignment assignmentPolicyType;
47     AttributeAssignment assignmentUnknown;
48
49     Obligation obligation;
50
51     /**
52      * setup - create test data.
53      */
54     @Before
55     public void setup() {
56         policyJson = ResourceUtils.getResourceAsString("test.policy.json");
57         policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
58
59         assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
60                 ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
61                 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
62                 policyJson
63                 );
64
65         assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
66                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
67                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
68                 policyJson
69                 );
70
71         assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
72                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
73                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
74                 policyBadJson
75                 );
76
77         assignmentWeight = TestUtilsCommon.createAttributeAssignment(
78                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
79                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
80                 0
81                 );
82
83         assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
84                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
85                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
86                 "onap.policies.Test"
87                 );
88
89         assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
90                 "foo:bar",
91                 XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
92                 10.2
93                 );
94
95         obligation = TestUtilsCommon.createXacmlObligation(
96                 ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
97                 Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType,
98                         assignmentUnknown));
99     }
100
101     @Test
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());
109     }
110
111     @Test
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();
119         //
120         // Create an obligation from it
121         //
122         ObligationExpressionType newObligation = onapObligation.generateObligation();
123         assertNotNull(newObligation);
124         assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(2);
125     }
126
127
128     @Test
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);
136         //
137         // Create an obligation from it
138         //
139         ObligationExpressionType newObligation = onapObligation.generateObligation();
140         assertNotNull(newObligation);
141         assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(4);
142     }
143
144 }