Reformat ONAP-XACML test cases
[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  * 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 package org.onap.policy.xacml.test.util;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
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;
38
39 public class XACMLPolicyWriterTest {
40     private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
41     private static Path configPolicyPathValue;
42     private static Path actionPolicyPathValue;
43
44     @Before
45     public void setUp() {
46         File templateFile;
47         ClassLoader classLoader = getClass().getClassLoader();
48         try {
49             templateFile =
50                     new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
51             configPolicyPathValue = templateFile.toPath();
52             templateFile =
53                     new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
54             actionPolicyPathValue = templateFile.toPath();
55         } catch (Exception e1) {
56             logger.error("Exception Occured" + e1);
57         }
58     }
59
60     @SuppressWarnings("static-access")
61     @Test
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"));
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 }