Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): fix GrantViewAccess sample IT failure #1816

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,6 @@
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.Table;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -44,8 +43,8 @@ public static void grantViewAccess(String srcDatasetId, String viewDatasetId, St
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

Dataset srcDataset = bigquery.getDataset(DatasetId.of(srcDatasetId));
Dataset viewDataset = bigquery.getDataset(DatasetId.of(viewDatasetId));
Dataset srcDataset = bigquery.getDataset(srcDatasetId);
Dataset viewDataset = bigquery.getDataset(viewDatasetId);
Table view = viewDataset.get(viewId);
// First, we'll add a group to the ACL for the dataset containing the view. This will allow
// users within that group to query the view, but they must have direct access to any tables
Expand Down
Expand Up @@ -36,14 +36,14 @@ public class GrantViewAccessIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private String datasetName;
private String viewDatasetName;
private String tableName;
private String viewName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
Expand All @@ -56,7 +56,6 @@ private static String requireEnvVar(String varName) {
@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("BIGQUERY_DATASET_NAME");
}

@Before
Expand All @@ -68,30 +67,32 @@ public void setUp() {

// create a temporary dataset, table and view to be deleted.
datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
viewDatasetName = "MY_VIEW_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);

CreateDataset.createDataset(datasetName);
CreateDataset.createDataset(viewDatasetName);

Schema schema =
Schema.of(
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
Field.of("stringField", StandardSQLTypeName.STRING),
Field.of("booleanField", StandardSQLTypeName.BOOL));
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
CreateTable.createTable(viewDatasetName, tableName, schema);

String query =
String.format(
"SELECT timestampField, stringField, booleanField FROM %s.%s",
BIGQUERY_DATASET_NAME, tableName);
CreateView.createView(BIGQUERY_DATASET_NAME, viewName, query);
viewDatasetName, tableName);
CreateView.createView(viewDatasetName, viewName, query);
}

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, viewName);
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
DeleteTable.deleteTable(viewDatasetName, viewName);
DeleteTable.deleteTable(viewDatasetName, tableName);
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
// restores print statements in the original method
System.out.flush();
Expand All @@ -101,7 +102,7 @@ public void tearDown() {

@Test
public void testGrantViewAccess() {
GrantViewAccess.grantViewAccess(datasetName, BIGQUERY_DATASET_NAME, viewName);
GrantViewAccess.grantViewAccess(datasetName, viewDatasetName, viewName);
assertThat(bout.toString()).contains("Grant view access successfully");
}
}