Skip to content

Commit 331a77d

Browse files
committed
add local test
1 parent c2e7e89 commit 331a77d

1 file changed

Lines changed: 49 additions & 12 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -394,20 +394,57 @@ public void testParseUserImpersonationForADC() {
394394
}
395395

396396
@Test
397-
@Disabled
398-
public void testGetServiceAccountImpersonatedCredentialsForADC() {
399-
Map<String, String> authProperties =
400-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
401-
DataSource.fromUrl(
402-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
403-
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
404-
405-
"");
397+
public void testGetServiceAccountImpersonatedCredentialsForADC() throws Exception {
398+
String dummyJson =
399+
"{\n"
400+
+ " \"type\": \"service_account\",\n"
401+
+ " \"project_id\": \"dummy-project\",\n"
402+
+ " \"private_key_id\": \"dummy-key-id\",\n"
403+
+ " \"private_key\": \""
404+
+ fake_pkcs8_key.replace("\n", "\\n")
405+
+ "\",\n"
406+
+ " \"client_email\": \"[email protected]\",\n"
407+
+ " \"client_id\": \"dummy-client-id\",\n"
408+
+ " \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n"
409+
+ " \"token_uri\": \"https://oauth2.googleapis.com/token\"\n"
410+
+ "}";
411+
412+
java.nio.file.Path tempFile = java.nio.file.Files.createTempFile("adc", ".json");
413+
java.nio.file.Files.write(tempFile, dummyJson.getBytes());
414+
tempFile.toFile().deleteOnExit();
415+
416+
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
417+
java.lang.reflect.Field theEnvironmentField =
418+
processEnvironmentClass.getDeclaredField("theEnvironment");
419+
theEnvironmentField.setAccessible(true);
420+
java.util.Map<String, String> env =
421+
(java.util.Map<String, String>) theEnvironmentField.get(null);
422+
423+
String originalEnv = env.get("GOOGLE_APPLICATION_CREDENTIALS");
406424

407-
GoogleCredentials credentials =
408-
BigQueryJdbcOAuthUtility.getCredentials(authProperties, null, false, null);
425+
try {
426+
if (originalEnv == null) {
427+
env.put("GOOGLE_APPLICATION_CREDENTIALS", tempFile.toAbsolutePath().toString());
428+
}
409429

410-
assertThat(credentials).isInstanceOf(ImpersonatedCredentials.class);
430+
Map<String, String> authProperties =
431+
BigQueryJdbcOAuthUtility.parseOAuthProperties(
432+
DataSource.fromUrl(
433+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
434+
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
435+
436+
"");
437+
438+
GoogleCredentials credentials =
439+
BigQueryJdbcOAuthUtility.getCredentials(
440+
authProperties, java.util.Collections.EMPTY_MAP, false, null);
441+
442+
assertThat(credentials).isInstanceOf(ImpersonatedCredentials.class);
443+
} finally {
444+
if (originalEnv == null) {
445+
env.remove("GOOGLE_APPLICATION_CREDENTIALS");
446+
}
447+
}
411448
}
412449

413450
@Test

0 commit comments

Comments
 (0)