Skip to content

Commit 375f0dc

Browse files
committed
refactor: Used DEEP_STUBS for chained mocking
Refs: #237
1 parent 21c37d0 commit 375f0dc

File tree

3 files changed

+43
-52
lines changed

3 files changed

+43
-52
lines changed

app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/AuthenticationFilterTest.java

+33-36
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import ch.sbb.polarion.extension.generic.auth.ValidatorType;
66
import com.polarion.core.config.Configuration;
77
import com.polarion.core.config.IConfiguration;
8-
import com.polarion.core.config.IRestConfiguration;
98
import com.polarion.platform.security.AuthenticationFailedException;
109
import com.polarion.platform.security.ISecurityService;
11-
import com.polarion.platform.security.login.AccessToken;
1210
import com.polarion.platform.security.login.ILogin;
1311
import com.polarion.platform.security.login.IToken;
1412
import org.junit.jupiter.api.Test;
1513
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.mockito.Answers;
1615
import org.mockito.Mock;
1716
import org.mockito.MockedStatic;
1817
import org.mockito.junit.jupiter.MockitoExtension;
@@ -34,22 +33,14 @@ class AuthenticationFilterTest {
3433

3534
@Mock
3635
private ContainerRequestContext requestContext;
37-
@Mock
36+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
3837
private ISecurityService securityService;
3938
@Mock
40-
private ILogin login;
41-
@Mock
42-
private ILogin.IBase base;
43-
@Mock
44-
private ILogin.IUsingAuthenticator authenticator;
45-
@Mock
4639
private ILogin.IFinal loginFinal;
47-
@Mock
40+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
4841
private HttpServletRequest httpServletRequest;
49-
@Mock
42+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
5043
private IConfiguration configuration;
51-
@Mock
52-
private IRestConfiguration restConfiguration;
5344

5445
@Test
5546
void filterRequestWithoutAuthorizationHeaderAndXsrfHeader() {
@@ -76,14 +67,15 @@ void filterRequestWithoutBearerInAuthorizationHeader() {
7667
}
7768

7869
@Test
70+
@SuppressWarnings("unchecked")
7971
void filterRequestWithValidBearerToken() throws IOException, AuthenticationFailedException {
8072
when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer token");
8173
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn(null);
8274

83-
when(securityService.login()).thenReturn(login);
84-
when(login.from("REST")).thenReturn(base);
85-
when(base.authenticator(any())).thenReturn(authenticator);
86-
when(authenticator.with((IToken<AccessToken>) any())).thenReturn(loginFinal);
75+
when(securityService.login()
76+
.from("REST")
77+
.authenticator(any())
78+
.with(any(IToken.class))).thenReturn(loginFinal);
8779

8880
Subject subject = new Subject();
8981
when(loginFinal.perform()).thenReturn(subject);
@@ -95,14 +87,15 @@ void filterRequestWithValidBearerToken() throws IOException, AuthenticationFaile
9587

9688

9789
@Test
90+
@SuppressWarnings("unchecked")
9891
void filterRequestWithFailedAuthentication() throws AuthenticationFailedException {
9992
when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer failed_token");
10093
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn(null);
10194

102-
when(securityService.login()).thenReturn(login);
103-
when(login.from("REST")).thenReturn(base);
104-
when(base.authenticator(any())).thenReturn(authenticator);
105-
when(authenticator.with((IToken<AccessToken>) any())).thenReturn(loginFinal);
95+
when(securityService.login()
96+
.from("REST")
97+
.authenticator(any())
98+
.with(any(IToken.class))).thenReturn(loginFinal);
10699

107100
when(loginFinal.perform()).thenThrow(new AuthenticationFailedException("Something went wrong"));
108101

@@ -114,6 +107,7 @@ void filterRequestWithFailedAuthentication() throws AuthenticationFailedExceptio
114107
}
115108

116109
@Test
110+
@SuppressWarnings("unused")
117111
void filterRequestWithValidXsrfToken() throws IOException, AuthenticationFailedException {
118112
when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn(null);
119113
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("validXsrfToken");
@@ -144,12 +138,13 @@ void filterRequestWithInvalidXsrfToken() {
144138

145139
try (MockedStatic<Configuration> configurationMockedStatic = mockStatic(Configuration.class)) {
146140
configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration);
147-
when(configuration.rest()).thenReturn(restConfiguration);
148-
when(restConfiguration.restApiTokenEnabled()).thenReturn(true);
141+
when(configuration
142+
.rest()
143+
.restApiTokenEnabled()).thenReturn(true);
149144

150-
Principal userPrincipal = mock(Principal.class);
151-
when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal);
152-
when(userPrincipal.getName()).thenReturn("user");
145+
when(httpServletRequest
146+
.getUserPrincipal()
147+
.getName()).thenReturn("user");
153148

154149
AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest);
155150

@@ -165,12 +160,13 @@ void filterRequestWithXsrfTokenButConfigurationIsDisabled() {
165160
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("xsrf_token");
166161
try (MockedStatic<Configuration> configurationMockedStatic = mockStatic(Configuration.class)) {
167162
configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration);
168-
when(configuration.rest()).thenReturn(restConfiguration);
169-
when(restConfiguration.restApiTokenEnabled()).thenReturn(false);
163+
when(configuration
164+
.rest()
165+
.restApiTokenEnabled()).thenReturn(false);
170166

171-
Principal userPrincipal = mock(Principal.class);
172-
when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal);
173-
when(userPrincipal.getName()).thenReturn("user");
167+
when(httpServletRequest
168+
.getUserPrincipal()
169+
.getName()).thenReturn("user");
174170

175171
AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest);
176172

@@ -186,12 +182,13 @@ void filterRequestWithXsrfTokenForDifferentUser() {
186182
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn("xsrf_token_for_different_user");
187183
try (MockedStatic<Configuration> configurationMockedStatic = mockStatic(Configuration.class)) {
188184
configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration);
189-
when(configuration.rest()).thenReturn(restConfiguration);
190-
when(restConfiguration.restApiTokenEnabled()).thenReturn(true);
185+
when(configuration
186+
.rest()
187+
.restApiTokenEnabled()).thenReturn(true);
191188

192-
Principal userPrincipal = mock(Principal.class);
193-
when(httpServletRequest.getUserPrincipal()).thenReturn(userPrincipal);
194-
when(userPrincipal.getName()).thenReturn("different_user");
189+
when(httpServletRequest
190+
.getUserPrincipal()
191+
.getName()).thenReturn("different_user");
195192

196193
AuthenticationFilter filter = new AuthenticationFilter(securityService, httpServletRequest);
197194

app/src/test/java/ch/sbb/polarion/extension/generic/rest/filter/CorsFilterTest.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.junit.jupiter.api.extension.ExtendWith;
1010
import org.junit.jupiter.params.ParameterizedTest;
1111
import org.junit.jupiter.params.provider.ValueSource;
12-
import org.mockito.Answers;
1312
import org.mockito.ArgumentCaptor;
1413
import org.mockito.Mock;
1514
import org.mockito.MockedStatic;
@@ -45,16 +44,14 @@ class CorsFilterTest {
4544
private ContainerRequestContext requestContext;
4645
@Mock
4746
private ContainerResponseContext responseContext;
48-
49-
@Mock
50-
private IConfiguration configuration;
5147
@Mock
5248
private IRestConfiguration restConfiguration;
53-
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
49+
@Mock
5450
MockedStatic<Configuration> configurationMockedStatic;
5551

5652
@BeforeEach
5753
void setUp() throws MalformedURLException {
54+
IConfiguration configuration = mock(IConfiguration.class);
5855
configurationMockedStatic.when(Configuration::getInstance).thenReturn(configuration);
5956
lenient().when(configuration.rest()).thenReturn(restConfiguration);
6057
lenient().when(configuration.getBaseURL()).thenReturn(URI.create(LOCALHOST_8080).toURL());
@@ -99,8 +96,7 @@ void requestOriginNotAllowed(String input) throws URISyntaxException {
9996
when(requestContext.getMethod()).thenReturn(HttpMethod.GET);
10097

10198
// no CORS enabled
102-
HashSet<String> corsAllowedOrigins = new HashSet<>();
103-
Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o));
99+
HashSet<String> corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(",")));
104100
when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins);
105101

106102
CorsFilter corsFilter = new CorsFilter();
@@ -122,8 +118,7 @@ void requestOriginAllowed(String input) throws URISyntaxException {
122118
when(requestContext.getHeaderString(CorsFilter.ORIGIN)).thenReturn(LOCALHOST_1111);
123119
when(requestContext.getMethod()).thenReturn(HttpMethod.GET);
124120

125-
HashSet<String> corsAllowedOrigins = new HashSet<>();
126-
Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o));
121+
HashSet<String> corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(",")));
127122
when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins);
128123

