2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import com.att.research.xacml.util.XACMLProperties;
29 import java.io.ByteArrayInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Modifier;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.LinkedHashMap;
39 import java.util.List;
41 import java.util.Properties;
43 import javax.servlet.ReadListener;
44 import javax.servlet.ServletConfig;
45 import javax.servlet.ServletInputStream;
46 import javax.servlet.ServletOutputStream;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52 import org.apache.log4j.Level;
53 import org.apache.log4j.Logger;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.mockito.Mockito;
57 import org.springframework.mock.web.MockHttpServletResponse;
58 import org.springframework.mock.web.MockServletConfig;
60 public class XacmlRestTest extends Mockito {
61 private static Log logger = LogFactory.getLog(XacmlRestTest.class);
63 private List<String> headers = new ArrayList<>();
65 private HttpServletRequest httpServletRequest;
66 private HttpServletResponse httpServletResponse;
67 private ServletOutputStream mockOutput;
68 private ServletConfig servletConfig;
69 private ServletInputStream servletInputStream;
72 * Prepare for the test.
76 httpServletRequest = Mockito.mock(HttpServletRequest.class);
77 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
78 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
79 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
81 mockOutput = Mockito.mock(ServletOutputStream.class);
83 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
86 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
87 } catch (IOException e) {
91 servletConfig = Mockito.mock(MockServletConfig.class);
92 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
93 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
95 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
96 System.setProperty("xacml.rest.pdp.config", "config_testing");
97 System.setProperty("xacml.rest.pep.idfile", "testclient.properties");
98 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
99 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
100 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
101 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
102 System.setProperty("xacml.rest.pdp.register", "false");
104 servletInputStream = Mockito.mock(ServletInputStream.class);
108 public void testXacmlInit() {
109 logger.info("XACMLRestTest - testInit");
112 XacmlRest.xacmlInit(servletConfig);
113 Logger.getRootLogger().setLevel(Level.DEBUG);
114 XacmlRest.dumpRequest(httpServletRequest);
115 XacmlRest.loadXacmlProperties(null, null);
116 } catch (Exception e) {
117 fail("Normal case of initiation of XACML REST failed");
120 System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
122 XacmlRest.xacmlInit(servletConfig);
123 Logger.getRootLogger().setLevel(Level.DEBUG);
124 XacmlRest.dumpRequest(httpServletRequest);
125 XacmlRest.loadXacmlProperties(null, null);
126 } catch (Exception e) {
127 fail("Normal case of initiation of XACML REST failed");
130 System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
132 Logger.getRootLogger().setLevel(Level.INFO);
133 XacmlRest.xacmlInit(servletConfig);
134 Logger.getRootLogger().setLevel(Level.DEBUG);
135 XacmlRest.dumpRequest(httpServletRequest);
136 XacmlRest.loadXacmlProperties(null, null);
137 } catch (Exception e) {
138 fail("Normal case of initiation of XACML REST failed");
141 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
144 Logger.getRootLogger().setLevel(Level.DEBUG);
145 XacmlRest.xacmlInit(servletConfig);
146 XacmlRest.dumpRequest(httpServletRequest);
147 XacmlRest.loadXacmlProperties(null, null);
148 } catch (Exception e) {
149 fail("Normal case of initiation of XACML REST failed");
152 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn(null);
154 XacmlRest.xacmlInit(servletConfig);
155 Logger.getRootLogger().setLevel(Level.DEBUG);
156 XacmlRest.dumpRequest(httpServletRequest);
157 XacmlRest.loadXacmlProperties(null, null);
158 } catch (Exception e) {
159 fail("Normal case of initiation of XACML REST failed");
163 Logger.getRootLogger().setLevel(Level.INFO);
164 XacmlRest.xacmlInit(servletConfig);
165 Logger.getRootLogger().setLevel(Level.DEBUG);
166 XacmlRest.dumpRequest(httpServletRequest);
167 XacmlRest.loadXacmlProperties(null, null);
168 } catch (Exception e) {
169 fail("Normal case of initiation of XACML REST failed");
172 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
174 List<String> parNameList = new ArrayList<String>() {
175 private static final long serialVersionUID = 1L;
181 add("XACML_PROPERTIES_NAME");
185 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(parNameList));
186 Mockito.when(servletConfig.getInitParameter("Name0")).thenReturn("Value0");
187 Mockito.when(servletConfig.getInitParameter("Name1")).thenReturn("Value1");
188 Mockito.when(servletConfig.getInitParameter("Name2")).thenReturn("Value2");
190 XacmlRest.xacmlInit(servletConfig);
191 Logger.getRootLogger().setLevel(Level.DEBUG);
192 XacmlRest.dumpRequest(httpServletRequest);
193 XacmlRest.loadXacmlProperties(null, null);
194 } catch (Exception e) {
196 fail("Normal case of initiation of XACML REST failed");
202 public void testConstructorIsPrivate()
203 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
204 Constructor<XacmlRestProperties> constructor = XacmlRestProperties.class.getDeclaredConstructor();
205 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
206 constructor.setAccessible(true);
207 constructor.newInstance();
211 public void testLoadXacmlProperties() {
212 XacmlRest.xacmlInit(servletConfig);
213 XacmlRest.loadXacmlProperties(null, null);
215 XacmlRest.loadXacmlProperties(new Properties(), new Properties());
217 Logger.getRootLogger().setLevel(Level.INFO);
218 XacmlRest.loadXacmlProperties(new Properties(), new Properties());
222 public void testDumpRequest() throws IOException {
223 XacmlRest.xacmlInit(servletConfig);
225 Logger.getRootLogger().setLevel(Level.INFO);
226 XacmlRest.dumpRequest(httpServletRequest);
227 Logger.getRootLogger().setLevel(Level.DEBUG);
229 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
230 XacmlRest.dumpRequest(httpServletRequest);
231 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
232 XacmlRest.dumpRequest(httpServletRequest);
233 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
235 List<String> headerNameList = new ArrayList<String>() {
236 private static final long serialVersionUID = 1L;
244 List<String> header0List = new ArrayList<String>() {
245 private static final long serialVersionUID = 1L;
254 List<String> header1List = new ArrayList<String>() {
255 private static final long serialVersionUID = 1L;
264 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headerNameList));
265 Mockito.when(httpServletRequest.getHeaders("Name0")).thenReturn(Collections.enumeration(header0List));
266 Mockito.when(httpServletRequest.getHeaders("Name1")).thenReturn(Collections.enumeration(header1List));
267 XacmlRest.dumpRequest(httpServletRequest);
269 List<String> attributeList = new ArrayList<String>() {
270 private static final long serialVersionUID = 1L;
277 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(attributeList));
278 Mockito.when(httpServletRequest.getAttribute("Attribute0")).thenReturn("AttributeValue0");
279 Mockito.when(httpServletRequest.getAttribute("Attribute1")).thenReturn("AttributeValue1");
280 XacmlRest.dumpRequest(httpServletRequest);
282 Mockito.when(httpServletRequest.getInputStream()).thenReturn(servletInputStream);
283 XacmlRest.dumpRequest(httpServletRequest);
285 Mockito.when(httpServletRequest.getInputStream()).thenThrow(new IOException());
286 XacmlRest.dumpRequest(httpServletRequest);
288 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
289 XacmlRest.dumpRequest(httpServletRequest);
291 Map<String, String[]> parameterMap = new LinkedHashMap<>();
292 String[] mapValue0 = {"MapValue0"};
293 String[] mapValue1 = {"MapValue0"};
294 String[] mapValue2 = {};
295 parameterMap.put("Key0", mapValue0);
296 parameterMap.put("Key1", mapValue1);
297 parameterMap.put("Key2", mapValue2);
299 Mockito.when(httpServletRequest.getMethod()).thenReturn("DELETE");
300 Mockito.when(httpServletRequest.getParameterMap()).thenReturn(parameterMap);
301 XacmlRest.dumpRequest(httpServletRequest);
302 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");