@@ -247,16 +247,10 @@ void sessionCredentials() throws IOException {
247247
248248 @ Test
249249 void cacheWithValidTokenTest () throws IOException {
250- // Create mock HTTP client for token refresh
250+ // Create mock HTTP client (shouldn't be called for valid token)
251251 HttpClient mockHttpClient = Mockito .mock (HttpClient .class );
252- String refreshResponse =
253- "{\" access_token\" : \" refreshed_access_token\" , \" token_type\" : \" Bearer\" , \" expires_in\" : \" 3600\" , \" refresh_token\" : \" new_refresh_token\" }" ;
254- URL url = new URL ("https://test.databricks.com/" );
255- Mockito .doAnswer (invocation -> new Response (refreshResponse , url ))
256- .when (mockHttpClient )
257- .execute (any (Request .class ));
258252
259- // Create an valid token with valid refresh token
253+ // Create a valid token with valid refresh token (expires in 1 hour - FRESH state)
260254 Instant futureTime = Instant .now ().plusSeconds (3600 );
261255 Token validToken = new Token ("valid_access_token" , "Bearer" , "valid_refresh_token" , futureTime );
262256
@@ -272,57 +266,46 @@ void cacheWithValidTokenTest() throws IOException {
272266 .setClientId ("test-client-id" )
273267 .setHttpClient (mockHttpClient );
274268
275- // We need to provide OIDC endpoints for token refresh
269+ // We need to provide OIDC endpoints
276270 OpenIDConnectEndpoints endpoints =
277271 new OpenIDConnectEndpoints (
278272 "https://test.databricks.com/token" , "https://test.databricks.com/authorize" );
279273
280- // Create our provider with the mock token cache and mock the browser auth method
274+ // Create our provider with the mock token cache
281275 ExternalBrowserCredentialsProvider provider =
282- Mockito . spy ( new ExternalBrowserCredentialsProvider (mockTokenCache ) );
276+ new ExternalBrowserCredentialsProvider (mockTokenCache );
283277
284278 // Spy on the config to inject the endpoints
285279 DatabricksConfig spyConfig = Mockito .spy (config );
286280 Mockito .doReturn (endpoints ).when (spyConfig ).getOidcEndpoints ();
287281
288282 // Configure provider
289283 HeaderFactory headerFactory = provider .configure (spyConfig );
284+ assertNotNull (headerFactory , "HeaderFactory should be created" );
290285
291- // Verify headers contain the refreshed token even though the cached token is valid
286+ // Verify headers contain the CACHED valid token (no refresh needed!)
292287 Map <String , String > headers = headerFactory .headers ();
293- assertEquals ("Bearer refreshed_access_token" , headers .get ("Authorization" ));
288+ assertEquals (
289+ "Bearer valid_access_token" ,
290+ headers .get ("Authorization" ),
291+ "Should use cached valid token without refreshing" );
294292
295293 // Verify token was loaded from cache
296294 Mockito .verify (mockTokenCache , Mockito .times (1 )).load ();
297295
298- // Verify HTTP call was made to refresh the token
299- Mockito .verify (mockHttpClient , Mockito .times ( 1 )).execute (any (Request .class ));
296+ // Verify NO HTTP call was made (token is still valid, no refresh needed)
297+ Mockito .verify (mockHttpClient , Mockito .never ( )).execute (any (Request .class ));
300298
301- // Verify performBrowserAuth was NOT called since refresh succeeded
299+ // Verify performBrowserAuth was NOT called since cached token is valid
302300 Mockito .verify (provider , Mockito .never ())
303301 .performBrowserAuth (
304302 any (DatabricksConfig .class ),
305303 any (String .class ),
306304 any (String .class ),
307305 any (TokenCache .class ));
308306
309- // Verify token was saved back to cache
310- Mockito .verify (mockTokenCache , Mockito .times (1 )).save (any (Token .class ));
311-
312- // Capture the token that was saved to cache to verify it's the refreshed token
313- ArgumentCaptor <Token > tokenCaptor = ArgumentCaptor .forClass (Token .class );
314- Mockito .verify (mockTokenCache ).save (tokenCaptor .capture ());
315- Token savedToken = tokenCaptor .getValue ();
316-
317- // Verify the saved token contains the refreshed values from the HTTP response
318- assertEquals (
319- "refreshed_access_token" ,
320- savedToken .getAccessToken (),
321- "Should save refreshed access token to cache" );
322- assertEquals (
323- "new_refresh_token" ,
324- savedToken .getRefreshToken (),
325- "Should save new refresh token to cache" );
307+ // Verify token was NOT saved back to cache (we're using the cached one as-is)
308+ Mockito .verify (mockTokenCache , Mockito .never ()).save (any (Token .class ));
326309 }
327310
328311 @ Test
0 commit comments