From 9e5c7c7a6a0e32ce145a727bcdc1493e149f3b14 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 30 Oct 2020 20:45:43 +0530 Subject: [PATCH] fix: nullpointerexception for listroutines and listmodels (#890) --- .../bigquery/spi/v2/HttpBigQueryRpc.java | 8 +++++-- .../cloud/bigquery/it/ITBigQueryTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) 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 d620f50cc..4d9558454 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 @@ -388,7 +388,8 @@ public Tuple> listModels( .setMaxResults(Option.MAX_RESULTS.getLong(options)) .setPageToken(Option.PAGE_TOKEN.getString(options)) .execute(); - Iterable models = modelList.getModels(); + Iterable models = + modelList.getModels() != null ? modelList.getModels() : ImmutableList.of(); return Tuple.of(modelList.getNextPageToken(), models); } catch (IOException ex) { throw translate(ex); @@ -456,7 +457,10 @@ public Tuple> listRoutines( .setMaxResults(Option.MAX_RESULTS.getLong(options)) .setPageToken(Option.PAGE_TOKEN.getString(options)) .execute(); - Iterable routines = routineList.getRoutines(); + Iterable routines = + routineList.getRoutines() != null + ? routineList.getRoutines() + : ImmutableList.of(); return Tuple.of(routineList.getNextPageToken(), routines); } catch (IOException ex) { throw translate(ex); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 0338d3505..29f19f3f4 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -146,6 +146,7 @@ public class ITBigQueryTest { private static final String MODEL_DATASET = RemoteBigQueryHelper.generateDatasetName(); private static final String ROUTINE_DATASET = RemoteBigQueryHelper.generateDatasetName(); private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); + private static final String RANDOM_ID = UUID.randomUUID().toString().substring(0, 8); private static final Map LABELS = ImmutableMap.of( "example-label1", "example-value1", @@ -1425,6 +1426,29 @@ public void testModelLifecycle() throws InterruptedException { assertTrue(bigquery.delete(modelId)); } + @Test + public void testEmptyListModels() { + String datasetId = "test_empty_dataset_list_models_" + RANDOM_ID; + assertNotNull(bigquery.create(DatasetInfo.of(datasetId))); + Page models = bigquery.listModels(datasetId, BigQuery.ModelListOption.pageSize(100)); + assertEquals(0, Iterables.size(models.getValues())); + assertFalse(models.hasNextPage()); + assertNull(models.getNextPageToken()); + assertTrue(bigquery.delete(datasetId)); + } + + @Test + public void testEmptyListRoutines() { + String datasetId = "test_empty_dataset_list_routines_" + RANDOM_ID; + assertNotNull(bigquery.create(DatasetInfo.of(datasetId))); + Page routines = + bigquery.listRoutines(datasetId, BigQuery.RoutineListOption.pageSize(100)); + assertEquals(0, Iterables.size(routines.getValues())); + assertFalse(routines.hasNextPage()); + assertNull(routines.getNextPageToken()); + assertTrue(bigquery.delete(datasetId)); + } + @Test public void testRoutineLifecycle() throws InterruptedException {