From 086406a3af88a1f77e2be783e6f458dc3c66d4ca Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 22 Feb 2021 09:50:07 +0000 Subject: Fix locally failing TCs in catalog-be Migrate to Junit 5 TCs that extends JerseyTest Signed-off-by: Vasyl Razinkov Change-Id: I4c81e70836ee5e634291d74cd1773d221dce3702 Issue-ID: SDC-3377 Signed-off-by: aribeiro --- catalog-fe/pom.xml | 39 +++ .../sdc/fe/servlets/PortalServletTest.java | 293 +++++++++++---------- 2 files changed, 186 insertions(+), 146 deletions(-) (limited to 'catalog-fe') diff --git a/catalog-fe/pom.xml b/catalog-fe/pom.xml index 903466a66c..36e623bb7b 100644 --- a/catalog-fe/pom.xml +++ b/catalog-fe/pom.xml @@ -194,6 +194,12 @@ jetty-proxy ${jetty.version} compile + + + org.eclipse.jetty + jetty-http + + @@ -201,6 +207,12 @@ jetty-servlets ${jetty.version} compile + + + org.eclipse.jetty + jetty-http + + @@ -292,6 +304,16 @@ ${jersey-bom.version} pom test + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-continuation + + @@ -306,6 +328,12 @@ jetty-servlet ${jetty.version} test + + + org.eclipse.jetty + jetty-server + + @@ -313,6 +341,17 @@ jetty-webapp ${jetty.version} test + + + org.eclipse.jetty + jetty-servlet + + + + + org.eclipse.jetty + jetty-http + ${jetty.version} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.java index 35b775a227..b31b2f970e 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.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. @@ -20,18 +20,16 @@ package org.openecomp.sdc.fe.servlets; -import org.glassfish.jersey.internal.inject.AbstractBinder; -import org.glassfish.jersey.server.ResourceConfig; -import org.glassfish.jersey.test.JerseyTest; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Mockito; -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 static org.glassfish.jersey.test.TestProperties.CONTAINER_PORT; +import static org.junit.Assert.assertFalse; +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.io.IOException; +import java.util.ArrayList; +import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -42,162 +40,165 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.ws.rs.core.Application; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import static org.glassfish.jersey.test.TestProperties.CONTAINER_PORT; -import static org.junit.Assert.assertFalse; -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 org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +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; +class PortalServletTest extends JerseyTest { -public class PortalServletTest extends JerseyTest { - - private final static HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + private final static HttpServletRequest request = Mockito.mock(HttpServletRequest.class); private final static HttpSession httpSession = Mockito.mock(HttpSession.class); private final static ServletContext servletContext = Mockito.mock(ServletContext.class); private final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); private final static Configuration configuration = Mockito.mock(Configuration.class); private final static HttpServletResponse response = Mockito.spy(HttpServletResponse.class); private final static RequestDispatcher rd = Mockito.spy(RequestDispatcher.class); - final static Configuration.CookieConfig cookieConfiguration = Mockito.mock(Configuration.CookieConfig.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); - when(configuration.getAuthCookie()).thenReturn(cookieConfiguration); - 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); - - } - - @After - public void tearDown() { - Mockito.reset(response, rd); + final static Configuration.CookieConfig cookieConfiguration = Mockito.mock(Configuration.CookieConfig.class); + + @SuppressWarnings("serial") + @BeforeAll + 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); + when(configuration.getAuthCookie()).thenReturn(cookieConfiguration); + 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); + + } + + @BeforeEach + public void before() throws Exception { + super.setUp(); } - @Test - public void testMissingHeadersRequest() throws IOException { - when(request.getHeader(Mockito.anyString())).thenReturn(null); + @AfterEach + public void tearDown() throws Exception { + super.tearDown(); + Mockito.reset(response, rd); + } + + @Test + void testMissingHeadersRequest() throws IOException { + when(request.getHeader(Mockito.anyString())).thenReturn(null); when(request.getCookies()).thenReturn(getCookies()); target().path("/portal").request().get(); - Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG); - } - - @Test - public void testSuccessfulRequest() throws IOException, ServletException { - ConfigurationManager.setTestInstance(configurationManager); - when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); - when(configuration.getAuthCookie().getPath()).thenReturn("/"); - when(configuration.getAuthCookie().getDomain()).thenReturn(""); - when(configuration.getAuthCookie().getSecurityKey()).thenReturn(""); + Mockito.verify(response, times(1)) + .sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG); + } + + @Test + void testSuccessfulRequest() throws IOException, ServletException { + ConfigurationManager.setTestInstance(configurationManager); + when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); + when(configuration.getAuthCookie().getPath()).thenReturn("/"); + when(configuration.getAuthCookie().getDomain()).thenReturn(""); + when(configuration.getAuthCookie().getSecurityKey()).thenReturn(""); Mockito.doAnswer((Answer) invocation -> { Object[] args = invocation.getArguments(); return (String) args[0]; }).when(request).getHeader(Mockito.anyString()); - target().path("/portal").request().get(); - verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class)); - } - - - @Test - public void testSuccessfullAddofAuthCookie() throws IOException, ServletException { - ConfigurationManager.setTestInstance(configurationManager); - when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); - when(configuration.getAuthCookie().getPath()).thenReturn("/"); - when(configuration.getAuthCookie().getDomain()).thenReturn(""); - when(configuration.getAuthCookie().getSecurityKey()).thenReturn("AGLDdG4D04BKm2IxIWEr8o=="); - PortalServlet pp = new PortalServlet(); - assertTrue(pp.addAuthCookie(response,"user", "test" ,"User")); - } - - @Test - public void testFailureMissingCookieConfiguration() throws IOException { - - //missing configuration mock therefore will fail - PortalServlet pp = new PortalServlet(); - pp.doGet(request,response); - assertFalse(pp.addAuthCookie(response,"user", "test" ,"User")); - - } - - + target().path("/portal").request().get(); + verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class)); + } - @Override - protected Application configure() { - // Use any available port - this allows us to run the BE tests in parallel with this one. - forceSet(CONTAINER_PORT, "0"); - ResourceConfig resourceConfig = new ResourceConfig(PortalServlet.class); + @Test + void testSuccessfullAddofAuthCookie() throws IOException, ServletException { + ConfigurationManager.setTestInstance(configurationManager); + when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); + when(configuration.getAuthCookie().getPath()).thenReturn("/"); + when(configuration.getAuthCookie().getDomain()).thenReturn(""); + when(configuration.getAuthCookie().getSecurityKey()).thenReturn("AGLDdG4D04BKm2IxIWEr8o=="); + PortalServlet pp = new PortalServlet(); + assertTrue(pp.addAuthCookie(response, "user", "test", "User")); + } - resourceConfig.register(new AbstractBinder() { - @Override - protected void configure() { - bind(request).to(HttpServletRequest.class); - bind(response).to(HttpServletResponse.class); - } - }); + @Test + void testFailureMissingCookieConfiguration() throws IOException { + //missing configuration mock therefore will fail + PortalServlet pp = new PortalServlet(); + pp.doGet(request, response); + assertFalse(pp.addAuthCookie(response, "user", "test", "User")); + } - return resourceConfig; - } + @Override + protected Application configure() { + // Use any available port - this allows us to run the BE tests in parallel with this one. + forceSet(CONTAINER_PORT, "0"); + 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; + } private Cookie[] getCookies() { - Cookie[] cookies = new Cookie [1]; + Cookie[] cookies = new Cookie[1]; cookies[0] = new Cookie("someName", "aaa"); return cookies; } -- cgit 1.2.3-korg