Skip to content

Commit aa3262a

Browse files
authored
chore(bigquery-jdbc): refactor tests to remove hardcoded base uri (#13420)
1 parent b721e43 commit aa3262a

12 files changed

Lines changed: 248 additions & 462 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.bigquery.jdbc;
1818

19+
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
1920
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
2021

2122
public class BigQueryJdbcBaseTest {
@@ -43,7 +44,7 @@ public class BigQueryJdbcBaseTest {
4344
"-----END PRIVATE KEY-----";
4445

4546
protected static URIBuilder getBaseUri() {
46-
return new URIBuilder("jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;");
47+
return new URIBuilder(TestUtilities.getBaseConnectionUrl());
4748
}
4849

4950
protected static URIBuilder getBaseUri(int authType) {

java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testValidServiceAccountAuthentication() throws SQLException, IOExcep
9090
Files.write(tempFile.toPath(), authJson.toString().getBytes());
9191

9292
String connection_uri =
93-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
93+
getBaseConnectionUrl()
9494
+ "ProjectId="
9595
+ authJson.get("project_id").getAsString()
9696
+ ";OAuthType=0;"
@@ -104,11 +104,7 @@ public void testValidServiceAccountAuthentication() throws SQLException, IOExcep
104104

105105
@Test
106106
public void testServiceAccountAuthenticationMissingOAuthPvtKeyPath() throws SQLException {
107-
String connection_uri =
108-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
109-
+ "ProjectId="
110-
+ PROJECT_ID
111-
+ ";OAuthType=0;";
107+
String connection_uri = getBaseConnectionUrl() + "ProjectId=" + PROJECT_ID + ";OAuthType=0;";
112108

113109
try {
114110
DriverManager.getConnection(connection_uri);
@@ -127,7 +123,7 @@ public void testValidServiceAccountAuthenticationOAuthPvtKeyAsPath()
127123
Files.write(tempFile.toPath(), authJson.toString().getBytes());
128124

129125
String connection_uri =
130-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
126+
getBaseConnectionUrl()
131127
+ "ProjectId="
132128
+ authJson.get("project_id").getAsString()
133129
+ ";OAuthType=0;"
@@ -144,7 +140,7 @@ public void testValidServiceAccountAuthenticationViaEmailAndPkcs8Key()
144140
final JsonObject authJson = getAuthJson();
145141

146142
String connection_uri =
147-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
143+
getBaseConnectionUrl()
148144
+ "ProjectId="
149145
+ authJson.get("project_id").getAsString()
150146
+ ";OAuthType=0;"
@@ -162,7 +158,7 @@ public void testValidServiceAccountAuthenticationOAuthPvtKeyAsJson()
162158
final JsonObject authJson = getAuthJson();
163159

164160
String connection_uri =
165-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
161+
getBaseConnectionUrl()
166162
+ "ProjectId="
167163
+ authJson.get("project_id").getAsString()
168164
+ ";OAuthType=0;"
@@ -192,9 +188,12 @@ public void testValidServiceAccountAuthenticationP12() throws SQLException, IOEx
192188
@Disabled
193189
public void testValidGoogleUserAccountAuthentication() throws SQLException {
194190
String connection_uri =
195-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
191+
getBaseConnectionUrl()
192+
+ "PROJECTID="
196193
+ PROJECT_ID
197-
+ ";OAuthType=1;OAuthClientId=client_id;OAuthClientSecret=client_secret;";
194+
+ ";OAuthType=1;"
195+
+ "OAuthClientId=client_id;"
196+
+ "OAuthClientSecret=client_secret;";
198197

199198
Connection connection = DriverManager.getConnection(connection_uri);
200199
assertNotNull(connection);
@@ -213,12 +212,15 @@ public void testValidGoogleUserAccountAuthentication() throws SQLException {
213212
@Disabled
214213
public void testValidExternalAccountAuthentication() throws SQLException {
215214
String connection_uri =
216-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
215+
getBaseConnectionUrl()
216+
+ "PROJECTID="
217217
+ PROJECT_ID
218218
+ ";OAUTHTYPE=4;"
219219
+ "BYOID_AudienceUri=//iam.googleapis.com/projects/<project>/locations/<location>/workloadIdentityPools/<pool>/providers/<provider>;"
220-
+ "BYOID_SubjectTokenType=<type>;BYOID_CredentialSource={\"file\":\"/path/to/file\"};"
221-
+ "BYOID_SA_Impersonation_Uri=<sa>;BYOID_TokenUri=<uri>;";
220+
+ "BYOID_SubjectTokenType=<type>;"
221+
+ "BYOID_CredentialSource={\"file\":\"/path/to/file\"};"
222+
+ "BYOID_SA_Impersonation_Uri=<sa>;"
223+
+ "BYOID_TokenUri=<uri>;";
222224

223225
Connection connection = DriverManager.getConnection(connection_uri);
224226
assertNotNull(connection);
@@ -237,7 +239,8 @@ public void testValidExternalAccountAuthentication() throws SQLException {
237239
@Disabled
238240
public void testValidExternalAccountAuthenticationFromFile() throws SQLException {
239241
String connection_uri =
240-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
242+
getBaseConnectionUrl()
243+
+ "PROJECTID="
241244
+ PROJECT_ID
242245
+ ";OAUTHTYPE=4;"
243246
+ "OAuthPvtKeyPath=/path/to/file;";
@@ -259,9 +262,11 @@ public void testValidExternalAccountAuthenticationFromFile() throws SQLException
259262
@Disabled
260263
public void testValidExternalAccountAuthenticationRawJson() throws SQLException {
261264
String connection_uri =
262-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
265+
getBaseConnectionUrl()
266+
+ "PROJECTID="
263267
+ PROJECT_ID
264-
+ ";OAUTHTYPE=4;OAuthPvtKey={\n"
268+
+ ";OAUTHTYPE=4;"
269+
+ "OAuthPvtKey={\n"
265270
+ " \"universe_domain\": \"googleapis.com\",\n"
266271
+ " \"type\": \"external_account\",\n"
267272
+ " \"audience\":"
@@ -303,7 +308,8 @@ public void testValidPreGeneratedAccessTokenAuthentication(String scope, boolean
303308
String accessToken = credentials.getAccessToken().getTokenValue();
304309

305310
String connectionUri =
306-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId="
311+
getBaseConnectionUrl()
312+
+ "ProjectId="
307313
+ authJson.get("project_id").getAsString()
308314
+ ";OAuthType=2"
309315
+ ";OAuthAccessToken="
@@ -319,10 +325,13 @@ public void testValidPreGeneratedAccessTokenAuthentication(String scope, boolean
319325
@Disabled
320326
public void testValidRefreshTokenAuthentication() throws SQLException {
321327
String connection_uri =
322-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID="
328+
getBaseConnectionUrl()
329+
+ "PROJECTID="
323330
+ PROJECT_ID
324-
+ ";OAUTHTYPE=2;OAuthRefreshToken=refresh_token;"
325-
+ ";OAuthClientId=client;OAuthClientSecret=secret;";
331+
+ ";OAUTHTYPE=2;"
332+
+ "OAuthRefreshToken=refresh_token;"
333+
+ ";OAuthClientId=client;"
334+
+ "OAuthClientSecret=secret;";
326335

327336
Connection connection = DriverManager.getConnection(connection_uri);
328337
assertNotNull(connection);

java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.bigquery.BigQueryOptions;
2424
import com.google.cloud.bigquery.QueryJobConfiguration;
2525
import com.google.cloud.bigquery.jdbc.BigQueryJdbcBaseTest;
26+
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
2627
import java.sql.Connection;
2728
import java.sql.ResultSet;
2829
import java.sql.SQLException;
@@ -177,10 +178,13 @@ private static void registerShutdownHook(final String dataset) {
177178
}
178179

179180
public static final String DEFAULT_CATALOG = ServiceOptions.getDefaultProjectId();
180-
public static String connectionUrl =
181-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId="
182-
+ DEFAULT_CATALOG
183-
+ ";OAuthType=3;Timeout=3600;";
181+
182+
public static String getBaseConnectionUrl() {
183+
return TestUtilities.getBaseConnectionUrl();
184+
}
185+
186+
public static final String connectionUrl =
187+
getBaseConnectionUrl() + "ProjectId=" + DEFAULT_CATALOG + ";OAuthType=3;Timeout=3600;";
184188

185189
public static final String createDatasetQuery =
186190
"CREATE SCHEMA IF NOT EXISTS `%s.%s` OPTIONS(default_table_expiration_days = 5)";

0 commit comments

Comments
 (0)