diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SerializationTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SerializationTest.java index 542b72e1c693..5e7a47781ca3 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SerializationTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SerializationTest.java @@ -230,10 +230,7 @@ protected Serializable[] serializableObjects() { .setProjectId("p1") .setNoCredentials() .build(); - BigQueryOptions otherOptions = options.toBuilder() - .setProjectId("p2") - .setCredentials(null) - .build(); + BigQueryOptions otherOptions = options.toBuilder().setProjectId("p2").build(); return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION, EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java index 5d98610d98a9..779bfa37b6ff 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java @@ -269,7 +269,6 @@ protected Serializable[] serializableObjects() { ComputeOptions otherOptions = options.toBuilder() .setProjectId("p2") .setRetryParams(RetryParams.getDefaultInstance()) - .setCredentials(null) .build(); return new Serializable[]{DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID, REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE, DEPRECATION_STATUS, GLOBAL_OPERATION_ID, diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 33ecac6eee1a..c938c0eb411d 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -18,6 +18,7 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.auth.Credentials; @@ -213,13 +214,18 @@ public B setHost(String host) { } /** - * Sets the service authentication credentials. + * Sets the service authentication credentials. If this method or {@link #setNoCredentials() are + * not used on the builder, {@link GoogleCredentials#getApplicationDefault()} will be used to + * attempt getting credentials from the environment. * + * @param credentials authentication credentials, should not be {@code null} * @return the builder + * @throws NullPointerException if {@code credentials} is {@code null}. To disable + * authentication use {@link Builder#setNoCredentials()} */ public B setCredentials(Credentials credentials) { + this.credentials = checkNotNull(credentials); this.noCredentials = false; - this.credentials = credentials; return self(); } diff --git a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index f21c37ad55d8..1565aa4aecd8 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -26,7 +26,9 @@ import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spi.ServiceRpcFactory; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -105,6 +107,9 @@ public class ServiceOptionsTest { private static final Pattern APPLICATION_NAME_PATTERN = Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?"); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static class TestClock extends Clock { @Override public long millis() { @@ -226,6 +231,12 @@ public void testBuilderNoCredentials() { assertSame(RetryParams.noRetries(), OPTIONS_NO_CREDENTIALS.getRetryParams()); } + @Test + public void testBuilderNullCredentials() { + thrown.expect(NullPointerException.class); + TestServiceOptions.newBuilder().setCredentials(null).build(); + } + @Test public void testBuilderDeprecated() { assertSame(credentials, DEPRECATED_OPTIONS.getCredentials()); diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/SerializationTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/SerializationTest.java index 40be3a3321ac..b3135c247e9e 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/SerializationTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/SerializationTest.java @@ -113,10 +113,7 @@ protected java.io.Serializable[] serializableObjects() { .setNoCredentials() .setProjectId("ds1") .build(); - DatastoreOptions otherOptions = options.toBuilder() - .setNamespace("ns1") - .setCredentials(null) - .build(); + DatastoreOptions otherOptions = options.toBuilder().setNamespace("ns1").build(); return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, diff --git a/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java b/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java index 344d7dabb808..37ac272d010c 100644 --- a/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java +++ b/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java @@ -91,9 +91,7 @@ protected Serializable[] serializableObjects() { .setNoCredentials() .setProjectId("id1") .build(); - DnsOptions otherOptions = options.toBuilder() - .setCredentials(null) - .build(); + DnsOptions otherOptions = options.toBuilder().build(); return new Serializable[]{FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION, RECORD_SET_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION, PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE, diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java index a12c1602d18d..354a49dded60 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java @@ -67,10 +67,7 @@ protected Serializable[] serializableObjects() { .setProjectId("p1") .setNoCredentials() .build(); - StorageOptions otherOptions = options.toBuilder() - .setProjectId("p2") - .setCredentials(null) - .build(); + StorageOptions otherOptions = options.toBuilder().setProjectId("p2").build(); return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, diff --git a/google-cloud-translate/src/test/java/com/google/cloud/translate/SerializationTest.java b/google-cloud-translate/src/test/java/com/google/cloud/translate/SerializationTest.java index 03a4e9af8c2a..d6cfdc40e93e 100644 --- a/google-cloud-translate/src/test/java/com/google/cloud/translate/SerializationTest.java +++ b/google-cloud-translate/src/test/java/com/google/cloud/translate/SerializationTest.java @@ -49,9 +49,7 @@ protected Serializable[] serializableObjects() { .setApiKey(API_KEY) .setNoCredentials() .build(); - TranslateOptions otherOptions = options.toBuilder() - .setCredentials(null) - .build(); + TranslateOptions otherOptions = options.toBuilder().build(); return new Serializable[]{DETECTION, TRANSLATION, TRANSLATE_EXCEPTION, LANGUAGE_LIST_OPTION, TRANSLATE_OPTION, options, otherOptions}; }