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