2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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=========================================================
22 package org.onap.policy.xacml.test.util;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.nio.file.Path;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.xacml.util.XACMLPolicyWriter;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
39 public class XACMLPolicyWriterTest {
40 private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
41 private static Path configPolicyPathValue;
42 private static Path actionPolicyPathValue;
47 ClassLoader classLoader = getClass().getClassLoader();
50 new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
51 configPolicyPathValue = templateFile.toPath();
53 new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
54 actionPolicyPathValue = templateFile.toPath();
55 } catch (Exception e1) {
56 logger.error("Exception Occured" + e1);
60 @SuppressWarnings("static-access")
62 public void xacmlPolicyWriterTest() throws IOException {
63 XACMLPolicyWriter writer = new XACMLPolicyWriter();
64 String configResponseValue =
65 writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
66 assertTrue(configResponseValue.equals("txt"));
67 String actionResponseValue =
68 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());