re base code
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / servlets / FeProxyServletTest.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.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.http.HttpFields;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.openecomp.sdc.fe.config.Configuration;
30 import org.openecomp.sdc.fe.config.ConfigurationManager;
31 import org.openecomp.sdc.fe.servlets.FeProxyServlet;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39
40 import static org.junit.Assert.assertTrue;
41 import static org.mockito.Mockito.*;
42
43 public class FeProxyServletTest {
44         /*
45          * Example Url Mappings:
46          * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar
47          * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar
48          * http://localhost:8080/sdc1/feProxy/dummy/not/working -->
49          * http://localhost:8090/sdc2/dummy/not/working
50          */
51         FeProxyServletForTest feProxy = new FeProxyServletForTest();
52         final static HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
53         final static HttpSession httpSession = Mockito.mock(HttpSession.class);
54         final static ServletContext servletContext = Mockito.mock(ServletContext.class);
55         final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
56         final static Configuration configuration = Mockito.mock(Configuration.class);
57     final static Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class);
58         final static Request proxyRequest = Mockito.spy(Request.class);
59         final static HttpFields httpFields = Mockito.mock(HttpFields.class);
60
61         final static String BE_PROTOCOL = "http";
62         final static String BE_HOST = "172.20.43.124";
63         final static int BE_PORT = 8090;
64         final static String ONBOARDING_BE_PROTOCOL = "http";
65         final static String ONBOARDING_BE_HOST = "172.20.43.125";
66         final static int ONBOARDING_BE_PORT = 8091;
67         final static String HEADER_1 = "Header1";
68         final static String HEADER_2 = "Header2";
69         final static String HEADER_3 = "Header3";
70         final static String HEADER_1_VAL = "Header1_Val";
71         final static String HEADER_2_VAL = "Header2_Val";
72         final static String HEADER_3_VAL = "Header3_Val";
73         final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b";
74
75         @BeforeClass
76         public static void beforeClass() {
77                 when(servletRequest.getSession()).thenReturn(httpSession);
78                 when(httpSession.getServletContext()).thenReturn(servletContext);
79                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
80                 when(configurationManager.getConfiguration()).thenReturn(configuration);
81                 when(configuration.getBeProtocol()).thenReturn(BE_PROTOCOL);
82                 when(configuration.getBeHost()).thenReturn(BE_HOST);
83                 when(configuration.getBeHttpPort()).thenReturn(BE_PORT);
84                 when(configuration.getOnboarding()).thenReturn(onboardingConfiguration);
85                 when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL);
86                 when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST);
87                 when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT);
88
89                 List<String> strList = new ArrayList<String>();
90                 strList.add(HEADER_1);
91                 strList.add(HEADER_2);
92                 strList.add(HEADER_3);
93
94                 when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList));
95                 when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL);
96                 when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL);
97                 when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL);
98                 when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL);
99
100                 when(proxyRequest.getHeaders()).thenReturn(httpFields);
101                 when(httpFields.containsKey(HEADER_1)).thenReturn(true);
102                 when(httpFields.containsKey(HEADER_2)).thenReturn(true);
103                 when(httpFields.containsKey(HEADER_3)).thenReturn(false);
104
105         }
106
107         @Test
108         public void testRewriteURI_APIRequest() {
109                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI");
110                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI";
111                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/dummyBeAPI";
112                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
113
114                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
115                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI");
116
117                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
118
119                 assertTrue(rewriteURI.equals(expectedChangedUrl));
120         }
121
122         @Test
123         public void testRewriteURIWithOnboardingAPIRequest() {
124                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF");
125                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF";
126                 String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF";
127                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
128
129                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
130                 when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF");
131
132                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
133
134                 assertTrue(rewriteURI.equals(expectedChangedUrl));
135         }
136
137
138         @Test
139         public void testRewriteURIWithQureyParam_APIRequest() {
140                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF");
141                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF";
142                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/dcae-api/gg%20g?subtype=VF";
143                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
144
145                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
146                 when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF");
147
148                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
149
150                 assertTrue(rewriteURI.equals(expectedChangedUrl));
151         }
152
153         @Test
154         public void testRewriteTargetWithRedeirectAPIRequest() {
155                 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF");
156                 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF";
157                 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/gg%20g?subtype=VF";
158                 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
159
160                 when(servletRequest.getContextPath()).thenReturn("/sdc1");
161                 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF");
162
163                 String rewriteURI = feProxy.rewriteTarget(servletRequest);
164
165                 assertTrue(rewriteURI.equals(expectedChangedUrl));
166         }
167
168
169
170         @Test
171         public void testCustomizeProxyRequest() {
172                 feProxy.customizeProxyRequest(proxyRequest, servletRequest);
173                 verify(proxyRequest).header(HEADER_3, HEADER_3_VAL);
174                 verify(proxyRequest, times(1)).header(Mockito.anyString(), Mockito.anyString());
175
176         }
177
178         /**
179          * class for testing only exposes the protected method.
180          */
181         public static class FeProxyServletForTest extends FeProxyServlet{
182
183                 @Override
184                 public String rewriteTarget(HttpServletRequest request) {
185                         return super.rewriteTarget(request);
186                 }
187         }
188 }