2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.admin;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24 import java.io.ByteArrayInputStream;
25 import java.io.InputStream;
27 import javax.servlet.http.HttpServletResponse;
28 import java.net.HttpURLConnection;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mockito;
35 import org.onap.policy.rest.adapter.PolicyRestAdapter;
36 import org.onap.policy.xacml.api.pap.OnapPDP;
37 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import com.att.research.xacml.api.pap.PAPException;
42 import com.att.research.xacml.api.pap.PDPPolicy;
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({URL.class, RESTfulPAPEngine.class})
46 public class RESTfulPAPEngineTest {
48 public ExpectedException thrown = ExpectedException.none();
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 public void runConstructor() throws Exception {
64 // Mock url and connection
65 URL url = PowerMockito.mock(URL.class);
66 PowerMockito.whenNew(URL.class).withArguments(Mockito.any()).thenReturn(url);
67 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
68 Mockito.when(url.openConnection()).thenReturn(connection);
69 Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);
71 // Set the system property temporarily
72 String systemKey = "xacml.properties";
73 String oldProperty = System.getProperty(systemKey);
74 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
77 String urlName = "localhost:1234";
78 engine = new RESTfulPAPEngine(urlName);
81 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
83 // Restore the original system property
84 if (oldProperty != null) {
85 System.setProperty(systemKey, oldProperty);
88 System.clearProperty(systemKey);
93 public void testGroups() throws Exception {
94 engine.setDefaultGroup(group);
95 assertEquals(engine.getDefaultGroup(), null);
96 engine.newGroup(name, description);
97 engine.removeGroup(group, newGroup );
98 assertEquals(engine.getPDPGroup(pdp ), null);
99 assertEquals(engine.getPDPGroup(id), null);
100 assertEquals(engine.getPDP(id), null);
101 assertEquals(engine.getStatus(pdp), null);
103 thrown.expect(NullPointerException.class);
104 engine.getOnapPDPGroups();
105 fail("Expecting an exception.");
109 public void testUpdateGroup() throws PAPException {
110 thrown.expect(PAPException.class);
111 engine.updateGroup(group);
112 fail("Expecting an exception.");
116 public void testPDP() throws PAPException {
117 assertEquals(engine.getGroup(name), null);
118 engine.movePDP(pdp, newGroup);
120 thrown.expect(PAPException.class);
121 engine.newPDP(id, newGroup, name, description, jmxport);
122 fail("Expecting an exception.");
126 public void testUpdatePDP() throws PAPException {
127 thrown.expect(NullPointerException.class);
128 engine.updatePDP(pdp);
129 fail("Expecting an exception.");
133 public void testRemovePDP() throws PAPException {
134 thrown.expect(NullPointerException.class);
135 engine.removePDP(pdp);
136 fail("Expecting an exception.");
140 public void testValidatePolicy() throws PAPException {
141 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
143 thrown.expect(PAPException.class);
144 engine.validatePolicyRequest(policyAdapter, policyType);
145 fail("Expecting an exception.");
149 public void testPublishPolicy() throws PAPException {
150 thrown.expect(PAPException.class);
151 engine.publishPolicy(id, name, false, policy, newGroup);
152 fail("Expecting an exception.");
156 public void testCopy() throws PAPException {
157 engine.copyFile(id, newGroup, policy);
158 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
160 thrown.expect(PAPException.class);
161 engine.copyPolicy(pdpPolicy , newGroup);
162 fail("Expecting an exception.");