Skip to content

Commit cfce6a6

Browse files
committed
fix: address remaining test hygiene and consistency findings from PR review
1 parent d085feb commit cfce6a6

8 files changed

Lines changed: 36 additions & 17 deletions

File tree

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class ComputeEngineCredentialsTest extends BaseSerializationTest {
9393

9494
@BeforeEach
9595
void setUp() throws IOException {
96+
AgentIdentityUtils.clearCertInfoCache();
9697
envProvider = new TestEnvironmentProvider();
9798
// Inject our test environment reader into AgentIdentityUtils
9899
AgentIdentityUtils.setEnvReader(envProvider::getEnv);
@@ -117,10 +118,17 @@ public void sleep(long millis) {
117118
}
118119

119120
@AfterEach
120-
void tearDown() {
121+
void tearDown() throws IOException {
121122
// Reset the mocks
123+
AgentIdentityUtils.clearCertInfoCache();
122124
AgentIdentityUtils.resetTimeService();
123125
AgentIdentityUtils.setEnvReader(System::getenv);
126+
if (tempDir != null) {
127+
Files.walk(tempDir)
128+
.sorted(java.util.Comparator.reverseOrder())
129+
.map(Path::toFile)
130+
.forEach(File::delete);
131+
}
124132
}
125133

126134
private static final String TOKEN_URL =

sdk-platform-java/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ void refresh() {
516516
}
517517

518518
// Double-check fingerprint inside the lock
519-
if (currentDiskFingerprint.equals(this.activeCertFingerprint)) {
519+
if (currentDiskFingerprint.equalsIgnoreCase(this.activeCertFingerprint)) {
520520
LOG.fine(
521521
"Channel pool was already refreshed by a concurrent thread, skipping duplicate refresh");
522522
return;

sdk-platform-java/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/ChannelPoolTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,18 @@
8181
class ChannelPoolTest {
8282
private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10;
8383
private ChannelPool pool;
84+
private java.nio.file.Path tempCert;
8485

8586
@AfterEach
86-
void cleanup() throws InterruptedException {
87+
void cleanup() throws InterruptedException, IOException {
8788
if (pool != null) {
8889
pool.shutdown();
8990
pool.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
9091
}
92+
if (tempCert != null) {
93+
java.nio.file.Files.deleteIfExists(tempCert);
94+
tempCert = null;
95+
}
9196
}
9297

9398
@Test
@@ -439,8 +444,7 @@ void channelReactiveMTlsRefreshShouldConditionallySwapChannels() throws IOExcept
439444
new FakeChannelFactory(ImmutableList.of(underlyingChannel1, underlyingChannel2));
440445

441446
// Create a temp file to act as the cert
442-
java.nio.file.Path tempCert = java.nio.file.Files.createTempFile("cert", ".pem");
443-
tempCert.toFile().deleteOnExit();
447+
tempCert = java.nio.file.Files.createTempFile("cert", ".pem");
444448

445449
java.nio.file.Path clientCert = java.nio.file.Paths.get("src", "test", "resources", "client_cert.pem");
446450
java.nio.file.Files.copy(clientCert, tempCert, java.nio.file.StandardCopyOption.REPLACE_EXISTING);

sdk-platform-java/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcCallContextTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,5 @@ public void testEqualsAndHashCode() {
511511
org.junit.jupiter.api.Assertions.assertEquals(context1.hashCode(), context2.hashCode());
512512

513513
org.junit.jupiter.api.Assertions.assertNotEquals(context1, context3);
514-
org.junit.jupiter.api.Assertions.assertNotEquals(context1.hashCode(), context3.hashCode());
515514
}
516515
}

sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/RefreshingHttpJsonChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void refresh() {
147147
}
148148

149149
// Double-check inside refreshLock
150-
if (currentDiskFingerprint.equals(this.activeCertFingerprint)) {
150+
if (currentDiskFingerprint.equalsIgnoreCase(this.activeCertFingerprint)) {
151151
LOG.fine(
152152
"HTTP/JSON channel was already refreshed by a concurrent thread, skipping duplicate refresh");
153153
return;

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/ServerStreamingAttemptCallableTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
package com.google.api.gax.rpc;
3131

32+
import static org.junit.jupiter.api.Assertions.assertThrows;
3233
import static org.mockito.Mockito.mock;
3334

3435
import com.google.api.core.AbstractApiFuture;
@@ -270,14 +271,11 @@ void testUnauthenticatedRefresh() {
270271
call.getController().getObserver().onError(initialError);
271272

272273
// Should notify the outer future
273-
Throwable outerError = null;
274-
try {
275-
fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
276-
} catch (ExecutionException e) {
277-
outerError = e.getCause();
278-
} catch (Throwable e) {
279-
outerError = e;
280-
}
274+
ExecutionException ee =
275+
assertThrows(
276+
ExecutionException.class,
277+
() -> fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS));
278+
Throwable outerError = ee.getCause();
281279
Mockito.verify(transportChannel).refresh();
282280
Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
283281
Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isFalse();

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/StreamingCallableTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void testClientStreamingCall() {
129129
ClientStreamingCallable<Integer, Integer> callable =
130130
stashCallable.withDefaultCallContext(defaultCallContext);
131131
callable.clientStreamingCall(observer);
132-
org.junit.jupiter.api.Assertions.assertNotNull(stashCallable.getActualObserver());
132+
assertSame(observer, stashCallable.getActualObserver());
133133
assertSame(defaultCallContext, stashCallable.getContext());
134134
}
135135

@@ -158,7 +158,7 @@ void testClientStreamingCallWithContext() {
158158
ClientStreamingCallable<Integer, Integer> callable =
159159
stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
160160
callable.clientStreamingCall(observer, context);
161-
org.junit.jupiter.api.Assertions.assertNotNull(stashCallable.getActualObserver());
161+
assertSame(observer, stashCallable.getActualObserver());
162162
FakeCallContext actualContext = (FakeCallContext) stashCallable.getContext();
163163
assertSame(channel, actualContext.getChannel());
164164
assertSame(credentials, actualContext.getCredentials());

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/mtls/CertificateBasedAccessTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ void testUseMtlsClientCertificateConfigMissingFile() {
197197
assertTrue(ex.getMessage().contains("configured but the file does not exist"));
198198
}
199199

200+
@Test
201+
void testUseMtlsClientCertificateEnvTrueOverride() {
202+
TestEnv env = new TestEnv();
203+
env.set("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true");
204+
205+
CertificateBasedAccess cba = createCba(env, new TestFileSystem());
206+
207+
assertTrue(cba.useMtlsClientCertificate());
208+
}
209+
200210
@Test
201211
void testUseMtlsClientCertificateConfigMalformedJson() {
202212
TestEnv env = new TestEnv();

0 commit comments

Comments
 (0)