Unit/SONAR/Checkstyle in ONAP-REST
[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.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;
37 import java.util.Map;
38 import java.util.Properties;
39
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;
45
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;
55
56 public class XacmlRestTest extends Mockito {
57     private static Log logger = LogFactory.getLog(XacmlRestTest.class);
58
59     private List<String> headers = new ArrayList<>();
60
61     private HttpServletRequest httpServletRequest;
62     private HttpServletResponse httpServletResponse;
63     private ServletOutputStream mockOutput;
64     private ServletConfig servletConfig;
65     private ServletInputStream servletInputStream;
66
67     /**
68      * Prepare for the test.
69      */
70     @Before
71     public void setUp() {
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));
76
77         mockOutput = Mockito.mock(ServletOutputStream.class);
78
79         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
80
81         try {
82             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
83         } catch (IOException e) {
84             fail();
85         }
86
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");
90
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");
99
100         servletInputStream = Mockito.mock(ServletInputStream.class);
101     }
102
103     @Test
104     public void testXacmlInit() {
105         logger.info("XACMLRestTest - testInit");
106
107         try {
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");
114         }
115
116         System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
117         try {
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");
124         }
125
126         System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME);
127         try {
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");
135         }
136
137         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.pdp.properties");
138
139         try {
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");
146         }
147
148         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn(null);
149         try {
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");
156         }
157
158         try {
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");
166         }
167
168         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
169
170         List<String> parNameList = new ArrayList<String>() {
171             private static final long serialVersionUID = 1L;
172
173             {
174                 add("Name0");
175                 add("Name1");
176                 add("Name2");
177                 add("XACML_PROPERTIES_NAME");
178             }
179         };
180
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");
185         try {
186             XacmlRest.xacmlInit(servletConfig);
187             Logger.getRootLogger().setLevel(Level.DEBUG);
188             XacmlRest.dumpRequest(httpServletRequest);
189             XacmlRest.loadXacmlProperties(null, null);
190         } catch (Exception e) {
191             e.printStackTrace();
192             fail("Normal case of initiation of XACML REST failed");
193         }
194
195     }
196
197     @Test
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();
204     }
205
206     @Test
207     public void testLoadXacmlProperties() {
208         XacmlRest.xacmlInit(servletConfig);
209         XacmlRest.loadXacmlProperties(null, null);
210
211         XacmlRest.loadXacmlProperties(new Properties(), new Properties());
212
213         Logger.getRootLogger().setLevel(Level.INFO);
214         XacmlRest.loadXacmlProperties(new Properties(), new Properties());
215     }
216
217     @Test
218     public void testDumpRequest() throws IOException {
219         XacmlRest.xacmlInit(servletConfig);
220
221         Logger.getRootLogger().setLevel(Level.INFO);
222         XacmlRest.dumpRequest(httpServletRequest);
223         Logger.getRootLogger().setLevel(Level.DEBUG);
224
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");
230
231         List<String> headerNameList = new ArrayList<String>() {
232             private static final long serialVersionUID = 1L;
233
234             {
235                 add("Name0");
236                 add("Name1");
237             }
238         };
239
240         List<String> header0List = new ArrayList<String>() {
241             private static final long serialVersionUID = 1L;
242
243             {
244                 add("Name0H0");
245                 add("Name0H1");
246                 add("Name0H2");
247             }
248         };
249
250         List<String> header1List = new ArrayList<String>() {
251             private static final long serialVersionUID = 1L;
252
253             {
254                 add("Name1H0");
255                 add("Name1H1");
256                 add("Name1H2");
257             }
258         };
259
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);
264
265         List<String> attributeList = new ArrayList<String>() {
266             private static final long serialVersionUID = 1L;
267
268             {
269                 add("Attribute0");
270                 add("Attribute1");
271             }
272         };
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);
277
278         Mockito.when(httpServletRequest.getInputStream()).thenReturn(servletInputStream);
279         XacmlRest.dumpRequest(httpServletRequest);
280
281         Mockito.when(httpServletRequest.getInputStream()).thenThrow(new IOException());
282         XacmlRest.dumpRequest(httpServletRequest);
283
284         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
285         XacmlRest.dumpRequest(httpServletRequest);
286
287         Map<String, String[]> parameterMap = new LinkedHashMap<>();
288         String[] mapValue0 =
289             { "MapValue0" };
290         String[] mapValue1 =
291             { "MapValue0" };
292         String[] mapValue2 =
293             {};
294         parameterMap.put("Key0", mapValue0);
295         parameterMap.put("Key1", mapValue1);
296         parameterMap.put("Key2", mapValue2);
297
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");
302     }
303 }