diff options
Diffstat (limited to 'catalog-fe/src')
5 files changed, 275 insertions, 298 deletions
diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java index b095a1cde7..e1b4572a05 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java @@ -21,6 +21,8 @@ package org.openecomp.sdc.fe.impl; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; @@ -41,8 +43,6 @@ import org.openecomp.sdc.fe.config.ConfigurationManager; import org.openecomp.sdc.fe.config.PluginsConfiguration; import org.openecomp.sdc.fe.config.PluginsConfiguration.Plugin; -import java.io.IOException; - public class PluginStatusBL { private static final Logger log = Logger.getLogger(PluginStatusBL.class.getName()); @@ -74,24 +74,23 @@ public class PluginStatusBL { private boolean hasSecuredPlugins() { if (this.getPluginsList() != null) { return pluginsConfiguration.getPluginsList().stream() - .anyMatch(plugin -> plugin.getPluginDiscoveryUrl().toLowerCase().startsWith("https")); + .anyMatch(plugin -> plugin.getPluginDiscoveryUrl().toLowerCase().startsWith("https")); } return false; } - private CloseableHttpClient getPooledClient(final boolean isSecured) throws Exception { + private CloseableHttpClient getPooledClient(boolean isSecured) throws GeneralSecurityException, IOException { final PoolingHttpClientConnectionManager poolingConnManager; - if (isSecured) { - final SSLConnectionSocketFactory s = new SSLConnectionSocketFactory(JettySSLUtils.getSslContext(), new NoopHostnameVerifier()); - final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() - .register("http", new PlainConnectionSocketFactory()) - .register("https", s).build(); - poolingConnManager = new PoolingHttpClientConnectionManager(registry); - } else { + if (!isSecured) { poolingConnManager = new PoolingHttpClientConnectionManager(); + } else { + SSLConnectionSocketFactory s = new SSLConnectionSocketFactory(JettySSLUtils.getSslContext(), new NoopHostnameVerifier()); + Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() + .register("http", new PlainConnectionSocketFactory()).register("https", s).build(); + poolingConnManager = new PoolingHttpClientConnectionManager(registry); } - final int maxTotal = System.getProperties().containsKey(MAX_CONNECTION_POOL) ? Integer.parseInt(System.getProperty(MAX_CONNECTION_POOL)) : 5; - final int routeMax = System.getProperties().containsKey(MAX_ROUTE_POOL) ? Integer.parseInt(System.getProperty(MAX_ROUTE_POOL)) : 20; + int maxTotal = System.getProperties().containsKey(MAX_CONNECTION_POOL) ? Integer.parseInt(System.getProperty(MAX_CONNECTION_POOL)) : 5; + int routeMax = System.getProperties().containsKey(MAX_ROUTE_POOL) ? Integer.parseInt(System.getProperty(MAX_ROUTE_POOL)) : 20; poolingConnManager.setMaxTotal(maxTotal); poolingConnManager.setDefaultMaxPerRoute(routeMax); return HttpClients.custom().setConnectionManager(poolingConnManager).setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); @@ -116,9 +115,9 @@ public class PluginStatusBL { log.debug("The value returned from getConfig is {}", pluginsConfiguration); Integer connectionTimeout = pluginsConfiguration.getConnectionTimeout(); this.requestConfig = RequestConfig.custom().setSocketTimeout(connectionTimeout).setConnectTimeout(connectionTimeout) - .setConnectionRequestTimeout(connectionTimeout).build(); + .setConnectionRequestTimeout(connectionTimeout).build(); Plugin wantedPlugin = pluginsConfiguration.getPluginsList().stream().filter(plugin -> plugin.getPluginId().equals(pluginId)).findAny() - .orElse(null); + .orElse(null); if (wantedPlugin != null) { result = gson.toJson(checkPluginAvailability(wantedPlugin)); } diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/listen/FEAppContextListener.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/listen/FEAppContextListener.java index 79ef07a5da..877d637a3e 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/listen/FEAppContextListener.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/listen/FEAppContextListener.java @@ -38,7 +38,6 @@ public class FEAppContextListener extends AppContextListener implements ServletC private static final int PROBE_INTERVALE = 15; private static Logger log = Logger.getLogger(FEAppContextListener.class.getName()); - @Override public void contextInitialized(ServletContextEvent context) { super.contextInitialized(context); ConfigurationManager configurationManager = new ConfigurationManager(ExternalConfiguration.getConfigurationSource()); @@ -62,7 +61,6 @@ public class FEAppContextListener extends AppContextListener implements ServletC log.debug("After executing {}", this.getClass()); } - @Override public void contextDestroyed(ServletContextEvent context) { ExecutorService executorPool = (ExecutorService) context.getServletContext().getAttribute(Constants.THREAD_EXECUTOR_ATTR); if (executorPool != null) { diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java index 1bec4e48c4..0ef435311f 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java @@ -19,7 +19,13 @@ */ package org.openecomp.sdc.fe.servlets; +import static org.apache.commons.lang3.StringUtils.isEmpty; + import com.google.common.annotations.VisibleForTesting; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Base64; +import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.client.api.Request; @@ -38,13 +44,6 @@ import org.openecomp.sdc.fe.config.PluginsConfiguration.Plugin; import org.openecomp.sdc.fe.impl.LogHandler; import org.openecomp.sdc.fe.utils.BeProtocol; -import javax.servlet.http.HttpServletRequest; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Base64; - -import static org.apache.commons.lang3.StringUtils.isEmpty; - public class FeProxyServlet extends SSLProxyServlet { public static final String UUID = "uuid"; @@ -102,7 +101,7 @@ public class FeProxyServlet extends SSLProxyServlet { BasicAuthConfig basicAuth = config.getBasicAuth(); if (basicAuth.isEnabled()) { proxyRequest.header(HttpHeader.AUTHORIZATION, - "Basic " + Base64.getEncoder().encodeToString((basicAuth.getUserName() + ":" + basicAuth.getUserPass()).getBytes())); + "Basic " + Base64.getEncoder().encodeToString((basicAuth.getUserName() + ":" + basicAuth.getUserPass()).getBytes())); } super.addProxyHeaders(clientRequest, proxyRequest); } @@ -128,7 +127,7 @@ public class FeProxyServlet extends SSLProxyServlet { } private String getModifiedUrl(Configuration config, PluginsConfiguration pluginConf, String uri, String queryString) - throws MalformedURLException { + throws MalformedURLException { if (config == null) { log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "FeProxyServlet getModifiedUrl", "sdc-FE", "failed to retrieve configuration."); throw new RuntimeException("failed to read FE configuration"); @@ -154,8 +153,8 @@ public class FeProxyServlet extends SSLProxyServlet { } else if (uri.contains(WORKFLOW_CONTEXT)) { uri = uri.replace(SDC1_FE_PROXY + WORKFLOW_CONTEXT, WORKFLOW_CONTEXT); String workflowPluginURL = pluginConf.getPluginsList().stream() - .filter(plugin -> plugin.getPluginId().equalsIgnoreCase(PLUGIN_ID_WORKFLOW)).map(Plugin::getPluginDiscoveryUrl).findFirst() - .orElse(null); + .filter(plugin -> plugin.getPluginId().equalsIgnoreCase(PLUGIN_ID_WORKFLOW)).map(Plugin::getPluginDiscoveryUrl).findFirst() + .orElse(null); java.net.URL workflowURL = new URL(workflowPluginURL); protocol = workflowURL.getProtocol(); host = workflowURL.getHost(); @@ -193,7 +192,7 @@ public class FeProxyServlet extends SSLProxyServlet { private PluginsConfiguration getPluginConfiguration(HttpServletRequest request) { return ((ConfigurationManager) request.getSession().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)) - .getPluginsConfiguration(); + .getPluginsConfiguration(); } private boolean isMsToggleOn(Configuration config) { @@ -225,7 +224,7 @@ public class FeProxyServlet extends SSLProxyServlet { String facadeSuffix = String.format("%s%s", FACADE_PATH_IDENTIFIER, CATALOG_REQUEST_IDENTIFIER); String nonFacadeUrl = currentURI.replace(facadeSuffix, "rest/v1/screen"); redirectValue = getModifiedUrl(config, getPluginConfiguration(request), nonFacadeUrl, - "excludeTypes=VFCMT&excludeTypes=Configuration"); + "excludeTypes=VFCMT&excludeTypes=Configuration"); } // Home else if (currentURI.endsWith(HOME_REQUEST_IDENTIFIER)) { @@ -250,10 +249,10 @@ public class FeProxyServlet extends SSLProxyServlet { String facadeSuffix = String.format("%s%s", FACADE_PATH_IDENTIFIER, CATALOG_REQUEST_IDENTIFIER); String nonFacadeUrl = currentURI.replace(facadeSuffix, "rest/v1/screen"); redirectValue = getModifiedUrl(config, getPluginConfiguration(request), nonFacadeUrl, - "excludeTypes=VFCMT&excludeTypes=Configuration"); + "excludeTypes=VFCMT&excludeTypes=Configuration"); } else { String message = String - .format("facade is toggled off, Could not rediret url %s with query params %s", currentURI, getQueryString(request)); + .format("facade is toggled off, Could not rediret url %s with query params %s", currentURI, getQueryString(request)); log.error(message); throw new NotImplementedException(message); } @@ -266,7 +265,7 @@ public class FeProxyServlet extends SSLProxyServlet { if (StringUtils.isEmpty(msUrl)) { // do that only once msUrl = String.format(MS_URL, config.getCatalogFacadeMs().getProtocol(), config.getCatalogFacadeMs().getHost(), - config.getCatalogFacadeMs().getPort()); + config.getCatalogFacadeMs().getPort()); } StringBuilder url; String queryString; @@ -294,7 +293,7 @@ public class FeProxyServlet extends SSLProxyServlet { private Configuration getConfiguration(HttpServletRequest request) { return ((ConfigurationManager) request.getSession().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)) - .getConfiguration(); + .getConfiguration(); } private String getAuthority(String host, String port) { diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/SSLProxyServlet.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/SSLProxyServlet.java index 812be7f8ea..891bc4ae34 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/SSLProxyServlet.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/SSLProxyServlet.java @@ -19,12 +19,10 @@ */ package org.openecomp.sdc.fe.servlets; +import javax.servlet.ServletException; import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic; -import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.proxy.ProxyServlet; import org.eclipse.jetty.util.ssl.SslContextFactory; -import org.onap.config.api.JettySSLUtils; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.fe.config.Configuration; import org.openecomp.sdc.fe.config.ConfigurationManager; @@ -32,17 +30,15 @@ import org.openecomp.sdc.fe.utils.BeProtocol; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.servlet.ServletException; - public abstract class SSLProxyServlet extends ProxyServlet { private static final long serialVersionUID = 1L; - private static final Logger LOGGER = LoggerFactory.getLogger(SSLProxyServlet.class); + private static final Logger log = LoggerFactory.getLogger(SSLProxyServlet.class); @Override protected HttpClient createHttpClient() throws ServletException { Configuration config = ((ConfigurationManager) getServletConfig().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)) - .getConfiguration(); + .getConfiguration(); boolean isSecureClient = !config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName()); HttpClient client = (isSecureClient) ? getSecureHttpClient() : super.createHttpClient(); int requestTimeout = config.getRequestTimeout() * 1000; @@ -51,30 +47,22 @@ public abstract class SSLProxyServlet extends ProxyServlet { } setTimeout(requestTimeout); client.setIdleTimeout(requestTimeout); + client.setStopTimeout(requestTimeout); return client; } private HttpClient getSecureHttpClient() throws ServletException { - final SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(true); - try { - sslContextFactory.setSslContext(JettySSLUtils.getSslContext()); - } catch (Exception e) { - LOGGER.error("Exception thrown while getting SslContext", e); - throw new ServletException(e); - } - final ClientConnector clientConnector = new ClientConnector(); - clientConnector.setSslContextFactory(sslContextFactory); - final HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector)); + // Instantiate HttpClient with the SslContextFactory + final var httpClient = new HttpClient(new SslContextFactory.Client(true)); // Configure HttpClient, for example: httpClient.setFollowRedirects(false); // Start HttpClient try { httpClient.start(); } catch (Exception x) { - LOGGER.error("Exception thrown while starting httpClient", x); + log.error("Exception thrown while starting httpClient", x); throw new ServletException(x); } return httpClient; } - } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java index ef31b49669..36f218a391 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,9 +22,9 @@ package org.openecomp.sdc.fe.servlets; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpFields; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.mockito.Mockito; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.fe.config.Configuration; @@ -41,248 +41,241 @@ import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; public class FeProxyServletTest { - /* - * Example Url Mappings: - * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar - * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar - * http://localhost:8080/sdc1/feProxy/dummy/not/working --> - * http://localhost:8090/sdc2/dummy/not/working - */ - private final FeProxyServletForTest feProxy = new FeProxyServletForTest(); - private static final HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class); - private static final HttpSession httpSession = Mockito.mock(HttpSession.class); - private static final ServletContext servletContext = Mockito.mock(ServletContext.class); - private static final ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); - private static final Configuration configuration = Mockito.mock(Configuration.class); - private static final Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class); - private static final Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = Mockito.mock(Configuration.CatalogFacadeMsConfig.class); - private static final Request proxyRequest = Mockito.spy(Request.class); - private static final HttpFields httpFields = Mockito.mock(HttpFields.class); - private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class); - private static final PluginsConfiguration.Plugin plugin = Mockito.mock(PluginsConfiguration.Plugin.class); - - private static final boolean sslEnabled = false; - private static final String BE_PROTOCOL = "http"; - private static final String BE_PROTOCOL_SSL = "https"; - private static final String BE_HOST = "172.20.43.124"; - private static final int BE_PORT = 8090; - private static final int BE_PORT_SSL = 9443; - private static final String ONBOARDING_BE_PROTOCOL = "http"; - private static final String ONBOARDING_BE_HOST = "172.20.43.125"; - private static final int ONBOARDING_BE_PORT = 8091; - private static final String WF_PROTOCOL = "http"; - private static final String WF_HOST = "172.20.43.126"; - private static final int WF_PORT = 8092; - private static final String HEADER_1 = "Header1"; - private static final String HEADER_2 = "Header2"; - private static final String HEADER_3 = "Header3"; - private static final String HEADER_1_VAL = "Header1_Val"; - private static final String HEADER_2_VAL = "Header2_Val"; - private static final String HEADER_3_VAL = "Header3_Val"; - private static final String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b"; - private static final String msProtocol = "http"; - private static final String msHealth = "/healthCheck"; - private static final String msHost = "localhost"; - private static final Integer msPort = 8080; - private static final String msPath = "/uicache"; - private static final String msUrl = String.format("%s://%s:%s", msProtocol, msHost, msPort); - - @BeforeAll - public static void beforeClass() { - when(servletRequest.getSession()).thenReturn(httpSession); - when(httpSession.getServletContext()).thenReturn(servletContext); - when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager); - when(configurationManager.getConfiguration()).thenReturn(configuration); - when(configuration.getBeProtocol()).thenReturn(sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL); - when(configuration.getBeHost()).thenReturn(BE_HOST); - when(configuration.getBeHttpPort()).thenReturn(BE_PORT); - when(configuration.getBeSslPort()).thenReturn(BE_PORT_SSL); - when(configuration.getOnboarding()).thenReturn(onboardingConfiguration); - when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL); - when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST); - when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT); - - List<String> strList = new ArrayList<>(); - strList.add(HEADER_1); - strList.add(HEADER_2); - strList.add(HEADER_3); - - when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList)); - when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL); - when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL); - when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL); - when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL); - - when(proxyRequest.getHeaders()).thenReturn(httpFields); - when(httpFields.contains(HEADER_1)).thenReturn(true); - when(httpFields.contains(HEADER_2)).thenReturn(true); - when(httpFields.contains(HEADER_3)).thenReturn(false); - - List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>(); - when(plugin.getPluginId()).thenReturn("WORKFLOW"); - when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); - when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); - pluginList.add(plugin); - when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration); - when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList); - - } - - @BeforeEach - public void setUp() { - when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); - when(servletRequest.getQueryString()).thenReturn(null); - when(catalogFacadeMsConfig.getPath()).thenReturn(null); - } - - @Test - void testRewriteURI_APIRequest() { - when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI"); - String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI"; - String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/sdc2/rest/dummyBeAPI"; - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); - - when(servletRequest.getContextPath()).thenReturn("/sdc1"); - when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI"); - - String rewriteURI = feProxy.rewriteTarget(servletRequest); - - assertEquals(expectedChangedUrl, rewriteURI); - } - - @Test - void testRewriteURIWithOnboardingAPIRequest() { - when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF"); - String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF"; - String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF"; - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); - - when(servletRequest.getContextPath()).thenReturn("/sdc1"); - when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF"); - - String rewriteURI = feProxy.rewriteTarget(servletRequest); - - assertEquals(expectedChangedUrl, rewriteURI); - } - - @Test - void testRewriteURIWithQureyParam_APIRequest() { - when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF"); - String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF"; - String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/dcae-api/gg%20g?subtype=VF"; - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); - - when(servletRequest.getContextPath()).thenReturn("/sdc1"); - when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF"); - - String rewriteURI = feProxy.rewriteTarget(servletRequest); - - assertEquals(expectedChangedUrl, rewriteURI); - } - - @Test - void testRewriteTargetWithRedeirectAPIRequest() { - when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF"); - String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF"; - String expectedChangedUrl = (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL) + "://" + BE_HOST + ":" + (sslEnabled ? BE_PORT_SSL : BE_PORT) + "/sdc2/rest/gg%20g?subtype=VF"; - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); - - when(servletRequest.getContextPath()).thenReturn("/sdc1"); - when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF"); - - String rewriteURI = feProxy.rewriteTarget(servletRequest); - - assertEquals(expectedChangedUrl, rewriteURI); - } - - @Test - void testRewriteURIWithWFAPIRequest() { - when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows"); - String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows"; - String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/wf/workflows"; - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); - - when(servletRequest.getContextPath()).thenReturn("/sdc1"); - when(servletRequest.getServletPath()).thenReturn("/feProxy/wf/workflows"); - - String rewriteURI = feProxy.rewriteTarget(servletRequest); - - assertEquals(expectedChangedUrl, rewriteURI); - } - - @Test - void testRedirectToMSWhenMsUrlExists() throws MalformedURLException { - final String urlParams = "x=1&y=2&z=3"; - final String url = "http//test.com:8080/uicache/v1/catalog"; - setUpConfigMocks(); - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); - when(servletRequest.getQueryString()).thenReturn(urlParams); - assertTrue(feProxy.isMsRequest(url + urlParams)); - assertEquals(msUrl + "/uicache/v1/catalog?" + urlParams, - feProxy.redirectMsRequestToMservice(servletRequest, configuration)); - } - - @Test - void testRedirectToMSWhenMsUrlExistsWithoutParams() throws MalformedURLException { - final String uri = "/uicache/v1/home"; - final String url = String.format("http//test.com:8080%s", uri); - setUpConfigMocks(); - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); - when(servletRequest.getRequestURI()).thenReturn(uri); - assertTrue(feProxy.isMsRequest(url)); - assertEquals(msUrl + "/uicache/v1/home", feProxy.redirectMsRequestToMservice(servletRequest, configuration)); - } - - @Test - void testRedirectToBeOnToggleOff() throws MalformedURLException { - final String uri = "/uicache/v1/catalog"; - final String url = String.format("http//test.com:8080%s", uri); - when(catalogFacadeMsConfig.getPath()).thenReturn(null); - - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); - when(servletRequest.getRequestURI()).thenReturn(uri); - assertTrue(feProxy.isMsRequest(url)); - String expectedUrl = String.format("%s://%s:%s/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration", - (sslEnabled ? BE_PROTOCOL_SSL : BE_PROTOCOL), BE_HOST, (sslEnabled ? BE_PORT_SSL : BE_PORT)); - assertEquals(expectedUrl, feProxy.redirectMsRequestToMservice(servletRequest, configuration)); - } - - @Test - void testRedirectToMSWhenMsUrlExistsButItIsNotCatalogRequest() throws MalformedURLException { - final String url = "http//test.com:8080/rest/v1/sc"; - final String urlParams = "x=1&y=2&z=3"; - setUpConfigMocks(); - when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); - when(servletRequest.getQueryString()).thenReturn(urlParams); - assertFalse(feProxy.isMsRequest(url)); - assertThrows(StringIndexOutOfBoundsException.class, () -> feProxy.redirectMsRequestToMservice(servletRequest, configuration)); - } - - private void setUpConfigMocks() { - when(catalogFacadeMsConfig.getPath()).thenReturn(msPath); - when(catalogFacadeMsConfig.getProtocol()).thenReturn(msProtocol); - when(catalogFacadeMsConfig.getHost()).thenReturn(msHost); - when(catalogFacadeMsConfig.getPort()).thenReturn(msPort); - when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(msHealth); - } - - /* class for testing only exposes the protected method.*/ - public static class FeProxyServletForTest extends FeProxyServlet { - private static final long serialVersionUID = 1L; - - @Override - public String rewriteTarget(HttpServletRequest request) { - return super.rewriteTarget(request); - } - - @Override - boolean isMsRequest(String currentUrl) { - return super.isMsRequest(currentUrl); - } - } + /* + * Example Url Mappings: + * http://localhost:8080/sdc1/feProxy/rest/services/MichaelTest2/0.0.1/csar + * --> http://localhost:8090/sdc2/rest/services/MichaelTest2/0.0.1/csar + * http://localhost:8080/sdc1/feProxy/dummy/not/working --> + * http://localhost:8090/sdc2/dummy/not/working + */ + FeProxyServletForTest feProxy = new FeProxyServletForTest(); + final static HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class); + final static HttpSession httpSession = Mockito.mock(HttpSession.class); + final static ServletContext servletContext = Mockito.mock(ServletContext.class); + final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); + final static Configuration configuration = Mockito.mock(Configuration.class); + final static Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class); + private final static Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = Mockito.mock(Configuration.CatalogFacadeMsConfig.class); + final static Request proxyRequest = Mockito.spy(Request.class); + final static HttpFields httpFields = Mockito.mock(HttpFields.class); + private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class); + private static final PluginsConfiguration.Plugin plugin = Mockito.mock(PluginsConfiguration.Plugin.class); + + final static String BE_PROTOCOL = "http"; + final static String BE_HOST = "172.20.43.124"; + final static int BE_PORT = 8090; + final static String ONBOARDING_BE_PROTOCOL = "http"; + final static String ONBOARDING_BE_HOST = "172.20.43.125"; + final static int ONBOARDING_BE_PORT = 8091; + final static String WF_PROTOCOL = "http"; + final static String WF_HOST = "172.20.43.126"; + final static int WF_PORT = 8092; + final static String HEADER_1 = "Header1"; + final static String HEADER_2 = "Header2"; + final static String HEADER_3 = "Header3"; + final static String HEADER_1_VAL = "Header1_Val"; + final static String HEADER_2_VAL = "Header2_Val"; + final static String HEADER_3_VAL = "Header3_Val"; + final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b"; + private final static String msProtocol = "http"; + private final static String msHealth = "/healthCheck"; + private final static String msHost = "localhost"; + private final static Integer msPort = 8080; + private final static String msPath = "/uicache"; + private final static String msUrl = String.format("%s://%s:%s", msProtocol, msHost, msPort); + + @BeforeClass + public static void beforeClass() { + when(servletRequest.getSession()).thenReturn(httpSession); + when(httpSession.getServletContext()).thenReturn(servletContext); + when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager); + when(configurationManager.getConfiguration()).thenReturn(configuration); + when(configuration.getBeProtocol()).thenReturn(BE_PROTOCOL); + when(configuration.getBeHost()).thenReturn(BE_HOST); + when(configuration.getBeHttpPort()).thenReturn(BE_PORT); + when(configuration.getOnboarding()).thenReturn(onboardingConfiguration); + when(configuration.getOnboarding().getProtocolBe()).thenReturn(ONBOARDING_BE_PROTOCOL); + when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST); + when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT); + + List<String> strList = new ArrayList<>(); + strList.add(HEADER_1); + strList.add(HEADER_2); + strList.add(HEADER_3); + + when(servletRequest.getHeaderNames()).thenReturn(Collections.enumeration(strList)); + when(servletRequest.getHeader(HEADER_1)).thenReturn(HEADER_1_VAL); + when(servletRequest.getHeader(HEADER_2)).thenReturn(HEADER_2_VAL); + when(servletRequest.getHeader(HEADER_3)).thenReturn(HEADER_3_VAL); + when(servletRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID_VAL); + + when(proxyRequest.getHeaders()).thenReturn(httpFields); + when(httpFields.containsKey(HEADER_1)).thenReturn(true); + when(httpFields.containsKey(HEADER_2)).thenReturn(true); + when(httpFields.containsKey(HEADER_3)).thenReturn(false); + + List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>(); + when(plugin.getPluginId()).thenReturn("WORKFLOW"); + when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); + when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); + pluginList.add(plugin); + when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration); + when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList); + + } + + @Before + public void setUp() { + when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); + when(servletRequest.getQueryString()).thenReturn(null); + when(catalogFacadeMsConfig.getPath()).thenReturn(null); + } + @Test + public void testRewriteURI_APIRequest() { + when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI"); + String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/dummyBeAPI"; + String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/dummyBeAPI"; + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); + + when(servletRequest.getContextPath()).thenReturn("/sdc1"); + when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/dummyBeAPI"); + + String rewriteURI = feProxy.rewriteTarget(servletRequest); + + assertTrue(rewriteURI.equals(expectedChangedUrl)); + } + + @Test + public void testRewriteURIWithOnboardingAPIRequest() { + when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF"); + String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/onboarding-api/gg%20g?subtype=VF"; + String expectedChangedUrl = ONBOARDING_BE_PROTOCOL + "://" + ONBOARDING_BE_HOST + ":" + ONBOARDING_BE_PORT + "/onboarding-api/gg%20g?subtype=VF"; + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); + + when(servletRequest.getContextPath()).thenReturn("/sdc1"); + when(servletRequest.getServletPath()).thenReturn("/feProxy/onboarding-api/gg%20g?subtype=VF"); + + String rewriteURI = feProxy.rewriteTarget(servletRequest); + + assertTrue(rewriteURI.equals(expectedChangedUrl)); + } + + + @Test + public void testRewriteURIWithQureyParam_APIRequest() { + when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/dcae-api/gg%20g?subtype=VF"); + String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/dcae-api/gg%20g?subtype=VF"; + String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/dcae-api/gg%20g?subtype=VF"; + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); + + when(servletRequest.getContextPath()).thenReturn("/sdc1"); + when(servletRequest.getServletPath()).thenReturn("/feProxy/dcae-api/gg%20g?subtype=VF"); + + String rewriteURI = feProxy.rewriteTarget(servletRequest); + + assertTrue(rewriteURI.equals(expectedChangedUrl)); + } + + @Test + public void testRewriteTargetWithRedeirectAPIRequest() { + when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/gg%20g?subtype=VF"); + String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/rest/gg%20g?subtype=VF"; + String expectedChangedUrl = BE_PROTOCOL + "://" + BE_HOST + ":" + BE_PORT + "/sdc2/rest/gg%20g?subtype=VF"; + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); + + when(servletRequest.getContextPath()).thenReturn("/sdc1"); + when(servletRequest.getServletPath()).thenReturn("/feProxy/rest/gg%20g?subtype=VF"); + + String rewriteURI = feProxy.rewriteTarget(servletRequest); + + assertTrue(rewriteURI.equals(expectedChangedUrl)); + } + + + + @Test + public void testRewriteURIWithWFAPIRequest() { + when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows"); + String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows"; + String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/wf/workflows"; + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); + + when(servletRequest.getContextPath()).thenReturn("/sdc1"); + when(servletRequest.getServletPath()).thenReturn("/feProxy/wf/workflows"); + + String rewriteURI = feProxy.rewriteTarget(servletRequest); + + assertEquals(expectedChangedUrl, rewriteURI); + } + + @Test + public void testRedirectToMSWhenMsUrlExists() throws MalformedURLException { + final String urlParams = "x=1&y=2&z=3"; + final String url = "http//test.com:8080/uicache/v1/catalog"; + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getQueryString()).thenReturn(urlParams); + assertTrue(feProxy.isMsRequest(url + urlParams)); + assertEquals(msUrl + "/uicache/v1/catalog?" + urlParams, + feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + + @Test + public void testRedirectToMSWhenMsUrlExistsWithoutParams() throws MalformedURLException { + final String uri = "/uicache/v1/home"; + final String url = String.format("http//test.com:8080%s", uri); + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getRequestURI()).thenReturn(uri); + assertTrue(feProxy.isMsRequest(url)); + assertEquals(msUrl + "/uicache/v1/home", feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + @Test + public void testRedirectToBeOnToggleOff() throws MalformedURLException { + final String uri = "/uicache/v1/catalog"; + final String url = String.format("http//test.com:8080%s", uri); + when(catalogFacadeMsConfig.getPath()).thenReturn(null); + + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getRequestURI()).thenReturn(uri); + assertTrue(feProxy.isMsRequest(url)); + String expectedUrl = String.format("%s://%s:%s/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration", + BE_PROTOCOL, BE_HOST, BE_PORT); + assertEquals(expectedUrl, feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + @Test(expected = StringIndexOutOfBoundsException.class) + public void testRedirectToMSWhenMsUrlExistsButItIsNotCatalogRequest() throws MalformedURLException { + final String url = "http//test.com:8080/rest/v1/sc"; + final String urlParams = "x=1&y=2&z=3"; + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getQueryString()).thenReturn(urlParams); + assertFalse(feProxy.isMsRequest(url)); + feProxy.redirectMsRequestToMservice(servletRequest, configuration); + } + + private void setUpConfigMocks() { + when(catalogFacadeMsConfig.getPath()).thenReturn(msPath); + when(catalogFacadeMsConfig.getProtocol()).thenReturn(msProtocol); + when(catalogFacadeMsConfig.getHost()).thenReturn(msHost); + when(catalogFacadeMsConfig.getPort()).thenReturn(msPort); + when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(msHealth); + } + + /* class for testing only exposes the protected method.*/ + public static class FeProxyServletForTest extends FeProxyServlet{ + private static final long serialVersionUID = 1L; + @Override + public String rewriteTarget(HttpServletRequest request) { + return super.rewriteTarget(request); + } + @Override + boolean isMsRequest(String currentUrl) { + return super.isMsRequest(currentUrl); + } + } } |