2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
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;
49 public class XACMLRestTest extends Mockito{
50 private static Log logger = LogFactory.getLog(XACMLRestTest.class);
52 private List<String> headers = new ArrayList<>();
54 private HttpServletRequest httpServletRequest;
55 private HttpServletResponse httpServletResponse;
56 private ServletOutputStream mockOutput;
57 private ServletConfig servletConfig;
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));
67 mockOutput = Mockito.mock(ServletOutputStream.class);
69 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
72 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
73 } catch (IOException e) {
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");
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");
92 public void testXacmlInit(){
93 logger.info("XACMLRestTest - testInit");
95 XACMLRest.xacmlInit(servletConfig);
96 Logger.getRootLogger().setLevel(Level.DEBUG);
97 XACMLRest.dumpRequest(httpServletRequest);
98 XACMLRest.loadXacmlProperties(null, null);
99 } catch (Exception e) {
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();