3333
3434import static com .google .auth .oauth2 .OAuth2Utils .TOKEN_EXCHANGE_URL_FORMAT ;
3535import static org .junit .Assert .assertEquals ;
36+ import static org .junit .Assert .assertNotNull ;
3637import static org .junit .Assert .assertThrows ;
3738import static org .junit .Assert .fail ;
3839import static org .mockito .Mockito .mock ;
5455import java .io .IOException ;
5556import java .time .Duration ;
5657import java .util .Map ;
57- import java .util .Objects ;
5858import java .util .concurrent .CountDownLatch ;
5959import org .junit .Before ;
6060import org .junit .Test ;
@@ -170,12 +170,8 @@ public void fetchIntermediateCredentials_sourceCredentialCannotRefresh_throwsIOE
170170 .setHttpTransportFactory (mockStsTransportFactory )
171171 .build ();
172172
173- try {
174- factory .fetchIntermediateCredentials (); // Expecting an IOException
175- fail ("Should fail as the source credential should not be able to be refreshed." );
176- } catch (IOException e ) {
177- assertEquals ("Unable to refresh the provided source credential." , e .getMessage ());
178- }
173+ IOException thrown = assertThrows (IOException .class , factory ::fetchIntermediateCredentials );
174+ assertEquals ("Unable to refresh the provided source credential." , thrown .getMessage ());
179175 }
180176
181177 @ Test
@@ -201,9 +197,10 @@ public void fetchIntermediateCredentials_noExpiresInReturned_copiesSourceExpirat
201197 intermediateAccessToken .getTokenValue ());
202198
203199 // Validate that the expires_in has been copied from the source credential.
200+ AccessToken sourceAccessToken = sourceCredentials .getAccessToken ();
201+ assertNotNull (sourceAccessToken ); // Assert that sourceAccessToken is not null
204202 assertEquals (
205- Objects .requireNonNull (sourceCredentials .getAccessToken ()).getExpirationTime (),
206- intermediateAccessToken .getExpirationTime ());
203+ sourceAccessToken .getExpirationTime (), intermediateAccessToken .getExpirationTime ());
207204 }
208205
209206 @ Test
@@ -288,13 +285,14 @@ public void refreshCredentialsIfRequired_asyncSingleThread() throws IOException
288285 // Introduce a small delay to allow the asynchronous refresh task to complete. This is
289286 // necessary because the async task runs on a separate thread.
290287 try {
291- Thread .sleep (100 );
288+ Thread .sleep (1000 );
292289 } catch (InterruptedException e ) {
293290 throw new IOException (e );
294291 }
295292
296- // After the delay, the request count should be 2, indicating that the async refresh has made
297- // a single request to the STS endpoint.
293+ // After the delay, the request count should be 2 (initial fetch + one async refresh).
294+ // Subsequent calls to refreshCredentialsIfRequired() in ASYNC mode re-use the in-progress
295+ // refresh task, so they don't trigger additional STS requests.
298296 assertEquals (2 , mockStsTransportFactory .transport .getRequestCount ());
299297 }
300298
0 commit comments