diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcBaseTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcBaseTest.java index d9b1ce473324..8be466e8178c 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcBaseTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcBaseTest.java @@ -16,8 +16,11 @@ package com.google.cloud.bigquery.jdbc; +import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.jdbc.utils.TestUtilities; import com.google.cloud.bigquery.jdbc.utils.URIBuilder; +import java.sql.DriverManager; +import java.sql.SQLException; public class BigQueryJdbcBaseTest { @@ -43,6 +46,16 @@ public class BigQueryJdbcBaseTest { + // "-----END PRIVATE KEY-----"; + protected static BigQuery getBigQuery(String connectionUrl) { + try { + return DriverManager.getConnection(connectionUrl) + .unwrap(BigQueryConnection.class) + .getBigQuery(); + } catch (SQLException e) { + throw new RuntimeException("Failed to initialize BigQuery client", e); + } + } + protected static URIBuilder getBaseUri() { return new URIBuilder(TestUtilities.getBaseConnectionUrl()); } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java index 1d6bac0cd257..a95a05229f9b 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java @@ -25,12 +25,10 @@ import com.google.cloud.ServiceOptions; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Connection; @@ -300,10 +298,10 @@ public void testValidExternalAccountAuthenticationRawJson() throws SQLException public void testValidPreGeneratedAccessTokenAuthentication(String scope, boolean isReadOnly) throws Exception { final JsonObject authJson = getAuthJson(); - InputStream stream = - new ByteArrayInputStream(authJson.toString().getBytes(StandardCharsets.UTF_8)); + GoogleCredentials credentials = - GoogleCredentials.fromStream(stream).createScoped(Arrays.asList(scope)); + ((GoogleCredentials) bigQuery.getOptions().getCredentials()) + .createScoped(Arrays.asList(scope)); credentials.refresh(); String accessToken = credentials.getAccessToken().getTokenValue(); diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java index 9a9b947674a2..0224cafab695 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBase.java @@ -20,7 +20,6 @@ import com.google.cloud.ServiceOptions; import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.jdbc.BigQueryJdbcBaseTest; import com.google.cloud.bigquery.jdbc.utils.TestUtilities; @@ -236,7 +235,6 @@ public static synchronized String getSharedDataset2() { } private static void createSharedResources(String dataset) throws InterruptedException { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); String project = DEFAULT_CATALOG; String script = String.format(CREATE_RESOURCES_SCRIPT, project, dataset); bigQuery.query(QueryJobConfiguration.of(script)); @@ -248,7 +246,6 @@ private static void registerShutdownHook(final String dataset) { new Thread( () -> { try { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); bigQuery.query( QueryJobConfiguration.of( String.format(dropSchema, DEFAULT_CATALOG, dataset))); @@ -268,6 +265,7 @@ public static String getBaseConnectionUrl() { public static final String connectionUrl = getBaseConnectionUrl() + "ProjectId=" + DEFAULT_CATALOG + ";OAuthType=3;Timeout=3600;"; + public static final BigQuery bigQuery = BigQueryJdbcBaseTest.getBigQuery(connectionUrl); public static final String createDatasetQuery = "CREATE SCHEMA IF NOT EXISTS `%s.%s` OPTIONS(default_table_expiration_days = 5)"; @@ -341,7 +339,6 @@ public static String getBaseConnectionUrl() { public static void setUpProcedure(String dataset, String table) throws InterruptedException { { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); bigQuery.query( QueryJobConfiguration.of( String.format( @@ -350,13 +347,11 @@ public static void setUpProcedure(String dataset, String table) throws Interrupt } public static void setUpDataset(String dataset) throws InterruptedException { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); bigQuery.query( QueryJobConfiguration.of(String.format(createDatasetQuery, DEFAULT_CATALOG, dataset))); } public static void setUpTable(String dataset, String table) throws InterruptedException { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); bigQuery.query( QueryJobConfiguration.of(String.format(createTableQuery, DEFAULT_CATALOG, dataset, table))); bigQuery.query( @@ -366,7 +361,6 @@ public static void setUpTable(String dataset, String table) throws InterruptedEx } public static void cleanUp(String dataset) throws InterruptedException { - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); bigQuery.query(QueryJobConfiguration.of(String.format(dropSchema, DEFAULT_CATALOG, dataset))); } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java index 161f2ac173a6..47b0d0893f53 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java @@ -25,9 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.cloud.ServiceOptions; -import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryError; -import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.DatasetId; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobInfo; @@ -74,7 +72,6 @@ public class ITBigQueryJDBCTest extends ITBase { private static String DATASET; private static final Object EXCEPTION_REPLACEMENT = "EXCEPTION-WAS-RAISED"; static Connection bigQueryConnection; - static BigQuery bigQuery; static Statement bigQueryStatement; static Connection bigQueryConnectionNoReadApi; static Statement bigQueryStatementNoReadApi; @@ -89,7 +86,6 @@ public static void beforeClass() throws SQLException { noReadApi.setProperty("EnableHighThroughputAPI", "0"); bigQueryConnectionNoReadApi = DriverManager.getConnection(connection_uri, noReadApi); bigQueryStatementNoReadApi = bigQueryConnectionNoReadApi.createStatement(); - bigQuery = BigQueryOptions.newBuilder().build().getService(); } @AfterAll diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITDriverTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITDriverTest.java index c52e47b293b8..93d8727482f9 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITDriverTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITDriverTest.java @@ -21,8 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.cloud.ServiceOptions; -import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.Dataset; import com.google.cloud.bigquery.DatasetId; import java.sql.Connection; @@ -36,7 +34,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -public class ITDriverTest { +public class ITDriverTest extends ITBase { private static final String DEFAULT_CATALOG = ServiceOptions.getDefaultProjectId(); static Random random = new Random(); @@ -96,14 +94,10 @@ public void testDriverLocation() throws SQLException, InterruptedException { String datasetUS = "JDBC_DRIVER_US_TEST_DATASET" + random.nextInt(999); String tableNameUS = "JDBC_DRIVER_US_TEST_TABLE" + randomNumber; - String OAUTH_TYPE = "3"; - String CONNECTION_URL = - "jdbc:bigquery://https://bigquery.googleapis.com/bigquery/v2/:443;ProjectId=%s;OAuthType=%s;LOCATION=%s;"; + String CONNECTION_URL = ITBase.connectionUrl + "LOCATION=us-east5;"; // US Connection - Connection connectionUS = - DriverManager.getConnection( - String.format(CONNECTION_URL, DEFAULT_CATALOG, OAUTH_TYPE, "us-east5")); + Connection connectionUS = DriverManager.getConnection(CONNECTION_URL); Statement statementUS = connectionUS.createStatement(); statementUS.execute(String.format(createDataset, DEFAULT_CATALOG, datasetUS)); statementUS.execute(String.format(createQuery, DEFAULT_CATALOG, datasetUS, tableNameUS)); @@ -114,7 +108,6 @@ public void testDriverLocation() throws SQLException, InterruptedException { ResultSet res = statementUS.getResultSet(); assertTrue(res.next()); - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); Dataset retrievedDataset = bigQuery.getDataset(DatasetId.of(DEFAULT_CATALOG, datasetUS)); assertEquals("us-east5", retrievedDataset.getLocation()); ITBase.cleanUp(datasetUS); diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITStatementTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITStatementTest.java index addbbb3f0b27..5d69867b01fe 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITStatementTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITStatementTest.java @@ -28,7 +28,6 @@ import com.google.api.gax.paging.Page; import com.google.cloud.ServiceOptions; import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.Dataset; import com.google.cloud.bigquery.DatasetId; import com.google.cloud.bigquery.FieldValueList; @@ -378,21 +377,18 @@ public void testRangeSelectDataset() throws SQLException { @Test public void testTemporaryDatasetLocation() throws SQLException, InterruptedException { - String projectId = DEFAULT_CATALOG; String location = "europe-west3"; String randomSuffix = String.valueOf(100 + new Random().nextInt(900)); String tempDatasetName = "jdbc_temp_dataset_" + System.currentTimeMillis() + "_" + randomSuffix; String customConnectionUrl = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=" - + projectId - + ";OAuthType=3;Timeout=3600;Location=" + ITBase.connectionUrl + + "Location=" + location + ";AllowLargeResults=true;LargeResultDataset=" + tempDatasetName + ";"; - BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); try (Connection connection = DriverManager.getConnection(customConnectionUrl)) { Statement statement = connection.createStatement(); String query = "SELECT 1 as val;";