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;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.HttpURLConnection;
31 import java.net.URLConnection;
32 import javax.servlet.http.HttpServletResponse;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mockito;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.onap.policy.xacml.api.pap.OnapPDP;
42 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
43 import com.att.research.xacml.api.pap.PAPException;
44 import com.att.research.xacml.api.pap.PDPPolicy;
45 import com.att.research.xacml.util.XACMLProperties;
47 public class RESTfulPAPEngineTest {
49 public ExpectedException thrown = ExpectedException.none();
51 private RESTfulPAPEngine engine = null;
52 private String name = "testName";
53 private String id = "testID";
54 private String description = "testDescription";
55 private String policyType = "testType";
56 private String policyContent = "testContent";
57 private int jmxport = 0;
58 OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
59 OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
60 OnapPDP pdp = Mockito.mock(OnapPDP.class);
64 public static void setUpBeforeClass() {
65 XACMLProperties.reloadProperties();
69 public static void tearDownAfterClass() {
70 XACMLProperties.reloadProperties();
74 public void runConstructor() throws Exception {
76 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
77 Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);
79 // Set the system property temporarily
80 String systemKey = "xacml.properties";
81 String oldProperty = System.getProperty(systemKey);
82 System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
85 String urlName = "localhost:1234";
86 engine = new RESTfulPAPEngine(urlName) {
88 protected URLConnection makeConnection(String fullURL) throws IOException {
94 policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
96 // Restore the original system property
97 if (oldProperty != null) {
98 System.setProperty(systemKey, oldProperty);
100 System.clearProperty(systemKey);
105 public void testGroups() throws Exception {
106 engine.setDefaultGroup(group);
107 assertEquals(engine.getDefaultGroup(), null);
108 engine.newGroup(name, description);
109 engine.removeGroup(group, newGroup);
110 assertEquals(engine.getPDPGroup(pdp), null);
111 assertEquals(engine.getPDPGroup(id), null);
112 assertEquals(engine.getPDP(id), null);
113 assertEquals(engine.getStatus(pdp), null);
115 thrown.expect(NullPointerException.class);
116 engine.getOnapPDPGroups();
117 fail("Expecting an exception.");
121 public void testUpdateGroup() throws PAPException {
122 thrown.expect(PAPException.class);
123 engine.updateGroup(group);
124 fail("Expecting an exception.");
128 public void testPDP() throws PAPException {
129 assertEquals(engine.getGroup(name), null);
130 engine.movePDP(pdp, newGroup);
132 thrown.expect(PAPException.class);
133 engine.newPDP(id, newGroup, name, description, jmxport);
134 fail("Expecting an exception.");
138 public void testUpdatePDP() throws PAPException {
139 thrown.expect(NullPointerException.class);
140 engine.updatePDP(pdp);
141 fail("Expecting an exception.");
145 public void testRemovePDP() throws PAPException {
146 thrown.expect(NullPointerException.class);
147 engine.removePDP(pdp);
148 fail("Expecting an exception.");
152 public void testValidatePolicy() throws PAPException {
153 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
155 thrown.expect(PAPException.class);
156 engine.validatePolicyRequest(policyAdapter, policyType);
157 fail("Expecting an exception.");
161 public void testPublishPolicy() throws PAPException {
162 thrown.expect(PAPException.class);
163 engine.publishPolicy(id, name, false, policy, newGroup);
164 fail("Expecting an exception.");
168 public void testCopy() throws PAPException {
169 engine.copyFile(id, newGroup, policy);
170 PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
172 thrown.expect(PAPException.class);
173 engine.copyPolicy(pdpPolicy, newGroup);
174 fail("Expecting an exception.");