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.IOException;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Modifier;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.LinkedHashMap;
36 import java.util.List;
38 import java.util.Properties;
40 import javax.servlet.ServletConfig;
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.apache.log4j.Level;
49 import org.apache.log4j.Logger;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.mockito.Mockito;
53 import org.springframework.mock.web.MockHttpServletResponse;
54 import org.springframework.mock.web.MockServletConfig;
56 public class XacmlRestTest extends Mockito {
57 private static Log logger = LogFactory.getLog(XacmlRestTest.class);
59 private List<String> headers = new ArrayList<>();
61 private HttpServletRequest httpServletRequest;
62 private HttpServletResponse httpServletResponse;
63 private ServletOutputStream mockOutput;
64 private ServletConfig servletConfig;
65 private ServletInputStream servletInputStream;
68 * Prepare for the test.
72 httpServletRequest = Mockito.mock(HttpServletRequest.class);
73 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
74 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
75 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
77 mockOutput = Mockito.mock(ServletOutputStream.class);
79 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
82 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
83 } catch (IOException e) {
87 servletConfig = Mockito.mock(MockServletConfig.class);
88 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
89 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
91 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
92 System.setProperty("xacml.rest.pdp.config", "config_testing");
93 System.setProperty("xacml.rest.pep.idfile", "testclient.properties");
94 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
95 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
96 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
97 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
98 System.setProperty("xacml.rest.pdp.register", "false");
100 servletInputStream = Mockito.mock(ServletInputStream.class);
104 public void testXacmlInit() {
105 logger.info("XACMLRestTest - testInit");
108 XacmlRest.xacmlInit(servletConfig);
109 Logger.getRootLogger().setLevel(Level.DEBUG);
110 XacmlRest.dumpRequest(httpServletRequest);
111 XacmlRest.loadXacmlProperties(null, null);
112 } catch (Exception e) {
113 fail("Normal case of initiation of XACML REST failed");
116 System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
118 XacmlRest.xacmlInit(servletConfig);
119 Logger.getRootLogger().setLevel(Level.DEBUG);
120 XacmlRest.dumpRequest(httpServletRequest);
121 XacmlRest.loadXacmlProperties(null, null);
122 } catch (Exception e) {
123 fail("Normal case of initiation of XACML REST failed");
126 System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
128 Logger.getRootLogger().setLevel(Level.INFO);
129 XacmlRest.xacmlInit(servletConfig);
130 Logger.getRootLogger().setLevel(Level.DEBUG);
131 XacmlRest.dumpRequest(httpServletRequest);
132 XacmlRest.loadXacmlProperties(null, null);
133 } catch (Exception e) {
134 fail("Normal case of initiation of XACML REST failed");
137 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
140 Logger.getRootLogger().setLevel(Level.DEBUG);
141 XacmlRest.xacmlInit(servletConfig);
142 XacmlRest.dumpRequest(httpServletRequest);
143 XacmlRest.loadXacmlProperties(null, null);
144 } catch (Exception e) {
145 fail("Normal case of initiation of XACML REST failed");
148 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn(null);
150 XacmlRest.xacmlInit(servletConfig);
151 Logger.getRootLogger().setLevel(Level.DEBUG);
152 XacmlRest.dumpRequest(httpServletRequest);
153 XacmlRest.loadXacmlProperties(null, null);
154 } catch (Exception e) {
155 fail("Normal case of initiation of XACML REST failed");
159 Logger.getRootLogger().setLevel(Level.INFO);
160 XacmlRest.xacmlInit(servletConfig);
161 Logger.getRootLogger().setLevel(Level.DEBUG);
162 XacmlRest.dumpRequest(httpServletRequest);
163 XacmlRest.loadXacmlProperties(null, null);
164 } catch (Exception e) {
165 fail("Normal case of initiation of XACML REST failed");
168 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
170 List<String> parNameList = new ArrayList<String>() {
171 private static final long serialVersionUID = 1L;
177 add("XACML_PROPERTIES_NAME");
181 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(parNameList));
182 Mockito.when(servletConfig.getInitParameter("Name0")).thenReturn("Value0");
183 Mockito.when(servletConfig.getInitParameter("Name1")).thenReturn("Value1");
184 Mockito.when(servletConfig.getInitParameter("Name2")).thenReturn("Value2");
186 XacmlRest.xacmlInit(servletConfig);
187 Logger.getRootLogger().setLevel(Level.DEBUG);
188 XacmlRest.dumpRequest(httpServletRequest);
189 XacmlRest.loadXacmlProperties(null, null);
190 } catch (Exception e) {
192 fail("Normal case of initiation of XACML REST failed");
198 public void testConstructorIsPrivate() throws NoSuchMethodException, IllegalAccessException,
199 InvocationTargetException, InstantiationException {
200 Constructor<XacmlRestProperties> constructor = XacmlRestProperties.class.getDeclaredConstructor();
201 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
202 constructor.setAccessible(true);
203 constructor.newInstance();
207 public void testLoadXacmlProperties() {
208 XacmlRest.xacmlInit(servletConfig);
209 XacmlRest.loadXacmlProperties(null, null);
211 XacmlRest.loadXacmlProperties(new Properties(), new Properties());
213 Logger.getRootLogger().setLevel(Level.INFO);
214 XacmlRest.loadXacmlProperties(new Properties(), new Properties());
218 public void testDumpRequest() throws IOException {
219 XacmlRest.xacmlInit(servletConfig);
221 Logger.getRootLogger().setLevel(Level.INFO);
222 XacmlRest.dumpRequest(httpServletRequest);
223 Logger.getRootLogger().setLevel(Level.DEBUG);
225 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
226 XacmlRest.dumpRequest(httpServletRequest);
227 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
228 XacmlRest.dumpRequest(httpServletRequest);
229 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
231 List<String> headerNameList = new ArrayList<String>() {
232 private static final long serialVersionUID = 1L;
240 List<String> header0List = new ArrayList<String>() {
241 private static final long serialVersionUID = 1L;
250 List<String> header1List = new ArrayList<String>() {
251 private static final long serialVersionUID = 1L;
260 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headerNameList));
261 Mockito.when(httpServletRequest.getHeaders("Name0")).thenReturn(Collections.enumeration(header0List));
262 Mockito.when(httpServletRequest.getHeaders("Name1")).thenReturn(Collections.enumeration(header1List));
263 XacmlRest.dumpRequest(httpServletRequest);
265 List<String> attributeList = new ArrayList<String>() {
266 private static final long serialVersionUID = 1L;
273 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(attributeList));
274 Mockito.when(httpServletRequest.getAttribute("Attribute0")).thenReturn("AttributeValue0");
275 Mockito.when(httpServletRequest.getAttribute("Attribute1")).thenReturn("AttributeValue1");
276 XacmlRest.dumpRequest(httpServletRequest);
278 Mockito.when(httpServletRequest.getInputStream()).thenReturn(servletInputStream);
279 XacmlRest.dumpRequest(httpServletRequest);
281 Mockito.when(httpServletRequest.getInputStream()).thenThrow(new IOException());
282 XacmlRest.dumpRequest(httpServletRequest);
284 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
285 XacmlRest.dumpRequest(httpServletRequest);
287 Map<String, String[]> parameterMap = new LinkedHashMap<>();
294 parameterMap.put("Key0", mapValue0);
295 parameterMap.put("Key1", mapValue1);
296 parameterMap.put("Key2", mapValue2);
298 Mockito.when(httpServletRequest.getMethod()).thenReturn("DELETE");
299 Mockito.when(httpServletRequest.getParameterMap()).thenReturn(parameterMap);
300 XacmlRest.dumpRequest(httpServletRequest);
301 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");