19800b42ead8cf9e08c4e6ed51fa9a6ad1710369
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / XacmlRestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.rest;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import com.att.research.xacml.util.XACMLProperties;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.File;
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;
40 import java.util.Map;
41 import java.util.Properties;
42
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;
49
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;
59
60 public class XacmlRestTest extends Mockito {
61     private static Log logger = LogFactory.getLog(XacmlRestTest.class);
62
63     private List<String> headers = new ArrayList<>();
64
65     private HttpServletRequest httpServletRequest;
66     private HttpServletResponse httpServletResponse;
67     private ServletOutputStream mockOutput;
68     private ServletConfig servletConfig;
69     private ServletInputStream servletInputStream;
70
71     /**
72      * Prepare for the test.
73      */
74     @Before
75     public void setUp() {
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));
80
81         mockOutput = Mockito.mock(ServletOutputStream.class);
82
83         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
84
85         try {
86             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
87         } catch (IOException e) {
88             fail();
89         }
90
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");
94
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");
103
104         servletInputStream = Mockito.mock(ServletInputStream.class);
105     }
106
107     @Test
108     public void testXacmlInit() {
109         logger.info("XACMLRestTest - testInit");
110
111         try {
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");
118         }
119
120         System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
121         try {
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");
128         }
129
130         System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
131         try {
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");
139         }
140
141         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
142
143         try {
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");
150         }
151
152         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn(null);
153         try {
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");
160         }
161
162         try {
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");
170         }
171
172         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
173
174         List<String> parNameList = new ArrayList<String>() {
175             private static final long serialVersionUID = 1L;
176
177             {
178                 add("Name0");
179                 add("Name1");
180                 add("Name2");
181                 add("XACML_PROPERTIES_NAME");
182             }
183         };
184
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");
189         try {
190             XacmlRest.xacmlInit(servletConfig);
191             Logger.getRootLogger().setLevel(Level.DEBUG);
192             XacmlRest.dumpRequest(httpServletRequest);
193             XacmlRest.loadXacmlProperties(null, null);
194         } catch (Exception e) {
195             e.printStackTrace();
196             fail("Normal case of initiation of XACML REST failed");
197         }
198
199     }
200
201     @Test
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();
208     }
209
210     @Test
211     public void testLoadXacmlProperties() {
212         XacmlRest.xacmlInit(servletConfig);
213         XacmlRest.loadXacmlProperties(null, null);
214
215         XacmlRest.loadXacmlProperties(new Properties(), new Properties());
216
217         Logger.getRootLogger().setLevel(Level.INFO);
218         XacmlRest.loadXacmlProperties(new Properties(), new Properties());
219     }
220
221     @Test
222     public void testDumpRequest() throws IOException {
223         XacmlRest.xacmlInit(servletConfig);
224
225         Logger.getRootLogger().setLevel(Level.INFO);
226         XacmlRest.dumpRequest(httpServletRequest);
227         Logger.getRootLogger().setLevel(Level.DEBUG);
228
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");
234
235         List<String> headerNameList = new ArrayList<String>() {
236             private static final long serialVersionUID = 1L;
237
238             {
239                 add("Name0");
240                 add("Name1");
241             }
242         };
243
244         List<String> header0List = new ArrayList<String>() {
245             private static final long serialVersionUID = 1L;
246
247             {
248                 add("Name0H0");
249                 add("Name0H1");
250                 add("Name0H2");
251             }
252         };
253
254         List<String> header1List = new ArrayList<String>() {
255             private static final long serialVersionUID = 1L;
256
257             {
258                 add("Name1H0");
259                 add("Name1H1");
260                 add("Name1H2");
261             }
262         };
263
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);
268
269         List<String> attributeList = new ArrayList<String>() {
270             private static final long serialVersionUID = 1L;
271
272             {
273                 add("Attribute0");
274                 add("Attribute1");
275             }
276         };
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);
281
282         Mockito.when(httpServletRequest.getInputStream()).thenReturn(servletInputStream);
283         XacmlRest.dumpRequest(httpServletRequest);
284
285         Mockito.when(httpServletRequest.getInputStream()).thenThrow(new IOException());
286         XacmlRest.dumpRequest(httpServletRequest);
287
288         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
289         XacmlRest.dumpRequest(httpServletRequest);
290
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);
298
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");
303     }
304 }