Format ONAP-XACML and add JUnit
[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-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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.xacml.test.util;
24
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.file.Path;
33
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;
36
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;
42
43 public class XACMLPolicyWriterTest {
44     private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
45     private static Path configPolicyPathValue;
46     private static Path actionPolicyPathValue;
47
48     @Before
49     public void setUp() {
50         File templateFile;
51         ClassLoader classLoader = getClass().getClassLoader();
52         try {
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);
59         }
60     }
61
62     @SuppressWarnings("static-access")
63     @Test
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"));
70     }
71
72     @Test
73     public void testWrites() throws Exception {
74         // Set up test data
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();
80
81         // delete tmp file before running the test
82         tmpfile.delete();
83
84         // ensure its deleted after writePolicyFile() re-creates it
85         tmpfile.deleteOnExit();
86
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());
95     }
96 }