2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.xacml.test.std.pap;
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;
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;
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;
44 public class StdEngineFactoryTest {
46 private static String systemProperty;
49 public TemporaryFolder folder = new TemporaryFolder();
52 public static void saveSystemProperty() {
53 systemProperty = System.getProperty(StdEngine.PROP_PAP_REPO);
57 * restoreSystemProperty.
60 public static void restoreSystemProperty() {
61 if (systemProperty != null) {
62 System.setProperty(StdEngine.PROP_PAP_REPO, systemProperty);
64 System.clearProperty(StdEngine.PROP_PAP_REPO);
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));
77 StdEngineFactory stdFactoryNew = new StdEngineFactory();
78 System.setProperty("xacml.pap.pdps", "src/test/resources/pdpstest");
79 PAPPolicyEngine engine = stdFactoryNew.newEngine();
80 assertNotNull(engine);
82 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
83 engine.newGroup(null, null)
86 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
87 engine.movePDP(null, null)
93 public void testNegativeCase() throws Exception {
95 Properties props = new Properties();
96 String tmpdir = System.getProperty("java.io.tmpdir");
97 props.setProperty(StdEngine.PROP_PAP_REPO, tmpdir);
99 // Test factory failure cases
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);
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));
121 myFolder.setReadOnly();
122 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
123 factory.newEngine(props)
129 File myFile = folder.newFile("iamafile");
130 props.setProperty(StdEngine.PROP_PAP_REPO, myFile.getAbsolutePath());
131 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
132 factory.newEngine(props)
138 props.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
139 assertNull(factory.newEngine(props));
144 System.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
146 assertNull(factory.newEngine());