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 org.glassfish.jersey.internal.inject.AbstractBinder;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.jersey.test.JerseyTest;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.mockito.stubbing.Answer;
30 import org.openecomp.sdc.common.api.Constants;
31 import org.openecomp.sdc.fe.config.Configuration;
32 import org.openecomp.sdc.fe.config.ConfigurationManager;
33 import org.openecomp.sdc.fe.servlets.PortalServlet;
35 import javax.servlet.*;
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;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
44 import static org.glassfish.jersey.test.TestProperties.CONTAINER_PORT;
45 import static org.mockito.Mockito.*;
47 public class PortalServletTest extends JerseyTest {
49 private final static HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
50 private final static HttpSession httpSession = Mockito.mock(HttpSession.class);
51 private final static ServletContext servletContext = Mockito.mock(ServletContext.class);
52 private final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
53 private final static Configuration configuration = Mockito.mock(Configuration.class);
54 private final static HttpServletResponse response = Mockito.spy(HttpServletResponse.class);
55 private final static RequestDispatcher rd = Mockito.spy(RequestDispatcher.class);
57 @SuppressWarnings("serial")
59 public static void setUpTests() {
60 when(request.getRequestDispatcher(Mockito.anyString())).thenReturn(rd);
61 when(request.getSession()).thenReturn(httpSession);
62 when(httpSession.getServletContext()).thenReturn(servletContext);
63 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
64 when(configurationManager.getConfiguration()).thenReturn(configuration);
65 List<List<String>> mandatoryHeaders = new ArrayList<>();
66 mandatoryHeaders.add(new ArrayList<String>() {
72 mandatoryHeaders.add(new ArrayList<String>() {
74 add("HTTP_CSP_ATTUID");
78 mandatoryHeaders.add(new ArrayList<String>() {
84 mandatoryHeaders.add(new ArrayList<String>() {
86 add("HTTP_CSP_WSTYPE");
87 add("csp-wstype csp-wstype");
91 List<List<String>> optionalHeaders = new ArrayList<>();
92 optionalHeaders.add(new ArrayList<String>() {
94 add("HTTP_CSP_FIRSTNAME");
98 optionalHeaders.add(new ArrayList<String>() {
100 add("HTTP_CSP_LASTNAME");
104 optionalHeaders.add(new ArrayList<String>() {
106 add("HTTP_IV_REMOTE_ADDRESS");
107 add("iv-remote-address");
111 when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders);
112 when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders);
117 public void testMissingHeadersRequest() throws IOException {
118 when(request.getHeader(Mockito.anyString())).thenReturn(null);
119 target().path("/portal").request().get();
120 Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG);
121 Mockito.reset(response, rd);
125 public void testSuccessfulRequest() throws IOException, ServletException {
126 Mockito.doAnswer((Answer<Object>) invocation -> {
127 Object[] args = invocation.getArguments();
128 return (String) args[0];
129 }).when(request).getHeader(Mockito.anyString());
130 target().path("/portal").request().get();
131 verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class));
132 Mockito.reset(response, rd);
136 protected Application configure() {
137 // Use any available port - this allows us to run the BE tests in parallel with this one.
138 forceSet(CONTAINER_PORT, "0");
139 ResourceConfig resourceConfig = new ResourceConfig(PortalServlet.class);
141 resourceConfig.register(new AbstractBinder() {
143 protected void configure() {
144 bind(request).to(HttpServletRequest.class);
145 bind(response).to(HttpServletResponse.class);
149 return resourceConfig;