Skip to content

Commit

Permalink
Fixes when found return null value
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani committed Jan 18, 2019
1 parent 62d4bd3 commit 9b5ede8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ public com.google.api.services.bigquery.model.Dataset call() {
getOptions().getRetrySettings(),
EXCEPTION_HANDLER,
getOptions().getClock());
if (getOptions().getThrowNotFound() && answer == null) {
throw new BigQueryException(404, "Dataset not found");
}
return answer == null ? null : Dataset.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
Expand Down Expand Up @@ -482,6 +485,9 @@ public com.google.api.services.bigquery.model.Table call() {
getOptions().getRetrySettings(),
EXCEPTION_HANDLER,
getOptions().getClock());
if (getOptions().getThrowNotFound() && answer == null) {
throw new BigQueryException(404, "Table not found");
}
return answer == null ? null : Table.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
Expand Down Expand Up @@ -696,6 +702,9 @@ public com.google.api.services.bigquery.model.Job call() {
getOptions().getRetrySettings(),
EXCEPTION_HANDLER,
getOptions().getClock());
if (getOptions().getThrowNotFound() && answer == null) {
throw new BigQueryException(404, "Job not found");
}
return answer == null ? null : Job.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery";
private static final Set<String> SCOPES = ImmutableSet.of(BIGQUERY_SCOPE);
private static final long serialVersionUID = -2437598817433266049L;
private boolean setThrowNotFound;

public static class DefaultBigQueryFactory implements BigQueryFactory {

Expand Down Expand Up @@ -112,6 +113,14 @@ protected BigQueryRpc getBigQueryRpcV2() {
return (BigQueryRpc) getRpc();
}

public void setThrowNotFound(boolean setThrowNotFound) {
this.setThrowNotFound = setThrowNotFound;
}

public boolean getThrowNotFound() {
return setThrowNotFound;
}

@SuppressWarnings("unchecked")
@Override
public Builder toBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ public void testGetDataset() {
new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset);
}

@Test
public void testGetDatasetNotFound() {
EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS))
.andThrow(new BigQueryException(404, "Dataset not found"));
EasyMock.replay(bigqueryRpcMock);
options.setThrowNotFound(true);
bigquery = options.getService();
thrown.expect(BigQueryException.class);
bigquery.getDataset("dataset-not-found");
}

@Test
public void testGetDatasetFromDatasetId() {
EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS))
Expand Down Expand Up @@ -592,6 +603,18 @@ public void testGetTable() {
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table);
}

@Test
public void testGetTableNotFound() {
EasyMock.expect(
bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS))
.andThrow(new BigQueryException(404, "Table not found"));
EasyMock.replay(bigqueryRpcMock);
options.setThrowNotFound(true);
bigquery = options.getService();
thrown.expect(BigQueryException.class);
bigquery.getTable(DATASET, "table-not-found");
}

@Test
public void testGetTableFromTableId() {
EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
Expand Down Expand Up @@ -1179,6 +1202,17 @@ public void testGetJob() {
assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job);
}

@Test
public void testGetJobNotFound() {
EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS))
.andThrow(new BigQueryException(404, "Job not found"));
EasyMock.replay(bigqueryRpcMock);
options.setThrowNotFound(true);
bigquery = options.getService();
thrown.expect(BigQueryException.class);
bigquery.getJob("job-not-found");
}

@Test
public void testGetJobFromJobId() {
EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS))
Expand Down

0 comments on commit 9b5ede8

Please sign in to comment.