129124
CorsFilter corsFilter = new CorsFilter();
@@ -139,8 +134,7 @@ void testContainerResponseFilter(String input) throws URISyntaxException {
139134
when(uriInfo.getRequestUri()).thenReturn(new URI(LOCALHOST_8080 + "/some-extension"));
140135
when(requestContext.getHeaderString(CorsFilter.ORIGIN)).thenReturn(LOCALHOST_1111);
141136

142-
HashSet<String> corsAllowedOrigins = new HashSet<>();
143-
Arrays.stream(input.split( "," )).forEach(o -> corsAllowedOrigins.add(o));
137+
HashSet<String> corsAllowedOrigins = new HashSet<>(Arrays.asList(input.split(",")));
144138
when(restConfiguration.corsAllowedOrigins()).thenReturn(corsAllowedOrigins);
145139

146140
MultivaluedMap<String, Object> responseHeaders = new MultivaluedHashMap<>();

app/src/test/java/ch/sbb/polarion/extension/generic/util/RequestContextUtilTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package ch.sbb.polarion.extension.generic.util;
22

33
import org.junit.jupiter.api.Test;
4+
import org.mockito.Answers;
45
import org.springframework.web.context.request.RequestContextHolder;
56
import org.springframework.web.context.request.ServletRequestAttributes;
67

78
import javax.security.auth.Subject;
8-
import javax.servlet.http.HttpServletRequest;
99

1010
import static org.assertj.core.api.Assertions.assertThat;
1111
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -17,11 +17,11 @@ class RequestContextUtilTest {
1717
@Test
1818
void shouldReturnUserSubject() {
1919
// Arrange
20-
ServletRequestAttributes requestAttributes = mock(ServletRequestAttributes.class);
21-
HttpServletRequest request = mock(HttpServletRequest.class);
20+
ServletRequestAttributes requestAttributes = mock(ServletRequestAttributes.class, Answers.RETURNS_DEEP_STUBS);
2221
Subject subject = mock(Subject.class);
23-
when(requestAttributes.getRequest()).thenReturn(request);
24-
when(request.getAttribute("user_subject")).thenReturn(subject);
22+
when(requestAttributes
23+
.getRequest()
24+
.getAttribute("user_subject")).thenReturn(subject);
2525
RequestContextHolder.setRequestAttributes(requestAttributes);
2626

2727
// Act

0 commit comments

Comments
 (0)