Merge "JUnit additions for PDP,PDP-REST,SDK,XACML"
[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 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 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.xacml.util.XACMLPolicyWriter;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
37
38 public class XACMLPolicyWriterTest {
39   private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
40   private static Path configPolicyPathValue;
41   private static Path actionPolicyPathValue;
42
43   @Before
44   public void setUp() {
45     File templateFile;
46     ClassLoader classLoader = getClass().getClassLoader();
47     try {
48       templateFile = new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
49       configPolicyPathValue = templateFile.toPath();
50       templateFile = new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
51       actionPolicyPathValue = templateFile.toPath();
52     } catch (Exception e1) {
53       logger.error("Exception Occured" + e1);
54     }
55   }
56
57   @SuppressWarnings("static-access")
58   @Test
59   public void xacmlPolicyWriterTest() throws IOException {
60     XACMLPolicyWriter writer = new XACMLPolicyWriter();
61     String configResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
62     assertTrue(configResponseValue.equals("txt"));
63     String actionResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(actionPolicyPathValue);
64     assertTrue(actionResponseValue.equals("json"));
65   }
66
67   @Test
68   public void testWrites() {
69     // Set up test data
70     PolicyType policyType = new PolicyType();
71     OutputStream os = new ByteArrayOutputStream();
72     Path filename = Paths.get("/tmp/foo");
73     PolicySetType policySet = new PolicySetType();
74
75     // Test write combinations
76     assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policySet));
77     assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policyType));
78     assertNotNull(XACMLPolicyWriter.getXmlAsInputStream(policyType));
79     XACMLPolicyWriter.writePolicyFile(os, policySet);
80     assertNotNull(os.toString());
81     XACMLPolicyWriter.writePolicyFile(os, policyType);
82     assertNotNull(os.toString());
83   }
84 }