Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}
}
Comment on lines +49 to +57

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doubt: So here we are unwrapping the BigQuery client from the connection directly. Since we don't save a reference to the Connection itself, how will it get closed later in the tests? I'm just thinking about potential resource leaks or background threads staying open


protected static URIBuilder getBaseUri() {
return new URIBuilder(TestUtilities.getBaseConnectionUrl());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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)));
Expand All @@ -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)";
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use try-with resource

Statement statementUS = connectionUS.createStatement();
statementUS.execute(String.format(createDataset, DEFAULT_CATALOG, datasetUS));
statementUS.execute(String.format(createQuery, DEFAULT_CATALOG, datasetUS, tableNameUS));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;";
Expand Down
Loading