Add NSS libraries to images
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / XacmlUpdatePolicyUtilsTest.java
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.assertThatCode;
26
27 import com.att.research.xacml.api.XACML3;
28 import com.att.research.xacml.util.XACMLPolicyWriter;
29 import com.att.research.xacml.util.XACMLProperties;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
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;
55
56 /**
57  * Utility methods for storing policies to disk and updating Properties objects
58  * that reference policies.
59  *
60  * @author pameladragosh
61  *
62  */
63 public class XacmlUpdatePolicyUtilsTest {
64     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlUpdatePolicyUtilsTest.class);
65
66     static Properties properties;
67
68     static PolicySetType rootPolicy = new PolicySetType();
69
70     static Path rootPath;
71
72     static PolicyType policy1 = new PolicyType();
73     static PolicyType policy2 = new PolicyType();
74
75     static PolicySetType policySet3 = new PolicySetType();
76
77     static Path path1;
78     static Path path2;
79
80     static Path policySetPath;
81
82     /**
83      * Temporary folder where we will store newly created policies.
84      */
85     @ClassRule
86     public static TemporaryFolder policyFolder = new TemporaryFolder();
87
88     /**
89      * Setup the JUnit tests.
90      *
91      * @throws Exception thrown
92      */
93     @BeforeClass
94     public static void setUp() throws Exception {
95         assertThatCode(() -> {
96             //
97             // Load our test property object
98             //
99             try (InputStream is = new FileInputStream("src/test/resources/test.properties")) {
100                 properties = new Properties();
101                 properties.load(is);
102             }
103             //
104             // Create a very basic Root policy
105             //
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);
112             //
113             // Create policies
114             //
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)));
119             //
120             // Create another PolicySet
121             //
122             policySet3.setPolicySetId("policyset1");
123             policySet3.setTarget(new TargetType());
124             policySet3.setPolicyCombiningAlgId(XACML3.ID_POLICY_FIRST_APPLICABLE.stringValue());
125             ObjectFactory factory = new ObjectFactory();
126
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);
132
133         }).doesNotThrowAnyException();
134     }
135
136     private static Path createPolicy(PolicyType policy, String id, String resource) throws IOException {
137         //
138         // Create Policy 1
139         //
140         policy.setPolicyId(id);
141         MatchType matchPolicyId = ToscaPolicyConverterUtils.buildMatchTypeDesignator(
142                 XACML3.ID_FUNCTION_STRING_EQUAL,
143                 resource,
144                 XACML3.ID_DATATYPE_STRING,
145                 XACML3.ID_RESOURCE_RESOURCE_ID,
146                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
147         //
148         // This is our outer AnyOf - which is an OR
149         //
150         AnyOfType anyOf = new AnyOfType();
151         //
152         // Create AllOf (AND) of just Policy Id
153         //
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());
162         //
163         // Add the rule to the policy
164         //
165         policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
166         //
167         // Create a file
168         //
169         File file = policyFolder.newFile(policy.getPolicyId() + ".xml");
170         LOGGER.info("Creating Policy {}", file.getAbsolutePath());
171         return XACMLPolicyWriter.writePolicyFile(file.toPath(), policy);
172     }
173
174     @Test
175     public void test() {
176         assertThatCode(() -> {
177             //
178             // Just update root and policies
179             //
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());
184             }
185             //
186             // Test updating the properties
187             //
188             XACMLProperties.setXacmlRootProperties(properties, rootPath);
189             XACMLProperties.setXacmlReferencedProperties(properties, path1, path2);
190             //
191             // Dump this out so I can see what I'm doing
192             //
193             for (Entry<Object, Object> entry : properties.entrySet()) {
194                 LOGGER.info("{}={}", entry.getKey(), entry.getValue());
195             }
196             LOGGER.info("Properties {}", properties.toString());
197             //
198             // Somehow I have to figure out how to test this in assertj
199             //
200             //
201             // Just update root and PolicySet
202             //
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());
207             }
208             //
209             // Test updating the properties
210             //
211             XACMLProperties.setXacmlRootProperties(properties, rootPath);
212             XACMLProperties.setXacmlReferencedProperties(properties, policySetPath);
213             //
214             // Dump this out so I can see what I'm doing
215             //
216             for (Entry<Object, Object> entry : properties.entrySet()) {
217                 LOGGER.info("{}={}", entry.getKey(), entry.getValue());
218             }
219             LOGGER.info("Properties {}", properties.toString());
220             //
221             // Somehow I have to figure out how to test this in assertj
222             //
223
224         }).doesNotThrowAnyException();
225     }
226 }