From 451a3400b76511393c62a444f588a4ed15f4a549 Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 19 Feb 2017 10:28:42 +0200 Subject: Initial OpenECOMP SDC commit Change-Id: I0924d5a6ae9cdc161ae17c68d3689a30d10f407b Signed-off-by: Michael Lando --- .../java/org/openecomp/sdc/ApplicationConfig.java | 41 ++++++ .../sdc/ContentDisposiotionDelegator.java | 35 +++++ .../src/test/java/org/openecomp/sdc/Main.java | 63 ++++++++ .../openecomp/sdc/TestExternalConfiguration.java | 66 +++++++++ .../openecomp/sdc/servlets/FeProxyServletTest.java | 140 ++++++++++++++++++ .../openecomp/sdc/servlets/PortalServletTest.java | 159 +++++++++++++++++++++ 6 files changed, 504 insertions(+) create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/ApplicationConfig.java create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/ContentDisposiotionDelegator.java create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/Main.java create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/TestExternalConfiguration.java create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java (limited to 'catalog-fe/src/test/java/org/openecomp') diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/ApplicationConfig.java b/catalog-fe/src/test/java/org/openecomp/sdc/ApplicationConfig.java new file mode 100644 index 0000000000..6297156eb6 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/ApplicationConfig.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc; + +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.media.multipart.MultiPartFeature; + +public class ApplicationConfig extends Application { + + public Set> getClasses() { + final Set> resources = new HashSet>(); + + // Add additional features such as support for Multipart. + resources.add(MultiPartFeature.class); + + return resources; + } + +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/ContentDisposiotionDelegator.java b/catalog-fe/src/test/java/org/openecomp/sdc/ContentDisposiotionDelegator.java new file mode 100644 index 0000000000..9b873879e5 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/ContentDisposiotionDelegator.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc; + +import java.util.Date; + +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; + +public class ContentDisposiotionDelegator extends FormDataContentDisposition { + + public ContentDisposiotionDelegator(String type, String name, String fileName, Date creationDate, + Date modificationDate, Date readDate, long size) { + super(type, name, fileName, creationDate, modificationDate, readDate, size); + // TODO Auto-generated constructor stub + } + +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/Main.java b/catalog-fe/src/test/java/org/openecomp/sdc/Main.java new file mode 100644 index 0000000000..0737b414fd --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/Main.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.util.component.Container.Listener; +import org.eclipse.jetty.webapp.WebAppContext; +import org.openecomp.sdc.fe.listen.FEAppContextListener; + +public class Main { + public static void main(String[] args) throws Exception { + // The port that we should run on can be set into an environment + // variable + // Look for that variable and default to 8080 if it isn't there. + String webPort = System.getenv("PORT"); + if (webPort == null || webPort.isEmpty()) { + webPort = "8080"; + } + // String webPort = "9998"; + + final Server server = new Server(Integer.valueOf(webPort)); + server.addEventListener((Listener) new FEAppContextListener()); + final WebAppContext root = new WebAppContext(); + + root.setContextPath("/sdc1"); + // Parent loader priority is a class loader setting that Jetty accepts. + // By default Jetty will behave like most web containers in that it will + // allow your application to replace non-server libraries that are part + // of the + // container. Setting parent loader priority to true changes this + // behavior. + // Read more here: + // http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading + root.setParentLoaderPriority(true); + + final String webappDirLocation = "src/main/webapp/"; + root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml"); + root.setResourceBase(webappDirLocation); + + server.setHandler(root); + + server.start(); + server.join(); + } +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/TestExternalConfiguration.java b/catalog-fe/src/test/java/org/openecomp/sdc/TestExternalConfiguration.java new file mode 100644 index 0000000000..ecbc055a81 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/TestExternalConfiguration.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc; + +import java.io.IOException; + +import org.openecomp.sdc.common.api.BasicConfiguration; +import org.openecomp.sdc.common.api.ConfigurationListener; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.api.FileChangeCallback; +import org.openecomp.sdc.common.impl.ConfigFileChangeListener; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.fe.config.Configuration; + +public class TestExternalConfiguration { + + public static void main(String[] args) throws IOException { + + ExternalConfiguration.setAppName("catalog-server"); + ExternalConfiguration + .setConfigDir("C:\\Users\\esofer\\workspaceLuna\\catalog-server\\src\\test\\resources\\config"); + ExternalConfiguration.listenForChanges(); + + ConfigurationListener configurationListener = new ConfigurationListener(Configuration.class, + new FileChangeCallback() { + + @Override + public void reconfigure(BasicConfiguration obj) { + // TODO Auto-generated method stub + + } + }); + + ConfigurationSource configurationSource1 = new FSConfigurationSource(new ConfigFileChangeListener(), + ExternalConfiguration.getConfigDir()); + configurationSource1.getAndWatchConfiguration(Configuration.class, configurationListener); + + try { + Thread.currentThread().sleep(100 * 1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java new file mode 100644 index 0000000000..4c4eb1ca2a --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java @@ -0,0 +1,140 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.servlets; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.http.HttpFields; +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; +import org.openecomp.sdc.fe.config.ConfigurationManager; +import org.openecomp.sdc.fe.servlets.FeProxyServlet; + +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 + */ + FeProxyServlet feProxy = new FeProxyServlet(); + 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 Request proxyRequest = Mockito.spy(Request.class); + final static HttpFields httpFields = Mockito.mock(HttpFields.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 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"; + + @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); + + List 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); + + } + + @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"); + + URI rewriteURI = feProxy.rewriteURI(servletRequest); + + assertTrue(rewriteURI.toString().equals(expectedChangedUrl)); + } + + @Test + public void testRewriteURIWithQureyParam_APIRequest() { + 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"); + + URI rewriteURI = feProxy.rewriteURI(servletRequest); + + assertTrue(rewriteURI.toString().equals(expectedChangedUrl)); + } + + @Test + public void testCustomizeProxyRequest() { + feProxy.customizeProxyRequest(proxyRequest, servletRequest); + verify(proxyRequest).header(HEADER_3, HEADER_3_VAL); + verify(proxyRequest, times(1)).header(Mockito.anyString(), Mockito.anyString()); + + } +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java new file mode 100644 index 0000000000..f2ff870a91 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.servlets; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.ws.rs.core.Application; + +import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.fe.config.Configuration; +import org.openecomp.sdc.fe.config.ConfigurationManager; +import org.openecomp.sdc.fe.servlets.PortalServlet; + +public class PortalServletTest extends JerseyTest { + + final static HttpServletRequest request = 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 HttpServletResponse response = Mockito.spy(HttpServletResponse.class); + final static RequestDispatcher rd = Mockito.spy(RequestDispatcher.class); + + @SuppressWarnings("serial") + @BeforeClass + public static void setUpTests() { + when(request.getRequestDispatcher(Mockito.anyString())).thenReturn(rd); + when(request.getSession()).thenReturn(httpSession); + when(httpSession.getServletContext()).thenReturn(servletContext); + when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager); + when(configurationManager.getConfiguration()).thenReturn(configuration); + List> mandatoryHeaders = new ArrayList>(); + mandatoryHeaders.add(new ArrayList() { + { + add("HTTP_IV_USER"); + add("iv-user"); + } + }); + mandatoryHeaders.add(new ArrayList() { + { + add("HTTP_CSP_ATTUID"); + add("csp-attuid"); + } + }); + mandatoryHeaders.add(new ArrayList() { + { + add("USER_ID"); + add("csp-userId"); + } + }); + mandatoryHeaders.add(new ArrayList() { + { + add("HTTP_CSP_WSTYPE"); + add("csp-wstype csp-wstype"); + } + }); + + List> optionalHeaders = new ArrayList>(); + optionalHeaders.add(new ArrayList() { + { + add("HTTP_CSP_FIRSTNAME"); + add("csp-firstname"); + } + }); + optionalHeaders.add(new ArrayList() { + { + add("HTTP_CSP_LASTNAME"); + add("csp-lastname"); + } + }); + optionalHeaders.add(new ArrayList() { + { + add("HTTP_IV_REMOTE_ADDRESS"); + add("iv-remote-address"); + } + }); + + when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders); + when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders); + + } + + @Test + public void testMissingHeadersRequest() throws IOException { + when(request.getHeader(Mockito.anyString())).thenReturn(null); + target().path("/portal").request().get(); + Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG); + Mockito.reset(response, rd); + } + + @Test + public void testSuccesfulRequest() throws IOException, ServletException { + Mockito.doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + String headerName = (String) args[0]; + return headerName; + } + }).when(request).getHeader(Mockito.anyString()); + target().path("/portal").request().get(); + verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class)); + Mockito.reset(response, rd); + } + + @Override + protected Application configure() { + ResourceConfig resourceConfig = new ResourceConfig(PortalServlet.class); + + resourceConfig.register(new AbstractBinder() { + @Override + protected void configure() { + bind(request).to(HttpServletRequest.class); + bind(response).to(HttpServletResponse.class); + } + }); + + return resourceConfig; + } +} -- cgit 1.2.3-korg