2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.servlets;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import javax.servlet.RequestDispatcher;
32 import javax.servlet.ServletContext;
33 import javax.servlet.ServletException;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.ServletResponse;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.http.HttpSession;
39 import javax.ws.rs.core.Application;
41 import org.glassfish.jersey.internal.inject.AbstractBinder;
42 import org.glassfish.jersey.server.ResourceConfig;
43 import org.glassfish.jersey.test.JerseyTest;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47 import org.mockito.invocation.InvocationOnMock;
48 import org.mockito.stubbing.Answer;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.openecomp.sdc.fe.config.Configuration;
51 import org.openecomp.sdc.fe.config.ConfigurationManager;
52 import org.openecomp.sdc.fe.servlets.PortalServlet;
54 public class PortalServletTest extends JerseyTest {
56 final static HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
57 final static HttpSession httpSession = Mockito.mock(HttpSession.class);
58 final static ServletContext servletContext = Mockito.mock(ServletContext.class);
59 final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
60 final static Configuration configuration = Mockito.mock(Configuration.class);
61 final static HttpServletResponse response = Mockito.spy(HttpServletResponse.class);
62 final static RequestDispatcher rd = Mockito.spy(RequestDispatcher.class);
64 @SuppressWarnings("serial")
66 public static void setUpTests() {
67 when(request.getRequestDispatcher(Mockito.anyString())).thenReturn(rd);
68 when(request.getSession()).thenReturn(httpSession);
69 when(httpSession.getServletContext()).thenReturn(servletContext);
70 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
71 when(configurationManager.getConfiguration()).thenReturn(configuration);
72 List<List<String>> mandatoryHeaders = new ArrayList<List<String>>();
73 mandatoryHeaders.add(new ArrayList<String>() {
79 mandatoryHeaders.add(new ArrayList<String>() {
81 add("HTTP_CSP_ATTUID");
85 mandatoryHeaders.add(new ArrayList<String>() {
91 mandatoryHeaders.add(new ArrayList<String>() {
93 add("HTTP_CSP_WSTYPE");
94 add("csp-wstype csp-wstype");
98 List<List<String>> optionalHeaders = new ArrayList<List<String>>();
99 optionalHeaders.add(new ArrayList<String>() {
101 add("HTTP_CSP_FIRSTNAME");
102 add("csp-firstname");
105 optionalHeaders.add(new ArrayList<String>() {
107 add("HTTP_CSP_LASTNAME");
111 optionalHeaders.add(new ArrayList<String>() {
113 add("HTTP_IV_REMOTE_ADDRESS");
114 add("iv-remote-address");
118 when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders);
119 when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders);
124 public void testMissingHeadersRequest() throws IOException {
125 when(request.getHeader(Mockito.anyString())).thenReturn(null);
126 target().path("/portal").request().get();
127 Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG);
128 Mockito.reset(response, rd);
132 public void testSuccesfulRequest() throws IOException, ServletException {
133 Mockito.doAnswer(new Answer<Object>() {
134 public Object answer(InvocationOnMock invocation) {
135 Object[] args = invocation.getArguments();
136 String headerName = (String) args[0];
139 }).when(request).getHeader(Mockito.anyString());
140 target().path("/portal").request().get();
141 verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class));
142 Mockito.reset(response, rd);
146 protected Application configure() {
147 ResourceConfig resourceConfig = new ResourceConfig(PortalServlet.class);
149 resourceConfig.register(new AbstractBinder() {
151 protected void configure() {
152 bind(request).to(HttpServletRequest.class);
153 bind(response).to(HttpServletResponse.class);
157 return resourceConfig;