CSIT Fix for SDC-2585
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / servlets / PortalServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.openecomp.sdc.servlets;
22
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;
34
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;
43
44 import static org.glassfish.jersey.test.TestProperties.CONTAINER_PORT;
45 import static org.mockito.Mockito.*;
46
47 public class PortalServletTest extends JerseyTest {
48         
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);
56
57         @SuppressWarnings("serial")
58         @BeforeClass
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>() {
67                         {
68                                 add("HTTP_IV_USER");
69                                 add("iv-user");
70                         }
71                 });
72                 mandatoryHeaders.add(new ArrayList<String>() {
73                         {
74                                 add("HTTP_CSP_ATTUID");
75                                 add("csp-attuid");
76                         }
77                 });
78                 mandatoryHeaders.add(new ArrayList<String>() {
79                         {
80                                 add("USER_ID");
81                                 add("csp-userId");
82                         }
83                 });
84                 mandatoryHeaders.add(new ArrayList<String>() {
85                         {
86                                 add("HTTP_CSP_WSTYPE");
87                                 add("csp-wstype csp-wstype");
88                         }
89                 });
90
91                 List<List<String>> optionalHeaders = new ArrayList<>();
92                 optionalHeaders.add(new ArrayList<String>() {
93                         {
94                                 add("HTTP_CSP_FIRSTNAME");
95                                 add("csp-firstname");
96                         }
97                 });
98                 optionalHeaders.add(new ArrayList<String>() {
99                         {
100                                 add("HTTP_CSP_LASTNAME");
101                                 add("csp-lastname");
102                         }
103                 });
104                 optionalHeaders.add(new ArrayList<String>() {
105                         {
106                                 add("HTTP_IV_REMOTE_ADDRESS");
107                                 add("iv-remote-address");
108                         }
109                 });
110
111                 when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders);
112                 when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders);
113
114         }
115
116         @Test
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);
122         }
123
124         @Test
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);
133         }
134
135         @Override
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);
140
141                 resourceConfig.register(new AbstractBinder() {
142                         @Override
143                         protected void configure() {
144                                 bind(request).to(HttpServletRequest.class);
145                                 bind(response).to(HttpServletResponse.class);
146                         }
147                 });
148
149                 return resourceConfig;
150         }
151 }