diff --git a/google-cloud-bigquery/clirr-ignored-differences.xml b/google-cloud-bigquery/clirr-ignored-differences.xml index 9ef680c66..37e349413 100644 --- a/google-cloud-bigquery/clirr-ignored-differences.xml +++ b/google-cloud-bigquery/clirr-ignored-differences.xml @@ -14,6 +14,13 @@ com.google.api.services.bigquery.model.GetQueryResultsResponse getQueryResultsWithRowLimit(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer) getQueryResultsWithRowLimit is just used by ConnectionImpl at the moment so it should be fine to update the signature instead of writing an overloaded method + + 7006 + com/google/cloud/bigquery/BigQueryOptions* + *getBigQueryRpcV2(*) + com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc + getBigQueryRpcV2 is protected and is only used within the BigQuery package + 7013 com/google/cloud/bigquery/ExternalTableDefinition* diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java index aefb4329b..976015873 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java @@ -31,6 +31,7 @@ protected BigQueryBaseService(ServiceOptions options) { .abortOn(RuntimeException.class) .retryOn(java.net.ConnectException.class) // retry on Connection Exception .retryOn(java.net.UnknownHostException.class) // retry on UnknownHostException + .retryOn(java.net.SocketException.class) // retry on SocketException .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) .build(); } 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 23cb001ac..3db8c88f7 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 @@ -15,7 +15,6 @@ */ package com.google.cloud.bigquery; -import static com.google.cloud.RetryHelper.runWithRetries; import static com.google.cloud.bigquery.PolicyHelper.convertFromApiPolicy; import static com.google.cloud.bigquery.PolicyHelper.convertToApiPolicy; import static com.google.common.base.Preconditions.checkArgument; @@ -37,13 +36,13 @@ import com.google.cloud.PageImpl; import com.google.cloud.PageImpl.NextPageFetcher; import com.google.cloud.Policy; -import com.google.cloud.RetryHelper; -import com.google.cloud.RetryHelper.RetryHelperException; import com.google.cloud.RetryOption; import com.google.cloud.Tuple; +import com.google.cloud.bigquery.BigQueryRetryHelper.BigQueryRetryHelperException; import com.google.cloud.bigquery.InsertAllRequest.RowToInsert; import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; +import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Strings; @@ -53,6 +52,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -242,7 +242,11 @@ public Page getNextPage() { } } - private final BigQueryRpc bigQueryRpc; + private final HttpBigQueryRpc bigQueryRpc; + + private static final BigQueryRetryConfig EMPTY_RETRY_CONFIG = + BigQueryRetryConfig.newBuilder().build(); + private static final BigQueryRetryConfig DEFAULT_RETRY_CONFIG = BigQueryRetryConfig.newBuilder() .retryOnMessage(BigQueryErrorMessages.RATE_LIMIT_EXCEEDED_MSG) @@ -268,17 +272,18 @@ public Dataset create(DatasetInfo datasetInfo, DatasetOption... options) { try { return Dataset.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Dataset call() { - return bigQueryRpc.create(datasetPb, optionsMap); + public com.google.api.services.bigquery.model.Dataset call() throws IOException { + return bigQueryRpc.createSkipExceptionTranslation(datasetPb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -297,17 +302,18 @@ public Table create(TableInfo tableInfo, TableOption... options) { try { return Table.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Table call() { - return bigQueryRpc.create(tablePb, optionsMap); + public com.google.api.services.bigquery.model.Table call() throws IOException { + return bigQueryRpc.createSkipExceptionTranslation(tablePb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -335,17 +341,18 @@ public Routine create(RoutineInfo routineInfo, RoutineOption... options) { try { return Routine.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.create(routinePb, optionsMap); + public com.google.api.services.bigquery.model.Routine call() throws IOException { + return bigQueryRpc.createSkipExceptionTranslation(routinePb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -397,7 +404,7 @@ Job create(JobInfo jobInfo, Supplier idProvider, JobOption... options) { BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Job call() { + public com.google.api.services.bigquery.model.Job call() throws IOException { if (idRandom) { // re-generate a new random job with the same jobInfo when jobId is not // provided by the user @@ -406,11 +413,11 @@ public com.google.api.services.bigquery.model.Job call() { com.google.api.services.bigquery.model.Job newJobPb = recreatedJobInfo.setProjectId(getOptions().getProjectId()).toPb(); finalJobId[0] = recreatedJobInfo.getJobId(); - return bigQueryRpc.create(newJobPb, optionsMap); + return bigQueryRpc.createSkipExceptionTranslation(newJobPb, optionsMap); } else { com.google.api.services.bigquery.model.Job jobPb = jobInfo.setProjectId(getOptions().getProjectId()).toPb(); - return bigQueryRpc.create(jobPb, optionsMap); + return bigQueryRpc.createSkipExceptionTranslation(jobPb, optionsMap); } } }, @@ -423,7 +430,7 @@ public com.google.api.services.bigquery.model.Job call() { getBigQueryRetryConfig(optionsMap) != null ? getBigQueryRetryConfig(optionsMap) : DEFAULT_RETRY_CONFIG)); - } catch (BigQueryRetryHelper.BigQueryRetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } catch (BigQueryException e) { @@ -490,22 +497,26 @@ public Dataset getDataset(final DatasetId datasetId, DatasetOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Dataset answer = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Dataset call() { - return bigQueryRpc.getDataset( + public com.google.api.services.bigquery.model.Dataset call() throws IOException { + return bigQueryRpc.getDatasetSkipExceptionTranslation( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Dataset not found"); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + return Dataset.fromPb(this, answer); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + if (getOptions().getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Dataset not found"); + } + return null; } - return answer == null ? null : Dataset.fromPb(this, answer); - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -526,18 +537,21 @@ private static Page listDatasets( final Map optionsMap) { try { Tuple> result = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable< Tuple>>() { @Override public Tuple> - call() { - return serviceOptions.getBigQueryRpcV2().listDatasets(projectId, optionsMap); + call() throws IOException { + return serviceOptions + .getBigQueryRpcV2() + .listDatasetsSkipExceptionTranslation(projectId, optionsMap); } }, serviceOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); String cursor = result.x(); return new PageImpl<>( new DatasetPageFetcher(projectId, serviceOptions, cursor, optionsMap), @@ -550,7 +564,7 @@ public Dataset apply(com.google.api.services.bigquery.model.Dataset dataset) { return Dataset.fromPb(serviceOptions.getService(), dataset); } })); - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -565,18 +579,22 @@ public boolean delete(DatasetId datasetId, DatasetDeleteOption... options) { final DatasetId completeDatasetId = datasetId.setProjectId(getOptions().getProjectId()); final Map optionsMap = optionMap(options); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.deleteDataset( + public Boolean call() throws IOException { + return bigQueryRpc.deleteDatasetSkipExceptionTranslation( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + return false; + } throw BigQueryException.translateAndThrow(e); } } @@ -594,11 +612,11 @@ public boolean delete(TableId tableId) { ? getOptions().getProjectId() : tableId.getProject()); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.deleteTable( + public Boolean call() throws IOException { + return bigQueryRpc.deleteTableSkipExceptionTranslation( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable()); @@ -606,8 +624,12 @@ public Boolean call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + return false; + } throw BigQueryException.translateAndThrow(e); } } @@ -620,11 +642,11 @@ public boolean delete(ModelId modelId) { ? getOptions().getProjectId() : modelId.getProject()); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.deleteModel( + public Boolean call() throws IOException { + return bigQueryRpc.deleteModelSkipExceptionTranslation( completeModelId.getProject(), completeModelId.getDataset(), completeModelId.getModel()); @@ -632,8 +654,12 @@ public Boolean call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + return false; + } throw BigQueryException.translateAndThrow(e); } } @@ -646,11 +672,11 @@ public boolean delete(RoutineId routineId) { ? getOptions().getProjectId() : routineId.getProject()); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.deleteRoutine( + public Boolean call() throws IOException { + return bigQueryRpc.deleteRoutineSkipExceptionTranslation( completeRoutineId.getProject(), completeRoutineId.getDataset(), completeRoutineId.getRoutine()); @@ -658,8 +684,12 @@ public Boolean call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + return false; + } throw BigQueryException.translateAndThrow(e); } } @@ -672,18 +702,19 @@ public boolean delete(JobId jobId) { ? getOptions().getProjectId() : jobId.getProject()); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.deleteJob( + public Boolean call() throws IOException { + return bigQueryRpc.deleteJobSkipExceptionTranslation( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation()); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -696,17 +727,18 @@ public Dataset update(DatasetInfo datasetInfo, DatasetOption... options) { try { return Dataset.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Dataset call() { - return bigQueryRpc.patch(datasetPb, optionsMap); + public com.google.api.services.bigquery.model.Dataset call() throws IOException { + return bigQueryRpc.patchSkipExceptionTranslation(datasetPb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -725,17 +757,18 @@ public Table update(TableInfo tableInfo, TableOption... options) { try { return Table.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Table call() { - return bigQueryRpc.patch(tablePb, optionsMap); + public com.google.api.services.bigquery.model.Table call() throws IOException { + return bigQueryRpc.patchSkipExceptionTranslation(tablePb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -753,17 +786,18 @@ public Model update(ModelInfo modelInfo, ModelOption... options) { try { return Model.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Model call() { - return bigQueryRpc.patch(modelPb, optionsMap); + public com.google.api.services.bigquery.model.Model call() throws IOException { + return bigQueryRpc.patchSkipExceptionTranslation(modelPb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -781,17 +815,18 @@ public Routine update(RoutineInfo routineInfo, RoutineOption... options) { try { return Routine.fromPb( this, - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.update(routinePb, optionsMap); + public com.google.api.services.bigquery.model.Routine call() throws IOException { + return bigQueryRpc.updateSkipExceptionTranslation(routinePb, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -813,11 +848,11 @@ public Table getTable(TableId tableId, TableOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Table answer = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Table call() { - return bigQueryRpc.getTable( + public com.google.api.services.bigquery.model.Table call() throws IOException { + return bigQueryRpc.getTableSkipExceptionTranslation( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), @@ -826,12 +861,16 @@ public com.google.api.services.bigquery.model.Table call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Table not found"); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + return Table.fromPb(this, answer); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + if (getOptions().getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Table not found"); + } + return null; } - return answer == null ? null : Table.fromPb(this, answer); - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -851,11 +890,11 @@ public Model getModel(ModelId modelId, ModelOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Model answer = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Model call() { - return bigQueryRpc.getModel( + public com.google.api.services.bigquery.model.Model call() throws IOException { + return bigQueryRpc.getModelSkipExceptionTranslation( completeModelId.getProject(), completeModelId.getDataset(), completeModelId.getModel(), @@ -864,12 +903,16 @@ public com.google.api.services.bigquery.model.Model call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Model not found"); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + return Model.fromPb(this, answer); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + if (getOptions().getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Model not found"); + } + return null; } - return answer == null ? null : Model.fromPb(this, answer); - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -889,11 +932,11 @@ public Routine getRoutine(RoutineId routineId, RoutineOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Routine answer = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Routine call() { - return bigQueryRpc.getRoutine( + public com.google.api.services.bigquery.model.Routine call() throws IOException { + return bigQueryRpc.getRoutineSkipExceptionTranslation( completeRoutineId.getProject(), completeRoutineId.getDataset(), completeRoutineId.getRoutine(), @@ -902,12 +945,16 @@ public com.google.api.services.bigquery.model.Routine call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Routine not found"); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + return Routine.fromPb(this, answer); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + if (getOptions().getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Routine not found"); + } + return null; } - return answer == null ? null : Routine.fromPb(this, answer); - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -978,20 +1025,22 @@ private static Page listTables( final Map optionsMap) { try { Tuple> result = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable< Tuple>>() { @Override - public Tuple> - call() { + public Tuple> call() + throws IOException { return serviceOptions .getBigQueryRpcV2() - .listTables(datasetId.getProject(), datasetId.getDataset(), optionsMap); + .listTablesSkipExceptionTranslation( + datasetId.getProject(), datasetId.getDataset(), optionsMap); } }, serviceOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); String cursor = result.x(); Iterable
tables = Iterables.transform( @@ -1004,7 +1053,7 @@ public Table apply(com.google.api.services.bigquery.model.Table table) { }); return new PageImpl<>( new TablePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, tables); - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1015,20 +1064,22 @@ private static Page listModels( final Map optionsMap) { try { Tuple> result = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable< Tuple>>() { @Override - public Tuple> - call() { + public Tuple> call() + throws IOException { return serviceOptions .getBigQueryRpcV2() - .listModels(datasetId.getProject(), datasetId.getDataset(), optionsMap); + .listModelsSkipExceptionTranslation( + datasetId.getProject(), datasetId.getDataset(), optionsMap); } }, serviceOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); String cursor = result.x(); Iterable models = Iterables.transform( @@ -1041,7 +1092,7 @@ public Model apply(com.google.api.services.bigquery.model.Model model) { }); return new PageImpl<>( new ModelPageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, models); - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1052,20 +1103,22 @@ private static Page listRoutines( final Map optionsMap) { try { Tuple> result = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable< Tuple>>() { @Override public Tuple> - call() { + call() throws IOException { return serviceOptions .getBigQueryRpcV2() - .listRoutines(datasetId.getProject(), datasetId.getDataset(), optionsMap); + .listRoutinesSkipExceptionTranslation( + datasetId.getProject(), datasetId.getDataset(), optionsMap); } }, serviceOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); String cursor = result.x(); Iterable routines = Iterables.transform( @@ -1078,7 +1131,7 @@ public Routine apply(com.google.api.services.bigquery.model.Routine routinePb) { }); return new PageImpl<>( new RoutinePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, routines); - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1120,21 +1173,23 @@ public Rows apply(RowToInsert rowToInsert) { // allowing retries only if all row insertIds are set (used for deduplication) try { responsePb = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override public TableDataInsertAllResponse call() throws Exception { - return bigQueryRpc.insertAll( + return bigQueryRpc.insertAllSkipExceptionTranslation( tableId.getProject(), tableId.getDataset(), tableId.getTable(), requestPb); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } else { + // Use insertAll that translate the exception as we are not retrying. responsePb = bigQueryRpc.insertAll( tableId.getProject(), tableId.getDataset(), tableId.getTable(), requestPb); @@ -1183,13 +1238,13 @@ private static Tuple, Long> listTableData( ? serviceOptions.getProjectId() : tableId.getProject()); TableDataList result = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public TableDataList call() { + public TableDataList call() throws IOException { return serviceOptions .getBigQueryRpcV2() - .listTableData( + .listTableDataSkipExceptionTranslation( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), @@ -1198,7 +1253,8 @@ public TableDataList call() { }, serviceOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); String cursor = result.getPageToken(); Map pageOptionMap = Strings.isNullOrEmpty(cursor) ? optionsMap : optionMap(TableDataListOption.startIndex(0)); @@ -1208,7 +1264,7 @@ public TableDataList call() { cursor, transformTableData(result.getRows(), schema, serviceOptions.getUseInt64Timestamps())), result.getTotalRows()); - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1245,11 +1301,11 @@ public Job getJob(JobId jobId, JobOption... options) { : jobId.getLocation()); try { com.google.api.services.bigquery.model.Job answer = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Job call() { - return bigQueryRpc.getJob( + public com.google.api.services.bigquery.model.Job call() throws IOException { + return bigQueryRpc.getJobSkipExceptionTranslation( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation(), @@ -1258,12 +1314,16 @@ public com.google.api.services.bigquery.model.Job call() { }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Job not found"); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + return Job.fromPb(this, answer); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + if (getOptions().getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Job not found"); + } + return null; } - return answer == null ? null : Job.fromPb(this, answer); - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1275,30 +1335,36 @@ public Page listJobs(JobListOption... options) { private static Page listJobs( final BigQueryOptions serviceOptions, final Map optionsMap) { - Tuple> result = - runWithRetries( - new Callable>>() { - @Override - public Tuple> call() { - return serviceOptions - .getBigQueryRpcV2() - .listJobs(serviceOptions.getProjectId(), optionsMap); - } - }, - serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - serviceOptions.getClock()); - String cursor = result.x(); - Iterable jobs = - Iterables.transform( - result.y(), - new Function() { - @Override - public Job apply(com.google.api.services.bigquery.model.Job job) { - return Job.fromPb(serviceOptions.getService(), job); - } - }); - return new PageImpl<>(new JobPageFetcher(serviceOptions, cursor, optionsMap), cursor, jobs); + try { + Tuple> result = + BigQueryRetryHelper.runWithRetries( + new Callable>>() { + @Override + public Tuple> call() + throws IOException { + return serviceOptions + .getBigQueryRpcV2() + .listJobsSkipExceptionTranslation(serviceOptions.getProjectId(), optionsMap); + } + }, + serviceOptions.getRetrySettings(), + BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getClock(), + EMPTY_RETRY_CONFIG); + String cursor = result.x(); + Iterable jobs = + Iterables.transform( + result.y(), + new Function() { + @Override + public Job apply(com.google.api.services.bigquery.model.Job job) { + return Job.fromPb(serviceOptions.getService(), job); + } + }); + return new PageImpl<>(new JobPageFetcher(serviceOptions, cursor, optionsMap), cursor, jobs); + } catch (BigQueryRetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } } @Override @@ -1316,18 +1382,22 @@ public boolean cancel(JobId jobId) { ? getOptions().getLocation() : jobId.getLocation()); try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public Boolean call() { - return bigQueryRpc.cancel( + public Boolean call() throws IOException { + return bigQueryRpc.cancelSkipExceptionTranslation( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation()); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (isRetryErrorCodeHttpNotFound(e)) { + return false; + } throw BigQueryException.translateAndThrow(e); } } @@ -1370,8 +1440,9 @@ private TableResult queryRpc( BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.QueryResponse call() { - return bigQueryRpc.queryRpc(projectId, content); + public com.google.api.services.bigquery.model.QueryResponse call() + throws IOException { + return bigQueryRpc.queryRpcSkipExceptionTranslation(projectId, content); } }, getOptions().getRetrySettings(), @@ -1498,10 +1569,10 @@ private static QueryResponse getQueryResults( BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public GetQueryResultsResponse call() { + public GetQueryResultsResponse call() throws IOException { return serviceOptions .getBigQueryRpcV2() - .getQueryResults( + .getQueryResultsSkipExceptionTranslation( completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation(), @@ -1528,7 +1599,7 @@ public GetQueryResultsResponse call() { .setTotalRows(results.getTotalRows() == null ? 0 : results.getTotalRows().longValue()) .setErrors(errors.build()) .build(); - } catch (BigQueryRetryHelper.BigQueryRetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1558,17 +1629,19 @@ public Policy getIamPolicy(TableId tableId, IAMOption... options) { try { final Map optionsMap = optionMap(options); return convertFromApiPolicy( - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Policy call() { - return bigQueryRpc.getIamPolicy(completeTableId.getIAMResourceName(), optionsMap); + public com.google.api.services.bigquery.model.Policy call() throws IOException { + return bigQueryRpc.getIamPolicySkipExceptionTranslation( + completeTableId.getIAMResourceName(), optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelper.RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1583,18 +1656,19 @@ public Policy setIamPolicy(TableId tableId, final Policy policy, IAMOption... op try { final Map optionsMap = optionMap(options); return convertFromApiPolicy( - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Policy call() { - return bigQueryRpc.setIamPolicy( + public com.google.api.services.bigquery.model.Policy call() throws IOException { + return bigQueryRpc.setIamPolicySkipExceptionTranslation( completeTableId.getIAMResourceName(), convertToApiPolicy(policy), optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock())); - } catch (RetryHelperException e) { + getOptions().getClock(), + EMPTY_RETRY_CONFIG)); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1610,21 +1684,23 @@ public List testIamPermissions( try { final Map optionsMap = optionMap(options); com.google.api.services.bigquery.model.TestIamPermissionsResponse response = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() { - return bigQueryRpc.testIamPermissions( + public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() + throws IOException { + return bigQueryRpc.testIamPermissionsSkipExceptionTranslation( completeTableId.getIAMResourceName(), permissions, optionsMap); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); return response.getPermissions() == null ? ImmutableList.of() : ImmutableList.copyOf(response.getPermissions()); - } catch (RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1647,4 +1723,13 @@ static BigQueryRetryConfig getBigQueryRetryConfig(Map opt static RetryOption[] getRetryOptions(Map options) { return (RetryOption[]) options.getOrDefault(BigQueryRpc.Option.RETRY_OPTIONS, null); } + + private static boolean isRetryErrorCodeHttpNotFound(BigQueryRetryHelperException e) { + if (e.getCause() instanceof BigQueryException) { + if (((BigQueryException) e.getCause()).getCode() == HTTP_NOT_FOUND) { + return true; + } + } + return false; + } } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index 465cc8305..ca34a2d2d 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -21,7 +21,6 @@ import com.google.cloud.ServiceRpc; import com.google.cloud.TransportOptions; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; -import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import com.google.cloud.http.HttpTransportOptions; import com.google.common.annotations.VisibleForTesting; @@ -132,8 +131,8 @@ protected Set getScopes() { return SCOPES; } - protected BigQueryRpc getBigQueryRpcV2() { - return (BigQueryRpc) getRpc(); + protected HttpBigQueryRpc getBigQueryRpcV2() { + return (HttpBigQueryRpc) getRpc(); } public String getLocation() { diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryHelper.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryHelper.java index 405a2371e..d315241a3 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryHelper.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryHelper.java @@ -25,6 +25,7 @@ import com.google.api.gax.retrying.RetryingFuture; import com.google.api.gax.retrying.TimedRetryAlgorithm; import com.google.cloud.RetryHelper; +import java.io.IOException; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.logging.Level; @@ -52,6 +53,11 @@ public static V runWithRetries( algorithm, bigQueryRetryConfig); } catch (Exception e) { + // Checks for IOException and translate it into BigQueryException. The BigQueryException + // constructor parses the IOException and translate it into internal code. + if (e.getCause() instanceof IOException) { + throw new BigQueryRetryHelperException(new BigQueryException((IOException) e.getCause())); + } throw new BigQueryRetryHelperException(e.getCause()); } } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index 5ad9fe284..d524cedfc 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -16,7 +16,6 @@ package com.google.cloud.bigquery; -import static com.google.cloud.RetryHelper.runWithRetries; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import com.google.api.core.BetaApi; @@ -28,8 +27,8 @@ import com.google.api.services.bigquery.model.QueryRequest; import com.google.api.services.bigquery.model.TableDataList; import com.google.api.services.bigquery.model.TableRow; -import com.google.cloud.RetryHelper; import com.google.cloud.Tuple; +import com.google.cloud.bigquery.BigQueryRetryHelper.BigQueryRetryHelperException; import com.google.cloud.bigquery.JobStatistics.QueryStatistics; import com.google.cloud.bigquery.JobStatistics.SessionInfo; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; @@ -102,6 +101,8 @@ class ConnectionImpl implements Connection { bufferFvl; // initialized lazily iff we end up using the tabledata.list end point private BlockingQueue bufferRow; // initialized lazily iff we end up using Read API + private static final BigQueryRetryConfig EMPTY_RETRY_CONFIG = + BigQueryRetryConfig.newBuilder().build(); ConnectionImpl( ConnectionSettings connectionSettings, @@ -466,12 +467,15 @@ private BigQueryResult queryRpc( try { results = BigQueryRetryHelper.runWithRetries( - () -> bigQueryRpc.queryRpc(projectId, queryRequest), + () -> + bigQueryOptions + .getBigQueryRpcV2() + .queryRpcSkipExceptionTranslation(projectId, queryRequest), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), retryConfig); - } catch (BigQueryRetryHelper.BigQueryRetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -914,21 +918,30 @@ private Job getQueryJobRpc(JobId jobId) { com.google.api.services.bigquery.model.Job jobPb; try { jobPb = - runWithRetries( + BigQueryRetryHelper.runWithRetries( () -> - bigQueryRpc.getQueryJob( - completeJobId.getProject(), - completeJobId.getJob(), - completeJobId.getLocation()), + bigQueryOptions + .getBigQueryRpcV2() + .getQueryJobSkipExceptionTranslation( + completeJobId.getProject(), + completeJobId.getJob(), + completeJobId.getLocation()), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - bigQueryOptions.getClock()); - if (bigQueryOptions.getThrowNotFound() && jobPb == null) { - throw new BigQueryException(HTTP_NOT_FOUND, "Query job not found"); + bigQueryOptions.getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { + if (e.getCause() instanceof BigQueryException) { + if (((BigQueryException) e.getCause()).getCode() == HTTP_NOT_FOUND) { + if (bigQueryOptions.getThrowNotFound()) { + throw new BigQueryException(HTTP_NOT_FOUND, "Query job not found"); + } + return null; + } } - } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } + // getQueryJobSkipExceptionTranslation will never return null so this is safe. return Job.fromPb(bigQueryOptions.getService(), jobPb); } @@ -948,11 +961,11 @@ TableDataList tableDataListRpc(TableId destinationTable, String pageToken) { ? bigQueryOptions.getProjectId() : destinationTable.getProject()); TableDataList results = - runWithRetries( + BigQueryRetryHelper.runWithRetries( () -> bigQueryOptions .getBigQueryRpcV2() - .listTableDataWithRowLimit( + .listTableDataWithRowLimitSkipExceptionTranslation( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), @@ -960,10 +973,11 @@ TableDataList tableDataListRpc(TableId destinationTable, String pageToken) { pageToken), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - bigQueryOptions.getClock()); + bigQueryOptions.getClock(), + EMPTY_RETRY_CONFIG); return results; - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -1177,12 +1191,14 @@ GetQueryResultsResponse getQueryResultsFirstPage(JobId jobId) { results = BigQueryRetryHelper.runWithRetries( () -> - bigQueryRpc.getQueryResultsWithRowLimit( - completeJobId.getProject(), - completeJobId.getJob(), - completeJobId.getLocation(), - connectionSettings.getMaxResultPerPage(), - timeoutMs), + bigQueryOptions + .getBigQueryRpcV2() + .getQueryResultsWithRowLimitSkipExceptionTranslation( + completeJobId.getProject(), + completeJobId.getJob(), + completeJobId.getLocation(), + connectionSettings.getMaxResultPerPage(), + timeoutMs), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), @@ -1197,7 +1213,7 @@ GetQueryResultsResponse getQueryResultsFirstPage(JobId jobId) { // with the case where there is a HTTP error throw new BigQueryException(bigQueryErrors); } - } catch (BigQueryRetryHelper.BigQueryRetryHelperException e) { + } catch (BigQueryRetryHelperException e) { logger.log(Level.WARNING, "\n Error occurred while calling getQueryResultsWithRowLimit", e); throw BigQueryException.translateAndThrow(e); } @@ -1442,7 +1458,10 @@ com.google.api.services.bigquery.model.Job createQueryJob( try { queryJob = BigQueryRetryHelper.runWithRetries( - () -> bigQueryRpc.createJobForQuery(jobPb), + () -> + bigQueryOptions + .getBigQueryRpcV2() + .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), @@ -1482,7 +1501,10 @@ com.google.api.services.bigquery.model.Job createDryRunJob(String sql) { try { dryRunJob = BigQueryRetryHelper.runWithRetries( - () -> bigQueryRpc.createJobForQuery(jobPb), + () -> + bigQueryOptions + .getBigQueryRpcV2() + .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java index 0f9632aea..aabd87d47 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java @@ -16,12 +16,11 @@ package com.google.cloud.bigquery; -import static com.google.cloud.RetryHelper.runWithRetries; - import com.google.cloud.BaseWriteChannel; import com.google.cloud.RestorableState; -import com.google.cloud.RetryHelper; import com.google.cloud.WriteChannel; +import com.google.cloud.bigquery.BigQueryRetryHelper.BigQueryRetryHelperException; +import java.io.IOException; import java.util.List; import java.util.Objects; import java.util.concurrent.Callable; @@ -34,6 +33,9 @@ public class TableDataWriteChannel extends BaseWriteChannel { + private static final BigQueryRetryConfig EMPTY_RETRY_CONFIG = + BigQueryRetryConfig.newBuilder().build(); + private Job job; TableDataWriteChannel( @@ -50,20 +52,22 @@ public class TableDataWriteChannel protected void flushBuffer(final int length, final boolean last) { try { com.google.api.services.bigquery.model.Job jobPb = - runWithRetries( + BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public com.google.api.services.bigquery.model.Job call() { + public com.google.api.services.bigquery.model.Job call() throws IOException { return getOptions() .getBigQueryRpcV2() - .write(getUploadId(), getBuffer(), 0, getPosition(), length, last); + .writeSkipExceptionTranslation( + getUploadId(), getBuffer(), 0, getPosition(), length, last); } }, getOptions().getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - getOptions().getClock()); + getOptions().getClock(), + EMPTY_RETRY_CONFIG); job = jobPb != null ? Job.fromPb(getOptions().getService(), jobPb) : null; - } catch (RetryHelper.RetryHelperException e) { + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @@ -78,13 +82,13 @@ private static String open( final JobId jobId, final WriteChannelConfiguration writeChannelConfiguration) { try { - return runWithRetries( + return BigQueryRetryHelper.runWithRetries( new Callable() { @Override - public String call() { + public String call() throws IOException { return options .getBigQueryRpcV2() - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setConfiguration(writeChannelConfiguration.toPb()) .setJobReference(jobId.toPb())); @@ -92,8 +96,9 @@ public String call() { }, options.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, - options.getClock()); - } catch (RetryHelper.RetryHelperException e) { + options.getClock(), + EMPTY_RETRY_CONFIG); + } catch (BigQueryRetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index 3946f83f5..14a14fdb9 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -129,20 +129,7 @@ private void validateRPC() throws BigQueryException, IOException { @Override public Dataset getDataset(String projectId, String datasetId, Map options) { try { - validateRPC(); - - Bigquery.Datasets.Get bqGetRequest = - bigquery - .datasets() - .get(projectId, datasetId) - .setFields(Option.FIELDS.getString(options)) - .setPrettyPrint(false); - for (Map.Entry entry : options.entrySet()) { - if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { - bqGetRequest.setAccessPolicyVersion((Integer) entry.getValue()); - } - } - return bqGetRequest.execute(); + return getDatasetSkipExceptionTranslation(projectId, datasetId, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -152,130 +139,175 @@ public Dataset getDataset(String projectId, String datasetId, Map opt } } + @InternalApi("internal to java-bigquery") + public Dataset getDatasetSkipExceptionTranslation( + String projectId, String datasetId, Map options) throws IOException { + validateRPC(); + Bigquery.Datasets.Get bqGetRequest = + bigquery + .datasets() + .get(projectId, datasetId) + .setFields(Option.FIELDS.getString(options)) + .setPrettyPrint(false); + for (Map.Entry entry : options.entrySet()) { + if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { + bqGetRequest.setAccessPolicyVersion((Integer) entry.getValue()); + } + } + return bqGetRequest.execute(); + } + @Override public Tuple> listDatasets(String projectId, Map options) { try { - validateRPC(); - DatasetList datasetsList = - bigquery - .datasets() - .list(projectId) - .setPrettyPrint(false) - .setAll(Option.ALL_DATASETS.getBoolean(options)) - .setFilter(Option.LABEL_FILTER.getString(options)) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .execute(); - Iterable datasets = datasetsList.getDatasets(); - return Tuple.of( - datasetsList.getNextPageToken(), - Iterables.transform( - datasets != null ? datasets : ImmutableList.of(), - LIST_TO_DATASET)); + return listDatasetsSkipExceptionTranslation(projectId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Tuple> listDatasetsSkipExceptionTranslation( + String projectId, Map options) throws IOException { + validateRPC(); + DatasetList datasetsList = + bigquery + .datasets() + .list(projectId) + .setPrettyPrint(false) + .setAll(Option.ALL_DATASETS.getBoolean(options)) + .setFilter(Option.LABEL_FILTER.getString(options)) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); + Iterable datasets = datasetsList.getDatasets(); + return Tuple.of( + datasetsList.getNextPageToken(), + Iterables.transform( + datasets != null ? datasets : ImmutableList.of(), + LIST_TO_DATASET)); + } + @Override public Dataset create(Dataset dataset, Map options) { try { - validateRPC(); - Bigquery.Datasets.Insert bqCreateRequest = - bigquery - .datasets() - .insert(dataset.getDatasetReference().getProjectId(), dataset) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)); - for (Map.Entry entry : options.entrySet()) { - if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { - bqCreateRequest.setAccessPolicyVersion((Integer) entry.getValue()); - } - } - return bqCreateRequest.execute(); + return createSkipExceptionTranslation(dataset, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Dataset createSkipExceptionTranslation(Dataset dataset, Map options) + throws IOException { + validateRPC(); + Bigquery.Datasets.Insert bqCreateRequest = + bigquery + .datasets() + .insert(dataset.getDatasetReference().getProjectId(), dataset) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)); + for (Map.Entry entry : options.entrySet()) { + if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { + bqCreateRequest.setAccessPolicyVersion((Integer) entry.getValue()); + } + } + return bqCreateRequest.execute(); + } + @Override public Table create(Table table, Map options) { try { - validateRPC(); - // unset the type, as it is output only - table.setType(null); - TableReference reference = table.getTableReference(); - return bigquery - .tables() - .insert(reference.getProjectId(), reference.getDatasetId(), table) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return createSkipExceptionTranslation(table, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Table createSkipExceptionTranslation(Table table, Map options) + throws IOException { + validateRPC(); + // unset the type, as it is output only + table.setType(null); + TableReference reference = table.getTableReference(); + return bigquery + .tables() + .insert(reference.getProjectId(), reference.getDatasetId(), table) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Routine create(Routine routine, Map options) { try { - validateRPC(); - RoutineReference reference = routine.getRoutineReference(); - return bigquery - .routines() - .insert(reference.getProjectId(), reference.getDatasetId(), routine) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return createSkipExceptionTranslation(routine, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Routine createSkipExceptionTranslation(Routine routine, Map options) + throws IOException { + validateRPC(); + RoutineReference reference = routine.getRoutineReference(); + return bigquery + .routines() + .insert(reference.getProjectId(), reference.getDatasetId(), routine) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Job create(Job job, Map options) { try { - validateRPC(); - String projectId = - job.getJobReference() != null - ? job.getJobReference().getProjectId() - : this.options.getProjectId(); - return bigquery - .jobs() - .insert(projectId, job) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return createSkipExceptionTranslation(job, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Job createSkipExceptionTranslation(Job job, Map options) throws IOException { + validateRPC(); + String projectId = + job.getJobReference() != null + ? job.getJobReference().getProjectId() + : this.options.getProjectId(); + return bigquery + .jobs() + .insert(projectId, job) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Job createJobForQuery(Job job) { try { - validateRPC(); - String projectId = - job.getJobReference() != null - ? job.getJobReference().getProjectId() - : this.options.getProjectId(); - return bigquery.jobs().insert(projectId, job).setPrettyPrint(false).execute(); + return createJobForQuerySkipExceptionTranslation(job); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Job createJobForQuerySkipExceptionTranslation(Job job) throws IOException { + validateRPC(); + String projectId = + job.getJobReference() != null + ? job.getJobReference().getProjectId() + : this.options.getProjectId(); + return bigquery.jobs().insert(projectId, job).setPrettyPrint(false).execute(); + } + @Override public boolean deleteDataset(String projectId, String datasetId, Map options) { try { - validateRPC(); - bigquery - .datasets() - .delete(projectId, datasetId) - .setPrettyPrint(false) - .setDeleteContents(Option.DELETE_CONTENTS.getBoolean(options)) - .execute(); - return true; + return deleteDatasetSkipExceptionTranslation(projectId, datasetId, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -285,59 +317,77 @@ public boolean deleteDataset(String projectId, String datasetId, Map } } + @InternalApi("internal to java-bigquery") + public boolean deleteDatasetSkipExceptionTranslation( + String projectId, String datasetId, Map options) throws IOException { + validateRPC(); + bigquery + .datasets() + .delete(projectId, datasetId) + .setPrettyPrint(false) + .setDeleteContents(Option.DELETE_CONTENTS.getBoolean(options)) + .execute(); + return true; + } + @Override public Dataset patch(Dataset dataset, Map options) { try { - validateRPC(); - DatasetReference reference = dataset.getDatasetReference(); - Bigquery.Datasets.Patch bqPatchRequest = - bigquery - .datasets() - .patch(reference.getProjectId(), reference.getDatasetId(), dataset) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)); - for (Map.Entry entry : options.entrySet()) { - if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { - bqPatchRequest.setAccessPolicyVersion((Integer) entry.getValue()); - } - } - return bqPatchRequest.execute(); + return patchSkipExceptionTranslation(dataset, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Dataset patchSkipExceptionTranslation(Dataset dataset, Map options) + throws IOException { + validateRPC(); + DatasetReference reference = dataset.getDatasetReference(); + Bigquery.Datasets.Patch bqPatchRequest = + bigquery + .datasets() + .patch(reference.getProjectId(), reference.getDatasetId(), dataset) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)); + for (Map.Entry entry : options.entrySet()) { + if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) { + bqPatchRequest.setAccessPolicyVersion((Integer) entry.getValue()); + } + } + return bqPatchRequest.execute(); + } + @Override public Table patch(Table table, Map options) { try { - validateRPC(); - // unset the type, as it is output only - table.setType(null); - TableReference reference = table.getTableReference(); - return bigquery - .tables() - .patch(reference.getProjectId(), reference.getDatasetId(), reference.getTableId(), table) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .setAutodetectSchema(BigQueryRpc.Option.AUTODETECT_SCHEMA.getBoolean(options)) - .execute(); + return patchSkipExceptionTranslation(table, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Table patchSkipExceptionTranslation(Table table, Map options) + throws IOException { + validateRPC(); + // unset the type, as it is output only + table.setType(null); + TableReference reference = table.getTableReference(); + return bigquery + .tables() + .patch(reference.getProjectId(), reference.getDatasetId(), reference.getTableId(), table) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .setAutodetectSchema(BigQueryRpc.Option.AUTODETECT_SCHEMA.getBoolean(options)) + .execute(); + } + @Override public Table getTable( String projectId, String datasetId, String tableId, Map options) { try { - validateRPC(); - return bigquery - .tables() - .get(projectId, datasetId, tableId) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .setView(getTableMetadataOption(options)) - .execute(); + return getTableSkipExceptionTranslation(projectId, datasetId, tableId, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -347,6 +397,20 @@ public Table getTable( } } + @InternalApi("internal to java-bigquery") + public Table getTableSkipExceptionTranslation( + String projectId, String datasetId, String tableId, Map options) + throws IOException { + validateRPC(); + return bigquery + .tables() + .get(projectId, datasetId, tableId) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .setView(getTableMetadataOption(options)) + .execute(); + } + private String getTableMetadataOption(Map options) { if (options.containsKey(Option.TABLE_METADATA_VIEW)) { return options.get(Option.TABLE_METADATA_VIEW).toString(); @@ -358,46 +422,50 @@ private String getTableMetadataOption(Map options) { public Tuple> listTables( String projectId, String datasetId, Map options) { try { - validateRPC(); - TableList tableList = - bigquery - .tables() - .list(projectId, datasetId) - .setPrettyPrint(false) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .execute(); - Iterable tables = tableList.getTables(); - return Tuple.of( - tableList.getNextPageToken(), - Iterables.transform( - tables != null ? tables : ImmutableList.of(), - new Function() { - @Override - public Table apply(TableList.Tables tablePb) { - return new Table() - .setFriendlyName(tablePb.getFriendlyName()) - .setId(tablePb.getId()) - .setKind(tablePb.getKind()) - .setTableReference(tablePb.getTableReference()) - .setType(tablePb.getType()) - .setCreationTime(tablePb.getCreationTime()) - .setTimePartitioning(tablePb.getTimePartitioning()) - .setRangePartitioning(tablePb.getRangePartitioning()) - .setClustering(tablePb.getClustering()); - } - })); + return listTablesSkipExceptionTranslation(projectId, datasetId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Tuple> listTablesSkipExceptionTranslation( + String projectId, String datasetId, Map options) throws IOException { + validateRPC(); + TableList tableList = + bigquery + .tables() + .list(projectId, datasetId) + .setPrettyPrint(false) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); + Iterable tables = tableList.getTables(); + return Tuple.of( + tableList.getNextPageToken(), + Iterables.transform( + tables != null ? tables : ImmutableList.of(), + new Function() { + @Override + public Table apply(TableList.Tables tablePb) { + return new Table() + .setFriendlyName(tablePb.getFriendlyName()) + .setId(tablePb.getId()) + .setKind(tablePb.getKind()) + .setTableReference(tablePb.getTableReference()) + .setType(tablePb.getType()) + .setCreationTime(tablePb.getCreationTime()) + .setTimePartitioning(tablePb.getTimePartitioning()) + .setRangePartitioning(tablePb.getRangePartitioning()) + .setClustering(tablePb.getClustering()); + } + })); + } + @Override public boolean deleteTable(String projectId, String datasetId, String tableId) { try { - validateRPC(); - bigquery.tables().delete(projectId, datasetId, tableId).execute(); - return true; + return deleteTableSkipExceptionTranslation(projectId, datasetId, tableId); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -407,34 +475,42 @@ public boolean deleteTable(String projectId, String datasetId, String tableId) { } } + @InternalApi("internal to java-bigquery") + public boolean deleteTableSkipExceptionTranslation( + String projectId, String datasetId, String tableId) throws IOException { + validateRPC(); + bigquery.tables().delete(projectId, datasetId, tableId).execute(); + return true; + } + @Override public Model patch(Model model, Map options) { try { - validateRPC(); - // unset the type, as it is output only - ModelReference reference = model.getModelReference(); - return bigquery - .models() - .patch(reference.getProjectId(), reference.getDatasetId(), reference.getModelId(), model) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return patchSkipExceptionTranslation(model, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Model patchSkipExceptionTranslation(Model model, Map options) + throws IOException { + validateRPC(); + // unset the type, as it is output only + ModelReference reference = model.getModelReference(); + return bigquery + .models() + .patch(reference.getProjectId(), reference.getDatasetId(), reference.getModelId(), model) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Model getModel( String projectId, String datasetId, String modelId, Map options) { try { - validateRPC(); - return bigquery - .models() - .get(projectId, datasetId, modelId) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return getModelSkipExceptionTranslation(projectId, datasetId, modelId, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -444,33 +520,50 @@ public Model getModel( } } + @InternalApi("internal to java-bigquery") + public Model getModelSkipExceptionTranslation( + String projectId, String datasetId, String modelId, Map options) + throws IOException { + validateRPC(); + return bigquery + .models() + .get(projectId, datasetId, modelId) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Tuple> listModels( String projectId, String datasetId, Map options) { try { - validateRPC(); - ListModelsResponse modelList = - bigquery - .models() - .list(projectId, datasetId) - .setPrettyPrint(false) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .execute(); - Iterable models = - modelList.getModels() != null ? modelList.getModels() : ImmutableList.of(); - return Tuple.of(modelList.getNextPageToken(), models); + return listModelsSkipExceptionTranslation(projectId, datasetId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Tuple> listModelsSkipExceptionTranslation( + String projectId, String datasetId, Map options) throws IOException { + validateRPC(); + ListModelsResponse modelList = + bigquery + .models() + .list(projectId, datasetId) + .setPrettyPrint(false) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); + Iterable models = + modelList.getModels() != null ? modelList.getModels() : ImmutableList.of(); + return Tuple.of(modelList.getNextPageToken(), models); + } + @Override public boolean deleteModel(String projectId, String datasetId, String modelId) { try { - validateRPC(); - bigquery.models().delete(projectId, datasetId, modelId).execute(); - return true; + return deleteModelSkipExceptionTranslation(projectId, datasetId, modelId); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -480,34 +573,42 @@ public boolean deleteModel(String projectId, String datasetId, String modelId) { } } + @InternalApi("internal to java-bigquery") + public boolean deleteModelSkipExceptionTranslation( + String projectId, String datasetId, String modelId) throws IOException { + validateRPC(); + bigquery.models().delete(projectId, datasetId, modelId).execute(); + return true; + } + @Override public Routine update(Routine routine, Map options) { try { - validateRPC(); - RoutineReference reference = routine.getRoutineReference(); - return bigquery - .routines() - .update( - reference.getProjectId(), reference.getDatasetId(), reference.getRoutineId(), routine) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return updateSkipExceptionTranslation(routine, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Routine updateSkipExceptionTranslation(Routine routine, Map options) + throws IOException { + validateRPC(); + RoutineReference reference = routine.getRoutineReference(); + return bigquery + .routines() + .update( + reference.getProjectId(), reference.getDatasetId(), reference.getRoutineId(), routine) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Routine getRoutine( String projectId, String datasetId, String routineId, Map options) { try { - validateRPC(); - return bigquery - .routines() - .get(projectId, datasetId, routineId) - .setPrettyPrint(false) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return getRoutineSkipExceptionTranslation(projectId, datasetId, routineId, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -517,35 +618,50 @@ public Routine getRoutine( } } + @InternalApi("internal to java-bigquery") + public Routine getRoutineSkipExceptionTranslation( + String projectId, String datasetId, String routineId, Map options) + throws IOException { + validateRPC(); + return bigquery + .routines() + .get(projectId, datasetId, routineId) + .setPrettyPrint(false) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Tuple> listRoutines( String projectId, String datasetId, Map options) { try { - validateRPC(); - ListRoutinesResponse routineList = - bigquery - .routines() - .list(projectId, datasetId) - .setPrettyPrint(false) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .execute(); - Iterable routines = - routineList.getRoutines() != null - ? routineList.getRoutines() - : ImmutableList.of(); - return Tuple.of(routineList.getNextPageToken(), routines); + return listRoutinesSkipExceptionTranslation(projectId, datasetId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Tuple> listRoutinesSkipExceptionTranslation( + String projectId, String datasetId, Map options) throws IOException { + validateRPC(); + ListRoutinesResponse routineList = + bigquery + .routines() + .list(projectId, datasetId) + .setPrettyPrint(false) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .execute(); + Iterable routines = + routineList.getRoutines() != null ? routineList.getRoutines() : ImmutableList.of(); + return Tuple.of(routineList.getNextPageToken(), routines); + } + @Override public boolean deleteRoutine(String projectId, String datasetId, String routineId) { try { - validateRPC(); - bigquery.routines().delete(projectId, datasetId, routineId).execute(); - return true; + return deleteRoutineSkipExceptionTranslation(projectId, datasetId, routineId); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -555,42 +671,64 @@ public boolean deleteRoutine(String projectId, String datasetId, String routineI } } + @InternalApi("internal to java-bigquery") + public boolean deleteRoutineSkipExceptionTranslation( + String projectId, String datasetId, String routineId) throws IOException { + validateRPC(); + bigquery.routines().delete(projectId, datasetId, routineId).execute(); + return true; + } + @Override public TableDataInsertAllResponse insertAll( String projectId, String datasetId, String tableId, TableDataInsertAllRequest request) { try { - validateRPC(); - return bigquery - .tabledata() - .insertAll(projectId, datasetId, tableId, request) - .setPrettyPrint(false) - .execute(); + return insertAllSkipExceptionTranslation(projectId, datasetId, tableId, request); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public TableDataInsertAllResponse insertAllSkipExceptionTranslation( + String projectId, String datasetId, String tableId, TableDataInsertAllRequest request) + throws IOException { + validateRPC(); + return bigquery + .tabledata() + .insertAll(projectId, datasetId, tableId, request) + .setPrettyPrint(false) + .execute(); + } + @Override public TableDataList listTableData( String projectId, String datasetId, String tableId, Map options) { try { - validateRPC(); - return bigquery - .tabledata() - .list(projectId, datasetId, tableId) - .setPrettyPrint(false) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .setStartIndex( - Option.START_INDEX.getLong(options) != null - ? BigInteger.valueOf(Option.START_INDEX.getLong(options)) - : null) - .execute(); + return listTableDataSkipExceptionTranslation(projectId, datasetId, tableId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public TableDataList listTableDataSkipExceptionTranslation( + String projectId, String datasetId, String tableId, Map options) + throws IOException { + validateRPC(); + return bigquery + .tabledata() + .list(projectId, datasetId, tableId) + .setPrettyPrint(false) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .setStartIndex( + Option.START_INDEX.getLong(options) != null + ? BigInteger.valueOf(Option.START_INDEX.getLong(options)) + : null) + .execute(); + } + @Override public TableDataList listTableDataWithRowLimit( String projectId, @@ -599,30 +737,35 @@ public TableDataList listTableDataWithRowLimit( Integer maxResultPerPage, String pageToken) { try { - validateRPC(); - return bigquery - .tabledata() - .list(projectId, datasetId, tableId) - .setPrettyPrint(false) - .setMaxResults(Long.valueOf(maxResultPerPage)) - .setPageToken(pageToken) - .execute(); + return listTableDataWithRowLimitSkipExceptionTranslation( + projectId, datasetId, tableId, maxResultPerPage, pageToken); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public TableDataList listTableDataWithRowLimitSkipExceptionTranslation( + String projectId, + String datasetId, + String tableId, + Integer maxResultPerPage, + String pageToken) + throws IOException { + validateRPC(); + return bigquery + .tabledata() + .list(projectId, datasetId, tableId) + .setPrettyPrint(false) + .setMaxResults(Long.valueOf(maxResultPerPage)) + .setPageToken(pageToken) + .execute(); + } + @Override public Job getJob(String projectId, String jobId, String location, Map options) { try { - validateRPC(); - return bigquery - .jobs() - .get(projectId, jobId) - .setPrettyPrint(false) - .setLocation(location) - .setFields(Option.FIELDS.getString(options)) - .execute(); + return getJobSkipExceptionTranslation(projectId, jobId, location, options); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -632,16 +775,23 @@ public Job getJob(String projectId, String jobId, String location, Map options) throws IOException { + validateRPC(); + return bigquery + .jobs() + .get(projectId, jobId) + .setPrettyPrint(false) + .setLocation(location) + .setFields(Option.FIELDS.getString(options)) + .execute(); + } + @Override public Job getQueryJob(String projectId, String jobId, String location) { try { - validateRPC(); - return bigquery - .jobs() - .get(projectId, jobId) - .setPrettyPrint(false) - .setLocation(location) - .execute(); + return getQueryJobSkipExceptionTranslation(projectId, jobId, location); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -651,72 +801,83 @@ public Job getQueryJob(String projectId, String jobId, String location) { } } + @InternalApi("internal to java-bigquery") + public Job getQueryJobSkipExceptionTranslation(String projectId, String jobId, String location) + throws IOException { + validateRPC(); + return bigquery + .jobs() + .get(projectId, jobId) + .setPrettyPrint(false) + .setLocation(location) + .execute(); + } + @Override public Tuple> listJobs(String projectId, Map options) { try { - validateRPC(); - Bigquery.Jobs.List request = - bigquery - .jobs() - .list(projectId) - .setPrettyPrint(false) - .setAllUsers(Option.ALL_USERS.getBoolean(options)) - .setFields(Option.FIELDS.getString(options)) - .setStateFilter(Option.STATE_FILTER.>get(options)) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .setProjection(DEFAULT_PROJECTION) - .setParentJobId(Option.PARENT_JOB_ID.getString(options)); - if (Option.MIN_CREATION_TIME.getLong(options) != null) { - request.setMinCreationTime(BigInteger.valueOf(Option.MIN_CREATION_TIME.getLong(options))); - } - if (Option.MAX_CREATION_TIME.getLong(options) != null) { - request.setMaxCreationTime(BigInteger.valueOf(Option.MAX_CREATION_TIME.getLong(options))); - } - JobList jobsList = request.execute(); - - Iterable jobs = jobsList.getJobs(); - return Tuple.of( - jobsList.getNextPageToken(), - Iterables.transform( - jobs != null ? jobs : ImmutableList.of(), - new Function() { - @Override - public Job apply(JobList.Jobs jobPb) { - JobStatus statusPb = - jobPb.getStatus() != null ? jobPb.getStatus() : new JobStatus(); - if (statusPb.getState() == null) { - statusPb.setState(jobPb.getState()); - } - if (statusPb.getErrorResult() == null) { - statusPb.setErrorResult(jobPb.getErrorResult()); - } - return new Job() - .setConfiguration(jobPb.getConfiguration()) - .setId(jobPb.getId()) - .setJobReference(jobPb.getJobReference()) - .setKind(jobPb.getKind()) - .setStatistics(jobPb.getStatistics()) - .setStatus(statusPb) - .setUserEmail(jobPb.getUserEmail()); - } - })); + return listJobsSkipExceptionTranslation(projectId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Tuple> listJobsSkipExceptionTranslation( + String projectId, Map options) throws IOException { + validateRPC(); + Bigquery.Jobs.List request = + bigquery + .jobs() + .list(projectId) + .setPrettyPrint(false) + .setAllUsers(Option.ALL_USERS.getBoolean(options)) + .setFields(Option.FIELDS.getString(options)) + .setStateFilter(Option.STATE_FILTER.>get(options)) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .setProjection(DEFAULT_PROJECTION) + .setParentJobId(Option.PARENT_JOB_ID.getString(options)); + if (Option.MIN_CREATION_TIME.getLong(options) != null) { + request.setMinCreationTime(BigInteger.valueOf(Option.MIN_CREATION_TIME.getLong(options))); + } + if (Option.MAX_CREATION_TIME.getLong(options) != null) { + request.setMaxCreationTime(BigInteger.valueOf(Option.MAX_CREATION_TIME.getLong(options))); + } + JobList jobsList = request.execute(); + + Iterable jobs = jobsList.getJobs(); + return Tuple.of( + jobsList.getNextPageToken(), + Iterables.transform( + jobs != null ? jobs : ImmutableList.of(), + new Function() { + @Override + public Job apply(JobList.Jobs jobPb) { + JobStatus statusPb = + jobPb.getStatus() != null ? jobPb.getStatus() : new JobStatus(); + if (statusPb.getState() == null) { + statusPb.setState(jobPb.getState()); + } + if (statusPb.getErrorResult() == null) { + statusPb.setErrorResult(jobPb.getErrorResult()); + } + return new Job() + .setConfiguration(jobPb.getConfiguration()) + .setId(jobPb.getId()) + .setJobReference(jobPb.getJobReference()) + .setKind(jobPb.getKind()) + .setStatistics(jobPb.getStatistics()) + .setStatus(statusPb) + .setUserEmail(jobPb.getUserEmail()); + } + })); + } + @Override public boolean cancel(String projectId, String jobId, String location) { try { - validateRPC(); - bigquery - .jobs() - .cancel(projectId, jobId) - .setLocation(location) - .setPrettyPrint(false) - .execute(); - return true; + return cancelSkipExceptionTranslation(projectId, jobId, location); } catch (IOException ex) { BigQueryException serviceException = translate(ex); if (serviceException.getCode() == HTTP_NOT_FOUND) { @@ -726,95 +887,134 @@ public boolean cancel(String projectId, String jobId, String location) { } } + @InternalApi("internal to java-bigquery") + public boolean cancelSkipExceptionTranslation(String projectId, String jobId, String location) + throws IOException { + validateRPC(); + bigquery.jobs().cancel(projectId, jobId).setLocation(location).setPrettyPrint(false).execute(); + return true; + } + @Override public boolean deleteJob(String projectId, String jobName, String location) { try { - validateRPC(); - bigquery - .jobs() - .delete(projectId, jobName) - .setLocation(location) - .setPrettyPrint(false) - .execute(); - return true; + return deleteJobSkipExceptionTranslation(projectId, jobName, location); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public boolean deleteJobSkipExceptionTranslation( + String projectId, String jobName, String location) throws IOException { + validateRPC(); + bigquery + .jobs() + .delete(projectId, jobName) + .setLocation(location) + .setPrettyPrint(false) + .execute(); + return true; + } + @Override public GetQueryResultsResponse getQueryResults( String projectId, String jobId, String location, Map options) { try { - validateRPC(); - return bigquery - .jobs() - .getQueryResults(projectId, jobId) - .setPrettyPrint(false) - .setLocation(location) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .setStartIndex( - Option.START_INDEX.getLong(options) != null - ? BigInteger.valueOf(Option.START_INDEX.getLong(options)) - : null) - .setTimeoutMs(Option.TIMEOUT.getLong(options)) - .execute(); + return getQueryResultsSkipExceptionTranslation(projectId, jobId, location, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public GetQueryResultsResponse getQueryResultsSkipExceptionTranslation( + String projectId, String jobId, String location, Map options) throws IOException { + validateRPC(); + return bigquery + .jobs() + .getQueryResults(projectId, jobId) + .setPrettyPrint(false) + .setLocation(location) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .setStartIndex( + Option.START_INDEX.getLong(options) != null + ? BigInteger.valueOf(Option.START_INDEX.getLong(options)) + : null) + .setTimeoutMs(Option.TIMEOUT.getLong(options)) + .execute(); + } + @Override public GetQueryResultsResponse getQueryResultsWithRowLimit( String projectId, String jobId, String location, Integer maxResultPerPage, Long timeoutMs) { try { - validateRPC(); - return bigquery - .jobs() - .getQueryResults(projectId, jobId) - .setPrettyPrint(false) - .setLocation(location) - .setMaxResults(Long.valueOf(maxResultPerPage)) - .setTimeoutMs(timeoutMs) - .execute(); + return getQueryResultsWithRowLimitSkipExceptionTranslation( + projectId, jobId, location, maxResultPerPage, timeoutMs); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public GetQueryResultsResponse getQueryResultsWithRowLimitSkipExceptionTranslation( + String projectId, String jobId, String location, Integer maxResultPerPage, Long timeoutMs) + throws IOException { + validateRPC(); + return bigquery + .jobs() + .getQueryResults(projectId, jobId) + .setPrettyPrint(false) + .setLocation(location) + .setMaxResults(Long.valueOf(maxResultPerPage)) + .setTimeoutMs(timeoutMs) + .execute(); + } + @Override public QueryResponse queryRpc(String projectId, QueryRequest content) { try { - validateRPC(); - return bigquery.jobs().query(projectId, content).execute(); + return queryRpcSkipExceptionTranslation(projectId, content); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public QueryResponse queryRpcSkipExceptionTranslation(String projectId, QueryRequest content) + throws IOException { + validateRPC(); + return bigquery.jobs().query(projectId, content).execute(); + } + @Override public String open(Job loadJob) { try { - String builder = options.getResolvedApiaryHost("bigquery"); - if (!builder.endsWith("/")) { - builder += "/"; - } - builder += BASE_RESUMABLE_URI + options.getProjectId() + "/jobs"; - GenericUrl url = new GenericUrl(builder); - url.set("uploadType", "resumable"); - JsonFactory jsonFactory = bigquery.getJsonFactory(); - HttpRequestFactory requestFactory = bigquery.getRequestFactory(); - HttpRequest httpRequest = - requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob)); - httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream"); - HttpResponse response = httpRequest.execute(); - return response.getHeaders().getLocation(); + return openSkipExceptionTranslation(loadJob); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public String openSkipExceptionTranslation(Job loadJob) throws IOException { + String builder = options.getResolvedApiaryHost("bigquery"); + if (!builder.endsWith("/")) { + builder += "/"; + } + builder += BASE_RESUMABLE_URI + options.getProjectId() + "/jobs"; + GenericUrl url = new GenericUrl(builder); + url.set("uploadType", "resumable"); + JsonFactory jsonFactory = bigquery.getJsonFactory(); + HttpRequestFactory requestFactory = bigquery.getRequestFactory(); + HttpRequest httpRequest = + requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob)); + httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream"); + HttpResponse response = httpRequest.execute(); + return response.getHeaders().getLocation(); + } + @Override public Job write( String uploadId, @@ -824,101 +1024,126 @@ public Job write( int length, boolean last) { try { - if (length == 0) { - return null; - } - GenericUrl url = new GenericUrl(uploadId); - HttpRequest httpRequest = - bigquery - .getRequestFactory() - .buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length)); - httpRequest.setParser(bigquery.getObjectParser()); - long limit = destOffset + length; - StringBuilder range = new StringBuilder("bytes "); - range.append(destOffset).append('-').append(limit - 1).append('/'); - if (last) { - range.append(limit); - } else { - range.append('*'); - } - httpRequest.getHeaders().setContentRange(range.toString()); - int code; - String message; - IOException exception = null; - HttpResponse response = null; - try { - response = httpRequest.execute(); - code = response.getStatusCode(); - message = response.getStatusMessage(); - } catch (HttpResponseException ex) { - exception = ex; - code = ex.getStatusCode(); - message = ex.getStatusMessage(); - } - if (!last && code != HTTP_RESUME_INCOMPLETE - || last && !(code == HTTP_OK || code == HTTP_CREATED)) { - if (exception != null) { - throw exception; - } - throw new BigQueryException(code, message); - } - return last && response != null ? response.parseAs(Job.class) : null; + return writeSkipExceptionTranslation( + uploadId, toWrite, toWriteOffset, destOffset, length, last); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Job writeSkipExceptionTranslation( + String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) + throws IOException { + if (length == 0) { + return null; + } + GenericUrl url = new GenericUrl(uploadId); + HttpRequest httpRequest = + bigquery + .getRequestFactory() + .buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length)); + httpRequest.setParser(bigquery.getObjectParser()); + long limit = destOffset + length; + StringBuilder range = new StringBuilder("bytes "); + range.append(destOffset).append('-').append(limit - 1).append('/'); + if (last) { + range.append(limit); + } else { + range.append('*'); + } + httpRequest.getHeaders().setContentRange(range.toString()); + int code; + String message; + IOException exception = null; + HttpResponse response = null; + try { + response = httpRequest.execute(); + code = response.getStatusCode(); + message = response.getStatusMessage(); + } catch (HttpResponseException ex) { + exception = ex; + code = ex.getStatusCode(); + message = ex.getStatusMessage(); + } + if (!last && code != HTTP_RESUME_INCOMPLETE + || last && !(code == HTTP_OK || code == HTTP_CREATED)) { + if (exception != null) { + throw exception; + } + throw new BigQueryException(code, message); + } + return last && response != null ? response.parseAs(Job.class) : null; + } + @Override public Policy getIamPolicy(String resourceId, Map options) { try { - validateRPC(); - GetIamPolicyRequest policyRequest = new GetIamPolicyRequest(); - if (null != Option.REQUESTED_POLICY_VERSION.getLong(options)) { - policyRequest = - policyRequest.setOptions( - new GetPolicyOptions() - .setRequestedPolicyVersion( - Option.REQUESTED_POLICY_VERSION.getLong(options).intValue())); - } - return bigquery - .tables() - .getIamPolicy(resourceId, policyRequest) - .setPrettyPrint(false) - .execute(); + return getIamPolicySkipExceptionTranslation(resourceId, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Policy getIamPolicySkipExceptionTranslation(String resourceId, Map options) + throws IOException { + validateRPC(); + GetIamPolicyRequest policyRequest = new GetIamPolicyRequest(); + if (null != Option.REQUESTED_POLICY_VERSION.getLong(options)) { + policyRequest = + policyRequest.setOptions( + new GetPolicyOptions() + .setRequestedPolicyVersion( + Option.REQUESTED_POLICY_VERSION.getLong(options).intValue())); + } + return bigquery + .tables() + .getIamPolicy(resourceId, policyRequest) + .setPrettyPrint(false) + .execute(); + } + @Override public Policy setIamPolicy(String resourceId, Policy policy, Map options) { try { - validateRPC(); - SetIamPolicyRequest policyRequest = new SetIamPolicyRequest().setPolicy(policy); - return bigquery - .tables() - .setIamPolicy(resourceId, policyRequest) - .setPrettyPrint(false) - .execute(); + return setIamPolicySkipExceptionTranslation(resourceId, policy, options); } catch (IOException ex) { throw translate(ex); } } + @InternalApi("internal to java-bigquery") + public Policy setIamPolicySkipExceptionTranslation( + String resourceId, Policy policy, Map options) throws IOException { + validateRPC(); + SetIamPolicyRequest policyRequest = new SetIamPolicyRequest().setPolicy(policy); + return bigquery + .tables() + .setIamPolicy(resourceId, policyRequest) + .setPrettyPrint(false) + .execute(); + } + @Override public TestIamPermissionsResponse testIamPermissions( String resourceId, List permissions, Map options) { try { - validateRPC(); - TestIamPermissionsRequest permissionsRequest = - new TestIamPermissionsRequest().setPermissions(permissions); - return bigquery - .tables() - .testIamPermissions(resourceId, permissionsRequest) - .setPrettyPrint(false) - .execute(); + return testIamPermissionsSkipExceptionTranslation(resourceId, permissions, options); } catch (IOException ex) { throw translate(ex); } } + + public TestIamPermissionsResponse testIamPermissionsSkipExceptionTranslation( + String resourceId, List permissions, Map options) throws IOException { + validateRPC(); + TestIamPermissionsRequest permissionsRequest = + new TestIamPermissionsRequest().setPermissions(permissions); + return bigquery + .tables() + .testIamPermissions(resourceId, permissionsRequest) + .setPrettyPrint(false) + .execute(); + } } 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 c13d272d2..1fa748199 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 @@ -36,11 +36,14 @@ import com.google.cloud.bigquery.InsertAllRequest.RowToInsert; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; +import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import com.google.common.base.Function; import com.google.common.base.Supplier; import com.google.common.collect.*; import java.io.IOException; import java.math.BigInteger; +import java.net.ConnectException; +import java.net.UnknownHostException; import java.util.Collections; import java.util.List; import java.util.Map; @@ -50,6 +53,7 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) @@ -497,7 +501,7 @@ public class BigQueryImplTest { .build(); private BigQueryOptions options; private BigQueryRpcFactory rpcFactoryMock; - private BigQueryRpc bigqueryRpcMock; + private HttpBigQueryRpc bigqueryRpcMock; private BigQuery bigquery; private static final String RATE_LIMIT_ERROR_MSG = "Job exceeded rate limits: Your table exceeded quota for table update operations. For more information, see https://cloud.google.com/bigquery/docs/troubleshoot-quotas"; @@ -533,7 +537,7 @@ private BigQueryOptions createBigQueryOptionsForProjectWithLocation( @Before public void setUp() { rpcFactoryMock = mock(BigQueryRpcFactory.class); - bigqueryRpcMock = mock(BigQueryRpc.class); + bigqueryRpcMock = mock(HttpBigQueryRpc.class); when(rpcFactoryMock.create(any(BigQueryOptions.class))).thenReturn(bigqueryRpcMock); options = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); } @@ -545,21 +549,22 @@ public void testGetOptions() { } @Test - public void testCreateDataset() { + public void testCreateDataset() throws IOException { DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.create(datasetInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.createSkipExceptionTranslation(datasetInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(datasetInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Dataset dataset = bigquery.create(datasetInfo); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); - verify(bigqueryRpcMock).create(datasetInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).createSkipExceptionTranslation(datasetInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testCreateDatasetWithSelectedFields() { - when(bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), capturedOptions.capture())) + public void testCreateDatasetWithSelectedFields() throws IOException { + when(bigqueryRpcMock.createSkipExceptionTranslation( + eq(DATASET_INFO_WITH_PROJECT.toPb()), capturedOptions.capture())) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.create(DATASET_INFO, DATASET_OPTION_FIELDS); @@ -570,50 +575,54 @@ public void testCreateDatasetWithSelectedFields() { assertEquals(28, selector.length()); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock).create(eq(DATASET_INFO_WITH_PROJECT.toPb()), capturedOptions.capture()); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation( + eq(DATASET_INFO_WITH_PROJECT.toPb()), capturedOptions.capture()); } @Test - public void testCreateDatasetWithAccessPolicy() { + public void testCreateDatasetWithAccessPolicy() throws IOException { DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); DatasetOption datasetOption = DatasetOption.accessPolicyVersion(3); - when(bigqueryRpcMock.create(datasetInfo.toPb(), optionMap(datasetOption))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + datasetInfo.toPb(), optionMap(datasetOption))) .thenReturn(datasetInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Dataset dataset = bigquery.create(datasetInfo, datasetOption); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); - verify(bigqueryRpcMock).create(datasetInfo.toPb(), optionMap(datasetOption)); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation(datasetInfo.toPb(), optionMap(datasetOption)); } @Test - public void testGetDataset() { - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + public void testGetDataset() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DATASET); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testGetDatasetNotFoundWhenThrowIsDisabled() { - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + public void testGetDatasetNotFoundWhenThrowIsDisabled() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); options.setThrowNotFound(false); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DATASET); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testGetDatasetNotFoundWhenThrowIsEnabled() { - when(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) - .thenReturn(null) + public void testGetDatasetNotFoundWhenThrowIsEnabled() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation( + PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(404, "Dataset not found")); options.setThrowNotFound(true); bigquery = options.getService(); @@ -623,35 +632,39 @@ public void testGetDatasetNotFoundWhenThrowIsEnabled() { } catch (BigQueryException ex) { Assert.assertNotNull(ex.getMessage()); } - verify(bigqueryRpcMock).getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getDatasetSkipExceptionTranslation(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS); } @Test - public void testGetDatasetFromDatasetId() { - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + public void testGetDatasetFromDatasetId() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DatasetId.of(DATASET)); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testGetDatasetFromDatasetIdWithProject() { + public void testGetDatasetFromDatasetIdWithProject() throws IOException { DatasetInfo datasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT); DatasetId datasetId = DatasetId.of(OTHER_PROJECT, DATASET); - when(bigqueryRpcMock.getDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation( + OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenReturn(datasetInfo.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(datasetId); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetInfo)), dataset); - verify(bigqueryRpcMock).getDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getDatasetSkipExceptionTranslation(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testGetDatasetWithSelectedFields() { - when(bigqueryRpcMock.getDataset(eq(PROJECT), eq(DATASET), capturedOptions.capture())) + public void testGetDatasetWithSelectedFields() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation( + eq(PROJECT), eq(DATASET), capturedOptions.capture())) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DATASET, DATASET_OPTION_FIELDS); @@ -662,11 +675,12 @@ public void testGetDatasetWithSelectedFields() { assertEquals(28, selector.length()); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock).getDataset(eq(PROJECT), eq(DATASET), capturedOptions.capture()); + verify(bigqueryRpcMock) + .getDatasetSkipExceptionTranslation(eq(PROJECT), eq(DATASET), capturedOptions.capture()); } @Test - public void testListDatasets() { + public void testListDatasets() throws IOException { bigquery = options.getService(); ImmutableList datasetList = ImmutableList.of( @@ -674,16 +688,17 @@ public void testListDatasets() { new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listDatasets(PROJECT, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listDatasetsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listDatasets(); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals( datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); - verify(bigqueryRpcMock).listDatasets(PROJECT, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listDatasetsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS); } @Test - public void testListDatasetsWithProjects() { + public void testListDatasetsWithProjects() throws IOException { bigquery = options.getService(); ImmutableList datasetList = ImmutableList.of( @@ -691,30 +706,32 @@ public void testListDatasetsWithProjects() { bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO.setProjectId(OTHER_PROJECT)))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listDatasets(OTHER_PROJECT, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listDatasetsSkipExceptionTranslation(OTHER_PROJECT, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listDatasets(OTHER_PROJECT); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals( datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); - verify(bigqueryRpcMock).listDatasets(OTHER_PROJECT, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listDatasetsSkipExceptionTranslation(OTHER_PROJECT, EMPTY_RPC_OPTIONS); } @Test - public void testListEmptyDatasets() { + public void testListEmptyDatasets() throws IOException { ImmutableList datasets = ImmutableList.of(); Tuple> result = Tuple.>of(null, datasets); - when(bigqueryRpcMock.listDatasets(PROJECT, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listDatasetsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS)) + .thenReturn(result); bigquery = options.getService(); Page page = bigquery.listDatasets(); assertNull(page.getNextPageToken()); assertArrayEquals( ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Dataset.class)); - verify(bigqueryRpcMock).listDatasets(PROJECT, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listDatasetsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS); } @Test - public void testListDatasetsWithOptions() { + public void testListDatasetsWithOptions() throws IOException { bigquery = options.getService(); ImmutableList datasetList = ImmutableList.of( @@ -722,71 +739,85 @@ public void testListDatasetsWithOptions() { new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listDatasets(PROJECT, DATASET_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listDatasetsSkipExceptionTranslation(PROJECT, DATASET_LIST_OPTIONS)) + .thenReturn(result); Page page = bigquery.listDatasets(DATASET_LIST_ALL, DATASET_LIST_PAGE_TOKEN, DATASET_LIST_PAGE_SIZE); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals( datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); - verify(bigqueryRpcMock).listDatasets(PROJECT, DATASET_LIST_OPTIONS); + verify(bigqueryRpcMock).listDatasetsSkipExceptionTranslation(PROJECT, DATASET_LIST_OPTIONS); } @Test - public void testDeleteDataset() { - when(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(true); + public void testDeleteDataset() throws IOException { + when(bigqueryRpcMock.deleteDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(DATASET)); - verify(bigqueryRpcMock).deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .deleteDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testDeleteDatasetFromDatasetId() { - when(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(true); + public void testDeleteDatasetFromDatasetId() throws IOException { + when(bigqueryRpcMock.deleteDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(DatasetId.of(DATASET))); - verify(bigqueryRpcMock).deleteDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .deleteDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testDeleteDatasetFromDatasetIdWithProject() { + public void testDeleteDatasetFromDatasetIdWithProject() throws IOException { DatasetId datasetId = DatasetId.of(OTHER_PROJECT, DATASET); - when(bigqueryRpcMock.deleteDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(true); + when(bigqueryRpcMock.deleteDatasetSkipExceptionTranslation( + OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(datasetId)); - verify(bigqueryRpcMock).deleteDataset(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .deleteDatasetSkipExceptionTranslation(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testDeleteDatasetWithOptions() { - when(bigqueryRpcMock.deleteDataset(PROJECT, DATASET, DATASET_DELETE_OPTIONS)).thenReturn(true); + public void testDeleteDatasetWithOptions() throws IOException { + when(bigqueryRpcMock.deleteDatasetSkipExceptionTranslation( + PROJECT, DATASET, DATASET_DELETE_OPTIONS)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(DATASET, DATASET_DELETE_CONTENTS)); - verify(bigqueryRpcMock).deleteDataset(PROJECT, DATASET, DATASET_DELETE_OPTIONS); + verify(bigqueryRpcMock) + .deleteDatasetSkipExceptionTranslation(PROJECT, DATASET, DATASET_DELETE_OPTIONS); } @Test - public void testUpdateDataset() { + public void testUpdateDataset() throws IOException { DatasetInfo updatedDatasetInfo = DATASET_INFO .setProjectId(OTHER_PROJECT) .toBuilder() .setDescription("newDescription") .build(); - when(bigqueryRpcMock.patch(updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.patchSkipExceptionTranslation( + updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(updatedDatasetInfo.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.update(updatedDatasetInfo); assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfo)), dataset); - verify(bigqueryRpcMock).patch(updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .patchSkipExceptionTranslation(updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testUpdateDatasetWithSelectedFields() { + public void testUpdateDatasetWithSelectedFields() throws IOException { DatasetInfo updatedDatasetInfo = DATASET_INFO.toBuilder().setDescription("newDescription").build(); DatasetInfo updatedDatasetInfoWithProject = DATASET_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); - when(bigqueryRpcMock.patch(eq(updatedDatasetInfoWithProject.toPb()), capturedOptions.capture())) + when(bigqueryRpcMock.patchSkipExceptionTranslation( + eq(updatedDatasetInfoWithProject.toPb()), capturedOptions.capture())) .thenReturn(updatedDatasetInfoWithProject.toPb()); bigquery = options.getService(); Dataset dataset = bigquery.update(updatedDatasetInfo, DATASET_OPTION_FIELDS); @@ -798,23 +829,25 @@ public void testUpdateDatasetWithSelectedFields() { assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfoWithProject)), dataset); verify(bigqueryRpcMock) - .patch(eq(updatedDatasetInfoWithProject.toPb()), capturedOptions.capture()); + .patchSkipExceptionTranslation( + eq(updatedDatasetInfoWithProject.toPb()), capturedOptions.capture()); } @Test - public void testCreateTable() { + public void testCreateTable() throws IOException { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)).thenReturn(tableInfo.toPb()); + when(bigqueryRpcMock.createSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(tableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); - verify(bigqueryRpcMock).create(tableInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).createSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void tesCreateExternalTable() { + public void tesCreateExternalTable() throws IOException { TableInfo createTableInfo = TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build()) .setProjectId(OTHER_PROJECT); @@ -822,32 +855,34 @@ public void tesCreateExternalTable() { com.google.api.services.bigquery.model.Table expectedCreateInput = createTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb()); expectedCreateInput.getExternalDataConfiguration().setSchema(null); - when(bigqueryRpcMock.create(expectedCreateInput, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.createSkipExceptionTranslation(expectedCreateInput, EMPTY_RPC_OPTIONS)) .thenReturn(createTableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(createTableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(createTableInfo)), table); - verify(bigqueryRpcMock).create(expectedCreateInput, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).createSkipExceptionTranslation(expectedCreateInput, EMPTY_RPC_OPTIONS); } @Test - public void testCreateTableWithoutProject() { + public void testCreateTableWithoutProject() throws IOException { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); tableInfo.toBuilder().setTableId(tableId); - when(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)).thenReturn(tableInfo.toPb()); + when(bigqueryRpcMock.createSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(tableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); - verify(bigqueryRpcMock).create(tableInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).createSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testCreateTableWithSelectedFields() { - when(bigqueryRpcMock.create(eq(TABLE_INFO_WITH_PROJECT.toPb()), capturedOptions.capture())) + public void testCreateTableWithSelectedFields() throws IOException { + when(bigqueryRpcMock.createSkipExceptionTranslation( + eq(TABLE_INFO_WITH_PROJECT.toPb()), capturedOptions.capture())) .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Table table = bigquery.create(TABLE_INFO, TABLE_OPTION_FIELDS); @@ -857,34 +892,40 @@ public void testCreateTableWithSelectedFields() { assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); - verify(bigqueryRpcMock).create(eq(TABLE_INFO_WITH_PROJECT.toPb()), capturedOptions.capture()); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation( + eq(TABLE_INFO_WITH_PROJECT.toPb()), capturedOptions.capture()); } @Test - public void testGetTable() { - when(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + public void testGetTable() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Table table = bigquery.getTable(DATASET, TABLE); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); - verify(bigqueryRpcMock).getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetModel() { - when(bigqueryRpcMock.getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS)) + public void testGetModel() throws IOException { + when(bigqueryRpcMock.getModelSkipExceptionTranslation( + PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS)) .thenReturn(MODEL_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Model model = bigquery.getModel(DATASET, MODEL); assertEquals(new Model(bigquery, new ModelInfo.BuilderImpl(MODEL_INFO_WITH_PROJECT)), model); - verify(bigqueryRpcMock).getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getModelSkipExceptionTranslation(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); } @Test - public void testGetModelNotFoundWhenThrowIsEnabled() { + public void testGetModelNotFoundWhenThrowIsEnabled() throws IOException { String expected = "Model not found"; - when(bigqueryRpcMock.getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS)) - .thenReturn(null) + when(bigqueryRpcMock.getModelSkipExceptionTranslation( + PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(404, expected)); options.setThrowNotFound(true); bigquery = options.getService(); @@ -893,39 +934,45 @@ public void testGetModelNotFoundWhenThrowIsEnabled() { } catch (BigQueryException ex) { assertEquals(expected, ex.getMessage()); } - verify(bigqueryRpcMock).getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getModelSkipExceptionTranslation(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); } @Test - public void testListPartition() { - when(bigqueryRpcMock.getTable( + public void testListPartition() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( PROJECT, DATASET, "table$__PARTITIONS_SUMMARY__", EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_INFO_PARTITIONS.toPb()); - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_DATA_WITH_PARTITIONS); bigquery = options.getService(); List partition = bigquery.listPartitions(TABLE_ID_WITH_PROJECT); assertEquals(3, partition.size()); verify(bigqueryRpcMock) - .getTable(PROJECT, DATASET, "table$__PARTITIONS_SUMMARY__", EMPTY_RPC_OPTIONS); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + .getTableSkipExceptionTranslation( + PROJECT, DATASET, "table$__PARTITIONS_SUMMARY__", EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetTableNotFoundWhenThrowIsDisabled() { - when(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + public void testGetTableNotFoundWhenThrowIsDisabled() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); options.setThrowNotFound(false); bigquery = options.getService(); Table table = bigquery.getTable(DATASET, TABLE); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); - verify(bigqueryRpcMock).getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetTableNotFoundWhenThrowIsEnabled() { - when(bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS)) - .thenReturn(null) + public void testGetTableNotFoundWhenThrowIsEnabled() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(404, "Table not found")); options.setThrowNotFound(true); bigquery = options.getService(); @@ -935,49 +982,57 @@ public void testGetTableNotFoundWhenThrowIsEnabled() { } catch (BigQueryException ex) { Assert.assertNotNull(ex.getMessage()); } - verify(bigqueryRpcMock).getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS); } @Test - public void testGetTableFromTableId() { - when(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + public void testGetTableFromTableId() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); - verify(bigqueryRpcMock).getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetTableFromTableIdWithProject() { + public void testGetTableFromTableIdWithProject() throws IOException { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.getTable(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(tableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); - verify(bigqueryRpcMock).getTable(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetTableFromTableIdWithoutProject() { + public void testGetTableFromTableIdWithoutProject() throws IOException { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); - when(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(tableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); - verify(bigqueryRpcMock).getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getTableSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testGetTableWithSelectedFields() { - when(bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capturedOptions.capture())) + public void testGetTableWithSelectedFields() throws IOException { + when(bigqueryRpcMock.getTableSkipExceptionTranslation( + eq(PROJECT), eq(DATASET), eq(TABLE), capturedOptions.capture())) .thenReturn(TABLE_INFO_WITH_PROJECT.toPb()); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID, TABLE_OPTION_FIELDS); @@ -988,11 +1043,12 @@ public void testGetTableWithSelectedFields() { assertEquals(31, selector.length()); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); verify(bigqueryRpcMock) - .getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capturedOptions.capture()); + .getTableSkipExceptionTranslation( + eq(PROJECT), eq(DATASET), eq(TABLE), capturedOptions.capture()); } @Test - public void testListTables() { + public void testListTables() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( @@ -1001,60 +1057,67 @@ public void testListTables() { new Table(bigquery, new TableInfo.BuilderImpl(MODEL_TABLE_INFO_WITH_PROJECT))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DATASET); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listTablesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testListTablesReturnedParameters() { + public void testListTablesReturnedParameters() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PARTITIONS))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS); } @Test - public void testListTablesReturnedParametersNullType() { + public void testListTablesReturnedParametersNullType() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PARTITIONS_NULL_TYPE))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS); } @Test - public void testListTablesWithRangePartitioning() { + public void testListTablesWithRangePartitioning() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_RANGE_PARTITIONING))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS); } @Test - public void testListTablesFromDatasetId() { + public void testListTablesFromDatasetId() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( @@ -1062,30 +1125,34 @@ public void testListTablesFromDatasetId() { new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DatasetId.of(DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listTablesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testListTablesFromDatasetIdWithProject() { + public void testListTablesFromDatasetIdWithProject() throws IOException { bigquery = options.getService(); 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)); - when(bigqueryRpcMock.listTables(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation( + OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DatasetId.of(OTHER_PROJECT, DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listTablesSkipExceptionTranslation(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testListTablesWithOptions() { + public void testListTablesWithOptions() throws IOException { bigquery = options.getService(); ImmutableList
tableList = ImmutableList.of( @@ -1093,15 +1160,17 @@ public void testListTablesWithOptions() { new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS)) + .thenReturn(result); Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); - verify(bigqueryRpcMock).listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTablesSkipExceptionTranslation(PROJECT, DATASET, TABLE_LIST_OPTIONS); } @Test - public void testListModels() { + public void testListModels() throws IOException { bigquery = options.getService(); ImmutableList modelList = ImmutableList.of( @@ -1109,15 +1178,16 @@ public void testListModels() { new Model(bigquery, new ModelInfo.BuilderImpl(OTHER_MODEL_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(modelList, ModelInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listModelsSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listModels(DATASET); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(modelList.toArray(), Iterables.toArray(page.getValues(), Model.class)); - verify(bigqueryRpcMock).listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listModelsSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testListModelsWithModelId() { + public void testListModelsWithModelId() throws IOException { bigquery = options.getService(); ImmutableList modelList = ImmutableList.of( @@ -1125,92 +1195,100 @@ public void testListModelsWithModelId() { new Model(bigquery, new ModelInfo.BuilderImpl(OTHER_MODEL_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(modelList, ModelInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listModelsSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listModels(DatasetId.of(DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(modelList.toArray(), Iterables.toArray(page.getValues(), Model.class)); - verify(bigqueryRpcMock).listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listModelsSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testDeleteTable() { - when(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).thenReturn(true); + public void testDeleteTable() throws IOException { + when(bigqueryRpcMock.deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(TABLE_ID)); - verify(bigqueryRpcMock).deleteTable(PROJECT, DATASET, TABLE); + verify(bigqueryRpcMock).deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE); } @Test - public void testDeleteTableFromTableId() { - when(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).thenReturn(true); + public void testDeleteTableFromTableId() throws IOException { + when(bigqueryRpcMock.deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(TABLE_ID)); - verify(bigqueryRpcMock).deleteTable(PROJECT, DATASET, TABLE); + verify(bigqueryRpcMock).deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE); } @Test - public void testDeleteTableFromTableIdWithProject() { + public void testDeleteTableFromTableIdWithProject() throws IOException { TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.deleteTable(OTHER_PROJECT, DATASET, TABLE)).thenReturn(true); + when(bigqueryRpcMock.deleteTableSkipExceptionTranslation(OTHER_PROJECT, DATASET, TABLE)) + .thenReturn(true); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); assertTrue(bigquery.delete(tableId)); - verify(bigqueryRpcMock).deleteTable(OTHER_PROJECT, DATASET, TABLE); + verify(bigqueryRpcMock).deleteTableSkipExceptionTranslation(OTHER_PROJECT, DATASET, TABLE); } @Test - public void testDeleteTableFromTableIdWithoutProject() { + public void testDeleteTableFromTableIdWithoutProject() throws IOException { TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); - when(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).thenReturn(true); + when(bigqueryRpcMock.deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE)) + .thenReturn(true); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); assertTrue(bigquery.delete(tableId)); - verify(bigqueryRpcMock).deleteTable(PROJECT, DATASET, TABLE); + verify(bigqueryRpcMock).deleteTableSkipExceptionTranslation(PROJECT, DATASET, TABLE); } @Test - public void testDeleteModel() { - when(bigqueryRpcMock.deleteModel(PROJECT, DATASET, MODEL)).thenReturn(true); + public void testDeleteModel() throws IOException { + when(bigqueryRpcMock.deleteModelSkipExceptionTranslation(PROJECT, DATASET, MODEL)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(ModelId.of(DATASET, MODEL))); - verify(bigqueryRpcMock).deleteModel(PROJECT, DATASET, MODEL); + verify(bigqueryRpcMock).deleteModelSkipExceptionTranslation(PROJECT, DATASET, MODEL); } @Test - public void testUpdateModel() { + public void testUpdateModel() throws IOException { ModelInfo updateModelInfo = MODEL_INFO_WITH_PROJECT .setProjectId(OTHER_PROJECT) .toBuilder() .setDescription("newDescription") .build(); - when(bigqueryRpcMock.patch(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.patchSkipExceptionTranslation(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(updateModelInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Model actualModel = bigquery.update(updateModelInfo); assertEquals(new Model(bigquery, new ModelInfo.BuilderImpl(updateModelInfo)), actualModel); - verify(bigqueryRpcMock).patch(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .patchSkipExceptionTranslation(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testUpdateTable() { + public void testUpdateTable() throws IOException { TableInfo updatedTableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT).toBuilder().setDescription("newDescription").build(); - when(bigqueryRpcMock.patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.patchSkipExceptionTranslation(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(updatedTableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.update(updatedTableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table); - verify(bigqueryRpcMock).patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .patchSkipExceptionTranslation(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testUpdateExternalTableWithNewSchema() { + public void testUpdateExternalTableWithNewSchema() throws IOException { TableInfo updatedTableInfo = TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build()) .setProjectId(OTHER_PROJECT); @@ -1218,35 +1296,37 @@ public void testUpdateExternalTableWithNewSchema() { com.google.api.services.bigquery.model.Table expectedPatchInput = updatedTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb()); expectedPatchInput.getExternalDataConfiguration().setSchema(null); - when(bigqueryRpcMock.patch(expectedPatchInput, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.patchSkipExceptionTranslation(expectedPatchInput, EMPTY_RPC_OPTIONS)) .thenReturn(updatedTableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.update(updatedTableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table); - verify(bigqueryRpcMock).patch(expectedPatchInput, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).patchSkipExceptionTranslation(expectedPatchInput, EMPTY_RPC_OPTIONS); } @Test - public void testUpdateTableWithoutProject() { + public void testUpdateTableWithoutProject() throws IOException { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); tableInfo.toBuilder().setTableId(tableId); - when(bigqueryRpcMock.patch(tableInfo.toPb(), EMPTY_RPC_OPTIONS)).thenReturn(tableInfo.toPb()); + when(bigqueryRpcMock.patchSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(tableInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.update(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); - verify(bigqueryRpcMock).patch(tableInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).patchSkipExceptionTranslation(tableInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testUpdateTableWithSelectedFields() { + public void testUpdateTableWithSelectedFields() throws IOException { TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build(); TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); - when(bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture())) + when(bigqueryRpcMock.patchSkipExceptionTranslation( + eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture())) .thenReturn(updatedTableInfoWithProject.toPb()); bigquery = options.getService(); Table table = bigquery.update(updatedTableInfo, TABLE_OPTION_FIELDS); @@ -1258,15 +1338,17 @@ public void testUpdateTableWithSelectedFields() { assertEquals( new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table); verify(bigqueryRpcMock) - .patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()); + .patchSkipExceptionTranslation( + eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()); } @Test - public void testUpdateTableWithAutoDetectSchema() { + public void testUpdateTableWithAutoDetectSchema() throws IOException { TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build(); TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); - when(bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture())) + when(bigqueryRpcMock.patchSkipExceptionTranslation( + eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture())) .thenReturn(updatedTableInfoWithProject.toPb()); bigquery = options.getService(); Table table = bigquery.update(updatedTableInfo, BigQuery.TableOption.autodetectSchema(true)); @@ -1276,11 +1358,12 @@ public void testUpdateTableWithAutoDetectSchema() { assertEquals( new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table); verify(bigqueryRpcMock) - .patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()); + .patchSkipExceptionTranslation( + eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()); } @Test - public void testInsertAllWithRowIdShouldRetry() { + public void testInsertAllWithRowIdShouldRetry() throws IOException { Map row1 = ImmutableMap.of("field", "value1"); Map row2 = ImmutableMap.of("field", "value2"); List rows = @@ -1315,7 +1398,7 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { new TableDataInsertAllResponse.InsertErrors() .setIndex(0L) .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); - when(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb)) + when(bigqueryRpcMock.insertAllSkipExceptionTranslation(PROJECT, DATASET, TABLE, requestPb)) .thenThrow(new BigQueryException(500, "InternalError")) .thenReturn(responsePb); bigquery = @@ -1329,7 +1412,8 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { assertNull(response.getErrorsFor(1L)); assertEquals(1, response.getErrorsFor(0L).size()); assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage()); - verify(bigqueryRpcMock, times(2)).insertAll(PROJECT, DATASET, TABLE, requestPb); + verify(bigqueryRpcMock, times(2)) + .insertAllSkipExceptionTranslation(PROJECT, DATASET, TABLE, requestPb); } @Test @@ -1378,7 +1462,7 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { } @Test - public void testInsertAllWithProject() { + public void testInsertAllWithProject() throws IOException { Map row1 = ImmutableMap.of("field", "value1"); Map row2 = ImmutableMap.of("field", "value2"); List rows = @@ -1414,7 +1498,8 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { new TableDataInsertAllResponse.InsertErrors() .setIndex(0L) .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); - when(bigqueryRpcMock.insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb)) + when(bigqueryRpcMock.insertAllSkipExceptionTranslation( + OTHER_PROJECT, DATASET, TABLE, requestPb)) .thenReturn(responsePb); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); @@ -1424,11 +1509,12 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { assertNull(response.getErrorsFor(1L)); assertEquals(1, response.getErrorsFor(0L).size()); assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage()); - verify(bigqueryRpcMock).insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb); + verify(bigqueryRpcMock) + .insertAllSkipExceptionTranslation(OTHER_PROJECT, DATASET, TABLE, requestPb); } @Test - public void testInsertAllWithProjectInTable() { + public void testInsertAllWithProjectInTable() throws IOException { Map row1 = ImmutableMap.of("field", "value1"); Map row2 = ImmutableMap.of("field", "value2"); List rows = @@ -1464,7 +1550,8 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { new TableDataInsertAllResponse.InsertErrors() .setIndex(0L) .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); - when(bigqueryRpcMock.insertAll("project-different-from-option", DATASET, TABLE, requestPb)) + when(bigqueryRpcMock.insertAllSkipExceptionTranslation( + "project-different-from-option", DATASET, TABLE, requestPb)) .thenReturn(responsePb); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); @@ -1474,47 +1561,56 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { assertNull(response.getErrorsFor(1L)); assertEquals(1, response.getErrorsFor(0L).size()); assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage()); - verify(bigqueryRpcMock).insertAll("project-different-from-option", DATASET, TABLE, requestPb); + verify(bigqueryRpcMock) + .insertAllSkipExceptionTranslation( + "project-different-from-option", DATASET, TABLE, requestPb); } @Test - public void testListTableData() { - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + public void testListTableData() throws IOException { + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_DATA_PB); bigquery = options.getService(); Page page = bigquery.listTableData(DATASET, TABLE); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testListTableDataFromTableId() { - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + public void testListTableDataFromTableId() throws IOException { + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_DATA_PB); bigquery = options.getService(); Page page = bigquery.listTableData(TableId.of(DATASET, TABLE)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testListTableDataFromTableIdWithProject() { + public void testListTableDataFromTableIdWithProject() throws IOException { TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.listTableData(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(TABLE_DATA_PB); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Page page = bigquery.listTableData(tableId); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); - verify(bigqueryRpcMock).listTableData(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testListTableDataWithOptions() { - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS)) + public void testListTableDataWithOptions() throws IOException { + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS)) .thenReturn(TABLE_DATA_PB); bigquery = options.getService(); Page page = @@ -1526,14 +1622,15 @@ public void testListTableDataWithOptions() { TABLE_DATA_LIST_START_INDEX); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); } @Test - public void testListTableDataWithNextPage() { + public void testListTableDataWithNextPage() throws IOException { doReturn(TABLE_DATA_PB) .when(bigqueryRpcMock) - .listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); bigquery = options.getService(); TableResult page = bigquery.listTableData( @@ -1543,7 +1640,8 @@ public void testListTableDataWithNextPage() { TABLE_DATA_LIST_PAGE_TOKEN, TABLE_DATA_LIST_START_INDEX); assertEquals(CURSOR, page.getNextPageToken()); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); Map SECOND_TABLE_DATA_LIST_OPTIONS = ImmutableMap.of(BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.START_INDEX, 0L); @@ -1556,11 +1654,14 @@ public void testListTableDataWithNextPage() { new TableRow().setF(ImmutableList.of(new TableCell().setV("Value3"))), new TableRow().setF(ImmutableList.of(new TableCell().setV("Value4")))))) .when(bigqueryRpcMock) - .listTableData(PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS); + .listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS); assertTrue(page.hasNextPage()); page = page.getNextPage(); assertNull(page.getNextPageToken()); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS); } // The "minimally initialized" Job that lets Job.fromPb run without throwing. @@ -1572,23 +1673,47 @@ private static com.google.api.services.bigquery.model.Job newJobPb() { } @Test - public void testCreateJobSuccess() { + public void testCreateJobSuccess() throws IOException { String id = "testCreateJobSuccess-id"; JobId jobId = JobId.of(id); String query = "SELECT * in FOO"; - when(bigqueryRpcMock.create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) .thenReturn(newJobPb()); bigquery = options.getService(); assertThat(bigquery.create(JobInfo.of(jobId, QueryJobConfiguration.of(query)))).isNotNull(); assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); - verify(bigqueryRpcMock).create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); } @Test - public void testCreateJobFailureShouldRetry() { - when(bigqueryRpcMock.create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + public void testCreateJobFailureShouldRetryExceptionHandlerExceptions() throws IOException { + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + .thenThrow(new UnknownHostException()) + .thenThrow(new ConnectException()) + .thenReturn(newJobPb()); + + bigquery = options.getService(); + bigquery = + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); + + ((BigQueryImpl) bigquery).create(JobInfo.of(QUERY_JOB_CONFIGURATION_FOR_DMLQUERY)); + verify(bigqueryRpcMock, times(3)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + } + + @Test + public void testCreateJobFailureShouldRetry() throws IOException { + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -1607,11 +1732,12 @@ public void testCreateJobFailureShouldRetry() { .getService(); ((BigQueryImpl) bigquery).create(JobInfo.of(QUERY_JOB_CONFIGURATION_FOR_DMLQUERY)); - verify(bigqueryRpcMock, times(6)).create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + verify(bigqueryRpcMock, times(6)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); } @Test - public void testCreateJobWithBigQueryRetryConfigFailureShouldRetry() { + public void testCreateJobWithBigQueryRetryConfigFailureShouldRetry() throws IOException { // Validate create job with BigQueryRetryConfig that retries on rate limit error message. JobOption bigQueryRetryConfigOption = JobOption.bigQueryRetryConfig( @@ -1622,7 +1748,8 @@ public void testCreateJobWithBigQueryRetryConfigFailureShouldRetry() { .build()); Map bigQueryRpcOptions = optionMap(bigQueryRetryConfigOption); - when(bigqueryRpcMock.create(jobCapture.capture(), eq(bigQueryRpcOptions))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(bigQueryRpcOptions))) .thenThrow( new BigQueryException( 400, RATE_LIMIT_ERROR_MSG)) // retrial on based on RATE_LIMIT_EXCEEDED_MSG @@ -1639,19 +1766,27 @@ public void testCreateJobWithBigQueryRetryConfigFailureShouldRetry() { ((BigQueryImpl) bigquery) .create(JobInfo.of(QUERY_JOB_CONFIGURATION_FOR_DMLQUERY), bigQueryRetryConfigOption); - verify(bigqueryRpcMock, times(3)).create(jobCapture.capture(), eq(bigQueryRpcOptions)); + verify(bigqueryRpcMock, times(3)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(bigQueryRpcOptions)); } @Test - public void testCreateJobWithBigQueryRetryConfigFailureShouldNotRetry() { + public void testCreateJobWithBigQueryRetryConfigFailureShouldNotRetry() throws IOException { // Validate create job with BigQueryRetryConfig that does not retry on rate limit error message. JobOption bigQueryRetryConfigOption = JobOption.bigQueryRetryConfig(BigQueryRetryConfig.newBuilder().build()); Map bigQueryRpcOptions = optionMap(bigQueryRetryConfigOption); - when(bigqueryRpcMock.create(jobCapture.capture(), eq(bigQueryRpcOptions))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(bigQueryRpcOptions))) .thenThrow(new BigQueryException(400, RATE_LIMIT_ERROR_MSG)); + // Job create will attempt to retrieve the job even in the case when the job is created in a + // returned failure. + when(bigqueryRpcMock.getJobSkipExceptionTranslation( + nullable(String.class), nullable(String.class), nullable(String.class), Mockito.any())) + .thenThrow(new BigQueryException(500, "InternalError")); + bigquery = options.getService(); bigquery = options @@ -1669,15 +1804,17 @@ public void testCreateJobWithBigQueryRetryConfigFailureShouldNotRetry() { } // Verify that getQueryResults is attempted only once and not retried since the error message // does not match. - verify(bigqueryRpcMock, times(1)).create(jobCapture.capture(), eq(bigQueryRpcOptions)); + verify(bigqueryRpcMock, times(1)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(bigQueryRpcOptions)); } @Test - public void testCreateJobWithRetryOptionsFailureShouldRetry() { + public void testCreateJobWithRetryOptionsFailureShouldRetry() throws IOException { // Validate create job with RetryOptions. JobOption retryOptions = JobOption.retryOptions(RetryOption.maxAttempts(4)); Map bigQueryRpcOptions = optionMap(retryOptions); - when(bigqueryRpcMock.create(jobCapture.capture(), eq(bigQueryRpcOptions))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(bigQueryRpcOptions))) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -1693,18 +1830,26 @@ public void testCreateJobWithRetryOptionsFailureShouldRetry() { ((BigQueryImpl) bigquery) .create(JobInfo.of(QUERY_JOB_CONFIGURATION_FOR_DMLQUERY), retryOptions); - verify(bigqueryRpcMock, times(4)).create(jobCapture.capture(), eq(bigQueryRpcOptions)); + verify(bigqueryRpcMock, times(4)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(bigQueryRpcOptions)); } @Test - public void testCreateJobWithRetryOptionsFailureShouldNotRetry() { + public void testCreateJobWithRetryOptionsFailureShouldNotRetry() throws IOException { // Validate create job with RetryOptions that only attempts once (no retry). JobOption retryOptions = JobOption.retryOptions(RetryOption.maxAttempts(1)); Map bigQueryRpcOptions = optionMap(retryOptions); - when(bigqueryRpcMock.create(jobCapture.capture(), eq(bigQueryRpcOptions))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(bigQueryRpcOptions))) .thenThrow(new BigQueryException(500, "InternalError")) .thenReturn(newJobPb()); + // Job create will attempt to retrieve the job even in the case when the job is created in a + // returned failure. + when(bigqueryRpcMock.getJobSkipExceptionTranslation( + nullable(String.class), nullable(String.class), nullable(String.class), Mockito.any())) + .thenThrow(new BigQueryException(500, "InternalError")); + bigquery = options.getService(); bigquery = options @@ -1720,12 +1865,13 @@ public void testCreateJobWithRetryOptionsFailureShouldNotRetry() { } catch (BigQueryException e) { assertNotNull(e.getMessage()); } - verify(bigqueryRpcMock, times(1)).create(jobCapture.capture(), eq(bigQueryRpcOptions)); + verify(bigqueryRpcMock, times(1)) + .createSkipExceptionTranslation(jobCapture.capture(), eq(bigQueryRpcOptions)); } @Test - public void testCreateJobWithSelectedFields() { - when(bigqueryRpcMock.create( + public void testCreateJobWithSelectedFields() throws IOException { + when(bigqueryRpcMock.createSkipExceptionTranslation( any(com.google.api.services.bigquery.model.Job.class), capturedOptions.capture())) .thenReturn(newJobPb()); @@ -1740,16 +1886,18 @@ public void testCreateJobWithSelectedFields() { .asList() .containsExactly("jobReference", "configuration", "user_email"); verify(bigqueryRpcMock) - .create(any(com.google.api.services.bigquery.model.Job.class), capturedOptions.capture()); + .createSkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class), capturedOptions.capture()); } @Test - public void testCreateJobNoGet() { + public void testCreateJobNoGet() throws IOException { String id = "testCreateJobNoGet-id"; JobId jobId = JobId.of(id); String query = "SELECT * in FOO"; - when(bigqueryRpcMock.create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) .thenThrow(new BigQueryException(409, "already exists, for some reason")); bigquery = options.getService(); @@ -1759,11 +1907,12 @@ public void testCreateJobNoGet() { } catch (BigQueryException e) { assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); } - verify(bigqueryRpcMock).create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); } @Test - public void testCreateJobTryGet() { + public void testCreateJobTryGet() throws IOException { final String id = "testCreateJobTryGet-id"; String query = "SELECT * in FOO"; Supplier idProvider = @@ -1774,33 +1923,37 @@ public JobId get() { } }; - when(bigqueryRpcMock.create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) .thenThrow(new BigQueryException(409, "already exists, for some reason")); - when(bigqueryRpcMock.getJob( + when(bigqueryRpcMock.getJobSkipExceptionTranslation( any(String.class), eq(id), eq((String) null), eq(EMPTY_RPC_OPTIONS))) .thenReturn(newJobPb()); bigquery = options.getService(); ((BigQueryImpl) bigquery).create(JobInfo.of(QueryJobConfiguration.of(query)), idProvider); assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); - verify(bigqueryRpcMock).create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); verify(bigqueryRpcMock) - .getJob(any(String.class), eq(id), eq((String) null), eq(EMPTY_RPC_OPTIONS)); + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation( + any(String.class), eq(id), eq((String) null), eq(EMPTY_RPC_OPTIONS)); } @Test - public void testCreateJobTryGetNotRandom() { + public void testCreateJobTryGetNotRandom() throws IOException { Map withStatisticOption = optionMap(JobOption.fields(STATISTICS)); final String id = "testCreateJobTryGet-id"; String query = "SELECT * in FOO"; - when(bigqueryRpcMock.create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) + when(bigqueryRpcMock.createSkipExceptionTranslation( + jobCapture.capture(), eq(EMPTY_RPC_OPTIONS))) .thenThrow( new BigQueryException( 409, "already exists, for some reason", new RuntimeException("Already Exists: Job"))); - when(bigqueryRpcMock.getJob( + when(bigqueryRpcMock.getJobSkipExceptionTranslation( any(String.class), eq(id), eq((String) null), eq(withStatisticOption))) .thenReturn( newJobPb() @@ -1812,18 +1965,21 @@ public void testCreateJobTryGetNotRandom() { ((BigQueryImpl) bigquery).create(JobInfo.of(JobId.of(id), QueryJobConfiguration.of(query))); assertThat(job).isNotNull(); assertThat(jobCapture.getValue().getJobReference().getJobId()).isEqualTo(id); - verify(bigqueryRpcMock).create(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); verify(bigqueryRpcMock) - .getJob(any(String.class), eq(id), eq((String) null), eq(withStatisticOption)); + .createSkipExceptionTranslation(jobCapture.capture(), eq(EMPTY_RPC_OPTIONS)); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation( + any(String.class), eq(id), eq((String) null), eq(withStatisticOption)); } @Test - public void testCreateJobWithProjectId() { + public void testCreateJobWithProjectId() throws IOException { JobInfo jobInfo = JobInfo.newBuilder(QUERY_JOB_CONFIGURATION.setProjectId(OTHER_PROJECT)) .setJobId(JobId.of(OTHER_PROJECT, JOB)) .build(); - when(bigqueryRpcMock.create(eq(jobInfo.toPb()), capturedOptions.capture())) + when(bigqueryRpcMock.createSkipExceptionTranslation( + eq(jobInfo.toPb()), capturedOptions.capture())) .thenReturn(jobInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); @@ -1835,46 +1991,58 @@ public void testCreateJobWithProjectId() { assertTrue(selector.contains("configuration")); assertTrue(selector.contains("user_email")); assertEquals(37, selector.length()); - verify(bigqueryRpcMock).create(eq(jobInfo.toPb()), capturedOptions.capture()); + verify(bigqueryRpcMock) + .createSkipExceptionTranslation(eq(jobInfo.toPb()), capturedOptions.capture()); } @Test - public void testGetJob() { - when(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + public void testDeleteJob() throws IOException { + JobId jobId = JobId.newBuilder().setJob(JOB).setProject(PROJECT).setLocation(LOCATION).build(); + when(bigqueryRpcMock.deleteJobSkipExceptionTranslation(PROJECT, JOB, LOCATION)) + .thenReturn(true); + bigquery = options.getService(); + assertTrue(bigquery.delete(jobId)); + verify(bigqueryRpcMock).deleteJobSkipExceptionTranslation(PROJECT, JOB, LOCATION); + } + + @Test + public void testGetJob() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(COMPLETE_COPY_JOB.toPb()); bigquery = options.getService(); Job job = bigquery.getJob(JOB); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobWithLocation() { - when(bigqueryRpcMock.getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) + public void testGetJobWithLocation() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .thenReturn(COMPLETE_COPY_JOB.toPb()); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(JOB); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobNotFoundWhenThrowIsDisabled() { - when(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + public void testGetJobNotFoundWhenThrowIsDisabled() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(COMPLETE_COPY_JOB.toPb()); options.setThrowNotFound(false); bigquery = options.getService(); Job job = bigquery.getJob(JOB); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobNotFoundWhenThrowIsEnabled() { - when(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS)) - .thenReturn(null) - .thenThrow(new BigQueryException(404, "Job not found")); + public void testGetJobNotFoundWhenThrowIsEnabled() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation( + PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS)) + .thenThrow(new IOException("Job not found")); options.setThrowNotFound(true); bigquery = options.getService(); try { @@ -1883,57 +2051,63 @@ public void testGetJobNotFoundWhenThrowIsEnabled() { } catch (BigQueryException ex) { Assert.assertNotNull(ex.getMessage()); } - verify(bigqueryRpcMock).getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobFromJobId() { - when(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + public void testGetJobFromJobId() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(COMPLETE_COPY_JOB.toPb()); bigquery = options.getService(); Job job = bigquery.getJob(JobId.of(JOB)); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobFromJobIdWithLocation() { - when(bigqueryRpcMock.getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) + public void testGetJobFromJobIdWithLocation() throws IOException { + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .thenReturn(COMPLETE_COPY_JOB.toPb()); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(JobId.of(JOB)); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation(PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobFromJobIdWithProject() { + public void testGetJobFromJobIdWithProject() throws IOException { JobId jobId = JobId.of(OTHER_PROJECT, JOB); JobInfo jobInfo = COPY_JOB.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.getJob(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getJobSkipExceptionTranslation( + OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(jobInfo.toPb()); bigquery = options.getService(); Job job = bigquery.getJob(jobId); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); - verify(bigqueryRpcMock).getJob(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetJobFromJobIdWithProjectWithLocation() { + public void testGetJobFromJobIdWithProjectWithLocation() throws IOException { JobId jobId = JobId.of(OTHER_PROJECT, JOB); JobInfo jobInfo = COPY_JOB.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.getJob(OTHER_PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getJobSkipExceptionTranslation( + OTHER_PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS)) .thenReturn(jobInfo.toPb()); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); bigquery = options.getService(); Job job = bigquery.getJob(jobId); assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(jobInfo)), job); - verify(bigqueryRpcMock).getJob(OTHER_PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getJobSkipExceptionTranslation(OTHER_PROJECT, JOB, LOCATION, EMPTY_RPC_OPTIONS); } @Test - public void testListJobs() { + public void testListJobs() throws IOException { bigquery = options.getService(); ImmutableList jobList = ImmutableList.of( @@ -1950,15 +2124,16 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - when(bigqueryRpcMock.listJobs(PROJECT, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listJobsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listJobs(); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.getValues(), Job.class)); - verify(bigqueryRpcMock).listJobs(PROJECT, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).listJobsSkipExceptionTranslation(PROJECT, EMPTY_RPC_OPTIONS); } @Test - public void testListJobsWithOptions() { + public void testListJobsWithOptions() throws IOException { bigquery = options.getService(); ImmutableList jobList = ImmutableList.of( @@ -1975,17 +2150,18 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - when(bigqueryRpcMock.listJobs(PROJECT, JOB_LIST_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listJobsSkipExceptionTranslation(PROJECT, JOB_LIST_OPTIONS)) + .thenReturn(result); Page page = bigquery.listJobs( JOB_LIST_ALL_USERS, JOB_LIST_STATE_FILTER, JOB_LIST_PAGE_TOKEN, JOB_LIST_PAGE_SIZE); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.getValues(), Job.class)); - verify(bigqueryRpcMock).listJobs(PROJECT, JOB_LIST_OPTIONS); + verify(bigqueryRpcMock).listJobsSkipExceptionTranslation(PROJECT, JOB_LIST_OPTIONS); } @Test - public void testListJobsWithSelectedFields() { + public void testListJobsWithSelectedFields() throws IOException { bigquery = options.getService(); ImmutableList jobList = ImmutableList.of( @@ -2002,7 +2178,8 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { return job.toPb(); } })); - when(bigqueryRpcMock.listJobs(eq(PROJECT), capturedOptions.capture())).thenReturn(result); + when(bigqueryRpcMock.listJobsSkipExceptionTranslation(eq(PROJECT), capturedOptions.capture())) + .thenReturn(result); Page page = bigquery.listJobs(JOB_LIST_OPTION_FIELD); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.getValues(), Job.class)); @@ -2015,36 +2192,37 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { assertTrue(selector.contains("errorResult")); assertTrue(selector.contains(")")); assertEquals(75, selector.length()); - verify(bigqueryRpcMock).listJobs(eq(PROJECT), capturedOptions.capture()); + verify(bigqueryRpcMock) + .listJobsSkipExceptionTranslation(eq(PROJECT), capturedOptions.capture()); } @Test - public void testCancelJob() { - when(bigqueryRpcMock.cancel(PROJECT, JOB, null)).thenReturn(true); + public void testCancelJob() throws IOException { + when(bigqueryRpcMock.cancelSkipExceptionTranslation(PROJECT, JOB, null)).thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.cancel(JOB)); - verify(bigqueryRpcMock).cancel(PROJECT, JOB, null); + verify(bigqueryRpcMock).cancelSkipExceptionTranslation(PROJECT, JOB, null); } @Test - public void testCancelJobFromJobId() { - when(bigqueryRpcMock.cancel(PROJECT, JOB, null)).thenReturn(true); + public void testCancelJobFromJobId() throws IOException { + when(bigqueryRpcMock.cancelSkipExceptionTranslation(PROJECT, JOB, null)).thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.cancel(JobId.of(PROJECT, JOB))); - verify(bigqueryRpcMock).cancel(PROJECT, JOB, null); + verify(bigqueryRpcMock).cancelSkipExceptionTranslation(PROJECT, JOB, null); } @Test - public void testCancelJobFromJobIdWithProject() { + public void testCancelJobFromJobIdWithProject() throws IOException { JobId jobId = JobId.of(OTHER_PROJECT, JOB); - when(bigqueryRpcMock.cancel(OTHER_PROJECT, JOB, null)).thenReturn(true); + when(bigqueryRpcMock.cancelSkipExceptionTranslation(OTHER_PROJECT, JOB, null)).thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.cancel(jobId)); - verify(bigqueryRpcMock).cancel(OTHER_PROJECT, JOB, null); + verify(bigqueryRpcMock).cancelSkipExceptionTranslation(OTHER_PROJECT, JOB, null); } @Test - public void testQueryRequestCompleted() throws InterruptedException { + public void testQueryRequestCompleted() throws InterruptedException, IOException { JobId queryJob = JobId.of(PROJECT, JOB); com.google.api.services.bigquery.model.Job jobResponsePb = new com.google.api.services.bigquery.model.Job() @@ -2064,13 +2242,13 @@ public void testQueryRequestCompleted() throws InterruptedException { .setTotalRows(BigInteger.valueOf(1L)) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.create( + when(bigqueryRpcMock.createSkipExceptionTranslation( JOB_INFO.toPb(), Collections.emptyMap())) .thenReturn(jobResponsePb); - when(bigqueryRpcMock.getQueryResults( + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) .thenReturn(responsePb); - when(bigqueryRpcMock.listTableData( + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( PROJECT, DATASET, TABLE, Collections.emptyMap())) .thenReturn( new TableDataList() @@ -2087,16 +2265,19 @@ PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) assertThat(row.get(1).getLongValue()).isEqualTo(1); } verify(bigqueryRpcMock) - .create(JOB_INFO.toPb(), Collections.emptyMap()); + .createSkipExceptionTranslation( + JOB_INFO.toPb(), Collections.emptyMap()); verify(bigqueryRpcMock) - .getQueryResults(PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); + .getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); verify(bigqueryRpcMock) - .listTableData(PROJECT, DATASET, TABLE, Collections.emptyMap()); + .listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, Collections.emptyMap()); } @Test - public void testFastQueryRequestCompleted() throws InterruptedException { + public void testFastQueryRequestCompleted() throws InterruptedException, IOException { com.google.api.services.bigquery.model.QueryResponse queryResponsePb = new com.google.api.services.bigquery.model.QueryResponse() .setCacheHit(false) @@ -2108,7 +2289,7 @@ public void testFastQueryRequestCompleted() throws InterruptedException { .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenReturn(queryResponsePb); bigquery = options.getService(); @@ -2131,11 +2312,12 @@ public void testFastQueryRequestCompleted() throws InterruptedException { assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache()); assertNull(requestPb.getLocation()); - verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test - public void testFastQueryRequestCompletedWithLocation() throws InterruptedException { + public void testFastQueryRequestCompletedWithLocation() throws InterruptedException, IOException { com.google.api.services.bigquery.model.QueryResponse queryResponsePb = new com.google.api.services.bigquery.model.QueryResponse() .setCacheHit(false) @@ -2147,7 +2329,7 @@ public void testFastQueryRequestCompletedWithLocation() throws InterruptedExcept .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenReturn(queryResponsePb); BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock); @@ -2171,11 +2353,12 @@ public void testFastQueryRequestCompletedWithLocation() throws InterruptedExcept assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache()); assertEquals(LOCATION, requestPb.getLocation()); - verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test - public void testFastQueryMultiplePages() throws InterruptedException { + public void testFastQueryMultiplePages() throws InterruptedException, IOException { JobId queryJob = JobId.of(PROJECT, JOB); com.google.api.services.bigquery.model.Job responseJob = new com.google.api.services.bigquery.model.Job() @@ -2184,8 +2367,9 @@ public void testFastQueryMultiplePages() throws InterruptedException { .setId(JOB) .setStatus(new com.google.api.services.bigquery.model.JobStatus().setState("DONE")); responseJob.getConfiguration().getQuery().setDestinationTable(TABLE_ID.toPb()); - when(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)).thenReturn(responseJob); - when(bigqueryRpcMock.listTableData( + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + .thenReturn(responseJob); + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( PROJECT, DATASET, TABLE, optionMap(BigQuery.TableDataListOption.pageToken(CURSOR)))) .thenReturn( new TableDataList() @@ -2205,7 +2389,7 @@ PROJECT, DATASET, TABLE, optionMap(BigQuery.TableDataListOption.pageToken(CURSOR .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenReturn(queryResponsePb); bigquery = options.getService(); @@ -2221,15 +2405,16 @@ PROJECT, DATASET, TABLE, optionMap(BigQuery.TableDataListOption.pageToken(CURSOR requestPb.getDefaultDataset().getDatasetId()); assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache()); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); verify(bigqueryRpcMock) - .listTableData( + .listTableDataSkipExceptionTranslation( PROJECT, DATASET, TABLE, optionMap(BigQuery.TableDataListOption.pageToken(CURSOR))); - verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test - public void testFastQuerySlowDdl() throws InterruptedException { + public void testFastQuerySlowDdl() throws InterruptedException, IOException { // mock new fast query path response when running a query that takes more than 10s JobId queryJob = JobId.of(PROJECT, JOB); com.google.api.services.bigquery.model.QueryResponse queryResponsePb = @@ -2256,14 +2441,16 @@ public void testFastQuerySlowDdl() throws InterruptedException { .setTotalRows(BigInteger.valueOf(1L)) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenReturn(queryResponsePb); responseJob.getConfiguration().getQuery().setDestinationTable(TABLE_ID.toPb()); - when(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)).thenReturn(responseJob); - when(bigqueryRpcMock.getQueryResults( + when(bigqueryRpcMock.getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + .thenReturn(responseJob); + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) .thenReturn(queryResultsResponsePb); - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .thenReturn(new TableDataList().setRows(ImmutableList.of(TABLE_ROW)).setTotalRows(1L)); bigquery = options.getService(); @@ -2282,15 +2469,18 @@ PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) requestPb.getDefaultDataset().getDatasetId()); assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache()); - verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture()); - verify(bigqueryRpcMock).getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); verify(bigqueryRpcMock) - .getQueryResults(PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock).getJobSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); + verify(bigqueryRpcMock) + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS); } @Test - public void testQueryRequestCompletedOptions() throws InterruptedException { + public void testQueryRequestCompletedOptions() throws InterruptedException, IOException { JobId queryJob = JobId.of(PROJECT, JOB); com.google.api.services.bigquery.model.Job jobResponsePb = new com.google.api.services.bigquery.model.Job() @@ -2310,7 +2500,7 @@ public void testQueryRequestCompletedOptions() throws InterruptedException { .setTotalRows(BigInteger.valueOf(1L)) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.create( + when(bigqueryRpcMock.createSkipExceptionTranslation( JOB_INFO.toPb(), Collections.emptyMap())) .thenReturn(jobResponsePb); @@ -2318,10 +2508,10 @@ public void testQueryRequestCompletedOptions() throws InterruptedException { QueryResultsOption pageSizeOption = QueryResultsOption.pageSize(42L); optionMap.put(pageSizeOption.getRpcOption(), pageSizeOption.getValue()); - when(bigqueryRpcMock.getQueryResults( + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) .thenReturn(responsePb); - when(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, optionMap)) + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, optionMap)) .thenReturn( new TableDataList() .setPageToken("") @@ -2338,14 +2528,17 @@ PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) assertThat(row.get(1).getLongValue()).isEqualTo(1); } verify(bigqueryRpcMock) - .create(JOB_INFO.toPb(), Collections.emptyMap()); + .createSkipExceptionTranslation( + JOB_INFO.toPb(), Collections.emptyMap()); + verify(bigqueryRpcMock) + .getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); verify(bigqueryRpcMock) - .getQueryResults(PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); - verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, optionMap); + .listTableDataSkipExceptionTranslation(PROJECT, DATASET, TABLE, optionMap); } @Test - public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedException { + public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedException, IOException { JobId queryJob = JobId.of(PROJECT, JOB); com.google.api.services.bigquery.model.Job jobResponsePb1 = new com.google.api.services.bigquery.model.Job() @@ -2370,16 +2563,16 @@ public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedExcepti .setTotalRows(BigInteger.valueOf(1L)) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.create( + when(bigqueryRpcMock.createSkipExceptionTranslation( JOB_INFO.toPb(), Collections.emptyMap())) .thenReturn(jobResponsePb1); - when(bigqueryRpcMock.getQueryResults( + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) .thenReturn(responsePb1); - when(bigqueryRpcMock.getQueryResults( + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) .thenReturn(responsePb2); - when(bigqueryRpcMock.listTableData( + when(bigqueryRpcMock.listTableDataSkipExceptionTranslation( PROJECT, DATASET, TABLE, Collections.emptyMap())) .thenReturn( new TableDataList() @@ -2396,17 +2589,21 @@ PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS))) assertThat(row.get(1).getLongValue()).isEqualTo(1); } verify(bigqueryRpcMock) - .create(JOB_INFO.toPb(), Collections.emptyMap()); + .createSkipExceptionTranslation( + JOB_INFO.toPb(), Collections.emptyMap()); verify(bigqueryRpcMock) - .getQueryResults(PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); + .getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); verify(bigqueryRpcMock) - .getQueryResults(PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); + .getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)); verify(bigqueryRpcMock) - .listTableData(PROJECT, DATASET, TABLE, Collections.emptyMap()); + .listTableDataSkipExceptionTranslation( + PROJECT, DATASET, TABLE, Collections.emptyMap()); } @Test - public void testGetQueryResults() { + public void testGetQueryResults() throws IOException { JobId queryJob = JobId.of(JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() @@ -2418,17 +2615,19 @@ public void testGetQueryResults() { .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.getQueryResults(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(responsePb); bigquery = options.getService(); QueryResponse response = bigquery.getQueryResults(queryJob); assertEquals(true, response.getCompleted()); assertEquals(null, response.getSchema()); - verify(bigqueryRpcMock).getQueryResults(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getQueryResultsSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetQueryResultsRetry() { + public void testGetQueryResultsRetry() throws IOException { JobId queryJob = JobId.of(JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() @@ -2441,7 +2640,8 @@ public void testGetQueryResultsRetry() { .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.getQueryResults(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -2467,11 +2667,12 @@ public void testGetQueryResultsRetry() { // EMPTY_RPC_OPTIONS) as there is no // identifier in this method which will can potentially differ and which can be used to // establish idempotency - verify(bigqueryRpcMock, times(6)).getQueryResults(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock, times(6)) + .getQueryResultsSkipExceptionTranslation(PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetQueryResultsWithProject() { + public void testGetQueryResultsWithProject() throws IOException { JobId queryJob = JobId.of(OTHER_PROJECT, JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() @@ -2483,17 +2684,19 @@ public void testGetQueryResultsWithProject() { .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.getQueryResults(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( + OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) .thenReturn(responsePb); bigquery = options.getService(); QueryResponse response = bigquery.getQueryResults(queryJob); assertTrue(response.getCompleted()); assertEquals(null, response.getSchema()); - verify(bigqueryRpcMock).getQueryResults(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getQueryResultsSkipExceptionTranslation(OTHER_PROJECT, JOB, null, EMPTY_RPC_OPTIONS); } @Test - public void testGetQueryResultsWithOptions() { + public void testGetQueryResultsWithOptions() throws IOException { JobId queryJob = JobId.of(PROJECT, JOB); GetQueryResultsResponse responsePb = new GetQueryResultsResponse() @@ -2504,7 +2707,8 @@ public void testGetQueryResultsWithOptions() { .setPageToken(CURSOR) .setTotalBytesProcessed(42L) .setTotalRows(BigInteger.valueOf(1L)); - when(bigqueryRpcMock.getQueryResults(PROJECT, JOB, null, QUERY_RESULTS_OPTIONS)) + when(bigqueryRpcMock.getQueryResultsSkipExceptionTranslation( + PROJECT, JOB, null, QUERY_RESULTS_OPTIONS)) .thenReturn(responsePb); bigquery = options.getService(); QueryResponse response = @@ -2516,12 +2720,13 @@ public void testGetQueryResultsWithOptions() { QUERY_RESULTS_OPTION_PAGE_TOKEN); assertEquals(true, response.getCompleted()); assertEquals(null, response.getSchema()); - verify(bigqueryRpcMock).getQueryResults(PROJECT, JOB, null, QUERY_RESULTS_OPTIONS); + verify(bigqueryRpcMock) + .getQueryResultsSkipExceptionTranslation(PROJECT, JOB, null, QUERY_RESULTS_OPTIONS); } @Test - public void testGetDatasetRetryableException() { - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + public void testGetDatasetRetryableException() throws IOException { + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(500, "InternalError")) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); bigquery = @@ -2533,13 +2738,14 @@ public void testGetDatasetRetryableException() { Dataset dataset = bigquery.getDataset(DATASET); assertEquals( new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); - verify(bigqueryRpcMock, times(2)).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock, times(2)) + .getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testNonRetryableException() { + public void testNonRetryableException() throws IOException { String exceptionMessage = "Not Implemented"; - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(501, exceptionMessage)); bigquery = options @@ -2553,13 +2759,13 @@ public void testNonRetryableException() { } catch (BigQueryException ex) { Assert.assertEquals(exceptionMessage, ex.getMessage()); } - verify(bigqueryRpcMock).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testRuntimeException() { + public void testRuntimeException() throws IOException { String exceptionMessage = "Artificial runtime exception"; - when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenThrow(new RuntimeException(exceptionMessage)); bigquery = options @@ -2573,7 +2779,7 @@ public void testRuntimeException() { } catch (BigQueryException ex) { Assert.assertTrue(ex.getMessage().endsWith(exceptionMessage)); } - verify(bigqueryRpcMock).getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getDatasetSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test @@ -2604,7 +2810,7 @@ public void testFastQuerySQLShouldRetry() throws Exception { .setTotalRows(BigInteger.valueOf(1L)) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -2630,7 +2836,8 @@ public void testFastQuerySQLShouldRetry() throws Exception { } assertTrue(idempotent); - verify(bigqueryRpcMock, times(5)).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock, times(5)) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test @@ -2645,7 +2852,7 @@ public void testFastQueryDMLShouldRetry() throws Exception { .setNumDmlAffectedRows(1L) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -2671,7 +2878,8 @@ public void testFastQueryDMLShouldRetry() throws Exception { } assertTrue(idempotent); - verify(bigqueryRpcMock, times(5)).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock, times(5)) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test @@ -2686,7 +2894,7 @@ public void testFastQueryRateLimitIdempotency() throws Exception { .setNumDmlAffectedRows(1L) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -2719,7 +2927,8 @@ public void testFastQueryRateLimitIdempotency() throws Exception { } assertTrue(idempotent); - verify(bigqueryRpcMock, times(6)).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock, times(6)) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test @@ -2757,7 +2966,7 @@ public void testFastQueryDDLShouldRetry() throws Exception { .setTotalBytesProcessed(42L) .setSchema(TABLE_SCHEMA.toPb()); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) .thenThrow(new BigQueryException(500, "InternalError")) .thenThrow(new BigQueryException(502, "Bad Gateway")) .thenThrow(new BigQueryException(503, "Service Unavailable")) @@ -2783,11 +2992,12 @@ public void testFastQueryDDLShouldRetry() throws Exception { } assertTrue(idempotent); - verify(bigqueryRpcMock, times(5)).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock, times(5)) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test - public void testFastQueryBigQueryException() throws InterruptedException { + public void testFastQueryBigQueryException() throws InterruptedException, IOException { List errorProtoList = ImmutableList.of( new ErrorProto() @@ -2804,7 +3014,8 @@ public void testFastQueryBigQueryException() throws InterruptedException { .setPageToken(null) .setErrors(errorProtoList); - when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture())).thenReturn(responsePb); + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture())) + .thenReturn(responsePb); bigquery = options.getService(); try { @@ -2820,122 +3031,135 @@ public void testFastQueryBigQueryException() throws InterruptedException { QUERY_JOB_CONFIGURATION_FOR_QUERY.getDefaultDataset().getDataset(), requestPb.getDefaultDataset().getDatasetId()); assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache()); - verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture()); + verify(bigqueryRpcMock) + .queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()); } @Test - public void testCreateRoutine() { + public void testCreateRoutine() throws IOException { RoutineInfo routineInfo = ROUTINE_INFO.setProjectId(OTHER_PROJECT); - when(bigqueryRpcMock.create(routineInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.createSkipExceptionTranslation(routineInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(routineInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Routine actualRoutine = bigquery.create(routineInfo); assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(routineInfo)), actualRoutine); - verify(bigqueryRpcMock).create(routineInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).createSkipExceptionTranslation(routineInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testGetRoutine() { - when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) + public void testGetRoutine() throws IOException { + when(bigqueryRpcMock.getRoutineSkipExceptionTranslation( + PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) .thenReturn(ROUTINE_INFO.toPb()); bigquery = options.getService(); Routine routine = bigquery.getRoutine(DATASET, ROUTINE); assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)), routine); - verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getRoutineSkipExceptionTranslation(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); } @Test - public void testGetRoutineWithRountineId() { - when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) + public void testGetRoutineWithRountineId() throws IOException { + when(bigqueryRpcMock.getRoutineSkipExceptionTranslation( + PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) .thenReturn(ROUTINE_INFO.toPb()); bigquery = options.getService(); Routine routine = bigquery.getRoutine(ROUTINE_ID); assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)), routine); - verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getRoutineSkipExceptionTranslation(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); } @Test - public void testGetRoutineWithEnabledThrowNotFoundException() { - when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) - .thenReturn(null) + public void testGetRoutineWithEnabledThrowNotFoundException() throws IOException { + when(bigqueryRpcMock.getRoutineSkipExceptionTranslation( + PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(404, "Routine not found")); options.setThrowNotFound(true); bigquery = options.getService(); try { - Routine routine = bigquery.getRoutine(ROUTINE_ID); + bigquery.getRoutine(ROUTINE_ID); fail(); } catch (BigQueryException ex) { assertEquals("Routine not found", ex.getMessage()); } - verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .getRoutineSkipExceptionTranslation(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); } @Test - public void testUpdateRoutine() { + public void testUpdateRoutine() throws IOException { RoutineInfo updatedRoutineInfo = ROUTINE_INFO .setProjectId(OTHER_PROJECT) .toBuilder() .setDescription("newDescription") .build(); - when(bigqueryRpcMock.update(updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.updateSkipExceptionTranslation( + updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS)) .thenReturn(updatedRoutineInfo.toPb()); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Routine routine = bigquery.update(updatedRoutineInfo); assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(updatedRoutineInfo)), routine); - verify(bigqueryRpcMock).update(updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .updateSkipExceptionTranslation(updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS); } @Test - public void testListRoutines() { + public void testListRoutines() throws IOException { bigquery = options.getService(); ImmutableList routineList = ImmutableList.of(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(routineList, RoutineInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listRoutinesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listRoutines(DATASET); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(routineList.toArray(), Iterables.toArray(page.getValues(), Routine.class)); - verify(bigqueryRpcMock).listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listRoutinesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testListRoutinesWithDatasetId() { + public void testListRoutinesWithDatasetId() throws IOException { bigquery = options.getService(); ImmutableList routineList = ImmutableList.of(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(routineList, RoutineInfo.TO_PB_FUNCTION)); - when(bigqueryRpcMock.listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + when(bigqueryRpcMock.listRoutinesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .thenReturn(result); Page page = bigquery.listRoutines(DatasetId.of(PROJECT, DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(routineList.toArray(), Iterables.toArray(page.getValues(), Routine.class)); - verify(bigqueryRpcMock).listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .listRoutinesSkipExceptionTranslation(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } @Test - public void testDeleteRoutine() { - when(bigqueryRpcMock.deleteRoutine(PROJECT, DATASET, ROUTINE)).thenReturn(true); + public void testDeleteRoutine() throws IOException { + when(bigqueryRpcMock.deleteRoutineSkipExceptionTranslation(PROJECT, DATASET, ROUTINE)) + .thenReturn(true); bigquery = options.getService(); assertTrue(bigquery.delete(ROUTINE_ID)); - verify(bigqueryRpcMock).deleteRoutine(PROJECT, DATASET, ROUTINE); + verify(bigqueryRpcMock).deleteRoutineSkipExceptionTranslation(PROJECT, DATASET, ROUTINE); } @Test public void testWriteWithJob() throws IOException { bigquery = options.getService(); Job job = new Job(bigquery, new JobInfo.BuilderImpl(JOB_INFO)); - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -2943,24 +3167,25 @@ public void testWriteWithJob() throws IOException { assertEquals(job, writer.getJob()); bigquery.writer(JOB_INFO.getJobId(), LOAD_CONFIGURATION); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); } @Test public void testWriteChannel() throws IOException { bigquery = options.getService(); Job job = new Job(bigquery, new JobInfo.BuilderImpl(JOB_INFO)); - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -2968,43 +3193,47 @@ public void testWriteChannel() throws IOException { assertEquals(job, writer.getJob()); bigquery.writer(LOAD_CONFIGURATION); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); } @Test - public void testGetIamPolicy() { + public void testGetIamPolicy() throws IOException { final String resourceId = String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE); final com.google.api.services.bigquery.model.Policy apiPolicy = PolicyHelper.convertToApiPolicy(SAMPLE_IAM_POLICY); - when(bigqueryRpcMock.getIamPolicy(resourceId, EMPTY_RPC_OPTIONS)).thenReturn(apiPolicy); + when(bigqueryRpcMock.getIamPolicySkipExceptionTranslation(resourceId, EMPTY_RPC_OPTIONS)) + .thenReturn(apiPolicy); bigquery = options.getService(); Policy policy = bigquery.getIamPolicy(TABLE_ID); assertEquals(policy, SAMPLE_IAM_POLICY); - verify(bigqueryRpcMock).getIamPolicy(resourceId, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock).getIamPolicySkipExceptionTranslation(resourceId, EMPTY_RPC_OPTIONS); } @Test - public void testSetIamPolicy() { + public void testSetIamPolicy() throws IOException { final String resourceId = String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE); final com.google.api.services.bigquery.model.Policy apiPolicy = PolicyHelper.convertToApiPolicy(SAMPLE_IAM_POLICY); - when(bigqueryRpcMock.setIamPolicy(resourceId, apiPolicy, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.setIamPolicySkipExceptionTranslation( + resourceId, apiPolicy, EMPTY_RPC_OPTIONS)) .thenReturn(apiPolicy); bigquery = options.getService(); Policy returnedPolicy = bigquery.setIamPolicy(TABLE_ID, SAMPLE_IAM_POLICY); assertEquals(returnedPolicy, SAMPLE_IAM_POLICY); - verify(bigqueryRpcMock).setIamPolicy(resourceId, apiPolicy, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .setIamPolicySkipExceptionTranslation(resourceId, apiPolicy, EMPTY_RPC_OPTIONS); } @Test - public void testTestIamPermissions() { + public void testTestIamPermissions() throws IOException { final String resourceId = String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE); final List checkedPermissions = ImmutableList.of("foo", "bar", "baz"); @@ -3012,16 +3241,19 @@ public void testTestIamPermissions() { final com.google.api.services.bigquery.model.TestIamPermissionsResponse response = new com.google.api.services.bigquery.model.TestIamPermissionsResponse() .setPermissions(grantedPermissions); - when(bigqueryRpcMock.testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.testIamPermissionsSkipExceptionTranslation( + resourceId, checkedPermissions, EMPTY_RPC_OPTIONS)) .thenReturn(response); bigquery = options.getService(); List perms = bigquery.testIamPermissions(TABLE_ID, checkedPermissions); assertEquals(perms, grantedPermissions); - verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .testIamPermissionsSkipExceptionTranslation( + resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); } @Test - public void testTestIamPermissionsWhenNoPermissionsGranted() { + public void testTestIamPermissionsWhenNoPermissionsGranted() throws IOException { final String resourceId = String.format("projects/%s/datasets/%s/tables/%s", PROJECT, DATASET, TABLE); final List checkedPermissions = ImmutableList.of("foo", "bar", "baz"); @@ -3029,11 +3261,14 @@ public void testTestIamPermissionsWhenNoPermissionsGranted() { final com.google.api.services.bigquery.model.TestIamPermissionsResponse response = new com.google.api.services.bigquery.model.TestIamPermissionsResponse() .setPermissions(null); - when(bigqueryRpcMock.testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS)) + when(bigqueryRpcMock.testIamPermissionsSkipExceptionTranslation( + resourceId, checkedPermissions, EMPTY_RPC_OPTIONS)) .thenReturn(response); bigquery = options.getService(); List perms = bigquery.testIamPermissions(TABLE_ID, checkedPermissions); assertEquals(perms, ImmutableList.of()); - verify(bigqueryRpcMock).testIamPermissions(resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); + verify(bigqueryRpcMock) + .testIamPermissionsSkipExceptionTranslation( + resourceId, checkedPermissions, EMPTY_RPC_OPTIONS); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java index 7eea1570a..65bbb6c56 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java @@ -27,9 +27,10 @@ import com.google.cloud.ServiceOptions; import com.google.cloud.Tuple; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; -import com.google.cloud.bigquery.spi.v2.BigQueryRpc; +import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListenableFuture; +import java.io.IOException; import java.math.BigInteger; import java.sql.SQLException; import java.util.AbstractList; @@ -50,7 +51,7 @@ public class ConnectionImplTest { private BigQueryOptions options; private BigQueryRpcFactory rpcFactoryMock; - private BigQueryRpc bigqueryRpcMock; + private HttpBigQueryRpc bigqueryRpcMock; private Connection connectionMock; private BigQuery bigquery; private ConnectionImpl connection; @@ -142,7 +143,7 @@ private BigQueryOptions createBigQueryOptionsForProject( @Before public void setUp() { rpcFactoryMock = mock(BigQueryRpcFactory.class); - bigqueryRpcMock = mock(BigQueryRpc.class); + bigqueryRpcMock = mock(HttpBigQueryRpc.class); connectionMock = mock(Connection.class); when(rpcFactoryMock.create(any(BigQueryOptions.class))).thenReturn(bigqueryRpcMock); options = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); @@ -164,10 +165,11 @@ public void setUp() { } @Test - public void testFastQuerySinglePage() throws BigQuerySQLException { + public void testFastQuerySinglePage() throws BigQuerySQLException, IOException { com.google.api.services.bigquery.model.QueryResponse mockQueryRes = new QueryResponse().setSchema(FAST_QUERY_TABLESCHEMA).setJobComplete(true); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); ConnectionImpl connectionSpy = Mockito.spy(connection); doReturn(BQ_RS_MOCK_RES) @@ -185,13 +187,14 @@ public void testFastQuerySinglePage() throws BigQuerySQLException { @Test // NOTE: This doesn't truly paginates. Returns a response while mocking // processQueryResponseResults - public void testFastQueryMultiplePages() throws BigQuerySQLException { + public void testFastQueryMultiplePages() throws BigQuerySQLException, IOException { com.google.api.services.bigquery.model.QueryResponse mockQueryRes = new QueryResponse() .setSchema(FAST_QUERY_TABLESCHEMA) .setJobComplete(true) .setPageToken(PAGE_TOKEN); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); ConnectionImpl connectionSpy = Mockito.spy(connection); @@ -215,7 +218,7 @@ public void testClose() throws BigQuerySQLException { } @Test - public void testQueryDryRun() throws BigQuerySQLException { + public void testQueryDryRun() throws BigQuerySQLException, IOException { List queryParametersMock = ImmutableList.of( new QueryParameter().setParameterType(new QueryParameterType().setType("STRING"))); @@ -237,17 +240,19 @@ public void testQueryDryRun() throws BigQuerySQLException { new com.google.api.services.bigquery.model.Job() .setStatistics(jobStatsMock) .setConfiguration(jobConfig); - when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class))) + when(bigqueryRpcMock.createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class))) .thenReturn(mockDryRunJob); BigQueryDryRunResult dryRunResult = connection.dryRun(DRY_RUN_SQL); assertEquals(1, dryRunResult.getQueryParameters().size()); assertEquals(QUERY_SCHEMA, dryRunResult.getSchema()); verify(bigqueryRpcMock, times(1)) - .createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)); + .createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class)); } @Test - public void testQueryDryRunNoQueryParameters() throws BigQuerySQLException { + public void testQueryDryRunNoQueryParameters() throws BigQuerySQLException, IOException { com.google.api.services.bigquery.model.JobStatistics2 queryMock = new com.google.api.services.bigquery.model.JobStatistics2() .setSchema(FAST_QUERY_TABLESCHEMA); @@ -265,13 +270,15 @@ public void testQueryDryRunNoQueryParameters() throws BigQuerySQLException { new com.google.api.services.bigquery.model.Job() .setStatistics(jobStatsMock) .setConfiguration(jobConfig); - when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class))) + when(bigqueryRpcMock.createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class))) .thenReturn(mockDryRunJob); BigQueryDryRunResult dryRunResult = connection.dryRun(DRY_RUN_SQL); assertEquals(0, dryRunResult.getQueryParameters().size()); assertEquals(QUERY_SCHEMA, dryRunResult.getSchema()); verify(bigqueryRpcMock, times(1)) - .createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)); + .createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class)); } @Test @@ -358,8 +365,8 @@ public void testNextPageTask() throws InterruptedException { } @Test - public void testGetQueryResultsFirstPage() { - when(bigqueryRpcMock.getQueryResultsWithRowLimit( + public void testGetQueryResultsFirstPage() throws IOException { + when(bigqueryRpcMock.getQueryResultsWithRowLimitSkipExceptionTranslation( any(String.class), any(String.class), any(String.class), @@ -370,7 +377,7 @@ public void testGetQueryResultsFirstPage() { assertNotNull(response); assertEquals(GET_QUERY_RESULTS_RESPONSE, response); verify(bigqueryRpcMock, times(1)) - .getQueryResultsWithRowLimit( + .getQueryResultsWithRowLimitSkipExceptionTranslation( any(String.class), any(String.class), any(String.class), @@ -380,7 +387,7 @@ public void testGetQueryResultsFirstPage() { // calls executeSelect with a nonFast query and exercises createQueryJob @Test - public void testLegacyQuerySinglePage() throws BigQuerySQLException { + public void testLegacyQuerySinglePage() throws BigQuerySQLException, IOException { ConnectionImpl connectionSpy = Mockito.spy(connection); com.google.api.services.bigquery.model.Job jobResponseMock = new com.google.api.services.bigquery.model.Job() @@ -400,18 +407,20 @@ public void testLegacyQuerySinglePage() throws BigQuerySQLException { any(JobId.class), any(GetQueryResultsResponse.class), any(Boolean.class)); - when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class))) + when(bigqueryRpcMock.createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class))) .thenReturn(jobResponseMock); // RPC call in createQueryJob BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY); assertEquals(res.getTotalRows(), 2); assertEquals(QUERY_SCHEMA, res.getSchema()); verify(bigqueryRpcMock, times(1)) - .createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)); + .createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class)); } // calls executeSelect with a nonFast query where the query returns an empty result. @Test - public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException, SQLException { + public void testLegacyQuerySinglePageEmptyResults() throws SQLException, IOException { ConnectionImpl connectionSpy = Mockito.spy(connection); com.google.api.services.bigquery.model.Job jobResponseMock = new com.google.api.services.bigquery.model.Job() @@ -423,7 +432,8 @@ public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException, doReturn(GET_QUERY_RESULTS_RESPONSE_EMPTY) .when(connectionSpy) .getQueryResultsFirstPage(any(JobId.class)); - when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class))) + when(bigqueryRpcMock.createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class))) .thenReturn(jobResponseMock); // RPC call in createQueryJob BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY); assertEquals(res.getTotalRows(), 0); @@ -433,12 +443,13 @@ public void testLegacyQuerySinglePageEmptyResults() throws BigQuerySQLException, res.getResultSet() .next()); // Validates that NPE does not occur when reading from empty ResultSet. verify(bigqueryRpcMock, times(1)) - .createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)); + .createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class)); } // exercises getSubsequentQueryResultsWithJob for fast running queries @Test - public void testFastQueryLongRunning() throws SQLException { + public void testFastQueryLongRunning() throws SQLException, IOException { ConnectionImpl connectionSpy = Mockito.spy(connection); // emulating a fast query doReturn(true).when(connectionSpy).isFastQuerySupported(); @@ -458,17 +469,19 @@ public void testFastQueryLongRunning() throws SQLException { .setTotalRows(new BigInteger(String.valueOf(4L))) .setJobReference(QUERY_JOB.toPb()) .setRows(TABLE_ROWS); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY); assertEquals(res.getTotalRows(), 2); assertEquals(QUERY_SCHEMA, res.getSchema()); - verify(bigqueryRpcMock, times(1)).queryRpc(any(String.class), any(QueryRequest.class)); + verify(bigqueryRpcMock, times(1)) + .queryRpcSkipExceptionTranslation(any(String.class), any(QueryRequest.class)); } @Test public void testFastQueryLongRunningAsync() - throws SQLException, ExecutionException, InterruptedException { + throws SQLException, ExecutionException, InterruptedException, IOException { ConnectionImpl connectionSpy = Mockito.spy(connection); // emulating a fast query doReturn(true).when(connectionSpy).isFastQuerySupported(); @@ -488,7 +501,8 @@ public void testFastQueryLongRunningAsync() .setTotalRows(new BigInteger(String.valueOf(4L))) .setJobReference(QUERY_JOB.toPb()) .setRows(TABLE_ROWS); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); ListenableFuture executeSelectFut = connectionSpy.executeSelectAsync(SQL_QUERY); @@ -497,15 +511,17 @@ public void testFastQueryLongRunningAsync() assertEquals(res.getTotalRows(), 2); assertEquals(QUERY_SCHEMA, res.getSchema()); assertTrue(exSelRes.getIsSuccessful()); - verify(bigqueryRpcMock, times(1)).queryRpc(any(String.class), any(QueryRequest.class)); + verify(bigqueryRpcMock, times(1)) + .queryRpcSkipExceptionTranslation(any(String.class), any(QueryRequest.class)); } @Test public void testFastQuerySinglePageAsync() - throws BigQuerySQLException, ExecutionException, InterruptedException { + throws BigQuerySQLException, ExecutionException, InterruptedException, IOException { com.google.api.services.bigquery.model.QueryResponse mockQueryRes = new QueryResponse().setSchema(FAST_QUERY_TABLESCHEMA).setJobComplete(true); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); ConnectionImpl connectionSpy = Mockito.spy(connection); doReturn(BQ_RS_MOCK_RES) @@ -570,13 +586,14 @@ public void testExecuteSelectSlowWithParamsAsync() @Test public void testFastQueryMultiplePagesAsync() - throws BigQuerySQLException, ExecutionException, InterruptedException { + throws BigQuerySQLException, ExecutionException, InterruptedException, IOException { com.google.api.services.bigquery.model.QueryResponse mockQueryRes = new QueryResponse() .setSchema(FAST_QUERY_TABLESCHEMA) .setJobComplete(true) .setPageToken(PAGE_TOKEN); - when(bigqueryRpcMock.queryRpc(any(String.class), any(QueryRequest.class))) + when(bigqueryRpcMock.queryRpcSkipExceptionTranslation( + any(String.class), any(QueryRequest.class))) .thenReturn(mockQueryRes); ConnectionImpl connectionSpy = Mockito.spy(connection); @@ -600,7 +617,7 @@ public void testFastQueryMultiplePagesAsync() @Test // Emulates first page response using getQueryResultsFirstPage(jobId) and then subsequent pages // using getQueryResultsFirstPage(jobId) getSubsequentQueryResultsWithJob( - public void testLegacyQueryMultiplePages() throws SQLException { + public void testLegacyQueryMultiplePages() throws SQLException, IOException { ConnectionImpl connectionSpy = Mockito.spy(connection); com.google.api.services.bigquery.model.JobStatistics jobStatistics = new com.google.api.services.bigquery.model.JobStatistics(); @@ -619,13 +636,15 @@ public void testLegacyQueryMultiplePages() throws SQLException { .setId(JOB) .setStatus(new com.google.api.services.bigquery.model.JobStatus().setState("DONE")) .setStatistics(jobStatistics); - when(bigqueryRpcMock.createJobForQuery(any(com.google.api.services.bigquery.model.Job.class))) + when(bigqueryRpcMock.createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class))) .thenReturn(jobResponseMock); // RPC call in createQueryJob BigQueryResult res = connectionSpy.executeSelect(SQL_QUERY); assertEquals(res.getTotalRows(), 2); assertEquals(QUERY_SCHEMA, res.getSchema()); verify(bigqueryRpcMock, times(1)) - .createJobForQuery(any(com.google.api.services.bigquery.model.Job.class)); + .createJobForQuerySkipExceptionTranslation( + any(com.google.api.services.bigquery.model.Job.class)); verify(connectionSpy, times(1)) .tableDataList(any(GetQueryResultsResponse.class), any(JobId.class)); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java index a959a8991..a90b5c4d7 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java @@ -31,9 +31,11 @@ import com.google.cloud.RestorableState; import com.google.cloud.WriteChannel; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; -import com.google.cloud.bigquery.spi.v2.BigQueryRpc; +import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import java.io.IOException; +import java.net.ConnectException; import java.net.SocketException; +import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Random; @@ -68,7 +70,7 @@ public class TableDataWriteChannelTest { private BigQueryOptions options; private BigQueryRpcFactory rpcFactoryMock; - private BigQueryRpc bigqueryRpcMock; + private HttpBigQueryRpc bigqueryRpcMock; private BigQueryFactory bigqueryFactoryMock; private BigQuery bigqueryMock; private Job job; @@ -81,7 +83,7 @@ public class TableDataWriteChannelTest { @Before public void setUp() { rpcFactoryMock = mock(BigQueryRpcFactory.class); - bigqueryRpcMock = mock(BigQueryRpc.class); + bigqueryRpcMock = mock(HttpBigQueryRpc.class); bigqueryFactoryMock = mock(BigQueryFactory.class); bigqueryMock = mock(BigQuery.class); when(bigqueryMock.getOptions()).thenReturn(options); @@ -97,8 +99,8 @@ public void setUp() { } @Test - public void testCreate() { - when(bigqueryRpcMock.open( + public void testCreate() throws IOException { + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) @@ -107,26 +109,27 @@ public void testCreate() { assertTrue(writer.isOpen()); assertNull(writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); } @Test - public void testCreateRetryableError() { - BigQueryException exception = new BigQueryException(new SocketException("Socket closed")); - when(bigqueryRpcMock.open( + public void testCreateRetryableErrors() throws IOException { + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) - .thenThrow(exception) + .thenThrow(new SocketException("Socket closed")) + .thenThrow(new UnknownHostException()) + .thenThrow(new ConnectException()) .thenReturn(UPLOAD_ID); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); assertTrue(writer.isOpen()); assertNull(writer.getJob()); - verify(bigqueryRpcMock, times(2)) - .open( + verify(bigqueryRpcMock, times(4)) + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); @@ -134,12 +137,11 @@ public void testCreateRetryableError() { @Test public void testCreateNonRetryableError() throws IOException { - RuntimeException ex = new RuntimeException("expected"); - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) - .thenThrow(ex); + .thenThrow(new RuntimeException("expected")); try (TableDataWriteChannel channel = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION)) { Assert.fail(); @@ -147,7 +149,7 @@ public void testCreateNonRetryableError() throws IOException { Assert.assertEquals("java.lang.RuntimeException: expected", expected.getMessage()); } verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); @@ -155,7 +157,7 @@ public void testCreateNonRetryableError() throws IOException { @Test public void testWriteWithoutFlush() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) @@ -164,7 +166,7 @@ public void testWriteWithoutFlush() throws IOException { assertEquals(MIN_CHUNK_SIZE, writer.write(ByteBuffer.allocate(MIN_CHUNK_SIZE))); assertNull(writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); @@ -172,12 +174,12 @@ public void testWriteWithoutFlush() throws IOException { @Test public void testWriteWithFlush() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), @@ -192,12 +194,12 @@ public void testWriteWithFlush() throws IOException { assertArrayEquals(buffer.array(), capturedBuffer.getValue()); assertNull(writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write( + .writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), @@ -207,19 +209,22 @@ public void testWriteWithFlush() throws IOException { } @Test - public void testWritesAndFlush() throws IOException { - when(bigqueryRpcMock.open( + public void testWritesAndFlushRetryableErrors() throws IOException { + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(DEFAULT_CHUNK_SIZE), eq(false))) + .thenThrow(new SocketException("Socket closed")) + .thenThrow(new UnknownHostException()) + .thenThrow(new ConnectException()) .thenReturn(null); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE]; @@ -235,12 +240,53 @@ public void testWritesAndFlush() throws IOException { } assertNull(writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); + verify(bigqueryRpcMock, times(4)) + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), + capturedBuffer.capture(), + eq(0), + eq(0L), + eq(DEFAULT_CHUNK_SIZE), + eq(false)); + } + + @Test + public void testWritesAndFlushNonRetryableError() throws IOException { + when(bigqueryRpcMock.openSkipExceptionTranslation( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb()))) + .thenReturn(UPLOAD_ID); + when(bigqueryRpcMock.writeSkipExceptionTranslation( + eq(UPLOAD_ID), + capturedBuffer.capture(), + eq(0), + eq(0L), + eq(DEFAULT_CHUNK_SIZE), + eq(false))) + .thenThrow(new RuntimeException("expected")); + try { + writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); + ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE]; + for (int i = 0; i < buffers.length; i++) { + buffers[i] = randomBuffer(MIN_CHUNK_SIZE); + assertEquals(MIN_CHUNK_SIZE, writer.write(buffers[i])); + } + Assert.fail(); + } catch (RuntimeException expected) { + Assert.assertEquals("java.lang.RuntimeException: expected", expected.getMessage()); + } verify(bigqueryRpcMock) - .write( + .openSkipExceptionTranslation( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb())); + verify(bigqueryRpcMock, times(1)) + .writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), @@ -251,12 +297,12 @@ public void testWritesAndFlush() throws IOException { @Test public void testCloseWithoutFlush() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -266,23 +312,24 @@ public void testCloseWithoutFlush() throws IOException { assertTrue(!writer.isOpen()); assertEquals(job, writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); } @Test public void testCloseWithFlush() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); ByteBuffer buffer = randomBuffer(MIN_CHUNK_SIZE); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(MIN_CHUNK_SIZE), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -294,23 +341,23 @@ public void testCloseWithFlush() throws IOException { assertTrue(!writer.isOpen()); assertEquals(job, writer.getJob()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write( + .writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(MIN_CHUNK_SIZE), eq(true)); } @Test public void testWriteClosed() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -323,22 +370,23 @@ public void testWriteClosed() throws IOException { // expected } verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); } @Test public void testSaveAndRestore() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), @@ -359,12 +407,12 @@ public void testSaveAndRestore() throws IOException { assertArrayEquals(buffer2.array(), capturedBuffer.getAllValues().get(1)); assertEquals(new Long(DEFAULT_CHUNK_SIZE), capturedPosition.getAllValues().get(1)); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock, times(2)) - .write( + .writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), @@ -375,12 +423,12 @@ public void testSaveAndRestore() throws IOException { @Test public void testSaveAndRestoreClosed() throws IOException { - when(bigqueryRpcMock.open( + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) .thenReturn(UPLOAD_ID); - when(bigqueryRpcMock.write( + when(bigqueryRpcMock.writeSkipExceptionTranslation( eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) .thenReturn(job.toPb()); writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); @@ -398,17 +446,18 @@ public void testSaveAndRestoreClosed() throws IOException { assertArrayEquals(new byte[0], capturedBuffer.getValue()); assertEquals(expectedWriterState, restoredWriter.capture()); verify(bigqueryRpcMock) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb())); verify(bigqueryRpcMock) - .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + .writeSkipExceptionTranslation( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); } @Test - public void testStateEquals() { - when(bigqueryRpcMock.open( + public void testStateEquals() throws IOException { + when(bigqueryRpcMock.openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()))) @@ -424,7 +473,7 @@ public void testStateEquals() { assertEquals(state.hashCode(), state2.hashCode()); assertEquals(state.toString(), state2.toString()); verify(bigqueryRpcMock, times(2)) - .open( + .openSkipExceptionTranslation( new com.google.api.services.bigquery.model.Job() .setJobReference(JOB_INFO.getJobId().toPb()) .setConfiguration(LOAD_CONFIGURATION.toPb()));