Merge "Don't use hard-coded tmp path"
[policy/engine.git] / ONAP-XACML / src / test / java / org / onap / policy / xacml / test / util / XACMLPolicyWriterTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
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
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  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.xacml.test.util;
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.nio.file.Path;
30
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
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;
39
40 public class XACMLPolicyWriterTest {
41   private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
42   private static Path configPolicyPathValue;
43   private static Path actionPolicyPathValue;
44
45   @Before
46   public void setUp() {
47     File templateFile;
48     ClassLoader classLoader = getClass().getClassLoader();
49     try {
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);
56     }
57   }
58
59   @SuppressWarnings("static-access")
60   @Test
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"));
67   }
68
69   @Test
70   public void testWrites() throws Exception {
71     // Set up test data
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();
77     
78     // delete tmp file before running the test
79     tmpfile.delete();
80
81     // ensure its deleted after writePolicyFile() re-creates it
82     tmpfile.deleteOnExit();
83
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());
92   }
93 }