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