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.Before;
26 import org.junit.BeforeClass;
27 import org.junit.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.assertTrue;
45 import static org.mockito.Mockito.when;
47 public class FeProxyServletTest {
49 * Example Url Mappings:
50 * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar
51 * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar
52 * http://localhost:8080/sdc1/feProxy/dummy/not/working -->
53 * http://localhost:8090/sdc2/dummy/not/working
55 FeProxyServletForTest feProxy = new FeProxyServletForTest();
56 final static HttpServletRequest servletRequest = 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 Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class);
62 private final static Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = Mockito.mock(Configuration.CatalogFacadeMsConfig.class);
63 final static Request proxyRequest = Mockito.spy(Request.class);
64 final static HttpFields httpFields = Mockito.mock(HttpFields.class);
65 private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class);
66 private static final PluginsConfiguration.Plugin plugin = Mockito.mock(PluginsConfiguration.Plugin.class);
68 final static String BE_PROTOCOL = "http";
69 final static String BE_HOST = "172.20.43.124";
70 final static int BE_PORT = 8090;
71 final static String ONBOARDING_BE_PROTOCOL = "http";
72 final static String ONBOARDING_BE_HOST = "172.20.43.125";
73 final static int ONBOARDING_BE_PORT = 8091;
74 final static String WF_PROTOCOL = "http";
75 final static String WF_HOST = "172.20.43.126";
76 final static int WF_PORT = 8092;
77 final static String HEADER_1 = "Header1";
78 final static String HEADER_2 = "Header2";
79 final static String HEADER_3 = "Header3";
80 final static String HEADER_1_VAL = "Header1_Val";
81 final static String HEADER_2_VAL = "Header2_Val";
82 final static String HEADER_3_VAL = "Header3_Val";
83 final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b";
84 private final static String msProtocol = "http";
85 private final static String msHealth = "/healthCheck";
86 private final static String msHost = "localhost";
87 private final static Integer msPort = 8080;
88 private final static String msPath = "/uicache";
89 private final static String msUrl = String.format("%s://%s:%s", msProtocol, msHost, msPort);
92 public static void beforeClass() {
93 when(servletRequest.getSession()).thenReturn(httpSession);
94 when(httpSession.getServletContext()).thenReturn(servletContext);
95 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
96 when(configurationManager.getConfiguration()).thenReturn(configuration);
97 when(configuration.getBeProtocol()).thenReturn(BE_PROTOCOL);
98 when(configuration.getBeHost()).thenReturn(BE_HOST);
99 when(configuration.getBeHttpPort()).thenReturn(BE_PORT);
100 when(configuration.getOnboarding()).thenReturn(onboardingConfiguration);
101 when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL);
102 when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST);
103 when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT);
105 List<String> strList = new ArrayList<>();
106 strList.add(HEADER_1);
107 strList.add(HEADER_2);
108 strList.add(HEADER_3);
110 when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList));
111 when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL);
112 when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL);
113 when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL);
114 when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL);
116 when(proxyRequest.getHeaders()).thenReturn(httpFields);
117 when(httpFields.containsKey(HEADER_1)).thenReturn(true);
118 when(httpFields.containsKey(HEADER_2)).thenReturn(true);
119 when(httpFields.containsKey(HEADER_3)).thenReturn(false);
121 List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>();
122 when(plugin.getPluginId()).thenReturn("WORKFLOW");
123 when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT);
124 when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT);
125 pluginList.add(plugin);
126 when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration);
127 when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList);
132 public void setUp() {
133 when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig);
134 when(servletRequest.getQueryString()).thenReturn(null);
135 when(catalogFacadeMsConfig.getPath()).thenReturn(null);
138 public void testRewriteURI_APIRequest() {
139 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI");
140 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI";
141 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/dummyBeAPI";
142 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
144 when(servletRequest.getContextPath()).thenReturn("/sdc1");
145 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI");
147 String rewriteURI = feProxy.rewriteTarget(servletRequest);
149 assertTrue(rewriteURI.equals(expectedChangedUrl));
153 public void testRewriteURIWithOnboardingAPIRequest() {
154 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF");
155 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF";
156 String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF";
157 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
159 when(servletRequest.getContextPath()).thenReturn("/sdc1");
160 when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF");
162 String rewriteURI = feProxy.rewriteTarget(servletRequest);
164 assertTrue(rewriteURI.equals(expectedChangedUrl));
169 public void testRewriteURIWithQureyParam_APIRequest() {
170 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF");
171 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF";
172 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/dcae-api/gg%20g?subtype=VF";
173 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
175 when(servletRequest.getContextPath()).thenReturn("/sdc1");
176 when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF");
178 String rewriteURI = feProxy.rewriteTarget(servletRequest);
180 assertTrue(rewriteURI.equals(expectedChangedUrl));
184 public void testRewriteTargetWithRedeirectAPIRequest() {
185 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF");
186 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF";
187 String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/gg%20g?subtype=VF";
188 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
190 when(servletRequest.getContextPath()).thenReturn("/sdc1");
191 when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF");
193 String rewriteURI = feProxy.rewriteTarget(servletRequest);
195 assertTrue(rewriteURI.equals(expectedChangedUrl));
201 public void testRewriteURIWithWFAPIRequest() {
202 when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows");
203 String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows";
204 String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/wf/workflows";
205 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl));
207 when(servletRequest.getContextPath()).thenReturn("/sdc1");
208 when(servletRequest.getServletPath()).thenReturn("/feProxy/wf/workflows");
210 String rewriteURI = feProxy.rewriteTarget(servletRequest);
212 assertEquals(expectedChangedUrl, rewriteURI);
216 public void testRedirectToMSWhenMsUrlExists() throws MalformedURLException {
217 final String urlParams = "x=1&y=2&z=3";
218 final String url = "http//test.com:8080/uicache/v1/catalog";
220 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
221 when(servletRequest.getQueryString()).thenReturn(urlParams);
222 assertTrue(feProxy.isMsRequest(url + urlParams));
223 assertEquals(msUrl + "/uicache/v1/catalog?" + urlParams,
224 feProxy.redirectMsRequestToMservice(servletRequest, configuration));
228 public void testRedirectToMSWhenMsUrlExistsWithoutParams() throws MalformedURLException {
229 final String uri = "/uicache/v1/home";
230 final String url = String.format("http//test.com:8080%s", uri);
232 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
233 when(servletRequest.getRequestURI()).thenReturn(uri);
234 assertTrue(feProxy.isMsRequest(url));
235 assertEquals(msUrl + "/uicache/v1/home", feProxy.redirectMsRequestToMservice(servletRequest, configuration));
238 public void testRedirectToBeOnToggleOff() throws MalformedURLException {
239 final String uri = "/uicache/v1/catalog";
240 final String url = String.format("http//test.com:8080%s", uri);
241 when(catalogFacadeMsConfig.getPath()).thenReturn(null);
243 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
244 when(servletRequest.getRequestURI()).thenReturn(uri);
245 assertTrue(feProxy.isMsRequest(url));
246 String expectedUrl = String.format("%s://%s:%s/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration",
247 BE_PROTOCOL, BE_HOST, BE_PORT);
248 assertEquals(expectedUrl, feProxy.redirectMsRequestToMservice(servletRequest, configuration));
250 @Test(expected = StringIndexOutOfBoundsException.class)
251 public void testRedirectToMSWhenMsUrlExistsButItIsNotCatalogRequest() throws MalformedURLException {
252 final String url = "http//test.com:8080/rest/v1/sc";
253 final String urlParams = "x=1&y=2&z=3";
255 when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url));
256 when(servletRequest.getQueryString()).thenReturn(urlParams);
257 assertFalse(feProxy.isMsRequest(url));
258 feProxy.redirectMsRequestToMservice(servletRequest, configuration);
261 private void setUpConfigMocks() {
262 when(catalogFacadeMsConfig.getPath()).thenReturn(msPath);
263 when(catalogFacadeMsConfig.getProtocol()).thenReturn(msProtocol);
264 when(catalogFacadeMsConfig.getHost()).thenReturn(msHost);
265 when(catalogFacadeMsConfig.getPort()).thenReturn(msPort);
266 when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(msHealth);
269 /* class for testing only exposes the protected method.*/
270 public static class FeProxyServletForTest extends FeProxyServlet{
271 private static final long serialVersionUID = 1L;
273 public String rewriteTarget(HttpServletRequest request) {
274 return super.rewriteTarget(request);
277 boolean isMsRequest(String currentUrl) {
278 return super.isMsRequest(currentUrl);