2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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=========================================================
22 package org.onap.policy.admin;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
29 import javax.servlet.http.HttpServletResponse;
30 import java.net.HttpURLConnection;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.xacml.api.pap.OnapPDP;
39 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import com.att.research.xacml.api.pap.PAPException;
44 import com.att.research.xacml.api.pap.PDPPolicy;
46 @RunWith(PowerMockRunner.class)
47 @PrepareForTest({URL.class, RESTfulPAPEngine.class})
48 public class RESTfulPAPEngineTest {
50 public ExpectedException thrown = ExpectedException.none();
52 private RESTfulPAPEngine engine = null;
53 private String name = "testName";
54 private String id = "testID";
55 private String description = "testDescription";
56 private String policyType = "testType";
57 private String policyContent = "testContent";
58 private int jmxport = 0;
59 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
60 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
61 OnapPDP pdp = Mockito.mock(OnapPDP.class);
65 public void runConstructor() throws Exception {
66 // Mock url and connection
67 URL url = PowerMockito.mock(URL.class);
68 PowerMockito.whenNew(URL.class).withArguments(Mockito.any()).thenReturn(url);
69 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
70 Mockito.when(url.openConnection()).thenReturn(connection);
71 Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);
73 // Set the system property temporarily
74 String systemKey = "xacml.properties";
75 String oldProperty = System.getProperty(systemKey);
76 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
79 String urlName = "localhost:1234";
80 engine = new RESTfulPAPEngine(urlName);
83 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
85 // Restore the original system property
86 if (oldProperty != null) {
87 System.setProperty(systemKey, oldProperty);
89 System.clearProperty(systemKey);
94 public void testGroups() throws Exception {
95 engine.setDefaultGroup(group);
96 assertEquals(engine.getDefaultGroup(), null);
97 engine.newGroup(name, description);
98 engine.removeGroup(group, newGroup);
99 assertEquals(engine.getPDPGroup(pdp), null);
100 assertEquals(engine.getPDPGroup(id), null);
101 assertEquals(engine.getPDP(id), null);
102 assertEquals(engine.getStatus(pdp), null);
104 thrown.expect(NullPointerException.class);
105 engine.getOnapPDPGroups();
106 fail("Expecting an exception.");
110 public void testUpdateGroup() throws PAPException {
111 thrown.expect(PAPException.class);
112 engine.updateGroup(group);
113 fail("Expecting an exception.");
117 public void testPDP() throws PAPException {
118 assertEquals(engine.getGroup(name), null);
119 engine.movePDP(pdp, newGroup);
121 thrown.expect(PAPException.class);
122 engine.newPDP(id, newGroup, name, description, jmxport);
123 fail("Expecting an exception.");
127 public void testUpdatePDP() throws PAPException {
128 thrown.expect(NullPointerException.class);
129 engine.updatePDP(pdp);
130 fail("Expecting an exception.");
134 public void testRemovePDP() throws PAPException {
135 thrown.expect(NullPointerException.class);
136 engine.removePDP(pdp);
137 fail("Expecting an exception.");
141 public void testValidatePolicy() throws PAPException {
142 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
144 thrown.expect(PAPException.class);
145 engine.validatePolicyRequest(policyAdapter, policyType);
146 fail("Expecting an exception.");
150 public void testPublishPolicy() throws PAPException {
151 thrown.expect(PAPException.class);
152 engine.publishPolicy(id, name, false, policy, newGroup);
153 fail("Expecting an exception.");
157 public void testCopy() throws PAPException {
158 engine.copyFile(id, newGroup, policy);
159 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
161 thrown.expect(PAPException.class);
162 engine.copyPolicy(pdpPolicy, newGroup);
163 fail("Expecting an exception.");