This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new d77449d78c CXF-8872: Get rid of EasyMock in cxf-rt-rs-security-oauth2 
(#1369)
d77449d78c is described below

commit d77449d78c6aa0b110b0f4c326ed78dc628ed101
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)
---
 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 58021d27bc..bc6f17d619 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 018a266073..d481317069 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
@@ -36,14 +36,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 {
 
@@ -52,32 +50,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 39aa8bb77c..a3579c419c 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
@@ -27,23 +27,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
@@ -65,21 +64,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 31df547ca3..581640ee28 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
@@ -29,17 +29,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() {
@@ -54,10 +55,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
@@ -66,7 +66,6 @@ public class HawkAccessTokenValidatorTest {
                                  authData.split(" ")[1],
                                  null);
         assertNotNull(tokenValidation);
-        EasyMock.verify(dataProvider, messageContext, httpRequest);
     }
 
     private static String getClientAuthHeader(HawkAccessToken macAccessToken) {
@@ -79,14 +78,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

Reply via email to