This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit f614f30bc9f1570e55c5f395e4698b7d5b2c102e Author: Andriy Redko <[email protected]> AuthorDate: Wed Aug 16 16:23:32 2023 -0400 CXF-8872: Get rid of EasyMock in cxf-rt-rs-security-oauth2 (#1369) (cherry picked from commit d77449d78c6aa0b110b0f4c326ed78dc628ed101) --- rt/rs/security/oauth-parent/oauth2/pom.xml | 5 +++-- .../oauth2/client/OAuthClientUtilsTest.java | 20 ++++++------------- .../oauth2/grants/jwt/AbstractJwtHandlerTest.java | 19 +++++++----------- .../tokens/hawk/HawkAccessTokenValidatorTest.java | 23 +++++++++++----------- .../oauth2/tokens/hawk/NonceVerifierImplTest.java | 15 ++++++-------- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/rt/rs/security/oauth-parent/oauth2/pom.xml b/rt/rs/security/oauth-parent/oauth2/pom.xml index a3375c7123..24f4b25ef9 100644 --- a/rt/rs/security/oauth-parent/oauth2/pom.xml +++ b/rt/rs/security/oauth-parent/oauth2/pom.xml @@ -93,8 +93,9 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>${cxf.mockito.version}</version> <scope>test</scope> </dependency> <dependency> diff --git a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/client/OAuthClientUtilsTest.java b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/client/OAuthClientUtilsTest.java index a35ae24497..67295cd741 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/client/OAuthClientUtilsTest.java +++ b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/client/OAuthClientUtilsTest.java @@ -37,14 +37,12 @@ import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants; import org.junit.Test; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.mock; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class OAuthClientUtilsTest { @@ -53,32 +51,26 @@ public class OAuthClientUtilsTest { WebClient accessTokenService = mock(WebClient.class); String tokenKey = "tokenKey"; String response = "{\"" + OAuthConstants.ACCESS_TOKEN + "\":\"" + tokenKey + "\"}"; - expect(accessTokenService.form(anyObject(Form.class))).andReturn( + when(accessTokenService.form(any(Form.class))).thenReturn( Response.ok(new ByteArrayInputStream(response.getBytes()), MediaType.APPLICATION_JSON).build()); - replay(accessTokenService); ClientAccessToken cat = OAuthClientUtils.getAccessToken(accessTokenService, null, new RefreshTokenGrant(""), null, "defaultTokenType", false); assertEquals(tokenKey, cat.getTokenKey()); - - verify(accessTokenService); } @Test public void getAccessTokenInternalServerError() { WebClient accessTokenService = mock(WebClient.class); - expect(accessTokenService.form(anyObject(Form.class))) - .andReturn(Response.serverError().type(MediaType.TEXT_PLAIN) + when(accessTokenService.form(any(Form.class))) + .thenReturn(Response.serverError().type(MediaType.TEXT_PLAIN) .entity(new ByteArrayInputStream("Unrecoverable error in the server.".getBytes())).build()); - replay(accessTokenService); try { OAuthClientUtils.getAccessToken(accessTokenService, null, new RefreshTokenGrant(""), null, null, false); fail(); } catch (OAuthServiceException e) { assertEquals(OAuthConstants.SERVER_ERROR, e.getMessage()); - } finally { - verify(accessTokenService); } } diff --git a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/grants/jwt/AbstractJwtHandlerTest.java b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/grants/jwt/AbstractJwtHandlerTest.java index 77681c24f5..1c940c665a 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/grants/jwt/AbstractJwtHandlerTest.java +++ b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/grants/jwt/AbstractJwtHandlerTest.java @@ -28,23 +28,22 @@ import org.apache.cxf.rs.security.oauth2.common.Client; import org.apache.cxf.rs.security.oauth2.common.ServerAccessToken; import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException; -import org.easymock.EasyMockRule; -import org.easymock.Mock; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; public class AbstractJwtHandlerTest { private static final String UNSIGNED_TEXT = "myUnsignedText"; private static final byte[] SIGNATURE = "mySignature".getBytes(); @Rule - public EasyMockRule rule = new EasyMockRule(this); + public MockitoRule rule = MockitoJUnit.rule(); private AbstractJwtHandler handler; @Mock @@ -66,21 +65,17 @@ public class AbstractJwtHandlerTest { @Test public void testValidateSignatureWithValidSignature() { - expect(signatureVerifier.verify(headers, UNSIGNED_TEXT, SIGNATURE)).andReturn(true); - replay(signatureVerifier); + when(signatureVerifier.verify(headers, UNSIGNED_TEXT, SIGNATURE)).thenReturn(true); handler.validateSignature(headers, UNSIGNED_TEXT, SIGNATURE); - verify(signatureVerifier); } @Test public void testValidateSignatureWithInvalidSignature() { - expect(signatureVerifier.verify(headers, UNSIGNED_TEXT, SIGNATURE)).andReturn(false); - replay(signatureVerifier); + when(signatureVerifier.verify(headers, UNSIGNED_TEXT, SIGNATURE)).thenReturn(false); try { handler.validateSignature(headers, UNSIGNED_TEXT, SIGNATURE); fail("OAuthServiceException expected"); } catch (OAuthServiceException expected) { } - verify(signatureVerifier); } } diff --git a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/HawkAccessTokenValidatorTest.java b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/HawkAccessTokenValidatorTest.java index 3e6a5ea6de..a0bf0868a5 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/HawkAccessTokenValidatorTest.java +++ b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/HawkAccessTokenValidatorTest.java @@ -30,17 +30,18 @@ import org.apache.cxf.rs.security.oauth2.common.Client; import org.apache.cxf.rs.security.oauth2.provider.OAuthDataProvider; import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants; -import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class HawkAccessTokenValidatorTest { private HawkAccessTokenValidator validator = new HawkAccessTokenValidator(); - private OAuthDataProvider dataProvider = EasyMock.createMock(OAuthDataProvider.class); - private MessageContext messageContext = EasyMock.createMock(MessageContext.class); + private OAuthDataProvider dataProvider = mock(OAuthDataProvider.class); + private MessageContext messageContext = mock(MessageContext.class); @Before public void setUp() { @@ -55,10 +56,9 @@ public class HawkAccessTokenValidatorTest { HttpServletRequest httpRequest = mockHttpRequest(); UriInfo uriInfo = mockUriInfo(); - EasyMock.expect(dataProvider.getAccessToken(macAccessToken.getTokenKey())).andReturn(macAccessToken); - EasyMock.expect(messageContext.getHttpServletRequest()).andReturn(httpRequest); - EasyMock.expect(messageContext.getUriInfo()).andReturn(uriInfo); - EasyMock.replay(dataProvider, messageContext, httpRequest, uriInfo); + when(dataProvider.getAccessToken(macAccessToken.getTokenKey())).thenReturn(macAccessToken); + when(messageContext.getHttpServletRequest()).thenReturn(httpRequest); + when(messageContext.getUriInfo()).thenReturn(uriInfo); String authData = getClientAuthHeader(macAccessToken); AccessTokenValidation tokenValidation = validator @@ -67,7 +67,6 @@ public class HawkAccessTokenValidatorTest { authData.split(" ")[1], null); assertNotNull(tokenValidation); - EasyMock.verify(dataProvider, messageContext, httpRequest); } private static String getClientAuthHeader(HawkAccessToken macAccessToken) { @@ -80,14 +79,14 @@ public class HawkAccessTokenValidatorTest { } private static HttpServletRequest mockHttpRequest() { - HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class); - EasyMock.expect(httpRequest.getMethod()).andReturn("GET"); + HttpServletRequest httpRequest = mock(HttpServletRequest.class); + when(httpRequest.getMethod()).thenReturn("GET"); return httpRequest; } private static UriInfo mockUriInfo() { - UriInfo ui = EasyMock.createMock(UriInfo.class); - EasyMock.expect(ui.getRequestUri()).andReturn( + UriInfo ui = mock(UriInfo.class); + when(ui.getRequestUri()).thenReturn( URI.create("http://localhost:8080/appContext/oauth2/testResource")); return ui; } diff --git a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/NonceVerifierImplTest.java b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/NonceVerifierImplTest.java index befce77ab6..40cad5ee77 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/NonceVerifierImplTest.java +++ b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/tokens/hawk/NonceVerifierImplTest.java @@ -20,17 +20,18 @@ package org.apache.cxf.rs.security.oauth2.tokens.hawk; import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException; -import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class NonceVerifierImplTest { private NonceVerifierImpl nonceVerifier; - private NonceStore nonceStore = EasyMock.createMock(NonceStore.class); + private NonceStore nonceStore = mock(NonceStore.class); @Before public void setUp() { @@ -46,11 +47,9 @@ public class NonceVerifierImplTest { NonceHistory nonceHistory = new NonceHistory(200, nonce1); // first request time delta is 200ms nonceHistory.addNonce(nonce2); - EasyMock.expect(nonceStore.getNonceHistory("testTokenKey")).andReturn(nonceHistory); - EasyMock.replay(nonceStore); + when(nonceStore.getNonceHistory("testTokenKey")).thenReturn(nonceHistory); nonceVerifier.setAllowedWindow(2000); // allowed window is 2 seconds nonceVerifier.verifyNonce("testTokenKey", "nonce3", Long.toString(now - 500)); - EasyMock.verify(nonceStore); } @Test @@ -61,8 +60,7 @@ public class NonceVerifierImplTest { NonceHistory nonceHistory = new NonceHistory(200, nonce1); // first request time delta is 200ms nonceHistory.addNonce(nonce2); - EasyMock.expect(nonceStore.getNonceHistory("testTokenKey")).andReturn(nonceHistory); - EasyMock.replay(nonceStore); + when(nonceStore.getNonceHistory("testTokenKey")).thenReturn(nonceHistory); nonceVerifier.setAllowedWindow(2000); // allowed window is 2 seconds try { nonceVerifier.verifyNonce("testTokenKey", "nonce2", Long.toString(now - 1000)); @@ -80,8 +78,7 @@ public class NonceVerifierImplTest { NonceHistory nonceHistory = new NonceHistory(200, nonce1); // first request time delta is 200ms nonceHistory.addNonce(nonce2); - EasyMock.expect(nonceStore.getNonceHistory("testTokenKey")).andReturn(nonceHistory); - EasyMock.replay(nonceStore); + when(nonceStore.getNonceHistory("testTokenKey")).thenReturn(nonceHistory); nonceVerifier.setAllowedWindow(2000); // allowed window is 2 seconds try { nonceVerifier.verifyNonce("testTokenKey", "nonce3", Long.toString(now - 5000)); // very old timestamp
