2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.test.util;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.file.Path;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.xacml.util.XACMLPolicyWriter;
43 public class XACMLPolicyWriterTest {
44 private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
45 private static Path configPolicyPathValue;
46 private static Path actionPolicyPathValue;
51 ClassLoader classLoader = getClass().getClassLoader();
53 templateFile = new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
54 configPolicyPathValue = templateFile.toPath();
55 templateFile = new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
56 actionPolicyPathValue = templateFile.toPath();
57 } catch (Exception e1) {
58 logger.error("Exception Occured" + e1);
62 @SuppressWarnings("static-access")
64 public void xacmlPolicyWriterTest() throws IOException {
65 XACMLPolicyWriter writer = new XACMLPolicyWriter();
66 String configResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
67 assertTrue(configResponseValue.equals("txt"));
68 String actionResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(actionPolicyPathValue);
69 assertTrue(actionResponseValue.equals("json"));
73 public void testWrites() throws Exception {
75 PolicyType policyType = new PolicyType();
76 OutputStream os = new ByteArrayOutputStream();
77 File tmpfile = File.createTempFile("foo", null);
78 Path filename = tmpfile.toPath();
79 PolicySetType policySet = new PolicySetType();
81 // delete tmp file before running the test
84 // ensure its deleted after writePolicyFile() re-creates it
85 tmpfile.deleteOnExit();
87 // Test write combinations
88 assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policySet));
89 assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policyType));
90 assertNotNull(XACMLPolicyWriter.getXmlAsInputStream(policyType));
91 XACMLPolicyWriter.writePolicyFile(os, policySet);
92 assertNotNull(os.toString());
93 XACMLPolicyWriter.writePolicyFile(os, policyType);
94 assertNotNull(os.toString());