From 7309be5a0b8a4ff84f55bb0f7ea7e57a4e8d0a33 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 1 Sep 2016 09:18:14 +0200 Subject: [PATCH] Support listing/getting/creating/updating tables/datasets/jobs in other projects (#1217) --- .../com/google/cloud/bigquery/BigQuery.java | 11 + .../google/cloud/bigquery/BigQueryImpl.java | 95 ++-- .../java/com/google/cloud/bigquery/JobId.java | 4 + .../com/google/cloud/bigquery/JobInfo.java | 6 +- .../cloud/bigquery/spi/BigQueryRpc.java | 35 +- .../bigquery/spi/DefaultBigQueryRpc.java | 68 +-- .../cloud/bigquery/BigQueryImplTest.java | 509 ++++++++++++------ .../com/google/cloud/bigquery/JobIdTest.java | 5 + .../google/cloud/bigquery/JobInfoTest.java | 16 +- .../cloud/bigquery/it/ITBigQueryTest.java | 18 + 10 files changed, 500 insertions(+), 267 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java index 4dc41c183ffa..9a6a4a7cd444 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java @@ -482,6 +482,17 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { */ Page listDatasets(DatasetListOption... options); + /** + * Lists the datasets in the provided project. This method returns partial information on each + * dataset: ({@link Dataset#datasetId()}, {@link Dataset#friendlyName()} and + * {@link Dataset#generatedId()}). To get complete information use either + * {@link #getDataset(String, DatasetOption...)} or + * {@link #getDataset(DatasetId, DatasetOption...)}. + * + * @throws BigQueryException upon failure + */ + Page listDatasets(String projectId, DatasetListOption... options); + /** * Deletes the requested dataset. * diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index d8dfcaea445d..bb1c137c4a66 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -48,9 +48,11 @@ private static class DatasetPageFetcher implements NextPageFetcher { private static final long serialVersionUID = -3057564042439021278L; private final Map requestOptions; private final BigQueryOptions serviceOptions; + private final String projectId; - DatasetPageFetcher(BigQueryOptions serviceOptions, String cursor, + DatasetPageFetcher(String projectId, BigQueryOptions serviceOptions, String cursor, Map optionMap) { + this.projectId = projectId; this.requestOptions = PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); this.serviceOptions = serviceOptions; @@ -58,7 +60,7 @@ private static class DatasetPageFetcher implements NextPageFetcher { @Override public Page nextPage() { - return listDatasets(serviceOptions, requestOptions); + return listDatasets(projectId, serviceOptions, requestOptions); } } @@ -67,19 +69,19 @@ private static class TablePageFetcher implements NextPageFetcher { private static final long serialVersionUID = 8611248840504201187L; private final Map requestOptions; private final BigQueryOptions serviceOptions; - private final String dataset; + private final DatasetId datasetId; - TablePageFetcher(String dataset, BigQueryOptions serviceOptions, String cursor, + TablePageFetcher(DatasetId datasetId, BigQueryOptions serviceOptions, String cursor, Map optionMap) { this.requestOptions = PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); this.serviceOptions = serviceOptions; - this.dataset = dataset; + this.datasetId = datasetId; } @Override public Page
nextPage() { - return listTables(dataset, serviceOptions, requestOptions); + return listTables(datasetId, serviceOptions, requestOptions); } } @@ -213,13 +215,15 @@ public Dataset getDataset(String datasetId, DatasetOption... options) { @Override public Dataset getDataset(final DatasetId datasetId, DatasetOption... options) { + final DatasetId completeDatasetId = datasetId.setProjectId(options().projectId()); final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Dataset answer = runWithRetries(new Callable() { @Override public com.google.api.services.bigquery.model.Dataset call() { - return bigQueryRpc.getDataset(datasetId.dataset(), optionsMap); + return bigQueryRpc.getDataset( + completeDatasetId.project(), completeDatasetId.dataset(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); return answer == null ? null : Dataset.fromPb(this, answer); @@ -230,11 +234,16 @@ public com.google.api.services.bigquery.model.Dataset call() { @Override public Page listDatasets(DatasetListOption... options) { - return listDatasets(options(), optionMap(options)); + return listDatasets(options().projectId(), options); } - private static Page listDatasets(final BigQueryOptions serviceOptions, - final Map optionsMap) { + @Override + public Page listDatasets(String projectId, DatasetListOption... options) { + return listDatasets(projectId, options(), optionMap(options)); + } + + private static Page listDatasets(final String projectId, + final BigQueryOptions serviceOptions, final Map optionsMap) { try { BigQueryRpc.Tuple> result = runWithRetries(new Callable listDatasets(final BigQueryOptions serviceOptions, @Override public BigQueryRpc.Tuple> call() { - return serviceOptions.rpc().listDatasets(optionsMap); + return serviceOptions.rpc().listDatasets(projectId, optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER, serviceOptions.clock()); String cursor = result.x(); - return new PageImpl<>(new DatasetPageFetcher(serviceOptions, cursor, optionsMap), cursor, - Iterables.transform(result.y(), + return new PageImpl<>(new DatasetPageFetcher(projectId, serviceOptions, cursor, optionsMap), + cursor, Iterables.transform(result.y(), new Function() { @Override public Dataset apply(com.google.api.services.bigquery.model.Dataset dataset) { @@ -265,13 +274,15 @@ public boolean delete(String datasetId, DatasetDeleteOption... options) { } @Override - public boolean delete(final DatasetId datasetId, DatasetDeleteOption... options) { + public boolean delete(DatasetId datasetId, DatasetDeleteOption... options) { + final DatasetId completeDatasetId = datasetId.setProjectId(options().projectId()); final Map optionsMap = optionMap(options); try { return runWithRetries(new Callable() { @Override public Boolean call() { - return bigQueryRpc.deleteDataset(datasetId.dataset(), optionsMap); + return bigQueryRpc.deleteDataset( + completeDatasetId.project(), completeDatasetId.dataset(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); } catch (RetryHelper.RetryHelperException e) { @@ -285,12 +296,14 @@ public boolean delete(String datasetId, String tableId) { } @Override - public boolean delete(final TableId tableId) { + public boolean delete(TableId tableId) { + final TableId completeTableId = tableId.setProjectId(options().projectId()); try { return runWithRetries(new Callable() { @Override public Boolean call() { - return bigQueryRpc.deleteTable(tableId.dataset(), tableId.table()); + return bigQueryRpc.deleteTable( + completeTableId.project(), completeTableId.dataset(), completeTableId.table()); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); } catch (RetryHelper.RetryHelperException e) { @@ -340,14 +353,16 @@ public Table getTable(final String datasetId, final String tableId, TableOption. } @Override - public Table getTable(final TableId tableId, TableOption... options) { + public Table getTable(TableId tableId, TableOption... options) { + final TableId completeTableId = tableId.setProjectId(options().projectId()); final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Table answer = runWithRetries(new Callable() { @Override public com.google.api.services.bigquery.model.Table call() { - return bigQueryRpc.getTable(tableId.dataset(), tableId.table(), optionsMap); + return bigQueryRpc.getTable(completeTableId.project(), completeTableId.dataset(), + completeTableId.table(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); return answer == null ? null : Table.fromPb(this, answer); @@ -358,16 +373,18 @@ public com.google.api.services.bigquery.model.Table call() { @Override public Page
listTables(String datasetId, TableListOption... options) { - return listTables(datasetId, options(), optionMap(options)); + return listTables( + DatasetId.of(options().projectId(), datasetId), options(), optionMap(options)); } @Override public Page
listTables(DatasetId datasetId, TableListOption... options) { - return listTables(datasetId.dataset(), options(), optionMap(options)); + DatasetId completeDatasetId = datasetId.setProjectId(options().projectId()); + return listTables(completeDatasetId, options(), optionMap(options)); } - private static Page
listTables(final String datasetId, final BigQueryOptions - serviceOptions, final Map optionsMap) { + private static Page
listTables(final DatasetId datasetId, + final BigQueryOptions serviceOptions, final Map optionsMap) { try { BigQueryRpc.Tuple> result = runWithRetries(new Callable listTables(final String datasetId, final BigQueryOpti @Override public BigQueryRpc.Tuple> call() { - return serviceOptions.rpc().listTables(datasetId, optionsMap); + return serviceOptions.rpc().listTables( + datasetId.project(), datasetId.dataset(), optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER, serviceOptions.clock()); String cursor = result.x(); @@ -395,7 +413,7 @@ public Table apply(com.google.api.services.bigquery.model.Table table) { @Override public InsertAllResponse insertAll(InsertAllRequest request) { - final TableId tableId = request.table(); + final TableId tableId = request.table().setProjectId(options().projectId()); final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest(); requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues()); requestPb.setSkipInvalidRows(request.skipInvalidRows()); @@ -408,7 +426,7 @@ public Rows apply(RowToInsert rowToInsert) { }); requestPb.setRows(rowsPb); return InsertAllResponse.fromPb( - bigQueryRpc.insertAll(tableId.dataset(), tableId.table(), requestPb)); + bigQueryRpc.insertAll(tableId.project(), tableId.dataset(), tableId.table(), requestPb)); } @Override @@ -425,12 +443,14 @@ public Page> listTableData(TableId tableId, TableDataListOption private static Page> listTableData(final TableId tableId, final BigQueryOptions serviceOptions, final Map optionsMap) { try { + final TableId completeTableId = tableId.setProjectId(serviceOptions.projectId()); BigQueryRpc.Tuple> result = runWithRetries(new Callable>>() { @Override public BigQueryRpc.Tuple> call() { return serviceOptions.rpc() - .listTableData(tableId.dataset(), tableId.table(), optionsMap); + .listTableData(completeTableId.project(), completeTableId.dataset(), + completeTableId.table(), optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER, serviceOptions.clock()); String cursor = result.x(); @@ -458,14 +478,15 @@ public Job getJob(String jobId, JobOption... options) { } @Override - public Job getJob(final JobId jobId, JobOption... options) { + public Job getJob(JobId jobId, JobOption... options) { final Map optionsMap = optionMap(options); + final JobId completeJobId = jobId.setProjectId(options().projectId()); try { com.google.api.services.bigquery.model.Job answer = runWithRetries(new Callable() { @Override public com.google.api.services.bigquery.model.Job call() { - return bigQueryRpc.getJob(jobId.job(), optionsMap); + return bigQueryRpc.getJob(completeJobId.project(), completeJobId.job(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); return answer == null ? null : Job.fromPb(this, answer); @@ -487,7 +508,7 @@ private static Page listJobs(final BigQueryOptions serviceOptions, @Override public BigQueryRpc.Tuple> call() { - return serviceOptions.rpc().listJobs(optionsMap); + return serviceOptions.rpc().listJobs(serviceOptions.projectId(), optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER, serviceOptions.clock()); String cursor = result.x(); @@ -507,12 +528,13 @@ public boolean cancel(String jobId) { } @Override - public boolean cancel(final JobId jobId) { + public boolean cancel(JobId jobId) { + final JobId completeJobId = jobId.setProjectId(options().projectId()); try { return runWithRetries(new Callable() { @Override public Boolean call() { - return bigQueryRpc.cancel(jobId.job()); + return bigQueryRpc.cancel(completeJobId.project(), completeJobId.job()); } }, options().retryParams(), EXCEPTION_HANDLER, options().clock()); } catch (RetryHelper.RetryHelperException e) { @@ -565,19 +587,20 @@ public QueryResponse getQueryResults(JobId job, QueryResultsOption... options) { return getQueryResults(job, options(), optionsMap); } - private static QueryResponse getQueryResults(final JobId jobId, + private static QueryResponse getQueryResults(JobId jobId, final BigQueryOptions serviceOptions, final Map optionsMap) { + final JobId completeJobId = jobId.setProjectId(serviceOptions.projectId()); try { GetQueryResultsResponse results = runWithRetries(new Callable() { @Override public GetQueryResultsResponse call() { - return serviceOptions.rpc().getQueryResults(jobId.job(), optionsMap); + return serviceOptions.rpc().getQueryResults( + completeJobId.project(), completeJobId.job(), optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER, serviceOptions.clock()); QueryResponse.Builder builder = QueryResponse.builder(); - JobId completeJobId = JobId.fromPb(results.getJobReference()); - builder.jobId(completeJobId); + builder.jobId(JobId.fromPb(results.getJobReference())); builder.etag(results.getEtag()); builder.jobCompleted(results.getJobComplete()); List rowsPb = results.getRows(); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java index d2981151481f..9b7749059281 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java @@ -83,6 +83,10 @@ public String toString() { return toPb().toString(); } + JobId setProjectId(String projectId) { + return project() != null ? this : JobId.of(projectId, job()); + } + JobReference toPb() { return new JobReference().setProjectId(project).setJobId(job); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobInfo.java index 13addc9d6e7a..862bc62ac3bd 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobInfo.java @@ -326,7 +326,11 @@ public boolean equals(Object obj) { } JobInfo setProjectId(String projectId) { - return toBuilder().configuration(configuration.setProjectId(projectId)).build(); + Builder builder = toBuilder(); + if (jobId != null) { + builder.jobId(jobId.setProjectId(projectId)); + } + return builder.configuration(configuration.setProjectId(projectId)).build(); } Job toPb() { diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/BigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/BigQueryRpc.java index e221079cdd03..01bf226b844e 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/BigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/BigQueryRpc.java @@ -100,15 +100,16 @@ public Y y() { * * @throws BigQueryException upon failure */ - Dataset getDataset(String datasetId, Map options); + Dataset getDataset(String projectId, String datasetId, Map options); /** - * Lists the project's datasets. Partial information is returned on a dataset (datasetReference, - * friendlyName and id). To get full information use {@link #getDataset(String, Map)}. + * Lists the provided project's datasets. Partial information is returned on a dataset + * (datasetReference, friendlyName and id). To get full information use + * {@link #getDataset(String, String, Map)}. * * @throws BigQueryException upon failure */ - Tuple> listDatasets(Map options); + Tuple> listDatasets(String projectId, Map options); /** * Creates a new dataset. @@ -137,7 +138,7 @@ public Y y() { * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean deleteDataset(String datasetId, Map options); + boolean deleteDataset(String projectId, String datasetId, Map options); /** * Updates dataset information. @@ -158,15 +159,17 @@ public Y y() { * * @throws BigQueryException upon failure */ - Table getTable(String datasetId, String tableId, Map options); + Table getTable(String projectId, String datasetId, String tableId, Map options); /** * Lists the dataset's tables. Partial information is returned on a table (tableReference, - * friendlyName, id and type). To get full information use {@link #getTable(String, String, Map)}. + * friendlyName, id and type). To get full information use + * {@link #getTable(String, String, String, Map)}. * * @throws BigQueryException upon failure */ - Tuple> listTables(String dataset, Map options); + Tuple> listTables(String projectId, String dataset, + Map options); /** * Delete the requested table. @@ -174,14 +177,14 @@ public Y y() { * @return {@code true} if table was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean deleteTable(String datasetId, String tableId); + boolean deleteTable(String projectId, String datasetId, String tableId); /** * Sends an insert all request. * * @throws BigQueryException upon failure */ - TableDataInsertAllResponse insertAll(String datasetId, String tableId, + TableDataInsertAllResponse insertAll(String projectId, String datasetId, String tableId, TableDataInsertAllRequest request); /** @@ -189,22 +192,22 @@ TableDataInsertAllResponse insertAll(String datasetId, String tableId, * * @throws BigQueryException upon failure */ - Tuple> listTableData(String datasetId, String tableId, - Map options); + Tuple> listTableData(String projectId, String datasetId, + String tableId, Map options); /** * Returns the requested job or {@code null} if not found. * * @throws BigQueryException upon failure */ - Job getJob(String jobId, Map options); + Job getJob(String projectId, String jobId, Map options); /** * Lists the project's jobs. * * @throws BigQueryException upon failure */ - Tuple> listJobs(Map options); + Tuple> listJobs(String projectId, Map options); /** * Sends a job cancel request. This call will return immediately, and the client will need to poll @@ -214,14 +217,14 @@ Tuple> listTableData(String datasetId, String tableId * found * @throws BigQueryException upon failure */ - boolean cancel(String jobId); + boolean cancel(String projectId, String jobId); /** * Returns results of the query associated with the provided job. * * @throws BigQueryException upon failure */ - GetQueryResultsResponse getQueryResults(String jobId, Map options); + GetQueryResultsResponse getQueryResults(String projectId, String jobId, Map options); /** * Runs the query associated with the request. diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java index 165720b223c2..ef5834db786f 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/DefaultBigQueryRpc.java @@ -94,10 +94,10 @@ private static BigQueryException translate(IOException exception) { } @Override - public Dataset getDataset(String datasetId, Map options) { + public Dataset getDataset(String projectId, String datasetId, Map options) { try { return bigquery.datasets() - .get(this.options.projectId(), datasetId) + .get(projectId, datasetId) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -110,10 +110,10 @@ public Dataset getDataset(String datasetId, Map options) { } @Override - public Tuple> listDatasets(Map options) { + public Tuple> listDatasets(String projectId, Map options) { try { DatasetList datasetsList = bigquery.datasets() - .list(this.options.projectId()) + .list(projectId) .setAll(ALL_DATASETS.getBoolean(options)) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) @@ -141,7 +141,7 @@ public Dataset apply(DatasetList.Datasets datasetPb) { @Override public Dataset create(Dataset dataset, Map options) { try { - return bigquery.datasets().insert(this.options.projectId(), dataset) + return bigquery.datasets().insert(dataset.getDatasetReference().getProjectId(), dataset) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -154,8 +154,9 @@ public Table create(Table table, Map options) { try { // unset the type, as it is output only table.setType(null); + TableReference reference = table.getTableReference(); return bigquery.tables() - .insert(this.options.projectId(), table.getTableReference().getDatasetId(), table) + .insert(reference.getProjectId(), reference.getDatasetId(), table) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -166,8 +167,10 @@ public Table create(Table table, Map options) { @Override public Job create(Job job, Map options) { try { + String projectId = job.getJobReference() != null + ? job.getJobReference().getProjectId() : this.options.projectId(); return bigquery.jobs() - .insert(this.options.projectId(), job) + .insert(projectId, job) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -176,9 +179,9 @@ public Job create(Job job, Map options) { } @Override - public boolean deleteDataset(String datasetId, Map options) { + public boolean deleteDataset(String projectId, String datasetId, Map options) { try { - bigquery.datasets().delete(this.options.projectId(), datasetId) + bigquery.datasets().delete(projectId, datasetId) .setDeleteContents(DELETE_CONTENTS.getBoolean(options)) .execute(); return true; @@ -196,7 +199,7 @@ public Dataset patch(Dataset dataset, Map options) { try { DatasetReference reference = dataset.getDatasetReference(); return bigquery.datasets() - .patch(this.options.projectId(), reference.getDatasetId(), dataset) + .patch(reference.getProjectId(), reference.getDatasetId(), dataset) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -211,7 +214,7 @@ public Table patch(Table table, Map options) { table.setType(null); TableReference reference = table.getTableReference(); return bigquery.tables() - .patch(this.options.projectId(), reference.getDatasetId(), reference.getTableId(), table) + .patch(reference.getProjectId(), reference.getDatasetId(), reference.getTableId(), table) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -220,10 +223,11 @@ public Table patch(Table table, Map options) { } @Override - public Table getTable(String datasetId, String tableId, Map options) { + public Table getTable(String projectId, String datasetId, String tableId, + Map options) { try { return bigquery.tables() - .get(this.options.projectId(), datasetId, tableId) + .get(projectId, datasetId, tableId) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -236,10 +240,11 @@ public Table getTable(String datasetId, String tableId, Map options) } @Override - public Tuple> listTables(String datasetId, Map options) { + public Tuple> listTables(String projectId, String datasetId, + Map options) { try { TableList tableList = bigquery.tables() - .list(this.options.projectId(), datasetId) + .list(projectId, datasetId) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .execute(); @@ -263,9 +268,9 @@ public Table apply(TableList.Tables tablePb) { } @Override - public boolean deleteTable(String datasetId, String tableId) { + public boolean deleteTable(String projectId, String datasetId, String tableId) { try { - bigquery.tables().delete(this.options.projectId(), datasetId, tableId).execute(); + bigquery.tables().delete(projectId, datasetId, tableId).execute(); return true; } catch (IOException ex) { BigQueryException serviceException = translate(ex); @@ -277,23 +282,21 @@ public boolean deleteTable(String datasetId, String tableId) { } @Override - public TableDataInsertAllResponse insertAll(String datasetId, String tableId, + public TableDataInsertAllResponse insertAll(String projectId, String datasetId, String tableId, TableDataInsertAllRequest request) { try { - return bigquery.tabledata() - .insertAll(this.options.projectId(), datasetId, tableId, request) - .execute(); + return bigquery.tabledata().insertAll(projectId, datasetId, tableId, request).execute(); } catch (IOException ex) { throw translate(ex); } } @Override - public Tuple> listTableData(String datasetId, String tableId, - Map options) { + public Tuple> listTableData(String projectId, String datasetId, + String tableId, Map options) { try { TableDataList tableDataList = bigquery.tabledata() - .list(this.options.projectId(), datasetId, tableId) + .list(projectId, datasetId, tableId) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .setStartIndex(START_INDEX.getLong(options) != null @@ -307,10 +310,10 @@ public Tuple> listTableData(String datasetId, String } @Override - public Job getJob(String jobId, Map options) { + public Job getJob(String projectId, String jobId, Map options) { try { return bigquery.jobs() - .get(this.options.projectId(), jobId) + .get(projectId, jobId) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { @@ -323,10 +326,10 @@ public Job getJob(String jobId, Map options) { } @Override - public Tuple> listJobs(Map options) { + public Tuple> listJobs(String projectId, Map options) { try { JobList jobsList = bigquery.jobs() - .list(this.options.projectId()) + .list(projectId) .setAllUsers(ALL_USERS.getBoolean(options)) .setFields(FIELDS.getString(options)) .setStateFilter(STATE_FILTER.>get(options)) @@ -364,9 +367,9 @@ public Job apply(JobList.Jobs jobPb) { } @Override - public boolean cancel(String jobId) { + public boolean cancel(String projectId, String jobId) { try { - bigquery.jobs().cancel(this.options.projectId(), jobId).execute(); + bigquery.jobs().cancel(projectId, jobId).execute(); return true; } catch (IOException ex) { BigQueryException serviceException = translate(ex); @@ -378,9 +381,10 @@ public boolean cancel(String jobId) { } @Override - public GetQueryResultsResponse getQueryResults(String jobId, Map options) { + public GetQueryResultsResponse getQueryResults(String projectId, String jobId, + Map options) { try { - return bigquery.jobs().getQueryResults(this.options.projectId(), jobId) + return bigquery.jobs().getQueryResults(projectId, jobId) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .setStartIndex(START_INDEX.getLong(options) != null diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 4a29697a73dc..32457c2bb051 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import com.google.api.services.bigquery.model.ErrorProto; +import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.TableCell; import com.google.api.services.bigquery.model.TableDataInsertAllRequest; import com.google.api.services.bigquery.model.TableDataInsertAllResponse; @@ -59,6 +60,7 @@ public class BigQueryImplTest { private static final String PROJECT = "project"; + private static final String OTHER_PROJECT = "otherProject"; private static final String DATASET = "dataset"; private static final String TABLE = "table"; private static final String JOB = "job"; @@ -157,6 +159,16 @@ public class BigQueryImplTest { .useQueryCache(false) .defaultDataset(DatasetId.of(PROJECT, DATASET)) .build(); + private static final String CURSOR = "cursor"; + private static final TableCell CELL_PB1 = new TableCell().setV("Value1"); + private static final TableCell CELL_PB2 = new TableCell().setV("Value2"); + private static final ImmutableList> TABLE_DATA = ImmutableList.of( + (List) ImmutableList.of(FieldValue.fromPb(CELL_PB1)), + ImmutableList.of(FieldValue.fromPb(CELL_PB2))); + private static final Tuple> TABLE_DATA_PB = + Tuple.>of(CURSOR, ImmutableList.of( + new TableRow().setF(ImmutableList.of(new TableCell().setV("Value1"))), + new TableRow().setF(ImmutableList.of(new TableCell().setV("Value2"))))); // Empty BigQueryRpc options private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @@ -169,12 +181,12 @@ public class BigQueryImplTest { private static final BigQuery.DatasetListOption DATASET_LIST_ALL = BigQuery.DatasetListOption.all(); private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_TOKEN = - BigQuery.DatasetListOption.pageToken("cursor"); + BigQuery.DatasetListOption.pageToken(CURSOR); private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_SIZE = BigQuery.DatasetListOption.pageSize(42L); private static final Map DATASET_LIST_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.ALL_DATASETS, true, - BigQueryRpc.Option.PAGE_TOKEN, "cursor", + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.MAX_RESULTS, 42L); // Dataset delete options @@ -191,21 +203,21 @@ public class BigQueryImplTest { private static final BigQuery.TableListOption TABLE_LIST_PAGE_SIZE = BigQuery.TableListOption.pageSize(42L); private static final BigQuery.TableListOption TABLE_LIST_PAGE_TOKEN = - BigQuery.TableListOption.pageToken("cursor"); + BigQuery.TableListOption.pageToken(CURSOR); private static final Map TABLE_LIST_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.MAX_RESULTS, 42L, - BigQueryRpc.Option.PAGE_TOKEN, "cursor"); + BigQueryRpc.Option.PAGE_TOKEN, CURSOR); // TableData list options private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_SIZE = BigQuery.TableDataListOption.pageSize(42L); private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_TOKEN = - BigQuery.TableDataListOption.pageToken("cursor"); + BigQuery.TableDataListOption.pageToken(CURSOR); private static final BigQuery.TableDataListOption TABLE_DATA_LIST_START_INDEX = BigQuery.TableDataListOption.startIndex(0L); private static final Map TABLE_DATA_LIST_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.MAX_RESULTS, 42L, - BigQueryRpc.Option.PAGE_TOKEN, "cursor", + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.START_INDEX, 0L); // Job options @@ -220,13 +232,13 @@ public class BigQueryImplTest { private static final BigQuery.JobListOption JOB_LIST_STATE_FILTER = BigQuery.JobListOption.stateFilter(JobStatus.State.DONE, JobStatus.State.PENDING); private static final BigQuery.JobListOption JOB_LIST_PAGE_TOKEN = - BigQuery.JobListOption.pageToken("cursor"); + BigQuery.JobListOption.pageToken(CURSOR); private static final BigQuery.JobListOption JOB_LIST_PAGE_SIZE = BigQuery.JobListOption.pageSize(42L); private static final Map JOB_LIST_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.ALL_USERS, true, BigQueryRpc.Option.STATE_FILTER, ImmutableList.of("done", "pending"), - BigQueryRpc.Option.PAGE_TOKEN, "cursor", + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.MAX_RESULTS, 42L); // Query Results options @@ -235,13 +247,13 @@ public class BigQueryImplTest { private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_INDEX = BigQuery.QueryResultsOption.startIndex(1024L); private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_TOKEN = - BigQuery.QueryResultsOption.pageToken("cursor"); + BigQuery.QueryResultsOption.pageToken(CURSOR); private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_SIZE = BigQuery.QueryResultsOption.pageSize(0L); private static final Map QUERY_RESULTS_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.TIMEOUT, 42L, BigQueryRpc.Option.START_INDEX, 1024L, - BigQueryRpc.Option.PAGE_TOKEN, "cursor", + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.MAX_RESULTS, 0L); private BigQueryOptions options; @@ -280,20 +292,20 @@ public void testGetOptions() { @Test public void testCreateDataset() { - EasyMock.expect(bigqueryRpcMock.create(DATASET_INFO_WITH_PROJECT.toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); + DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); + EasyMock.expect(bigqueryRpcMock.create(datasetInfo.toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(datasetInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); - Dataset dataset = bigquery.create(DATASET_INFO); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + Dataset dataset = bigquery.create(datasetInfo); + assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); } @Test public void testCreateDatasetWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect( - bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) + EasyMock.expect(bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), + capture(capturedOptions))) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -309,7 +321,7 @@ public void testCreateDatasetWithSelectedFields() { @Test public void testGetDataset() { - EasyMock.expect(bigqueryRpcMock.getDataset(DATASET, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -320,19 +332,31 @@ public void testGetDataset() { @Test public void testGetDatasetFromDatasetId() { - EasyMock.expect(bigqueryRpcMock.getDataset(DATASET, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); - Dataset dataset = bigquery.getDataset(DatasetId.of(PROJECT, DATASET)); + Dataset dataset = bigquery.getDataset(DatasetId.of(DATASET)); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } + @Test + public void testGetDatasetFromDatasetIdWithProject() { + DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); + DatasetId datasetId = DatasetId.of(OTHER_PROJECT, DATASET); + EasyMock.expect(bigqueryRpcMock.getDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(datasetInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + Dataset dataset = bigquery.getDataset(datasetId); + assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); + } + @Test public void testGetDatasetWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(bigqueryRpcMock.getDataset(eq(DATASET), capture(capturedOptions))) + EasyMock.expect(bigqueryRpcMock.getDataset(eq(PROJECT), eq(DATASET), capture(capturedOptions))) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -348,17 +372,31 @@ public void testGetDatasetWithSelectedFields() { @Test public void testListDatasets() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList datasetList = ImmutableList.of( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = - Tuple.of(cursor, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); - EasyMock.expect(bigqueryRpcMock.listDatasets(EMPTY_RPC_OPTIONS)).andReturn(result); + Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listDatasets(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listDatasets(); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(datasetList.toArray(), Iterables.toArray(page.values(), DatasetInfo.class)); + } + + @Test + public void testListDatasetsWithProjects() { + bigquery = options.service(); + ImmutableList datasetList = ImmutableList.of(new Dataset(bigquery, + new DatasetInfo.BuilderImpl(DATASET_INFO.setProjectId(OTHER_PROJECT)))); + Tuple> result = + Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listDatasets(OTHER_PROJECT, EMPTY_RPC_OPTIONS)) + .andReturn(result); + EasyMock.replay(bigqueryRpcMock); + Page page = bigquery.listDatasets(OTHER_PROJECT); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(datasetList.toArray(), Iterables.toArray(page.values(), DatasetInfo.class)); } @@ -367,7 +405,7 @@ public void testListEmptyDatasets() { ImmutableList datasets = ImmutableList.of(); Tuple> result = Tuple.>of(null, datasets); - EasyMock.expect(bigqueryRpcMock.listDatasets(EMPTY_RPC_OPTIONS)).andReturn(result); + EasyMock.expect(bigqueryRpcMock.listDatasets(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Page page = bigquery.listDatasets(); @@ -378,24 +416,24 @@ public void testListEmptyDatasets() { @Test public void testListDatasetsWithOptions() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList datasetList = ImmutableList.of( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = - Tuple.of(cursor, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); - EasyMock.expect(bigqueryRpcMock.listDatasets(DATASET_LIST_OPTIONS)).andReturn(result); + Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listDatasets(PROJECT, DATASET_LIST_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listDatasets(DATASET_LIST_ALL, DATASET_LIST_PAGE_TOKEN, DATASET_LIST_PAGE_SIZE); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(datasetList.toArray(), Iterables.toArray(page.values(), DatasetInfo.class)); } @Test public void testDeleteDataset() { - EasyMock.expect(bigqueryRpcMock.deleteDataset(DATASET, EMPTY_RPC_OPTIONS)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.delete(DATASET)); @@ -403,15 +441,27 @@ public void testDeleteDataset() { @Test public void testDeleteDatasetFromDatasetId() { - EasyMock.expect(bigqueryRpcMock.deleteDataset(DATASET, EMPTY_RPC_OPTIONS)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); - assertTrue(bigquery.delete(DatasetId.of(PROJECT, DATASET))); + assertTrue(bigquery.delete(DatasetId.of(DATASET))); + } + + @Test + public void testDeleteDatasetFromDatasetIdWithProject() { + DatasetId datasetId = DatasetId.of(OTHER_PROJECT, DATASET); + EasyMock.expect(bigqueryRpcMock.deleteDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(true); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + assertTrue(bigquery.delete(datasetId)); } @Test public void testDeleteDatasetWithOptions() { - EasyMock.expect(bigqueryRpcMock.deleteDataset(DATASET, DATASET_DELETE_OPTIONS)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, DATASET_DELETE_OPTIONS)) + .andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.delete(DATASET, DATASET_DELETE_CONTENTS)); @@ -419,17 +469,15 @@ public void testDeleteDatasetWithOptions() { @Test public void testUpdateDataset() { - DatasetInfo updatedDatasetInfo = DATASET_INFO.toBuilder().description("newDescription").build(); - DatasetInfo updatedDatasetInfoWithProject = DATASET_INFO_WITH_PROJECT.toBuilder() + DatasetInfo updatedDatasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT).toBuilder() .description("newDescription") .build(); - EasyMock.expect(bigqueryRpcMock.patch(updatedDatasetInfoWithProject.toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(updatedDatasetInfoWithProject.toPb()); + EasyMock.expect(bigqueryRpcMock.patch(updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(updatedDatasetInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Dataset dataset = bigquery.update(updatedDatasetInfo); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfoWithProject)), - dataset); + assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfo)), dataset); } @Test @@ -456,12 +504,13 @@ public void testUpdateDatasetWithSelectedFields() { @Test public void testCreateTable() { - EasyMock.expect(bigqueryRpcMock.create(TABLE_INFO_WITH_PROJECT.toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); + TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); + EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); - Table table = bigquery.create(TABLE_INFO); - assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); + Table table = bigquery.create(tableInfo); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); } @Test @@ -483,7 +532,7 @@ public void testCreateTableWithSelectedFields() { @Test public void testGetTable() { - EasyMock.expect(bigqueryRpcMock.getTable(DATASET, TABLE, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -493,7 +542,7 @@ public void testGetTable() { @Test public void testGetTableFromTableId() { - EasyMock.expect(bigqueryRpcMock.getTable(DATASET, TABLE, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -501,10 +550,23 @@ public void testGetTableFromTableId() { assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); } + @Test + public void testGetTableFromTableIdWithProject() { + TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); + TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); + EasyMock.expect(bigqueryRpcMock.getTable(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(tableInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + Table table = bigquery.getTable(tableId); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); + } + @Test public void testGetTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(bigqueryRpcMock.getTable(eq(DATASET), eq(TABLE), capture(capturedOptions))) + EasyMock.expect( + bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -519,55 +581,70 @@ public void testGetTableWithSelectedFields() { @Test public void testListTables() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = - Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result); + Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(result); EasyMock.replay(bigqueryRpcMock); Page
page = bigquery.listTables(DATASET); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), Table.class)); } @Test public void testListTablesFromDatasetId() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = - Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result); + Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(result); + EasyMock.replay(bigqueryRpcMock); + Page
page = bigquery.listTables(DatasetId.of(DATASET)); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), Table.class)); + } + + @Test + public void testListTablesFromDatasetIdWithProject() { + bigquery = options.service(); + ImmutableList
tableList = ImmutableList.of( + new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO.setProjectId(OTHER_PROJECT)))); + Tuple> result = + Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listTables(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(result); EasyMock.replay(bigqueryRpcMock); - Page
page = bigquery.listTables(DatasetId.of(PROJECT, DATASET)); - assertEquals(cursor, page.nextPageCursor()); + Page
page = bigquery.listTables(DatasetId.of(OTHER_PROJECT, DATASET)); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), Table.class)); } @Test public void testListTablesWithOptions() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = - Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - EasyMock.expect(bigqueryRpcMock.listTables(DATASET, TABLE_LIST_OPTIONS)).andReturn(result); + Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); + EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)) + .andReturn(result); EasyMock.replay(bigqueryRpcMock); Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), Table.class)); } @Test public void testDeleteTable() { - EasyMock.expect(bigqueryRpcMock.deleteTable(DATASET, TABLE)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.delete(DATASET, TABLE)); @@ -575,25 +652,32 @@ public void testDeleteTable() { @Test public void testDeleteTableFromTableId() { - EasyMock.expect(bigqueryRpcMock.deleteTable(DATASET, TABLE)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.delete(TABLE_ID)); } + @Test + public void testDeleteTableFromTableIdWithProject() { + TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); + EasyMock.expect(bigqueryRpcMock.deleteTable(OTHER_PROJECT, DATASET, TABLE)).andReturn(true); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + assertTrue(bigquery.delete(tableId)); + } + @Test public void testUpdateTable() { - TableInfo updatedTableInfo = TABLE_INFO.toBuilder().description("newDescription").build(); - TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder() + TableInfo updatedTableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT).toBuilder() .description("newDescription") .build(); - EasyMock.expect(bigqueryRpcMock.patch(updatedTableInfoWithProject.toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(updatedTableInfoWithProject.toPb()); + EasyMock.expect(bigqueryRpcMock.patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(updatedTableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Table table = bigquery.update(updatedTableInfo); - assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), - table); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table); } @Test @@ -642,7 +726,44 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors( ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors( ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); - EasyMock.expect(bigqueryRpcMock.insertAll(DATASET, TABLE, requestPb)) + EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb)) + .andReturn(responsePb); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + InsertAllResponse response = bigquery.insertAll(request); + assertNotNull(response.errorsFor(0L)); + assertNull(response.errorsFor(1L)); + assertEquals(1, response.errorsFor(0L).size()); + assertEquals("ErrorMessage", response.errorsFor(0L).get(0).message()); + } + + @Test + public void testInsertAllWithProject() { + Map row1 = ImmutableMap.of("field", "value1"); + Map row2 = ImmutableMap.of("field", "value2"); + List rows = ImmutableList.of( + new RowToInsert("row1", row1), + new RowToInsert("row2", row2) + ); + TableId tableId = TableId.of(OTHER_PROJECT, DATASET, TABLE); + InsertAllRequest request = InsertAllRequest.builder(tableId) + .rows(rows) + .skipInvalidRows(false) + .ignoreUnknownValues(true) + .templateSuffix("suffix") + .build(); + TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows( + Lists.transform(rows, new Function() { + @Override + public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { + return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.id()) + .setJson(rowToInsert.content()); + } + })).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix"); + TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors( + ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors( + ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); + EasyMock.expect(bigqueryRpcMock.insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -655,87 +776,49 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { @Test public void testListTableData() { - String cursor = "cursor"; - com.google.api.services.bigquery.model.TableCell cell1 = - new com.google.api.services.bigquery.model.TableCell().setV("Value1"); - com.google.api.services.bigquery.model.TableCell cell2 = - new com.google.api.services.bigquery.model.TableCell().setV("Value2"); - ImmutableList> tableData = ImmutableList.of( - (List) ImmutableList.of(FieldValue.fromPb(cell1)), - ImmutableList.of(FieldValue.fromPb(cell2))); - Tuple> result = - Tuple.>of(cursor, - ImmutableList.of( - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value1"))), - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value2"))))); - EasyMock.expect(bigqueryRpcMock.listTableData(DATASET, TABLE, EMPTY_RPC_OPTIONS)) - .andReturn(result); + EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Page> page = bigquery.listTableData(DATASET, TABLE); - assertEquals(cursor, page.nextPageCursor()); - assertArrayEquals(tableData.toArray(), Iterables.toArray(page.values(), List.class)); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.values(), List.class)); } @Test public void testListTableDataFromTableId() { - String cursor = "cursor"; - com.google.api.services.bigquery.model.TableCell cell1 = - new com.google.api.services.bigquery.model.TableCell().setV("Value1"); - com.google.api.services.bigquery.model.TableCell cell2 = - new com.google.api.services.bigquery.model.TableCell().setV("Value2"); - ImmutableList> tableData = ImmutableList.of( - (List) ImmutableList.of(FieldValue.fromPb(cell1)), - ImmutableList.of(FieldValue.fromPb(cell2))); - Tuple> result = - Tuple.>of(cursor, - ImmutableList.of( - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value1"))), - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value2"))))); - EasyMock.expect(bigqueryRpcMock.listTableData(DATASET, TABLE, EMPTY_RPC_OPTIONS)) - .andReturn(result); + EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Page> page = bigquery.listTableData(TableId.of(DATASET, TABLE)); - assertEquals(cursor, page.nextPageCursor()); - assertArrayEquals(tableData.toArray(), Iterables.toArray(page.values(), List.class)); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.values(), List.class)); + } + + @Test + public void testListTableDataFromTableIdWithProject() { + TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); + EasyMock.expect( + bigqueryRpcMock.listTableData(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(TABLE_DATA_PB); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + Page> page = bigquery.listTableData(tableId); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.values(), List.class)); } @Test public void testListTableDataWithOptions() { - String cursor = "cursor"; - com.google.api.services.bigquery.model.TableCell cell1 = - new com.google.api.services.bigquery.model.TableCell().setV("Value1"); - com.google.api.services.bigquery.model.TableCell cell2 = - new com.google.api.services.bigquery.model.TableCell().setV("Value2"); - ImmutableList> tableData = ImmutableList.of( - (List) ImmutableList.of(FieldValue.fromPb(cell1)), - ImmutableList.of(FieldValue.fromPb(cell2))); - Tuple> result = - Tuple.>of(cursor, - ImmutableList.of( - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value1"))), - new com.google.api.services.bigquery.model.TableRow().setF( - ImmutableList.of(new com.google.api.services.bigquery.model.TableCell() - .setV("Value2"))))); - EasyMock.expect(bigqueryRpcMock.listTableData(DATASET, TABLE, TABLE_DATA_LIST_OPTIONS)) - .andReturn(result); + EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS)) + .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Page> page = bigquery.listTableData(DATASET, TABLE, TABLE_DATA_LIST_PAGE_SIZE, TABLE_DATA_LIST_PAGE_TOKEN, TABLE_DATA_LIST_START_INDEX); - assertEquals(cursor, page.nextPageCursor()); - assertArrayEquals(tableData.toArray(), Iterables.toArray(page.values(), List.class)); + assertEquals(CURSOR, page.nextPageCursor()); + assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.values(), List.class)); } @Test @@ -799,9 +882,28 @@ public void testCreateJobWithSelectedFields() { assertEquals(37, selector.length()); } + @Test + public void testCreateJobWithProjectId() { + JobInfo jobInfo = JobInfo.builder(QUERY_JOB_CONFIGURATION.setProjectId(OTHER_PROJECT)) + .jobId(JobId.of(OTHER_PROJECT, JOB)) + .build(); + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(bigqueryRpcMock.create(eq(jobInfo.toPb()), capture(capturedOptions))) + .andReturn(jobInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + Job job = bigquery.create(jobInfo, JOB_OPTION_FIELDS); + assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); + String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption()); + assertTrue(selector.contains("jobReference")); + assertTrue(selector.contains("configuration")); + assertTrue(selector.contains("user_email")); + assertEquals(37, selector.length()); + } + @Test public void testGetJob() { - EasyMock.expect(bigqueryRpcMock.getJob(JOB, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -811,79 +913,89 @@ public void testGetJob() { @Test public void testGetJobFromJobId() { - EasyMock.expect(bigqueryRpcMock.getJob(JOB, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, EMPTY_RPC_OPTIONS)) .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); - Job job = bigquery.getJob(JobId.of(PROJECT, JOB)); + Job job = bigquery.getJob(JobId.of(JOB)); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); } + @Test + public void testGetJobFromJobIdWithProject() { + JobId jobId = JobId.of(OTHER_PROJECT, JOB); + JobInfo jobInfo = COPY_JOB.setProjectId(OTHER_PROJECT); + EasyMock.expect(bigqueryRpcMock.getJob(OTHER_PROJECT, JOB, EMPTY_RPC_OPTIONS)) + .andReturn(jobInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + Job job = bigquery.getJob(jobId); + assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); + } + @Test public void testListJobs() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList jobList = ImmutableList.of( new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(cursor, Iterables.transform(jobList, + Tuple.of(CURSOR, Iterables.transform(jobList, new Function() { @Override public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - EasyMock.expect(bigqueryRpcMock.listJobs(EMPTY_RPC_OPTIONS)).andReturn(result); + EasyMock.expect(bigqueryRpcMock.listJobs(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listJobs(); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); } @Test public void testListJobsWithOptions() { - String cursor = "cursor"; bigquery = options.service(); ImmutableList jobList = ImmutableList.of( new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(cursor, Iterables.transform(jobList, + Tuple.of(CURSOR, Iterables.transform(jobList, new Function() { @Override public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - EasyMock.expect(bigqueryRpcMock.listJobs(JOB_LIST_OPTIONS)).andReturn(result); + EasyMock.expect(bigqueryRpcMock.listJobs(PROJECT, JOB_LIST_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listJobs(JOB_LIST_ALL_USERS, JOB_LIST_STATE_FILTER, JOB_LIST_PAGE_TOKEN, JOB_LIST_PAGE_SIZE); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); } @Test public void testListJobsWithSelectedFields() { - String cursor = "cursor"; Capture> capturedOptions = Capture.newInstance(); bigquery = options.service(); ImmutableList jobList = ImmutableList.of( new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(cursor, Iterables.transform(jobList, + Tuple.of(CURSOR, Iterables.transform(jobList, new Function() { @Override public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - EasyMock.expect(bigqueryRpcMock.listJobs(capture(capturedOptions))).andReturn(result); + EasyMock.expect(bigqueryRpcMock.listJobs(eq(PROJECT), capture(capturedOptions))) + .andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listJobs(JOB_LIST_OPTION_FIELD); - assertEquals(cursor, page.nextPageCursor()); + assertEquals(CURSOR, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption()); assertTrue(selector.contains("nextPageToken,jobs(")); @@ -898,7 +1010,7 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { @Test public void testCancelJob() { - EasyMock.expect(bigqueryRpcMock.cancel(JOB)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.cancel(PROJECT, JOB)).andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.cancel(JOB)); @@ -906,12 +1018,21 @@ public void testCancelJob() { @Test public void testCancelJobFromJobId() { - EasyMock.expect(bigqueryRpcMock.cancel(JOB)).andReturn(true); + EasyMock.expect(bigqueryRpcMock.cancel(PROJECT, JOB)).andReturn(true); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); assertTrue(bigquery.cancel(JobId.of(PROJECT, JOB))); } + @Test + public void testCancelJobFromJobIdWithProject() { + JobId jobId = JobId.of(OTHER_PROJECT, JOB); + EasyMock.expect(bigqueryRpcMock.cancel(OTHER_PROJECT, JOB)).andReturn(true); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + assertTrue(bigquery.cancel(jobId)); + } + @Test public void testQueryRequest() { JobId queryJob = JobId.of(PROJECT, JOB); @@ -941,7 +1062,7 @@ public void testQueryRequestCompleted() { .setRows(ImmutableList.of(TABLE_ROW)) .setJobComplete(true) .setCacheHit(false) - .setPageToken("cursor") + .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.query(QUERY_REQUEST_WITH_PROJECT.toPb())).andReturn(responsePb); @@ -961,23 +1082,56 @@ public void testQueryRequestCompleted() { assertEquals(false, row.get(0).booleanValue()); assertEquals(1L, row.get(1).longValue()); } - assertEquals("cursor", response.result().nextPageCursor()); + assertEquals(CURSOR, response.result().nextPageCursor()); } @Test public void testGetQueryResults() { - JobId queryJob = JobId.of(PROJECT, JOB); - com.google.api.services.bigquery.model.GetQueryResultsResponse responsePb = - new com.google.api.services.bigquery.model.GetQueryResultsResponse() - .setEtag("etag") - .setJobReference(queryJob.toPb()) - .setRows(ImmutableList.of(TABLE_ROW)) - .setJobComplete(true) - .setCacheHit(false) - .setPageToken("cursor") - .setTotalBytesProcessed(42L) - .setTotalRows(BigInteger.valueOf(1L)); - EasyMock.expect(bigqueryRpcMock.getQueryResults(JOB, EMPTY_RPC_OPTIONS)).andReturn(responsePb); + JobId queryJob = JobId.of(JOB); + GetQueryResultsResponse responsePb = new GetQueryResultsResponse() + .setEtag("etag") + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); + EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, EMPTY_RPC_OPTIONS)) + .andReturn(responsePb); + EasyMock.replay(bigqueryRpcMock); + bigquery = options.service(); + QueryResponse response = bigquery.getQueryResults(queryJob); + assertEquals("etag", response.etag()); + assertEquals(queryJob, response.jobId()); + assertEquals(true, response.jobCompleted()); + assertEquals(false, response.result().cacheHit()); + assertEquals(ImmutableList.of(), response.executionErrors()); + assertFalse(response.hasErrors()); + assertEquals(null, response.result().schema()); + assertEquals(42L, response.result().totalBytesProcessed()); + assertEquals(1L, response.result().totalRows()); + for (List row : response.result().values()) { + assertEquals(false, row.get(0).booleanValue()); + assertEquals(1L, row.get(1).longValue()); + } + assertEquals(CURSOR, response.result().nextPageCursor()); + } + + @Test + public void testGetQueryResultsWithProject() { + JobId queryJob = JobId.of(OTHER_PROJECT, JOB); + GetQueryResultsResponse responsePb = new GetQueryResultsResponse() + .setEtag("etag") + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); + EasyMock.expect(bigqueryRpcMock.getQueryResults(OTHER_PROJECT, JOB, EMPTY_RPC_OPTIONS)) + .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); QueryResponse response = bigquery.getQueryResults(queryJob); @@ -994,22 +1148,21 @@ public void testGetQueryResults() { assertEquals(false, row.get(0).booleanValue()); assertEquals(1L, row.get(1).longValue()); } - assertEquals("cursor", response.result().nextPageCursor()); + assertEquals(CURSOR, response.result().nextPageCursor()); } @Test public void testGetQueryResultsWithOptions() { JobId queryJob = JobId.of(PROJECT, JOB); - com.google.api.services.bigquery.model.GetQueryResultsResponse responsePb = - new com.google.api.services.bigquery.model.GetQueryResultsResponse() - .setJobReference(queryJob.toPb()) - .setRows(ImmutableList.of(TABLE_ROW)) - .setJobComplete(true) - .setCacheHit(false) - .setPageToken("cursor") - .setTotalBytesProcessed(42L) - .setTotalRows(BigInteger.valueOf(1L)); - EasyMock.expect(bigqueryRpcMock.getQueryResults(JOB, QUERY_RESULTS_OPTIONS)) + GetQueryResultsResponse responsePb = new GetQueryResultsResponse() + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); + EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, QUERY_RESULTS_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); @@ -1028,7 +1181,7 @@ public void testGetQueryResultsWithOptions() { assertEquals(false, row.get(0).booleanValue()); assertEquals(1L, row.get(1).longValue()); } - assertEquals("cursor", response.result().nextPageCursor()); + assertEquals(CURSOR, response.result().nextPageCursor()); } @Test @@ -1046,7 +1199,7 @@ public void testWriter() { @Test public void testRetryableException() { - EasyMock.expect(bigqueryRpcMock.getDataset(DATASET, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(500, "InternalError")) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); @@ -1059,7 +1212,7 @@ public void testRetryableException() { @Test public void testNonRetryableException() { String exceptionMessage = "Not Implemented"; - EasyMock.expect(bigqueryRpcMock.getDataset(DATASET, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(501, exceptionMessage)); EasyMock.replay(bigqueryRpcMock); bigquery = options.toBuilder().retryParams(RetryParams.defaultInstance()).build().service(); @@ -1071,7 +1224,7 @@ public void testNonRetryableException() { @Test public void testRuntimeException() { String exceptionMessage = "Artificial runtime exception"; - EasyMock.expect(bigqueryRpcMock.getDataset(DATASET, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(bigqueryRpcMock); bigquery = options.toBuilder().retryParams(RetryParams.defaultInstance()).build().service(); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobIdTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobIdTest.java index 68caf62413c9..79701720412e 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobIdTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobIdTest.java @@ -45,6 +45,11 @@ public void testToPbAndFromPb() { compareJobs(JOB_COMPLETE, JobId.fromPb(JOB_COMPLETE.toPb())); } + @Test + public void testSetProjectId() { + assertEquals(JOB_COMPLETE, JOB.setProjectId("project")); + } + private void compareJobs(JobId expected, JobId value) { assertEquals(expected, value); assertEquals(expected.hashCode(), value.hashCode()); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobInfoTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobInfoTest.java index d7fde0957a2f..b08660268a61 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobInfoTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobInfoTest.java @@ -341,16 +341,24 @@ public void testToPbAndFromPb() { @Test public void testSetProjectId() { - CopyJobConfiguration copyConfiguration = COPY_JOB.setProjectId("p").configuration(); + JobInfo jobInfo = COPY_JOB.setProjectId("p"); + assertEquals("p", jobInfo.jobId().project()); + CopyJobConfiguration copyConfiguration = jobInfo.configuration(); assertEquals("p", copyConfiguration.destinationTable().project()); for (TableId sourceTable : copyConfiguration.sourceTables()) { assertEquals("p", sourceTable.project()); } - ExtractJobConfiguration extractConfiguration = EXTRACT_JOB.setProjectId("p").configuration(); + jobInfo = EXTRACT_JOB.setProjectId("p"); + assertEquals("p", jobInfo.jobId().project()); + ExtractJobConfiguration extractConfiguration = jobInfo.configuration(); assertEquals("p", extractConfiguration.sourceTable().project()); - LoadJobConfiguration loadConfiguration = LOAD_JOB.setProjectId("p").configuration(); + jobInfo = LOAD_JOB.setProjectId("p"); + assertEquals("p", jobInfo.jobId().project()); + LoadJobConfiguration loadConfiguration = jobInfo.configuration(); assertEquals("p", loadConfiguration.destinationTable().project()); - QueryJobConfiguration queryConfiguration = QUERY_JOB.setProjectId("p").configuration(); + jobInfo = QUERY_JOB.setProjectId("p"); + assertEquals("p", jobInfo.jobId().project()); + QueryJobConfiguration queryConfiguration = jobInfo.configuration(); assertEquals("p", queryConfiguration.defaultDataset().project()); assertEquals("p", queryConfiguration.destinationTable().project()); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 0078f695e541..6eb4d6a6a7b7 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -71,6 +71,7 @@ import com.google.cloud.storage.testing.RemoteStorageHelper; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.common.io.BaseEncoding; import org.junit.AfterClass; @@ -82,8 +83,10 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -177,6 +180,8 @@ public class ITBigQueryTest { + "\"BytesField\": \"" + BYTES_BASE64 + "\"" + "}" + "}"; + private static final Set PUBLIC_DATASETS = ImmutableSet.of("github_repos", "hacker_news", + "noaa_gsod", "samples", "usa_names"); private static BigQuery bigquery; private static Storage storage; @@ -220,6 +225,19 @@ public static void afterClass() throws ExecutionException, InterruptedException } } + @Test + public void testListDatasets() { + Page datasets = bigquery.listDatasets("bigquery-public-data"); + Iterator iterator = datasets.iterateAll(); + Set datasetNames = new HashSet<>(); + while (iterator.hasNext()) { + datasetNames.add(iterator.next().datasetId().dataset()); + } + for (String type : PUBLIC_DATASETS) { + assertTrue(datasetNames.contains(type)); + } + } + @Test public void testGetDataset() { Dataset dataset = bigquery.getDataset(DATASET);