From 375f0dc149934b1c0c71b927199234ea5003b22d Mon Sep 17 00:00:00 2001 From: e550448 Date: Tue, 31 Dec 2024 01:07:18 +0100 Subject: [PATCH] refactor: Used DEEP_STUBS for chained mocking Refs: #237 --- .../rest/filter/AuthenticationFilterTest.java | 69 +++++++++---------- .../generic/rest/filter/CorsFilterTest.java | 16 ++--- .../generic/util/RequestContextUtilTest.java | 10 +-- 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/AuthenticationFilterTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/AuthenticationFilterTest.java index 7e00c35..5760ae1 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/AuthenticationFilterTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/AuthenticationFilterTest.java @@ -5,14 +5,13 @@ import ch.sbb.polarion.extension.generic.auth.ValidatorType; import com.polarion.core.config.Configuration; import com.polarion.core.config.IConfiguration; -import com.polarion.core.config.IRestConfiguration; import com.polarion.platform.security.AuthenticationFailedException; import com.polarion.platform.security.ISecurityService; -import com.polarion.platform.security.login.AccessToken; import com.polarion.platform.security.login.ILogin; import com.polarion.platform.security.login.IToken; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; @@ -34,22 +33,14 @@ class AuthenticationFilterTest { @Mock private ContainerRequestContext requestContext; - @Mock + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ISecurityService securityService; @Mock - private ILogin login; - @Mock - private ILogin.IBase base; - @Mock - private ILogin.IUsingAuthenticator authenticator; - @Mock private ILogin.IFinal loginFinal; - @Mock + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private HttpServletRequest httpServletRequest; - @Mock + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private IConfiguration configuration; - @Mock - private IRestConfiguration restConfiguration; @Test void filterRequestWithoutAuthorizationHeaderAndXsrfHeader() { @@ -76,14 +67,15 @@ void filterRequestWithoutBearerInAuthorizationHeader() { } @Test + @SuppressWarnings("unchecked") void filterRequestWithValidBearerToken() throws IOException, AuthenticationFailedException { when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer token"); when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn(null); - when(securityService.login()).thenReturn(login); - when(login.from("REST")).thenReturn(base); - when(base.authenticator(any())).thenReturn(authenticator); - when(authenticator.with((IToken) any())).thenReturn(loginFinal); + when(securityService.login() + .from("REST") + .authenticator(any()) + .with(any(IToken.class))).thenReturn(loginFinal); Subject subject = new Subject(); when(loginFinal.perform()).thenReturn(subject); @@ -95,14 +87,15 @@ void filterRequestWithValidBearerToken() throws IOException, AuthenticationFaile @Test + @SuppressWarnings("unchecked") void filterRequestWithFailedAuthentication() throws AuthenticationFailedException { when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer failed_token"); when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn(null); - when(securityService.login()).thenReturn(login); - when(login.from("REST")).thenReturn(base); - when(base.authenticator(any())).thenReturn(authenticator); - when(authenticator.with((IToken) any())).thenReturn(loginFinal); + when(securityService.login() + .from("REST") + .authenticator(any()) + .with(any(IToken.class))).thenReturn(loginFinal); when(loginFinal.perform()).thenThrow(new AuthenticationFailedException("Something went wrong")); @@ -114,6 +107,7 @@ void filterRequestWithFailedAuthentication() throws AuthenticationFailedExceptio } @Test + @SuppressWarnings("unused") void filterRequestWithValidXsrfToken() throws IOException, AuthenticationFailedException { when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn(null); when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("validXsrfToken"); @@ -144,12 +138,13 @@ void filterRequestWithInvalidXsrfToken() { try (MockedStatic configurationMockedStatic = mockStatic(Configuration.class)) { configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration); - when(configuration.rest()).thenReturn(restConfiguration); - when(restConfiguration.restApiTokenEnabled()).thenReturn(true); + when(configuration + .rest() + .restApiTokenEnabled()).thenReturn(true); - Principal userPrincipal = mock(Principal.class); - when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal); - when(userPrincipal.getName()).thenReturn("user"); + when(httpServletRequest + .getUserPrincipal() + .getName()).thenReturn("user"); AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest); @@ -165,12 +160,13 @@ void filterRequestWithXsrfTokenButConfigurationIsDisabled() { when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("xsrf_token"); try (MockedStatic configurationMockedStatic = mockStatic(Configuration.class)) { configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration); - when(configuration.rest()).thenReturn(restConfiguration); - when(restConfiguration.restApiTokenEnabled()).thenReturn(false); + when(configuration + .rest() + .restApiTokenEnabled()).thenReturn(false); - Principal userPrincipal = mock(Principal.class); - when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal); - when(userPrincipal.getName()).thenReturn("user"); + when(httpServletRequest + .getUserPrincipal() + .getName()).thenReturn("user"); AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest); @@ -186,12 +182,13 @@ void filterRequestWithXsrfTokenForDifferentUser() { when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("xsrf_token_for_different_user"); try (MockedStatic configurationMockedStatic = mockStatic(Configuration.class)) { configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration); - when(configuration.rest()).thenReturn(restConfiguration); - when(restConfiguration.restApiTokenEnabled()).thenReturn(true); + when(configuration + .rest() + .restApiTokenEnabled()).thenReturn(true); - Principal userPrincipal = mock(Principal.class); - when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal); - when(userPrincipal.getName()).thenReturn("different_user"); + when(httpServletRequest + .getUserPrincipal() + .getName()).thenReturn("different_user"); AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest); diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/CorsFilterTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/CorsFilterTest.java index dc3c801..cdca8c4 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/CorsFilterTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/CorsFilterTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.Answers; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockedStatic; @@ -45,16 +44,14 @@ class CorsFilterTest { private ContainerRequestContext requestContext; @Mock private ContainerResponseContext responseContext; - - @Mock - private IConfiguration configuration; @Mock private IRestConfiguration restConfiguration; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) + @Mock MockedStatic configurationMockedStatic; @BeforeEach void setUp() throws MalformedURLException { + IConfiguration configuration = mock(IConfiguration.class); configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration); lenient().when(configuration.rest()).thenReturn(restConfiguration); lenient().when(configuration.getBaseURL()).thenReturn(URI.create(LOCALHOST_8080).toURL()); @@ -99,8 +96,7 @@ void requestOriginNotAllowed(String input) throws URISyntaxException { when(requestContext.getMethod()).thenReturn(HttpMethod.GET); // no CORS enabled - HashSet corsAllowedOrigins = new HashSet<>(); - Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o)); + HashSet corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(","))); when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins); CorsFilter corsFilter = new CorsFilter(); @@ -122,8 +118,7 @@ void requestOriginAllowed(String input) throws URISyntaxException { when(requestContext.getHeaderString(CorsFilter.ORIGIN)).thenReturn(LOCALHOST_1111); when(requestContext.getMethod()).thenReturn(HttpMethod.GET); - HashSet corsAllowedOrigins = new HashSet<>(); - Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o)); + HashSet corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(","))); when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins); CorsFilter corsFilter = new CorsFilter(); @@ -139,8 +134,7 @@ void testContainerResponseFilter(String input) throws URISyntaxException { when(uriInfo.getRequestUri()).thenReturn(new URI(LOCALHOST_8080 + "/some-extension")); when(requestContext.getHeaderString(CorsFilter.ORIGIN)).thenReturn(LOCALHOST_1111); - HashSet corsAllowedOrigins = new HashSet<>(); - Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o)); + HashSet corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(","))); when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins); MultivaluedMap responseHeaders = new MultivaluedHashMap<>(); diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/util/RequestContextUtilTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/util/RequestContextUtilTest.java index 571190f..8363bfd 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/util/RequestContextUtilTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/util/RequestContextUtilTest.java @@ -1,11 +1,11 @@ package ch.sbb.polarion.extension.generic.util; import org.junit.jupiter.api.Test; +import org.mockito.Answers; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.security.auth.Subject; -import javax.servlet.http.HttpServletRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -17,11 +17,11 @@ class RequestContextUtilTest { @Test void shouldReturnUserSubject() { // Arrange - ServletRequestAttributes requestAttributes = mock(ServletRequestAttributes.class); - HttpServletRequest request = mock(HttpServletRequest.class); + ServletRequestAttributes requestAttributes = mock(ServletRequestAttributes.class, Answers.RETURNS_DEEP_STUBS); Subject subject = mock(Subject.class); - when(requestAttributes.getRequest()).thenReturn(request); - when(request.getAttribute("user_subject")).thenReturn(subject); + when(requestAttributes + .getRequest() + .getAttribute("user_subject")).thenReturn(subject); RequestContextHolder.setRequestAttributes(requestAttributes); // Act