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.assertThatCode;
27 import com.att.research.xacml.api.XACML3;
28 import com.att.research.xacml.util.XACMLPolicyWriter;
29 import com.att.research.xacml.util.XACMLProperties;
31 import java.io.ByteArrayOutputStream;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.util.Map.Entry;
40 import java.util.Properties;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
49 import org.junit.BeforeClass;
50 import org.junit.ClassRule;
51 import org.junit.Test;
52 import org.junit.rules.TemporaryFolder;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Utility methods for storing policies to disk and updating Properties objects
58 * that reference policies.
60 * @author pameladragosh
63 public class XacmlUpdatePolicyUtilsTest {
64 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlUpdatePolicyUtilsTest.class);
66 static Properties properties;
68 static PolicySetType rootPolicy = new PolicySetType();
72 static PolicyType policy1 = new PolicyType();
73 static PolicyType policy2 = new PolicyType();
75 static PolicySetType policySet3 = new PolicySetType();
80 static Path policySetPath;
83 * Temporary folder where we will store newly created policies.
86 public static TemporaryFolder policyFolder = new TemporaryFolder();
89 * Setup the JUnit tests.
91 * @throws Exception thrown
94 public static void setUp() throws Exception {
95 assertThatCode(() -> {
97 // Load our test property object
99 try (InputStream is = new FileInputStream("src/test/resources/test.properties")) {
100 properties = new Properties();
104 // Create a very basic Root policy
106 rootPolicy.setPolicySetId("root");
107 rootPolicy.setTarget(new TargetType());
108 rootPolicy.setPolicyCombiningAlgId(XACML3.ID_POLICY_FIRST_APPLICABLE.stringValue());
109 File rootFile = policyFolder.newFile("root.xml");
110 LOGGER.info("Creating Root Policy {}", rootFile.getAbsolutePath());
111 rootPath = XACMLPolicyWriter.writePolicyFile(rootFile.toPath(), rootPolicy);
115 path1 = createPolicy(policy1, "policy1", "resource1");
116 LOGGER.info(new String(Files.readAllBytes(path1)));
117 path2 = createPolicy(policy2, "policy2", "resource2");
118 LOGGER.info(new String(Files.readAllBytes(path2)));
120 // Create another PolicySet
122 policySet3.setPolicySetId("policyset1");
123 policySet3.setTarget(new TargetType());
124 policySet3.setPolicyCombiningAlgId(XACML3.ID_POLICY_FIRST_APPLICABLE.stringValue());
125 ObjectFactory factory = new ObjectFactory();
127 policySet3.getPolicySetOrPolicyOrPolicySetIdReference().add(factory.createPolicy(policy1));
128 policySet3.getPolicySetOrPolicyOrPolicySetIdReference().add(factory.createPolicy(policy2));
129 File policySetFile = policyFolder.newFile("policySet1.xml");
130 LOGGER.info("Creating PolicySet {}", policySetFile.getAbsolutePath());
131 policySetPath = XACMLPolicyWriter.writePolicyFile(policySetFile.toPath(), policySet3);
133 }).doesNotThrowAnyException();
136 private static Path createPolicy(PolicyType policy, String id, String resource) throws IOException {
140 policy.setPolicyId(id);
141 MatchType matchPolicyId = ToscaPolicyConverterUtils.buildMatchTypeDesignator(
142 XACML3.ID_FUNCTION_STRING_EQUAL,
144 XACML3.ID_DATATYPE_STRING,
145 XACML3.ID_RESOURCE_RESOURCE_ID,
146 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
148 // This is our outer AnyOf - which is an OR
150 AnyOfType anyOf = new AnyOfType();
152 // Create AllOf (AND) of just Policy Id
154 anyOf.getAllOf().add(ToscaPolicyConverterUtils.buildAllOf(matchPolicyId));
155 TargetType target = new TargetType();
156 target.getAnyOf().add(anyOf);
157 policy.setTarget(target);
158 RuleType rule = new RuleType();
159 rule.setRuleId(policy.getPolicyId() + ":rule");
160 rule.setEffect(EffectType.PERMIT);
161 rule.setTarget(new TargetType());
163 // Add the rule to the policy
165 policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
169 File file = policyFolder.newFile(policy.getPolicyId() + ".xml");
170 LOGGER.info("Creating Policy {}", file.getAbsolutePath());
171 return XACMLPolicyWriter.writePolicyFile(file.toPath(), policy);
176 assertThatCode(() -> {
178 // Just update root and policies
180 XacmlUpdatePolicyUtils.updateXacmlRootPolicy(rootPolicy, policy1, policy2);
181 try (OutputStream os = new ByteArrayOutputStream()) {
182 XACMLPolicyWriter.writePolicyFile(os, rootPolicy);
183 LOGGER.debug("New Root Policy:{}{}", System.lineSeparator(), os.toString());
186 // Test updating the properties
188 XACMLProperties.setXacmlRootProperties(properties, rootPath);
189 XACMLProperties.setXacmlReferencedProperties(properties, path1, path2);
191 // Dump this out so I can see what I'm doing
193 for (Entry<Object, Object> entry : properties.entrySet()) {
194 LOGGER.info("{}={}", entry.getKey(), entry.getValue());
196 LOGGER.info("Properties {}", properties.toString());
198 // Somehow I have to figure out how to test this in assertj
201 // Just update root and PolicySet
203 XacmlUpdatePolicyUtils.updateXacmlRootPolicy(rootPolicy, policySet3);
204 try (OutputStream os = new ByteArrayOutputStream()) {
205 XACMLPolicyWriter.writePolicyFile(os, rootPolicy);
206 LOGGER.debug("New Root Policy:{}{}", System.lineSeparator(), os.toString());
209 // Test updating the properties
211 XACMLProperties.setXacmlRootProperties(properties, rootPath);
212 XACMLProperties.setXacmlReferencedProperties(properties, policySetPath);
214 // Dump this out so I can see what I'm doing
216 for (Entry<Object, Object> entry : properties.entrySet()) {
217 LOGGER.info("{}={}", entry.getKey(), entry.getValue());
219 LOGGER.info("Properties {}", properties.toString());
221 // Somehow I have to figure out how to test this in assertj
224 }).doesNotThrowAnyException();