diff --git a/google-cloud-bigquery/pom.xml b/google-cloud-bigquery/pom.xml
index 102c405f2..9d5631a5f 100644
--- a/google-cloud-bigquery/pom.xml
+++ b/google-cloud-bigquery/pom.xml
@@ -102,6 +102,12 @@
org.objenesis
objenesis
+
+ org.hamcrest
+ hamcrest-core
+ 1.3
+ test
+
diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java
index 64f911d90..1c465343a 100644
--- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java
+++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardTableDefinition.java
@@ -272,7 +272,31 @@ static StandardTableDefinition fromPb(Table tablePb) {
builder.setStreamingBuffer(StreamingBuffer.fromPb(tablePb.getStreamingBuffer()));
}
if (tablePb.getTimePartitioning() != null) {
- builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
+ try {
+ builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(
+ "Illegal Argument - Got unexpected time partitioning "
+ + tablePb.getTimePartitioning().toString()
+ + " in project "
+ + tablePb.getTableReference().getProjectId()
+ + " in dataset "
+ + tablePb.getTableReference().getDatasetId()
+ + " in table "
+ + tablePb.getTableReference().getTableId(),
+ e);
+ } catch (NullPointerException e) {
+ throw new NullPointerException(
+ "Null pointer - Got unexpected time partitioning "
+ + tablePb.getTimePartitioning().toString()
+ + " in project "
+ + tablePb.getTableReference().getProjectId()
+ + " in dataset "
+ + tablePb.getTableReference().getDatasetId()
+ + " in table "
+ + tablePb.getTableReference().getTableId()
+ + e.toString());
+ }
}
if (tablePb.getRangePartitioning() != null) {
builder.setRangePartitioning(RangePartitioning.fromPb(tablePb.getRangePartitioning()));
diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java
index 7bfbf6275..d1dd0cc29 100644
--- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java
+++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java
@@ -16,11 +16,17 @@
package com.google.cloud.bigquery;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import com.google.api.services.bigquery.model.Streamingbuffer;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableReference;
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
@@ -118,6 +124,62 @@ public void testToAndFromPb() {
definition, TableDefinition.fromPb(definition.toPb()));
}
+ @Test
+ public void testFromPbWithUnexpectedTimePartitioningTypeRaisesInvalidArgumentException() {
+ Table invalidTable =
+ new Table()
+ .setType("TABLE")
+ .setTableReference(
+ new TableReference()
+ .setProjectId("ILLEGAL_ARG_TEST_PROJECT")
+ .setDatasetId("ILLEGAL_ARG_TEST_DATASET")
+ .setTableId("ILLEGAL_ARG_TEST_TABLE"))
+ .setTimePartitioning(
+ new com.google.api.services.bigquery.model.TimePartitioning().setType("GHURRY"));
+ try {
+ StandardTableDefinition.fromPb(invalidTable);
+ } catch (IllegalArgumentException ie) {
+ assertThat(
+ ie.getMessage(),
+ allOf(
+ containsString("Illegal Argument - Got unexpected time partitioning"),
+ containsString("GHURRY"),
+ containsString("ILLEGAL_ARG_TEST_PROJECT"),
+ containsString("ILLEGAL_ARG_TEST_DATASET"),
+ containsString("ILLEGAL_ARG_TEST_TABLE")));
+ return;
+ }
+ fail("testFromPb illegal argument exception did not throw!");
+ }
+
+ @Test
+ public void testFromPbWithNullTimePartitioningTypeRaisesNullPointerException() {
+ Table invalidTable =
+ new Table()
+ .setType("TABLE")
+ .setTableReference(
+ new TableReference()
+ .setProjectId("NULL_PTR_TEST_PROJECT")
+ .setDatasetId("NULL_PTR_TEST_DATASET")
+ .setTableId("NULL_PTR_TEST_TABLE"))
+ .setTimePartitioning(
+ new com.google.api.services.bigquery.model.TimePartitioning().setType(null));
+ try {
+ StandardTableDefinition.fromPb(invalidTable);
+ } catch (NullPointerException ne) {
+ assertThat(
+ ne.getMessage(),
+ allOf(
+ containsString("Null pointer - Got unexpected time partitioning"),
+ containsString("null"),
+ containsString("NULL_PTR_TEST_PROJECT"),
+ containsString("NULL_PTR_TEST_DATASET"),
+ containsString("NULL_PTR_TEST_TABLE")));
+ return;
+ }
+ fail("testFromPb null pointer exception did not throw!");
+ }
+
@Test
public void testFromPbWithNullEstimatedRowsAndBytes() {
StandardTableDefinition.fromPb(