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.fe.servlets;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.http.HttpFields;
25 import org.junit.jupiter.api.BeforeAll;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.Mockito;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.fe.config.Configuration;
31 import org.openecomp.sdc.fe.config.ConfigurationManager;
32 import org.openecomp.sdc.fe.config.PluginsConfiguration;
34 import javax.servlet.ServletContext;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpSession;
37 import java.net.MalformedURLException;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertThrows;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.Mockito.when;
48 public class FeProxyServletTest {
50 * Example Url Mappings:
51 * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar
52 * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar
53 * http://localhost:8080/sdc1/feProxy/dummy/not/working -->
54 * http://localhost:8090/sdc2/dummy/not/working
56 private final FeProxyServletForTest feProxy = new FeProxyServletForTest();
57 private static final HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
58 private static final HttpSession httpSession = Mockito.mock(HttpSession.class);
59 private static final ServletContext servletContext = Mockito.mock(ServletContext.class);
60 private static final ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
61 private static final Configuration configuration = Mockito.mock(Configuration.class);
62 private static final Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class);
63 private static final Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = Mockito.mock(Configuration.CatalogFacadeMsConfig.class);
64 private static final Request proxyRequest = Mockito.spy(Request.class);
65 private static final HttpFields httpFields = Mockito.mock(HttpFields.class);
66 private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class);
67 private static final PluginsConfiguration.Plugin plugin = Mockito.mock(PluginsConfiguration.Plugin.class);
69 private static final boolean sslEnabled = false;
70 private static final String BE_PROTOCOL = "http";
71 private static final String BE_PROTOCOL_SSL = "https";
72 private static final String BE_HOST = "172.20.43.124";
73 private static final int BE_PORT = 8090;
74 private static final int BE_PORT_SSL = 9443;
75 private static final String ONBOARDING_BE_PROTOCOL = "http";
76 private static final String ONBOARDING_BE_HOST = "172.20.43.125";
77 private static final int ONBOARDING_BE_PORT = 8091;
78 private static final String WF_PROTOCOL = "http";
79 private static final String WF_HOST = "172.20.43.126";
80 private static final int WF_PORT = 8092;
81 private static final String HEADER_1 = "Header1";
82 private static final String HEADER_2 = "Header2";
83 private static final String HEADER_3 = "Header3";
84 private static final String HEADER_1_VAL = "Header1_Val";
85 private static final String HEADER_2_VAL = "Header2_Val";
86 private static final String HEADER_3_VAL = "Header3_Val";
87 private static final String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b";
88 private static final String msProtocol = "http";
89 private static final String msHealth = "/healthCheck";
90 private static final String msHost = "localhost";
91 private static final Integer msPort = 8080;
92 private static final String msPath = "/uicache";
93 private static final String msUrl = String.format("%s://%s:%s", msProtocol, msHost, msPort);
96 public static void beforeClass() {
97 when(servletRequest.getSession()).thenReturn(httpSession);
98 when(httpSession.getServletContext()).thenReturn(servletContext);
99 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
100 when(configurationManager.getConfiguration()).thenReturn(configuration);
101 when(configuration.getBeProtocol()).thenReturn(sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL);
102 when(configuration.getBeHost()).thenReturn(BE_HOST);
103 when(configuration.getBeHttpPort()).thenReturn(BE_PORT);
104 when(configuration.getBeSslPort()).thenReturn(BE_PORT_SSL);
105 when(configuration.getOnboarding()).thenReturn(onboardingConfiguration);
106 when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL);
107 when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST);
108 when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT);
110 List<String> strList = new ArrayList<>();
111 strList.add(HEADER_1);
112 strList.add(HEADER_2);
113 strList.add(HEADER_3);
115 when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList));
116 when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL);
117 when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL);
118 when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL);
119 when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL);
121 when(proxyRequest.getHeaders()).thenReturn(httpFields);
122 when(httpFields.contains(HEADER_1)).thenReturn(true);
123 when(httpFields.contains(HEADER_2)).thenReturn(true);
124 when(httpFields.contains(HEADER_3)).thenReturn(false);
126 List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>();
127 when(plugin.getPluginId()).thenReturn("WORKFLOW");
128 when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT);
129 when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT);
130 pluginList.add(plugin);
131 when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration);
132 when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList);
137 public void setUp() {
138 when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
139 when(servletRequest.getQueryString()).thenReturn(null);
140 when(catalogFacadeMsConfig.getPath()).thenReturn(null);
144 void testRewriteURI_APIRequest() {
145 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI");
146 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI";
147 String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/sdc2/rest/dummyBeAPI";
148 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
150 when(servletRequest.getContextPath()).thenReturn("/sdc1");
151 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI");
153 String rewriteURI = feProxy.rewriteTarget(servletRequest);
155 assertEquals(expectedChangedUrl, rewriteURI);
159 void testRewriteURIWithOnboardingAPIRequest() {
160 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF");
161 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF";
162 String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF";
163 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
165 when(servletRequest.getContextPath()).thenReturn("/sdc1");
166 when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF");
168 String rewriteURI = feProxy.rewriteTarget(servletRequest);
170 assertEquals(expectedChangedUrl, rewriteURI);
174 void testRewriteURIWithQureyParam_APIRequest() {
175 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF");
176 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF";
177 String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/dcae-api/gg%20g?subtype=VF";
178 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
180 when(servletRequest.getContextPath()).thenReturn("/sdc1");
181 when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF");
183 String rewriteURI = feProxy.rewriteTarget(servletRequest);
185 assertEquals(expectedChangedUrl, rewriteURI);
189 void testRewriteTargetWithRedeirectAPIRequest() {
190 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF");
191 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF";
192 String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/sdc2/rest/gg%20g?subtype=VF";
193 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
195 when(servletRequest.getContextPath()).thenReturn("/sdc1");
196 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF");
198 String rewriteURI = feProxy.rewriteTarget(servletRequest);
200 assertEquals(expectedChangedUrl, rewriteURI);
204 void testRewriteURIWithWFAPIRequest() {
205 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows");
206 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows";
207 String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/wf/workflows";
208 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
210 when(servletRequest.getContextPath()).thenReturn("/sdc1");
211 when(servletRequest.getServletPath()).thenReturn("/feProxy/wf/workflows");
213 String rewriteURI = feProxy.rewriteTarget(servletRequest);
215 assertEquals(expectedChangedUrl, rewriteURI);
219 void testRedirectToMSWhenMsUrlExists() throws MalformedURLException {
220 final String urlParams = "x=1&y=2&z=3";
221 final String url = "http//test.com:8080/uicache/v1/catalog";
223 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
224 when(servletRequest.getQueryString()).thenReturn(urlParams);
225 assertTrue(feProxy.isMsRequest(url + urlParams));
226 assertEquals(msUrl + "/uicache/v1/catalog?" + urlParams,
227 feProxy.redirectMsRequestToMservice(servletRequest, configuration));
231 void testRedirectToMSWhenMsUrlExistsWithoutParams() throws MalformedURLException {
232 final String uri = "/uicache/v1/home";
233 final String url = String.format("http//test.com:8080%s", uri);
235 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
236 when(servletRequest.getRequestURI()).thenReturn(uri);
237 assertTrue(feProxy.isMsRequest(url));
238 assertEquals(msUrl + "/uicache/v1/home", feProxy.redirectMsRequestToMservice(servletRequest, configuration));
242 void testRedirectToBeOnToggleOff() throws MalformedURLException {
243 final String uri = "/uicache/v1/catalog";
244 final String url = String.format("http//test.com:8080%s", uri);
245 when(catalogFacadeMsConfig.getPath()).thenReturn(null);
247 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
248 when(servletRequest.getRequestURI()).thenReturn(uri);
249 assertTrue(feProxy.isMsRequest(url));
250 String expectedUrl = String.format("%s://%s:%s/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration",
251 (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL), BE_HOST, (sslEnabled ? BE_PORT_SSL : BE_PORT));
252 assertEquals(expectedUrl, feProxy.redirectMsRequestToMservice(servletRequest, configuration));
256 void testRedirectToMSWhenMsUrlExistsButItIsNotCatalogRequest() throws MalformedURLException {
257 final String url = "http//test.com:8080/rest/v1/sc";
258 final String urlParams = "x=1&y=2&z=3";
260 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
261 when(servletRequest.getQueryString()).thenReturn(urlParams);
262 assertFalse(feProxy.isMsRequest(url));
263 assertThrows(StringIndexOutOfBoundsException.class, () -> feProxy.redirectMsRequestToMservice(servletRequest, configuration));
266 private void setUpConfigMocks() {
267 when(catalogFacadeMsConfig.getPath()).thenReturn(msPath);
268 when(catalogFacadeMsConfig.getProtocol()).thenReturn(msProtocol);
269 when(catalogFacadeMsConfig.getHost()).thenReturn(msHost);
270 when(catalogFacadeMsConfig.getPort()).thenReturn(msPort);
271 when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(msHealth);
274 /* class for testing only exposes the protected method.*/
275 public static class FeProxyServletForTest extends FeProxyServlet {
276 private static final long serialVersionUID = 1L;
279 public String rewriteTarget(HttpServletRequest request) {
280 return super.rewriteTarget(request);
284 boolean isMsRequest(String currentUrl) {
285 return super.isMsRequest(currentUrl);