2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-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.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;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.HttpURLConnection;
36 import java.net.URLConnection;
38 import javax.servlet.http.HttpServletResponse;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
45 import org.onap.policy.xacml.api.pap.OnapPDP;
46 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
48 public class RESTfulPAPEngineTest {
50 private RESTfulPAPEngine engine = null;
51 private String name = "testName";
52 private String id = "testID";
53 private String description = "testDescription";
54 private String policyType = "testType";
55 private String policyContent = "testContent";
56 private int jmxport = 0;
57 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
58 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
59 OnapPDP pdp = Mockito.mock(OnapPDP.class);
63 * BeforeClass does some simple code coverage and sets up the
67 public static void setUpBeforeClass() {
69 // Test constructor with bad URL
71 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
72 new RESTfulPAPEngine(null));
74 XACMLProperties.reloadProperties();
78 public static void tearDownAfterClass() {
79 XACMLProperties.reloadProperties();
82 private void setupConnection(int responseCode) throws Exception {
84 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
85 Mockito.when(connection.getResponseCode()).thenReturn(responseCode);
86 Mockito.when(connection.getHeaderField("Location")).thenReturn("localhost:5678");
88 // Set the system property temporarily
89 String systemKey = "xacml.properties";
90 final String oldProperty = System.getProperty(systemKey);
91 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
94 String urlName = "localhost:1234";
95 engine = new RESTfulPAPEngine(urlName) {
97 protected URLConnection makeConnection(String fullUrl) throws IOException {
103 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
105 // Restore the original system property
106 if (oldProperty != null) {
107 System.setProperty(systemKey, oldProperty);
109 System.clearProperty(systemKey);
114 public void testAllTheExceptions() throws Exception {
115 setupConnection(HttpServletResponse.SC_NO_CONTENT);
117 engine.setDefaultGroup(group);
118 assertNull(engine.getDefaultGroup());
119 engine.newGroup(name, description);
120 engine.removeGroup(group, newGroup);
121 assertNull(engine.getPDPGroup(pdp));
122 assertNull(engine.getPDPGroup(id));
123 assertNull(engine.getPDP(id));
124 assertNull(engine.getStatus(pdp));
126 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
127 engine.getOnapPDPGroups()
130 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
131 engine.updateGroup(group)
134 assertNull(engine.getGroup(name));
135 engine.movePDP(pdp, newGroup);
137 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
138 engine.newPDP(id, newGroup, name, description, jmxport)
141 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
142 engine.updatePDP(pdp)
145 assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
146 engine.removePDP(pdp)
149 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
150 engine.validatePolicyRequest(new PolicyRestAdapter(), policyType)
153 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
154 engine.publishPolicy(id, name, false, policy, newGroup)
157 engine.copyFile(id, newGroup, policy);
158 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
159 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
160 engine.copyPolicy(pdpPolicy, newGroup)
163 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
164 engine.removePolicy(null, group)
167 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
168 engine.copyPolicy(null, null)
172 // Change the mockito to take a different path
174 assertThatExceptionOfType(PAPException.class).isThrownBy(() ->
175 setupConnection(HttpServletResponse.SC_FOUND)