Sync Integ to Master
[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 static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
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;
40
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;
53
54 public class PortalServletTest extends JerseyTest {
55         
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);
63
64         @SuppressWarnings("serial")
65         @BeforeClass
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>() {
74                         {
75                                 add("HTTP_IV_USER");
76                                 add("iv-user");
77                         }
78                 });
79                 mandatoryHeaders.add(new ArrayList<String>() {
80                         {
81                                 add("HTTP_CSP_ATTUID");
82                                 add("csp-attuid");
83                         }
84                 });
85                 mandatoryHeaders.add(new ArrayList<String>() {
86                         {
87                                 add("USER_ID");
88                                 add("csp-userId");
89                         }
90                 });
91                 mandatoryHeaders.add(new ArrayList<String>() {
92                         {
93                                 add("HTTP_CSP_WSTYPE");
94                                 add("csp-wstype csp-wstype");
95                         }
96                 });
97
98                 List<List<String>> optionalHeaders = new ArrayList<List<String>>();
99                 optionalHeaders.add(new ArrayList<String>() {
100                         {
101                                 add("HTTP_CSP_FIRSTNAME");
102                                 add("csp-firstname");
103                         }
104                 });
105                 optionalHeaders.add(new ArrayList<String>() {
106                         {
107                                 add("HTTP_CSP_LASTNAME");
108                                 add("csp-lastname");
109                         }
110                 });
111                 optionalHeaders.add(new ArrayList<String>() {
112                         {
113                                 add("HTTP_IV_REMOTE_ADDRESS");
114                                 add("iv-remote-address");
115                         }
116                 });
117
118                 when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders);
119                 when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders);
120
121         }
122
123         @Test
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);
129         }
130
131         @Test
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];
137                                 return headerName;
138                         }
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);
143         }
144
145         @Override
146         protected Application configure() {
147                 ResourceConfig resourceConfig = new ResourceConfig(PortalServlet.class);
148
149                 resourceConfig.register(new AbstractBinder() {
150                         @Override
151                         protected void configure() {
152                                 bind(request).to(HttpServletRequest.class);
153                                 bind(response).to(HttpServletResponse.class);
154                         }
155                 });
156
157                 return resourceConfig;
158         }
159 }