9b4cc3ffbac12fa982c008bf227187d0ba1fe5a0
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.rest;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Modifier;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.log4j.Level;
42 import org.apache.log4j.Logger;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46 import org.springframework.mock.web.MockHttpServletResponse;
47 import org.springframework.mock.web.MockServletConfig;
48
49 public class XACMLRestTest extends Mockito{
50         private static Log logger       = LogFactory.getLog(XACMLRestTest.class);
51
52         private List<String> headers = new ArrayList<>();
53
54         private HttpServletRequest httpServletRequest;
55         private HttpServletResponse httpServletResponse;
56         private ServletOutputStream mockOutput;
57         private ServletConfig servletConfig; 
58
59
60         @Before
61         public void setUp(){
62                 httpServletRequest = Mockito.mock(HttpServletRequest.class);
63                 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
64                 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
65                 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
66
67                 mockOutput = Mockito.mock(ServletOutputStream.class);
68
69                 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
70
71                 try {
72                         Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
73                 } catch (IOException e) {
74                         fail();
75                 }
76
77                 servletConfig = Mockito.mock(MockServletConfig.class);
78                 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
79                 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
80
81                 System.setProperty("xacml.properties", "xacml.pdp.properties");
82                 System.setProperty("xacml.rest.pdp.config", "config_testing");
83                 System.setProperty("xacml.rest.pep.idfile", "testclient.properties"); 
84                 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
85                 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
86                 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
87                 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
88                 System.setProperty("xacml.rest.pdp.register", "false");
89         }
90
91         @Test
92         public void testXacmlInit(){
93                 logger.info("XACMLRestTest - testInit");
94                 try {   
95                         XACMLRest.xacmlInit(servletConfig);
96                         Logger.getRootLogger().setLevel(Level.DEBUG);
97                         XACMLRest.dumpRequest(httpServletRequest);
98                         XACMLRest.loadXacmlProperties(null, null);
99                 } catch (Exception e) {
100                         fail();
101                 }
102         }
103         
104         @Test
105         public void testConstructorIsPrivate() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
106                 Constructor<XACMLRestProperties> constructor = XACMLRestProperties.class.getDeclaredConstructor();
107                 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
108                 constructor.setAccessible(true);
109                 constructor.newInstance();
110         }
111 }