Update css file name in conf.py
[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-2020 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.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.catchThrowable;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.fail;
31
32 import com.att.research.xacml.api.pap.PAPException;
33 import com.att.research.xacml.std.pap.StdEngine;
34 import com.att.research.xacml.util.FactoryException;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.Properties;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.TemporaryFolder;
43 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
44 import org.onap.policy.xacml.std.pap.StdEngineFactory;
45
46 public class StdEngineFactoryTest {
47
48     private static String systemProperty;
49
50     @Rule
51     public TemporaryFolder folder = new TemporaryFolder();
52
53     @BeforeClass
54     public static void saveSystemProperty() {
55         systemProperty = System.getProperty(StdEngine.PROP_PAP_REPO);
56     }
57
58     /**
59      * restoreSystemProperty.
60      */
61     @AfterClass
62     public static void restoreSystemProperty() {
63         if (systemProperty != null) {
64             System.setProperty(StdEngine.PROP_PAP_REPO, systemProperty);
65         } else {
66             System.clearProperty(StdEngine.PROP_PAP_REPO);
67         }
68     }
69
70     @Test
71     public void testStdEngineFactory() throws FactoryException, PAPException, IOException {
72         StdEngineFactory stdFactory = new StdEngineFactory();
73         System.setProperty("xacml.pap.pdps", "src/test/resources/pdps");
74         assertNotNull(stdFactory.newEngine());
75         Properties properties = new Properties();
76         properties.setProperty("xacml.pap.pdps", "src/test/resources/pdps");
77         assertNotNull(stdFactory.newEngine(properties));
78
79         StdEngineFactory stdFactoryNew = new StdEngineFactory();
80         System.setProperty("xacml.pap.pdps", "src/test/resources/pdpstest");
81         PAPPolicyEngine engine = stdFactoryNew.newEngine();
82         assertNotNull(engine);
83
84         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
85             engine.newGroup(null, null)
86         );
87
88         assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
89             engine.movePDP(null, null)
90         );
91
92     }
93
94     @Test
95     public void testNegativeCase() throws Exception {
96         // Setup test data
97         Properties props = new Properties();
98         String tmpdir = System.getProperty("java.io.tmpdir");
99         props.setProperty(StdEngine.PROP_PAP_REPO, tmpdir);
100
101         // Test factory failure cases
102         try {
103             StdEngineFactory factory = new StdEngineFactory();
104             assertNotNull(factory.newEngine());
105             assertNotNull(factory.newEngine(props));
106         } catch (Exception ex) {
107             fail("Not expecting any exceptions: " + ex);
108         }
109
110     }
111
112     @Test
113     public void testException() throws FactoryException, PAPException, IOException {
114         Properties props = new Properties();
115         File myFolder = folder.newFolder("idontexist");
116         props.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath());
117         StdEngineFactory factory = new StdEngineFactory();
118         assertNotNull(factory.newEngine(props));
119
120         //
121         //
122         //
123         myFolder.setReadOnly();
124         assertThat(catchThrowable(() -> { throw new PAPException(); }))
125         .isInstanceOf(PAPException.class);
126
127         //
128         //
129         //
130         File myFile = folder.newFile("iamafile");
131         props.setProperty(StdEngine.PROP_PAP_REPO, myFile.getAbsolutePath());
132         assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
133             factory.newEngine(props)
134         );
135
136         //
137         //
138         //
139         props.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
140         assertNull(factory.newEngine(props));
141
142         //
143         //
144         //
145         System.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
146
147         assertNull(factory.newEngine());
148
149     }
150 }