From eb35ab916ac66b1465ab3d28c8b14ef649def585 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 17 Jan 2019 11:41:41 +0530 Subject: [PATCH 1/4] Fixes when found return null value --- .../google/cloud/bigquery/BigQueryImpl.java | 9 +++++ .../cloud/bigquery/BigQueryOptions.java | 9 +++++ .../cloud/bigquery/BigQueryImplTest.java | 34 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 09376fe13154..ede96720bbdf 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -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); @@ -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); @@ -702,6 +708,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); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index fecf5005f157..f9a07b674461 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -34,6 +34,7 @@ public class BigQueryOptions extends ServiceOptions { private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); private static final long serialVersionUID = -2437598817433266049L; private final String location; + private boolean setThrowNotFound; public static class DefaultBigQueryFactory implements BigQueryFactory { @@ -125,6 +126,14 @@ public String getLocation() { return location; } + public void setThrowNotFound(boolean setThrowNotFound) { + this.setThrowNotFound = setThrowNotFound; + } + + public boolean getThrowNotFound() { + return setThrowNotFound; + } + @SuppressWarnings("unchecked") @Override public Builder toBuilder() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 6d3348cae0a6..ea71f9fb68ad 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -366,6 +366,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)) @@ -603,6 +614,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)) @@ -1201,6 +1224,17 @@ public void testGetJobWithLocation() { 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)) From c88ccb478d7191d935afcd12c50a96f0dcfe3500 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 18 Jan 2019 14:41:18 +0530 Subject: [PATCH 2/4] added comment and modified methods name --- .../main/java/com/google/cloud/bigquery/BigQueryImpl.java | 7 ++++--- .../java/com/google/cloud/bigquery/BigQueryOptions.java | 1 + .../java/com/google/cloud/bigquery/BigQueryImplTest.java | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index ede96720bbdf..8e26b20ce48e 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -16,6 +16,7 @@ package com.google.cloud.bigquery; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static com.google.cloud.RetryHelper.runWithRetries; import static com.google.common.base.Preconditions.checkArgument; @@ -294,7 +295,7 @@ public com.google.api.services.bigquery.model.Dataset call() { EXCEPTION_HANDLER, getOptions().getClock()); if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(404, "Dataset not found"); + throw new BigQueryException(HTTP_NOT_FOUND, "Dataset not found"); } return answer == null ? null : Dataset.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { @@ -486,7 +487,7 @@ public com.google.api.services.bigquery.model.Table call() { EXCEPTION_HANDLER, getOptions().getClock()); if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(404, "Table not found"); + throw new BigQueryException(HTTP_NOT_FOUND, "Table not found"); } return answer == null ? null : Table.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { @@ -709,7 +710,7 @@ public com.google.api.services.bigquery.model.Job call() { EXCEPTION_HANDLER, getOptions().getClock()); if (getOptions().getThrowNotFound() && answer == null) { - throw new BigQueryException(404, "Job not found"); + throw new BigQueryException(HTTP_NOT_FOUND, "Job not found"); } return answer == null ? null : Job.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index f9a07b674461..ae4db388f80d 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -34,6 +34,7 @@ public class BigQueryOptions extends ServiceOptions { private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); private static final long serialVersionUID = -2437598817433266049L; private final String location; + // set the option ThrowNotFound when you want to throw the exception when the value not found private boolean setThrowNotFound; public static class DefaultBigQueryFactory implements BigQueryFactory { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index ea71f9fb68ad..36eaafe16608 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -367,7 +367,7 @@ public void testGetDataset() { } @Test - public void testGetDatasetNotFound() { + public void testGetDatasetNotFoundWhenThrowIsEnabled() { EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(404, "Dataset not found")); EasyMock.replay(bigqueryRpcMock); @@ -615,7 +615,7 @@ public void testGetTable() { } @Test - public void testGetTableNotFound() { + public void testGetTableNotFoundWhenThrowIsEnabled() { EasyMock.expect( bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(404, "Table not found")); @@ -1225,7 +1225,7 @@ public void testGetJobWithLocation() { } @Test - public void testGetJobNotFound() { + public void testGetJobNotFoundWhenThrowIsEnabled() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS)) .andThrow(new BigQueryException(404, "Job not found")); EasyMock.replay(bigqueryRpcMock); From 78aecd08816da83596187ed78719a9a91bfd8e8d Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 18 Jan 2019 17:32:41 +0530 Subject: [PATCH 3/4] format --- .../src/main/java/com/google/cloud/bigquery/BigQueryImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 8e26b20ce48e..173e0ca2ca7b 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -16,9 +16,9 @@ package com.google.cloud.bigquery; -import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static com.google.cloud.RetryHelper.runWithRetries; import static com.google.common.base.Preconditions.checkArgument; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import com.google.api.core.InternalApi; import com.google.api.gax.paging.Page; From 9096224bb1ae326a3af3f9e41a0e364e85671329 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Mon, 25 Feb 2019 17:47:36 +0530 Subject: [PATCH 4/4] added more test cases to cover code coverage --- .../cloud/bigquery/BigQueryImplTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 36eaafe16608..8ee3bac25e55 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -366,6 +366,18 @@ public void testGetDataset() { new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } + @Test + public void testGetDatasetNotFoundWhenThrowIsDisabled() { + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) + .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(false); + bigquery = options.getService(); + Dataset dataset = bigquery.getDataset(DATASET); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); + } + @Test public void testGetDatasetNotFoundWhenThrowIsEnabled() { EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) @@ -614,6 +626,17 @@ public void testGetTable() { assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); } + @Test + public void testGetTableNotFoundWhenThrowIsDisabled() { + EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(false); + bigquery = options.getService(); + Table table = bigquery.getTable(DATASET, TABLE); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); + } + @Test public void testGetTableNotFoundWhenThrowIsEnabled() { EasyMock.expect( @@ -1224,6 +1247,17 @@ public void testGetJobWithLocation() { assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); } + @Test + public void testGetJobNotFoundWhenThrowIsDisabled() { + EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS)) + .andReturn(COMPLETE_COPY_JOB.toPb()); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(false); + bigquery = options.getService(); + Job job = bigquery.getJob(JOB); + assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); + } + @Test public void testGetJobNotFoundWhenThrowIsEnabled() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS))