2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-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.admin;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.junit.Assert.assertNull;
28 import com.att.research.xacml.api.pap.PAPException;
29 import com.att.research.xacml.api.pap.PDPPolicy;
30 import com.att.research.xacml.util.XACMLProperties;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.HttpURLConnection;
35 import java.net.URLConnection;
36 import javax.servlet.http.HttpServletResponse;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.rest.adapter.PolicyRestAdapter;
42 import org.onap.policy.xacml.api.pap.OnapPDP;
43 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
45 public class RESTfulPAPEngineTest {
47 private RESTfulPAPEngine engine = null;
48 private String name = "testName";
49 private String id = "testID";
50 private String description = "testDescription";
51 private String policyType = "testType";
52 private String policyContent = "testContent";
53 private int jmxport = 0;
54 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
55 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
56 OnapPDP pdp = Mockito.mock(OnapPDP.class);
60 * BeforeClass does some simple code coverage and sets up the
64 public static void setUpBeforeClass() {
66 // Test constructor with bad URL
68 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
69 new RESTfulPAPEngine(null));
71 XACMLProperties.reloadProperties();
75 public static void tearDownAfterClass() {
76 XACMLProperties.reloadProperties();
79 private void setupConnection(int responseCode, String location) throws Exception {
81 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
83 Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
84 Mockito.when(connection.getHeaderField("Location")).thenReturn(location);
86 InputStream mockInputStream = Mockito.mock(InputStream.class);
87 Mockito.when(connection.getInputStream()).thenReturn(mockInputStream);
89 // Set the system property temporarily
90 String systemKey = "xacml.properties";
91 final String oldProperty = System.getProperty(systemKey);
92 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
95 String urlName = "localhost:1234";
96 engine = new RESTfulPAPEngine(urlName) {
98 protected URLConnection makeConnection(String fullUrl) throws IOException {
104 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
106 // Restore the original system property
107 if (oldProperty != null) {
108 System.setProperty(systemKey, oldProperty);
110 System.clearProperty(systemKey);
115 public void testAllTheExceptions() throws Exception {
116 setupConnection(HttpServletResponse.SC_NO_CONTENT, "localhost:5678");
118 engine.setDefaultGroup(group);
119 assertNull(engine.getDefaultGroup());
120 engine.newGroup(name, description);
121 engine.removeGroup(group, newGroup);
122 assertNull(engine.getPDPGroup(pdp));
123 assertNull(engine.getPDPGroup(id));
124 assertNull(engine.getPDP(id));
125 assertNull(engine.getStatus(pdp));
127 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
128 engine.getOnapPDPGroups()
131 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
132 engine.updateGroup(group)
135 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
136 engine.updateGroup(group, "testUserName")
139 assertNull(engine.getGroup(name));
140 engine.movePDP(pdp, newGroup);
142 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
143 engine.newPDP(id, newGroup, name, description, jmxport)
146 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
147 engine.updatePDP(pdp)
150 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
151 engine.removePDP(pdp)
154 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
155 engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
158 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
159 engine.publishPolicy(id, name, false, policy, newGroup)
162 engine.copyFile(id, newGroup, policy);
163 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
164 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
165 engine.copyPolicy(pdpPolicy, newGroup)
168 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
169 engine.removePolicy(null, group)
172 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
173 engine.copyPolicy(null, null)
177 // Change the mockito to take a different path
179 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
180 setupConnection(HttpServletResponse.SC_FOUND, "localhost:5678")
183 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
184 setupConnection(200, "localhost:5678")
187 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
188 setupConnection(350, "localhost:5678")
191 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
192 setupConnection(500, "localhost:5678")
195 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
196 setupConnection(350, null)