2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.xacml.test.util;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
25 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 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;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
40 public class XACMLPolicyWriterTest {
41 private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
42 private static Path configPolicyPathValue;
43 private static Path actionPolicyPathValue;
48 ClassLoader classLoader = getClass().getClassLoader();
50 templateFile = new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
51 configPolicyPathValue = templateFile.toPath();
52 templateFile = new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
53 actionPolicyPathValue = templateFile.toPath();
54 } catch (Exception e1) {
55 logger.error("Exception Occured" + e1);
59 @SuppressWarnings("static-access")
61 public void xacmlPolicyWriterTest() throws IOException {
62 XACMLPolicyWriter writer = new XACMLPolicyWriter();
63 String configResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
64 assertTrue(configResponseValue.equals("txt"));
65 String actionResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(actionPolicyPathValue);
66 assertTrue(actionResponseValue.equals("json"));
70 public void testWrites() throws Exception {
72 PolicyType policyType = new PolicyType();
73 OutputStream os = new ByteArrayOutputStream();
74 File tmpfile = File.createTempFile("foo", null);
75 Path filename = tmpfile.toPath();
76 PolicySetType policySet = new PolicySetType();
78 // delete tmp file before running the test
81 // ensure its deleted after writePolicyFile() re-creates it
82 tmpfile.deleteOnExit();
84 // Test write combinations
85 assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policySet));
86 assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policyType));
87 assertNotNull(XACMLPolicyWriter.getXmlAsInputStream(policyType));
88 XACMLPolicyWriter.writePolicyFile(os, policySet);
89 assertNotNull(os.toString());
90 XACMLPolicyWriter.writePolicyFile(os, policyType);
91 assertNotNull(os.toString());