Added Junis for Policy 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  * ================================================================================
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.fail;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29
30 import javax.servlet.ServletConfig;
31 import javax.servlet.ServletOutputStream;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.log4j.Level;
38 import org.apache.log4j.Logger;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.springframework.mock.web.MockHttpServletResponse;
43 import org.springframework.mock.web.MockServletConfig;
44
45 public class XACMLRestTest extends Mockito{
46         private static Log logger       = LogFactory.getLog(XACMLRestTest.class);
47
48         private List<String> headers = new ArrayList<>();
49
50         private HttpServletRequest httpServletRequest;
51         private HttpServletResponse httpServletResponse;
52         private ServletOutputStream mockOutput;
53         private ServletConfig servletConfig; 
54
55
56         @Before
57         public void setUp(){
58                 httpServletRequest = Mockito.mock(HttpServletRequest.class);
59                 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
60                 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
61                 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
62
63                 mockOutput = Mockito.mock(ServletOutputStream.class);
64
65                 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
66
67                 try {
68                         Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
69                 } catch (IOException e) {
70                         fail();
71                 }
72
73                 servletConfig = Mockito.mock(MockServletConfig.class);
74                 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
75                 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
76
77                 System.setProperty("xacml.properties", "xacml.pdp.properties");
78                 System.setProperty("xacml.rest.pdp.config", "config_testing");
79                 System.setProperty("xacml.rest.pep.idfile", "testclient.properties"); 
80                 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
81                 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
82                 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
83                 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
84                 System.setProperty("xacml.rest.pdp.register", "false");
85         }
86
87         @Test
88         public void testXacmlInit(){
89                 logger.info("XACMLRestTest - testInit");
90                 try {   
91                         XACMLRest.xacmlInit(servletConfig);
92                         Logger.getRootLogger().setLevel(Level.DEBUG);
93                         XACMLRest.dumpRequest(httpServletRequest);
94                         XACMLRest.loadXacmlProperties(null, null);
95                 } catch (Exception e) {
96                         fail();
97                 }
98
99         }
100 }