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 private String userId = "testId";
55 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
56 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
57 OnapPDP pdp = Mockito.mock(OnapPDP.class);
61 * BeforeClass does some simple code coverage and sets up the
65 public static void setUpBeforeClass() {
67 // Test constructor with bad URL
69 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
70 new RESTfulPAPEngine(null));
72 XACMLProperties.reloadProperties();
76 public static void tearDownAfterClass() {
77 XACMLProperties.reloadProperties();
80 private void setupConnection(int responseCode, String location) throws Exception {
82 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
84 Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
85 Mockito.when(connection.getHeaderField("Location")).thenReturn(location);
87 InputStream mockInputStream = Mockito.mock(InputStream.class);
88 Mockito.when(connection.getInputStream()).thenReturn(mockInputStream);
90 // Set the system property temporarily
91 String systemKey = "xacml.properties";
92 final String oldProperty = System.getProperty(systemKey);
93 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
96 String urlName = "localhost:1234";
97 engine = new RESTfulPAPEngine(urlName) {
99 protected URLConnection makeConnection(String fullUrl) throws IOException {
105 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
107 // Restore the original system property
108 if (oldProperty != null) {
109 System.setProperty(systemKey, oldProperty);
111 System.clearProperty(systemKey);
116 public void testAllTheExceptions() throws Exception {
117 setupConnection(HttpServletResponse.SC_NO_CONTENT, "localhost:5678");
119 engine.setDefaultGroup(group);
120 assertNull(engine.getDefaultGroup());
121 engine.newGroup(name, description);
122 engine.removeGroup(group, newGroup);
123 assertNull(engine.getPDPGroup(pdp));
124 assertNull(engine.getPDPGroup(id));
125 assertNull(engine.getPDP(id));
126 assertNull(engine.getStatus(pdp));
128 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
129 engine.getOnapPDPGroups()
132 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
133 engine.updateGroup(group)
136 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
137 engine.updateGroup(group, "testUserName")
140 assertNull(engine.getGroup(name));
141 engine.movePDP(pdp, newGroup);
143 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
144 engine.newPDP(id, newGroup, name, description, jmxport)
147 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
148 engine.updatePDP(pdp)
151 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
152 engine.removePDP(pdp)
155 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
156 engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
159 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
160 engine.publishPolicy(id, name, false, policy, newGroup)
163 engine.copyFile(id, newGroup, policy, userId);
164 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
165 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
166 engine.copyPolicy(pdpPolicy, newGroup, userId)
169 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
170 engine.removePolicy(null, group)
173 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
174 engine.copyPolicy(null, null, null)
178 // Change the mockito to take a different path
180 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
181 setupConnection(HttpServletResponse.SC_FOUND, "localhost:5678")
184 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
185 setupConnection(200, "localhost:5678")
188 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
189 setupConnection(350, "localhost:5678")
192 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
193 setupConnection(500, "localhost:5678")
196 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
197 setupConnection(350, null)