From 8990b959011f37e548c99e1dd305c3111814c727 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 23 Jun 2020 16:32:08 +0530 Subject: [PATCH 1/6] docs(samples): add auth user flow and query --- samples/install-without-bom/pom.xml | 10 ++ samples/snapshot/pom.xml | 12 +- .../com/example/bigquery/AuthUserFlow.java | 119 +++++++++++++++++ .../com/example/bigquery/AuthUserQuery.java | 125 ++++++++++++++++++ .../com/example/bigquery/AuthUserFlowIT.java | 50 +++++++ .../com/example/bigquery/AuthUserQueryIT.java | 39 ++++++ 6 files changed, 353 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java create mode 100644 samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index a9626a7d9..52cae17a2 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -49,6 +49,16 @@ + + com.google.oauth-client + google-oauth-client-java6 + 1.30.6 + + + com.google.oauth-client + google-oauth-client-jetty + 1.30.6 + junit diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 83d3d3f83..31c5bcc22 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -47,7 +47,16 @@ 1.116.5-SNAPSHOT - + + com.google.oauth-client + google-oauth-client-java6 + 1.30.6 + + + com.google.oauth-client + google-oauth-client-jetty + 1.30.6 + junit @@ -61,7 +70,6 @@ 1.0.1 test - diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java new file mode 100644 index 000000000..78cb9b4e6 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java @@ -0,0 +1,119 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_auth_user_flow] +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.api.gax.paging.Page; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.UserCredentials; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.security.GeneralSecurityException; +import java.util.List; + +// Sample to authenticate by using a user credential +public class AuthUserFlow { + + private static final File DATA_STORE_DIR = + new File(AuthUserFlow.class.getResource("/").getPath(), "credentials"); + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + // i.e redirect_uri http://localhost:61984/Callback + private static final int LOCAL_RECEIVER_PORT = 61984; + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + /** + * Download your OAuth2 configuration from the Google Developers Console API Credentials page. + * https://console.cloud.google.com/apis/credentials + */ + File credentialsPath = new File("path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + authUserFlow(credentialsPath, scopes); + } + + public static void authUserFlow(File credentialsPath, List selectedScopes) { + try { + + // Load client_secret.json file + GoogleClientSecrets clientSecrets = + GoogleClientSecrets.load( + JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath))); + String clientId = clientSecrets.getDetails().getClientId(); + String clientSecret = clientSecrets.getDetails().getClientSecret(); + + // Generate the url that will be used for the consent dialog. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JSON_FACTORY, + clientSecrets, + selectedScopes) + .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) + .setAccessType("offline") + .setApprovalPrompt("force") + .build(); + + // Exchange an authorization code for refresh token + LocalServerReceiver receiver = + new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + + // OAuth2 Credentials representing a user's identity and consent + GoogleCredentials credentials = + UserCredentials.newBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .setRefreshToken(credential.getRefreshToken()) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = + BigQueryOptions.newBuilder().setCredentials(credentials).build().getService(); + + Page datasets = bigquery.listDatasets(BigQuery.DatasetListOption.pageSize(100)); + if (datasets == null) { + System.out.println("Dataset does not contain any models"); + return; + } + datasets + .iterateAll() + .forEach( + dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId())); + + } catch (BigQueryException | IOException | GeneralSecurityException ex) { + System.out.println("Project does not contain any datasets \n" + ex.toString()); + } + } +} +// [END bigquery_auth_user_flow] diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java new file mode 100644 index 000000000..3cedd1b59 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java @@ -0,0 +1,125 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_auth_user_query] +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.UserCredentials; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.TableResult; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.security.GeneralSecurityException; +import java.util.List; + +// Sample to query by using a user credential +public class AuthUserQuery { + + private static final File DATA_STORE_DIR = + new File(AuthUserQuery.class.getResource("/").getPath(), "credentials"); + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + // i.e redirect_uri http://localhost:61984/Callback + private static final int LOCAL_RECEIVER_PORT = 61984; + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + /** + * Download your OAuth2 configuration from the Google Developers Console API Credentials page. + * https://console.cloud.google.com/apis/credentials + */ + File credentialsPath = new File("path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + String query = + "SELECT name, SUM(number) as total" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE name = 'William'" + + " GROUP BY name;"; + authUserQuery(credentialsPath, scopes, query); + } + + public static void authUserQuery( + File credentialsPath, List selectedScopes, String query) { + try { + + // Load client_secret.json file + GoogleClientSecrets clientSecrets = + GoogleClientSecrets.load( + JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath))); + String clientId = clientSecrets.getDetails().getClientId(); + String clientSecret = clientSecrets.getDetails().getClientSecret(); + + // Generate the url that will be used for the consent dialog. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JSON_FACTORY, + clientSecrets, + selectedScopes) + .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) + .setAccessType("offline") + .setApprovalPrompt("force") + .build(); + + // Exchange an authorization code for refresh token + LocalServerReceiver receiver = + new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + + // OAuth2 Credentials representing a user's identity and consent + GoogleCredentials credentials = + UserCredentials.newBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .setRefreshToken(credential.getRefreshToken()) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = + BigQueryOptions.newBuilder().setCredentials(credentials).build().getService(); + + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); + + TableResult results = bigquery.query(queryConfig); + + results + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString()))); + + System.out.println("Query performed successfully."); + + } catch (BigQueryException | IOException | GeneralSecurityException | InterruptedException ex) { + System.out.println("Query not performed \n" + ex.toString()); + } + } +} +// [END bigquery_auth_user_query] diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java new file mode 100644 index 000000000..129bebe8d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AuthUserFlowIT { + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testAuthUserFlow() { + // TODO(stephaniewang526): Replace client_secret.json + /*File credentialsPath = new File(""path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + AuthUserFlow.authUserFlow(credentialsPath, scopes); + assertThat(bout.toString()).contains("Success! Dataset ID");*/ + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java new file mode 100644 index 000000000..b2be59544 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java @@ -0,0 +1,39 @@ +package com.example.bigquery; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AuthUserQueryIT { + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testAuthUserFlow() { + // TODO(stephaniewang526): Replace client_secret.json + /*File credentialsPath = new File("\"path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + String query = + "SELECT name, SUM(number) as total" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE name = 'William'" + + " GROUP BY name;"; + AuthUserQuery.authUserQuery(credentialsPath, scopes, query); + assertThat(bout.toString()).contains("Query performed successfully.");*/ + } +} From 5999fc76b2cb7ced874fde0bdcb18ac67d927d7f Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 23 Jun 2020 17:35:24 +0530 Subject: [PATCH 2/6] docs(samples): add dependencies --- samples/snippets/pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 2ca932e58..632f01667 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -59,6 +59,16 @@ + + com.google.oauth-client + google-oauth-client-java6 + 1.30.6 + + + com.google.oauth-client + google-oauth-client-jetty + 1.30.6 + junit From bb82aebd0649a3b7595dd5eebc1f70473dbd7827 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 23 Jun 2020 18:56:15 +0530 Subject: [PATCH 3/6] docs(samples): rename methods --- .../src/main/java/com/example/bigquery/AuthUserFlow.java | 2 +- .../src/main/java/com/example/bigquery/AuthUserQuery.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java index 78cb9b4e6..cdd20addd 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java @@ -50,7 +50,7 @@ public class AuthUserFlow { // i.e redirect_uri http://localhost:61984/Callback private static final int LOCAL_RECEIVER_PORT = 61984; - public static void main(String[] args) { + public static void runAuthUserFlow() { // TODO(developer): Replace these variables before running the sample. /** * Download your OAuth2 configuration from the Google Developers Console API Credentials page. diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java index 3cedd1b59..3d8417941 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java @@ -50,7 +50,7 @@ public class AuthUserQuery { // i.e redirect_uri http://localhost:61984/Callback private static final int LOCAL_RECEIVER_PORT = 61984; - public static void main(String[] args) { + public static void runAuthUserQuery() { // TODO(developer): Replace these variables before running the sample. /** * Download your OAuth2 configuration from the Google Developers Console API Credentials page. From 4be79ba6125db0269ea1babba9f1ee50be3e2e55 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 3 Jul 2020 18:27:36 +0530 Subject: [PATCH 4/6] docs(samples): address feedback --- samples/install-without-bom/pom.xml | 6 ++ samples/snapshot/pom.xml | 6 ++ samples/snippets/pom.xml | 6 ++ .../com/example/bigquery/AuthUserFlow.java | 15 +++-- .../com/example/bigquery/AuthUserQuery.java | 15 +++-- .../com/example/bigquery/AuthUserFlowIT.java | 37 +++++++++-- .../com/example/bigquery/AuthUserQueryIT.java | 63 +++++++++++++++---- 7 files changed, 119 insertions(+), 29 deletions(-) diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index 52cae17a2..f96093eb4 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -72,6 +72,12 @@ 1.0.1 test + + com.google.cloud + google-cloud-nio + 0.121.2 + test + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 31c5bcc22..22f2e5a0e 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -70,6 +70,12 @@ 1.0.1 test + + com.google.cloud + google-cloud-nio + 0.121.2 + test + diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 632f01667..fcb16b573 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -82,6 +82,12 @@ 1.0.1 test + + com.google.cloud + google-cloud-nio + 0.121.2 + test + diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java index cdd20addd..9c2adec1f 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java @@ -35,9 +35,12 @@ import com.google.cloud.bigquery.Dataset; import com.google.common.collect.ImmutableList; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.util.List; @@ -56,18 +59,18 @@ public static void runAuthUserFlow() { * Download your OAuth2 configuration from the Google Developers Console API Credentials page. * https://console.cloud.google.com/apis/credentials */ - File credentialsPath = new File("path/to/your/client_secret.json"); + Path credentialsPath = Paths.get("path/to/your/client_secret.json"); List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); authUserFlow(credentialsPath, scopes); } - public static void authUserFlow(File credentialsPath, List selectedScopes) { - try { + public static void authUserFlow(Path credentialsPath, List selectedScopes) { + // Reading credentials file + try (InputStream inputStream = Files.newInputStream(credentialsPath)) { // Load client_secret.json file GoogleClientSecrets clientSecrets = - GoogleClientSecrets.load( - JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath))); + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream)); String clientId = clientSecrets.getDetails().getClientId(); String clientSecret = clientSecrets.getDetails().getClientSecret(); diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java index 3d8417941..4bbef4f52 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java @@ -35,9 +35,12 @@ import com.google.cloud.bigquery.TableResult; import com.google.common.collect.ImmutableList; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.util.List; @@ -56,7 +59,7 @@ public static void runAuthUserQuery() { * Download your OAuth2 configuration from the Google Developers Console API Credentials page. * https://console.cloud.google.com/apis/credentials */ - File credentialsPath = new File("path/to/your/client_secret.json"); + Path credentialsPath = Paths.get("path/to/your/client_secret.json"); List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); String query = "SELECT name, SUM(number) as total" @@ -67,13 +70,13 @@ public static void runAuthUserQuery() { } public static void authUserQuery( - File credentialsPath, List selectedScopes, String query) { - try { + Path credentialsPath, List selectedScopes, String query) { + // Reading credentials file + try (InputStream inputStream = Files.newInputStream(credentialsPath)) { // Load client_secret.json file GoogleClientSecrets clientSecrets = - GoogleClientSecrets.load( - JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsPath))); + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream)); String clientId = clientSecrets.getDetails().getClientId(); String clientSecret = clientSecrets.getDetails().getClientSecret(); diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java index 129bebe8d..abdc867c2 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java @@ -16,10 +16,19 @@ package com.example.bigquery; +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; +import com.google.common.collect.ImmutableList; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Path; +import java.util.List; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; public class AuthUserFlowIT { @@ -27,6 +36,21 @@ public class AuthUserFlowIT { private ByteArrayOutputStream bout; private PrintStream out; + private static final String GCS_BUCKET = System.getenv("GCS_BUCKET"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GCS_BUCKET"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); @@ -40,11 +64,12 @@ public void tearDown() { } @Test - public void testAuthUserFlow() { - // TODO(stephaniewang526): Replace client_secret.json - /*File credentialsPath = new File(""path/to/your/client_secret.json"); - List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); - AuthUserFlow.authUserFlow(credentialsPath, scopes); - assertThat(bout.toString()).contains("Success! Dataset ID");*/ + public void testAuthUserFlow() throws IOException { + try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket(GCS_BUCKET)) { + Path credentialsPath = fs.getPath("client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + AuthUserFlow.authUserFlow(credentialsPath, scopes); + } + assertThat(bout.toString()).contains("Success! Dataset ID"); } } diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java index b2be59544..30c77451e 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java @@ -1,9 +1,34 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.bigquery; +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; +import com.google.common.collect.ImmutableList; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Path; +import java.util.List; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; public class AuthUserQueryIT { @@ -11,6 +36,21 @@ public class AuthUserQueryIT { private ByteArrayOutputStream bout; private PrintStream out; + private static final String GCS_BUCKET = System.getenv("GCS_BUCKET"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GCS_BUCKET"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); @@ -24,16 +64,17 @@ public void tearDown() { } @Test - public void testAuthUserFlow() { - // TODO(stephaniewang526): Replace client_secret.json - /*File credentialsPath = new File("\"path/to/your/client_secret.json"); - List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); - String query = - "SELECT name, SUM(number) as total" - + " FROM `bigquery-public-data.usa_names.usa_1910_current`" - + " WHERE name = 'William'" - + " GROUP BY name;"; - AuthUserQuery.authUserQuery(credentialsPath, scopes, query); - assertThat(bout.toString()).contains("Query performed successfully.");*/ + public void testAuthUserQuery() throws IOException { + try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket(GCS_BUCKET)) { + Path credentialsPath = fs.getPath("client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + String query = + "SELECT name, SUM(number) as total" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE name = 'William'" + + " GROUP BY name;"; + AuthUserQuery.authUserQuery(credentialsPath, scopes, query); + } + assertThat(bout.toString()).contains("Query performed successfully."); } } From 14a81860138a083525a10998eeebe3a9090dbc50 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Mon, 6 Jul 2020 22:21:53 +0530 Subject: [PATCH 5/6] docs(samples): set auto --- .../src/main/java/com/example/bigquery/AuthUserFlow.java | 2 +- .../src/main/java/com/example/bigquery/AuthUserQuery.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java index 9c2adec1f..4d2de24f4 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java @@ -83,7 +83,7 @@ public static void authUserFlow(Path credentialsPath, List selectedScope selectedScopes) .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) .setAccessType("offline") - .setApprovalPrompt("force") + .setApprovalPrompt("auto") .build(); // Exchange an authorization code for refresh token diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java index 4bbef4f52..7be558659 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java @@ -89,7 +89,7 @@ public static void authUserQuery( selectedScopes) .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) .setAccessType("offline") - .setApprovalPrompt("force") + .setApprovalPrompt("auto") .build(); // Exchange an authorization code for refresh token From 315cbb5c95eca208e40734a2ed16562907777e8b Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 17 Jul 2020 11:39:27 +0530 Subject: [PATCH 6/6] docs(samples): modified code and remove deps --- samples/install-without-bom/pom.xml | 6 -- samples/snapshot/pom.xml | 6 -- samples/snippets/pom.xml | 6 -- .../com/example/bigquery/AuthUserFlowIT.java | 75 ----------------- .../com/example/bigquery/AuthUserQueryIT.java | 80 ------------------- 5 files changed, 173 deletions(-) delete mode 100644 samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java delete mode 100644 samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index f96093eb4..52cae17a2 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -72,12 +72,6 @@ 1.0.1 test - - com.google.cloud - google-cloud-nio - 0.121.2 - test - diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 22f2e5a0e..31c5bcc22 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -70,12 +70,6 @@ 1.0.1 test - - com.google.cloud - google-cloud-nio - 0.121.2 - test - diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index fcb16b573..632f01667 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -82,12 +82,6 @@ 1.0.1 test - - com.google.cloud - google-cloud-nio - 0.121.2 - test - diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java deleted file mode 100644 index abdc867c2..000000000 --- a/samples/snippets/src/test/java/com/example/bigquery/AuthUserFlowIT.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.bigquery; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; -import com.google.common.collect.ImmutableList; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Path; -import java.util.List; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class AuthUserFlowIT { - - private ByteArrayOutputStream bout; - private PrintStream out; - - private static final String GCS_BUCKET = System.getenv("GCS_BUCKET"); - - private static String requireEnvVar(String varName) { - String value = System.getenv(varName); - assertNotNull( - "Environment variable " + varName + " is required to perform these tests.", - System.getenv(varName)); - return value; - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GCS_BUCKET"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - - @Test - public void testAuthUserFlow() throws IOException { - try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket(GCS_BUCKET)) { - Path credentialsPath = fs.getPath("client_secret.json"); - List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); - AuthUserFlow.authUserFlow(credentialsPath, scopes); - } - assertThat(bout.toString()).contains("Success! Dataset ID"); - } -} diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java deleted file mode 100644 index 30c77451e..000000000 --- a/samples/snippets/src/test/java/com/example/bigquery/AuthUserQueryIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.bigquery; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; -import com.google.common.collect.ImmutableList; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Path; -import java.util.List; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class AuthUserQueryIT { - - private ByteArrayOutputStream bout; - private PrintStream out; - - private static final String GCS_BUCKET = System.getenv("GCS_BUCKET"); - - private static String requireEnvVar(String varName) { - String value = System.getenv(varName); - assertNotNull( - "Environment variable " + varName + " is required to perform these tests.", - System.getenv(varName)); - return value; - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GCS_BUCKET"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - - @Test - public void testAuthUserQuery() throws IOException { - try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket(GCS_BUCKET)) { - Path credentialsPath = fs.getPath("client_secret.json"); - List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); - String query = - "SELECT name, SUM(number) as total" - + " FROM `bigquery-public-data.usa_names.usa_1910_current`" - + " WHERE name = 'William'" - + " GROUP BY name;"; - AuthUserQuery.authUserQuery(credentialsPath, scopes, query); - } - assertThat(bout.toString()).contains("Query performed successfully."); - } -}