Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / test / java / org / onap / policy / xacml / test / std / pap / StdEngineFactoryTest.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.std.pap;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import com.att.research.xacml.api.pap.PAPException;
29 import com.att.research.xacml.std.pap.StdEngine;
30 import com.att.research.xacml.util.FactoryException;
31
32 import java.io.IOException;
33 import java.util.Properties;
34
35 import org.junit.Test;
36 import org.onap.policy.xacml.std.pap.StdEngineFactory;
37
38 public class StdEngineFactoryTest {
39     @Test
40     public void testStdEngineFactory() throws FactoryException, PAPException, IOException {
41         StdEngineFactory stdFactory = new StdEngineFactory();
42         System.setProperty("xacml.pap.pdps", "src/test/resources/pdps");
43         assertTrue(stdFactory.newEngine() != null);
44         Properties properties = new Properties();
45         properties.setProperty("xacml.pap.pdps", "src/test/resources/pdps");
46         assertTrue(stdFactory.newEngine(properties) != null);
47
48         StdEngineFactory stdFactoryNew = new StdEngineFactory();
49         System.setProperty("xacml.pap.pdps", "src/test/resources/pdpstest");
50         assertTrue(stdFactoryNew.newEngine() != null);
51     }
52
53     @Test
54     public void testNegativeCase() throws Exception {
55         // Setup test data
56         Properties props = new Properties();
57         String tmpdir = System.getProperty("java.io.tmpdir");
58         props.setProperty(StdEngine.PROP_PAP_REPO, tmpdir);
59
60         // Set the system property temporarily
61         String systemKey = StdEngine.PROP_PAP_REPO;
62         String oldProperty = System.getProperty(systemKey);
63         System.setProperty(systemKey, tmpdir);
64
65         // Test factory failure cases
66         try {
67             StdEngineFactory factory = new StdEngineFactory();
68             factory.newEngine();
69             factory.newEngine(props);
70         } catch (Exception ex) {
71             fail("Not expecting any exceptions: " + ex);
72         }
73
74         // Restore the original system property
75         if (oldProperty != null) {
76             System.setProperty(systemKey, oldProperty);
77         } else {
78             System.clearProperty(systemKey);
79         }
80     }
81 }