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.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
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.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.ExpectedException;
46 import org.mockito.Mockito;
47 import org.onap.policy.rest.adapter.PolicyRestAdapter;
48 import org.onap.policy.xacml.api.pap.OnapPDP;
49 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
51 public class RESTfulPAPEngineTest {
53 public ExpectedException thrown = ExpectedException.none();
55 private RESTfulPAPEngine engine = null;
56 private String name = "testName";
57 private String id = "testID";
58 private String description = "testDescription";
59 private String policyType = "testType";
60 private String policyContent = "testContent";
61 private int jmxport = 0;
62 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
63 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
64 OnapPDP pdp = Mockito.mock(OnapPDP.class);
68 public static void setUpBeforeClass() {
69 XACMLProperties.reloadProperties();
73 public static void tearDownAfterClass() {
74 XACMLProperties.reloadProperties();
78 public void runConstructor() throws Exception {
80 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
81 Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);
83 // Set the system property temporarily
84 String systemKey = "xacml.properties";
85 String oldProperty = System.getProperty(systemKey);
86 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
89 String urlName = "localhost:1234";
90 engine = new RESTfulPAPEngine(urlName) {
92 protected URLConnection makeConnection(String fullURL) throws IOException {
98 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
100 // Restore the original system property
101 if (oldProperty != null) {
102 System.setProperty(systemKey, oldProperty);
104 System.clearProperty(systemKey);
109 public void testGroups() throws Exception {
110 engine.setDefaultGroup(group);
111 assertEquals(engine.getDefaultGroup(), null);
112 engine.newGroup(name, description);
113 engine.removeGroup(group, newGroup);
114 assertEquals(engine.getPDPGroup(pdp), null);
115 assertEquals(engine.getPDPGroup(id), null);
116 assertEquals(engine.getPDP(id), null);
117 assertEquals(engine.getStatus(pdp), null);
119 thrown.expect(NullPointerException.class);
120 engine.getOnapPDPGroups();
121 fail("Expecting an exception.");
125 public void testUpdateGroup() throws PAPException {
126 thrown.expect(PAPException.class);
127 engine.updateGroup(group);
128 fail("Expecting an exception.");
132 public void testPDP() throws PAPException {
133 assertEquals(engine.getGroup(name), null);
134 engine.movePDP(pdp, newGroup);
136 thrown.expect(PAPException.class);
137 engine.newPDP(id, newGroup, name, description, jmxport);
138 fail("Expecting an exception.");
142 public void testUpdatePDP() throws PAPException {
143 thrown.expect(NullPointerException.class);
144 engine.updatePDP(pdp);
145 fail("Expecting an exception.");
149 public void testRemovePDP() throws PAPException {
150 thrown.expect(NullPointerException.class);
151 engine.removePDP(pdp);
152 fail("Expecting an exception.");
156 public void testValidatePolicy() throws PAPException {
157 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
159 thrown.expect(PAPException.class);
160 engine.validatePolicyRequest(policyAdapter, policyType);
161 fail("Expecting an exception.");
165 public void testPublishPolicy() throws PAPException {
166 thrown.expect(PAPException.class);
167 engine.publishPolicy(id, name, false, policy, newGroup);
168 fail("Expecting an exception.");
172 public void testCopy() throws PAPException {
173 engine.copyFile(id, newGroup, policy);
174 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
176 thrown.expect(PAPException.class);
177 engine.copyPolicy(pdpPolicy, newGroup);
178 fail("Expecting an exception.");