Skip to content

Commit affd9cc

Browse files
authored
chore(bigquery-jdbc): enable proxy tests (#13617)
1 parent 9061a9f commit affd9cc

6 files changed

Lines changed: 22 additions & 32 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

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

19+
import com.google.cloud.bigquery.BigQuery;
1920
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
2021
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
22+
import java.sql.DriverManager;
23+
import java.sql.SQLException;
2124

2225
public class BigQueryJdbcBaseTest {
2326

@@ -43,6 +46,16 @@ public class BigQueryJdbcBaseTest {
4346
+ //
4447
"-----END PRIVATE KEY-----";
4548

49+
protected static BigQuery getBigQuery(String connectionUrl) {
50+
try {
51+
return DriverManager.getConnection(connectionUrl)
52+
.unwrap(BigQueryConnection.class)
53+
.getBigQuery();
54+
} catch (SQLException e) {
55+
throw new RuntimeException("Failed to initialize BigQuery client", e);
56+
}
57+
}
58+
4659
protected static URIBuilder getBaseUri() {
4760
return new URIBuilder(TestUtilities.getBaseConnectionUrl());
4861
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
import com.google.cloud.ServiceOptions;
2626
import com.google.gson.JsonObject;
2727
import com.google.gson.JsonParser;
28-
import java.io.ByteArrayInputStream;
2928
import java.io.File;
3029
import java.io.IOException;
3130
import java.io.InputStream;
3231
import java.io.InputStreamReader;
33-
import java.nio.charset.StandardCharsets;
3432
import java.nio.file.Files;
3533
import java.nio.file.Paths;
3634
import java.sql.Connection;
@@ -300,10 +298,10 @@ public void testValidExternalAccountAuthenticationRawJson() throws SQLException
300298
public void testValidPreGeneratedAccessTokenAuthentication(String scope, boolean isReadOnly)
301299
throws Exception {
302300
final JsonObject authJson = getAuthJson();
303-
InputStream stream =
304-
new ByteArrayInputStream(authJson.toString().getBytes(StandardCharsets.UTF_8));
301+
305302
GoogleCredentials credentials =
306-
GoogleCredentials.fromStream(stream).createScoped(Arrays.asList(scope));
303+
((GoogleCredentials) bigQuery.getOptions().getCredentials())
304+
.createScoped(Arrays.asList(scope));
307305
credentials.refresh();
308306
String accessToken = credentials.getAccessToken().getTokenValue();
309307

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.cloud.ServiceOptions;
2222
import com.google.cloud.bigquery.BigQuery;
23-
import com.google.cloud.bigquery.BigQueryOptions;
2423
import com.google.cloud.bigquery.QueryJobConfiguration;
2524
import com.google.cloud.bigquery.jdbc.BigQueryJdbcBaseTest;
2625
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
@@ -236,7 +235,6 @@ public static synchronized String getSharedDataset2() {
236235
}
237236

238237
private static void createSharedResources(String dataset) throws InterruptedException {
239-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
240238
String project = DEFAULT_CATALOG;
241239
String script = String.format(CREATE_RESOURCES_SCRIPT, project, dataset);
242240
bigQuery.query(QueryJobConfiguration.of(script));
@@ -248,7 +246,6 @@ private static void registerShutdownHook(final String dataset) {
248246
new Thread(
249247
() -> {
250248
try {
251-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
252249
bigQuery.query(
253250
QueryJobConfiguration.of(
254251
String.format(dropSchema, DEFAULT_CATALOG, dataset)));
@@ -268,6 +265,7 @@ public static String getBaseConnectionUrl() {
268265

269266
public static final String connectionUrl =
270267
getBaseConnectionUrl() + "ProjectId=" + DEFAULT_CATALOG + ";OAuthType=3;Timeout=3600;";
268+
public static final BigQuery bigQuery = BigQueryJdbcBaseTest.getBigQuery(connectionUrl);
271269

272270
public static final String createDatasetQuery =
273271
"CREATE SCHEMA IF NOT EXISTS `%s.%s` OPTIONS(default_table_expiration_days = 5)";
@@ -341,7 +339,6 @@ public static String getBaseConnectionUrl() {
341339

342340
public static void setUpProcedure(String dataset, String table) throws InterruptedException {
343341
{
344-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
345342
bigQuery.query(
346343
QueryJobConfiguration.of(
347344
String.format(
@@ -350,13 +347,11 @@ public static void setUpProcedure(String dataset, String table) throws Interrupt
350347
}
351348

352349
public static void setUpDataset(String dataset) throws InterruptedException {
353-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
354350
bigQuery.query(
355351
QueryJobConfiguration.of(String.format(createDatasetQuery, DEFAULT_CATALOG, dataset)));
356352
}
357353

358354
public static void setUpTable(String dataset, String table) throws InterruptedException {
359-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
360355
bigQuery.query(
361356
QueryJobConfiguration.of(String.format(createTableQuery, DEFAULT_CATALOG, dataset, table)));
362357
bigQuery.query(
@@ -366,7 +361,6 @@ public static void setUpTable(String dataset, String table) throws InterruptedEx
366361
}
367362

368363
public static void cleanUp(String dataset) throws InterruptedException {
369-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
370364
bigQuery.query(QueryJobConfiguration.of(String.format(dropSchema, DEFAULT_CATALOG, dataset)));
371365
}
372366

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
2626

2727
import com.google.cloud.ServiceOptions;
28-
import com.google.cloud.bigquery.BigQuery;
2928
import com.google.cloud.bigquery.BigQueryError;
30-
import com.google.cloud.bigquery.BigQueryOptions;
3129
import com.google.cloud.bigquery.DatasetId;
3230
import com.google.cloud.bigquery.Job;
3331
import com.google.cloud.bigquery.JobInfo;
@@ -74,7 +72,6 @@ public class ITBigQueryJDBCTest extends ITBase {
7472
private static String DATASET;
7573
private static final Object EXCEPTION_REPLACEMENT = "EXCEPTION-WAS-RAISED";
7674
static Connection bigQueryConnection;
77-
static BigQuery bigQuery;
7875
static Statement bigQueryStatement;
7976
static Connection bigQueryConnectionNoReadApi;
8077
static Statement bigQueryStatementNoReadApi;
@@ -89,7 +86,6 @@ public static void beforeClass() throws SQLException {
8986
noReadApi.setProperty("EnableHighThroughputAPI", "0");
9087
bigQueryConnectionNoReadApi = DriverManager.getConnection(connection_uri, noReadApi);
9188
bigQueryStatementNoReadApi = bigQueryConnectionNoReadApi.createStatement();
92-
bigQuery = BigQueryOptions.newBuilder().build().getService();
9389
}
9490

9591
@AfterAll

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import com.google.cloud.ServiceOptions;
24-
import com.google.cloud.bigquery.BigQuery;
25-
import com.google.cloud.bigquery.BigQueryOptions;
2624
import com.google.cloud.bigquery.Dataset;
2725
import com.google.cloud.bigquery.DatasetId;
2826
import java.sql.Connection;
@@ -36,7 +34,7 @@
3634
import org.junit.jupiter.api.Disabled;
3735
import org.junit.jupiter.api.Test;
3836

39-
public class ITDriverTest {
37+
public class ITDriverTest extends ITBase {
4038

4139
private static final String DEFAULT_CATALOG = ServiceOptions.getDefaultProjectId();
4240
static Random random = new Random();
@@ -96,14 +94,10 @@ public void testDriverLocation() throws SQLException, InterruptedException {
9694

9795
String datasetUS = "JDBC_DRIVER_US_TEST_DATASET" + random.nextInt(999);
9896
String tableNameUS = "JDBC_DRIVER_US_TEST_TABLE" + randomNumber;
99-
String OAUTH_TYPE = "3";
100-
String CONNECTION_URL =
101-
"jdbc:bigquery://https://bigquery.googleapis.com/bigquery/v2/:443;ProjectId=%s;OAuthType=%s;LOCATION=%s;";
97+
String CONNECTION_URL = ITBase.connectionUrl + "LOCATION=us-east5;";
10298

10399
// US Connection
104-
Connection connectionUS =
105-
DriverManager.getConnection(
106-
String.format(CONNECTION_URL, DEFAULT_CATALOG, OAUTH_TYPE, "us-east5"));
100+
Connection connectionUS = DriverManager.getConnection(CONNECTION_URL);
107101
Statement statementUS = connectionUS.createStatement();
108102
statementUS.execute(String.format(createDataset, DEFAULT_CATALOG, datasetUS));
109103
statementUS.execute(String.format(createQuery, DEFAULT_CATALOG, datasetUS, tableNameUS));
@@ -114,7 +108,6 @@ public void testDriverLocation() throws SQLException, InterruptedException {
114108
ResultSet res = statementUS.getResultSet();
115109
assertTrue(res.next());
116110

117-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
118111
Dataset retrievedDataset = bigQuery.getDataset(DatasetId.of(DEFAULT_CATALOG, datasetUS));
119112
assertEquals("us-east5", retrievedDataset.getLocation());
120113
ITBase.cleanUp(datasetUS);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.api.gax.paging.Page;
2929
import com.google.cloud.ServiceOptions;
3030
import com.google.cloud.bigquery.BigQuery;
31-
import com.google.cloud.bigquery.BigQueryOptions;
3231
import com.google.cloud.bigquery.Dataset;
3332
import com.google.cloud.bigquery.DatasetId;
3433
import com.google.cloud.bigquery.FieldValueList;
@@ -378,21 +377,18 @@ public void testRangeSelectDataset() throws SQLException {
378377

379378
@Test
380379
public void testTemporaryDatasetLocation() throws SQLException, InterruptedException {
381-
String projectId = DEFAULT_CATALOG;
382380
String location = "europe-west3";
383381
String randomSuffix = String.valueOf(100 + new Random().nextInt(900));
384382
String tempDatasetName = "jdbc_temp_dataset_" + System.currentTimeMillis() + "_" + randomSuffix;
385383

386384
String customConnectionUrl =
387-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId="
388-
+ projectId
389-
+ ";OAuthType=3;Timeout=3600;Location="
385+
ITBase.connectionUrl
386+
+ "Location="
390387
+ location
391388
+ ";AllowLargeResults=true;LargeResultDataset="
392389
+ tempDatasetName
393390
+ ";";
394391

395-
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
396392
try (Connection connection = DriverManager.getConnection(customConnectionUrl)) {
397393
Statement statement = connection.createStatement();
398394
String query = "SELECT 1 as val;";

0 commit comments

Comments
 (0)