2 * ============LICENSE_START=======================================================
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
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.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;
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;
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;
46 public class StdEngineFactoryTest {
48 private static String systemProperty;
51 public TemporaryFolder folder = new TemporaryFolder();
54 public static void saveSystemProperty() {
55 systemProperty = System.getProperty(StdEngine.PROP_PAP_REPO);
59 * restoreSystemProperty.
62 public static void restoreSystemProperty() {
63 if (systemProperty != null) {
64 System.setProperty(StdEngine.PROP_PAP_REPO, systemProperty);
66 System.clearProperty(StdEngine.PROP_PAP_REPO);
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));
79 StdEngineFactory stdFactoryNew = new StdEngineFactory();
80 System.setProperty("xacml.pap.pdps", "src/test/resources/pdpstest");
81 PAPPolicyEngine engine = stdFactoryNew.newEngine();
82 assertNotNull(engine);
84 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
85 engine.newGroup(null, null)
88 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
89 engine.movePDP(null, null)
95 public void testNegativeCase() throws Exception {
97 Properties props = new Properties();
98 String tmpdir = System.getProperty("java.io.tmpdir");
99 props.setProperty(StdEngine.PROP_PAP_REPO, tmpdir);
101 // Test factory failure cases
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);
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));
123 myFolder.setReadOnly();
124 assertThat(catchThrowable(() -> { throw new PAPException(); }))
125 .isInstanceOf(PAPException.class);
130 File myFile = folder.newFile("iamafile");
131 props.setProperty(StdEngine.PROP_PAP_REPO, myFile.getAbsolutePath());
132 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
133 factory.newEngine(props)
139 props.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
140 assertNull(factory.newEngine(props));
145 System.setProperty(StdEngine.PROP_PAP_REPO, myFolder.getAbsolutePath() + "/badparent/dontexist");
147 assertNull(factory.newEngine());