Skip to content

Commit aa38303

Browse files
committed
Minor code readability enhancements.
1 parent 2157fdb commit aa38303

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

cab-token-generator/javatests/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactoryTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT;
3535
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.assertNotNull;
3637
import static org.junit.Assert.assertThrows;
3738
import static org.junit.Assert.fail;
3839
import static org.mockito.Mockito.mock;
@@ -54,7 +55,6 @@
5455
import java.io.IOException;
5556
import java.time.Duration;
5657
import java.util.Map;
57-
import java.util.Objects;
5858
import java.util.concurrent.CountDownLatch;
5959
import org.junit.Before;
6060
import 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

Comments
 (0)