From 558af8eff27273fa7129917503ea1133e23fa91d Mon Sep 17 00:00:00 2001 From: detmerl Date: Tue, 27 Aug 2024 13:08:39 -0400 Subject: [PATCH 01/14] added setApiKey() method to client settings --- gapic-generator-java-pom-parent/pom.xml | 2 +- .../com/google/api/gax/rpc/ClientContext.java | 5 ++++ .../google/api/gax/rpc/ClientSettings.java | 14 +++++++++++ .../com/google/api/gax/rpc/StubSettings.java | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gapic-generator-java-pom-parent/pom.xml b/gapic-generator-java-pom-parent/pom.xml index c5cbdccaec..1f242ed128 100644 --- a/gapic-generator-java-pom-parent/pom.xml +++ b/gapic-generator-java-pom-parent/pom.xml @@ -27,7 +27,7 @@ consistent across modules in this repository --> 1.3.2 1.66.0 - 1.24.1 + 1.24.1-SNAPSHOT 1.44.2 2.11.0 33.2.1-jre diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 09c05b7838..0c6013db43 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -43,6 +43,7 @@ import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials; import com.google.api.gax.tracing.ApiTracerFactory; import com.google.api.gax.tracing.BaseApiTracerFactory; +import com.google.auth.ApiKeyCredentials; import com.google.auth.Credentials; import com.google.auth.oauth2.GdchCredentials; import com.google.auto.value.AutoValue; @@ -240,6 +241,10 @@ public static ClientContext create(StubSettings settings) throws IOException { if (credentials != null) { defaultCallContext = defaultCallContext.withCredentials(credentials); } + //TODO: if we decided to add setApiKey method need to check and throw exception if apikey and credentials are provided + if (settings.getApiKey() != null) { + defaultCallContext = defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); + } defaultCallContext = defaultCallContext.withEndpointContext(endpointContext); WatchdogProvider watchdogProvider = settings.getStreamWatchdogProvider(); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index b5e54484dd..ad06cd113d 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -107,6 +107,8 @@ public final String getQuotaProjectId() { return stubSettings.getQuotaProjectId(); } + public final String getApiKey() {return stubSettings.getApiKey();} + @Nullable public final WatchdogProvider getWatchdogProvider() { return stubSettings.getStreamWatchdogProvider(); @@ -302,6 +304,15 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { return self(); } + /** + * Sets the API key. The API key will be passed to API call request via the x-goog-api-key + * header to authenticate the API call. + */ + public B setApiKey(String apiKey) { + stubSettings.setApiKey(apiKey); + return self(); + } + /** * Gets the ExecutorProvider that was previously set on this Builder. This ExecutorProvider is * to use for running asynchronous API call logic (such as retries and long-running operations), @@ -364,6 +375,8 @@ public WatchdogProvider getWatchdogProvider() { return stubSettings.getStreamWatchdogProvider(); } + public String getApiKey() {return stubSettings.getApiKey(); } + /** This method is obsolete. Use {@link #getWatchdogCheckIntervalDuration()} instead */ @Nullable @ObsoleteApi("Use getWatchdogCheckIntervalDuration() instead") @@ -405,6 +418,7 @@ public String toString() { .add("watchdogProvider", getWatchdogProvider()) .add("watchdogCheckInterval", getWatchdogCheckIntervalDuration()) .add("gdchApiAudience", getGdchApiAudience()) + .add("apiKey", getApiKey()) .toString(); } } diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index daeda4c445..f78daf0eef 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -82,6 +82,7 @@ public abstract class StubSettings> { // Track if deprecated setExecutorProvider is called private boolean deprecatedExecutorProviderSet; @Nonnull private final EndpointContext endpointContext; + private final String apiKey; /** * Indicate when creating transport whether it is allowed to use mTLS endpoint instead of the @@ -107,6 +108,7 @@ protected StubSettings(Builder builder) { this.deprecatedExecutorProviderSet = builder.deprecatedExecutorProviderSet; this.gdchApiAudience = builder.gdchApiAudience; this.endpointContext = buildEndpointContext(builder); + this.apiKey = builder.apiKey; } /** @@ -234,6 +236,10 @@ public final String getGdchApiAudience() { return gdchApiAudience; } + public final String getApiKey() { + return apiKey; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -252,6 +258,7 @@ public String toString() { .add("streamWatchdogCheckInterval", streamWatchdogCheckInterval) .add("tracerFactory", tracerFactory) .add("gdchApiAudience", gdchApiAudience) + .add("apiKey", apiKey) .toString(); } @@ -277,6 +284,7 @@ public abstract static class Builder< private boolean deprecatedExecutorProviderSet; private String universeDomain; private final EndpointContext endpointContext; + private String apiKey; /** * Indicate when creating transport whether it is allowed to use mTLS endpoint instead of the @@ -301,6 +309,7 @@ protected Builder(StubSettings settings) { this.tracerFactory = settings.tracerFactory; this.deprecatedExecutorProviderSet = settings.deprecatedExecutorProviderSet; this.gdchApiAudience = settings.gdchApiAudience; + this.apiKey = settings.apiKey; // The follow settings will be set to the original user configurations as the // EndpointContext will be rebuilt in the constructor. @@ -353,6 +362,7 @@ protected Builder(ClientContext clientContext) { this.mtlsEndpoint = null; this.switchToMtlsEndpointAllowed = false; this.universeDomain = null; + this.apiKey = null; // Attempt to create an empty, non-functioning EndpointContext by default. The client will // have // a valid EndpointContext with user configurations after the client has been initialized. @@ -574,6 +584,15 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { return self(); } + /** + * Sets the API key. The API key will be passed to API call request via the x-goog-api-key + * header to authenticate the API call. + */ + public B setApiKey(String apiKey) { + this.apiKey = apiKey; + return self(); + } + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public ExecutorProvider getExecutorProvider() { @@ -616,6 +635,10 @@ public ApiClock getClock() { return clock; } + public String getApiKey() { + return apiKey; + } + /** * @return the resolved endpoint when the Builder was created. If invoked after * `StubSettings.newBuilder()` is called, it will return the clientSettingsEndpoint value. From dad48d4f2e1f508474b640b0f59307eb04682067 Mon Sep 17 00:00:00 2001 From: detmerl Date: Wed, 28 Aug 2024 10:29:16 -0400 Subject: [PATCH 02/14] cleaned up and added logic for throwing error if both api key and credentials are provided --- .../com/google/api/gax/rpc/ClientContext.java | 4 +- .../google/api/gax/rpc/ClientSettings.java | 16 ++++--- .../com/google/api/gax/rpc/StubSettings.java | 11 ++--- .../google/api/gax/rpc/ClientContextTest.java | 31 +++++++++++-- .../api/gax/rpc/ClientSettingsTest.java | 45 +++++++++++++++++++ 5 files changed, 92 insertions(+), 15 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 0c6013db43..d66406c0ad 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -238,12 +238,12 @@ public static ClientContext create(StubSettings settings) throws IOException { ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel); + if (credentials != null) { defaultCallContext = defaultCallContext.withCredentials(credentials); } - //TODO: if we decided to add setApiKey method need to check and throw exception if apikey and credentials are provided if (settings.getApiKey() != null) { - defaultCallContext = defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); + defaultCallContext = defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); } defaultCallContext = defaultCallContext.withEndpointContext(endpointContext); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index ad06cd113d..6e06cb0727 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -38,6 +38,7 @@ import com.google.api.gax.core.ExecutorProvider; import com.google.common.base.MoreObjects; import java.io.IOException; +import java.lang.IllegalArgumentException; import java.util.concurrent.Executor; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -58,6 +59,9 @@ public abstract class ClientSettings /** Constructs an instance of ClientSettings. */ protected ClientSettings(Builder builder) throws IOException { + if (builder.stubSettings.getApiKey() != null && builder.stubSettings.getCredentialsProvider().getCredentials() != null) { + throw new IllegalArgumentException("You can not provide both ApiKey and Credentials for a client."); + } this.stubSettings = builder.stubSettings.build(); } @@ -107,13 +111,13 @@ public final String getQuotaProjectId() { return stubSettings.getQuotaProjectId(); } - public final String getApiKey() {return stubSettings.getApiKey();} - @Nullable public final WatchdogProvider getWatchdogProvider() { return stubSettings.getStreamWatchdogProvider(); } + public final String getApiKey() { return stubSettings.getApiKey(); } + /** This method is obsolete. Use {@link #getWatchdogCheckIntervalDuration()} instead. */ @Nonnull @ObsoleteApi("Use getWatchdogCheckIntervalDuration() instead") @@ -131,6 +135,7 @@ public final String getGdchApiAudience() { return stubSettings.getGdchApiAudience(); } + public String toString() { return MoreObjects.toStringHelper(this) .add("executorProvider", getExecutorProvider()) @@ -146,6 +151,7 @@ public String toString() { .add("watchdogProvider", getWatchdogProvider()) .add("watchdogCheckInterval", getWatchdogCheckInterval()) .add("gdchApiAudience", getGdchApiAudience()) + .add("apiKey", getApiKey()) .toString(); } @@ -305,8 +311,7 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { } /** - * Sets the API key. The API key will be passed to API call request via the x-goog-api-key - * header to authenticate the API call. + * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in [CallContext]. */ public B setApiKey(String apiKey) { stubSettings.setApiKey(apiKey); @@ -375,7 +380,8 @@ public WatchdogProvider getWatchdogProvider() { return stubSettings.getStreamWatchdogProvider(); } - public String getApiKey() {return stubSettings.getApiKey(); } + /** Gets the ApiKey that was previously set on this Builder. */ + public String getApiKey() { return stubSettings.getApiKey(); } /** This method is obsolete. Use {@link #getWatchdogCheckIntervalDuration()} instead */ @Nullable diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index f78daf0eef..40b581868a 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -236,8 +236,9 @@ public final String getGdchApiAudience() { return gdchApiAudience; } + /** Gets the ApiKey that should be used for authentication. If an empty string was provided it will return null */ public final String getApiKey() { - return apiKey; + return (apiKey == null || apiKey.isEmpty()) ? null : apiKey; } @Override @@ -585,8 +586,7 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { } /** - * Sets the API key. The API key will be passed to API call request via the x-goog-api-key - * header to authenticate the API call. + * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in [CallContext]. */ public B setApiKey(String apiKey) { this.apiKey = apiKey; @@ -635,8 +635,9 @@ public ApiClock getClock() { return clock; } - public String getApiKey() { - return apiKey; + /** Gets the ApiKey that was previously set on this Builder. If an empty string was provided it will return null */ + public final String getApiKey() { + return (apiKey == null || apiKey.isEmpty()) ? null : apiKey; } /** diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 5efce3fbe9..39f716a879 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -52,6 +52,7 @@ import com.google.api.gax.rpc.testing.FakeClientSettings; import com.google.api.gax.rpc.testing.FakeStubSettings; import com.google.api.gax.rpc.testing.FakeTransportChannel; +import com.google.auth.ApiKeyCredentials; import com.google.auth.Credentials; import com.google.auth.oauth2.ComputeEngineCredentials; import com.google.auth.oauth2.GdchCredentials; @@ -205,9 +206,6 @@ public TransportChannelProvider withPoolSize(int size) { @Override public TransportChannel getTransportChannel() throws IOException { - if (needsCredentials()) { - throw new IllegalStateException("Needs Credentials"); - } transport.setExecutor(executor); return transport; } @@ -1074,4 +1072,31 @@ public void testStreamWatchdogInterval_backportMethodsBehaveCorrectly() { ct -> ct.getStreamWatchdogCheckIntervalDuration(), ct -> ct.getStreamWatchdogCheckInterval()); } + + @Test + public void testSetApiKey_createsApiCredentials() throws IOException { + String apiKey = "key"; + FakeStubSettings.Builder builder = new FakeStubSettings.Builder(); + InterceptingExecutor executor = new InterceptingExecutor(1); + FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); + FakeTransportProvider transportProvider = + new FakeTransportProvider( + transportChannel, + executor, + true, + ImmutableMap.of(), + null, + DEFAULT_ENDPOINT); + builder.setTransportChannelProvider(transportProvider); + + HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); + Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); + builder.setHeaderProvider(headerProvider); + builder.setApiKey(apiKey); + + ClientContext context = ClientContext.create(builder.build()); + + FakeCallContext fakeCallContext = (FakeCallContext) context.getDefaultCallContext(); + assertThat(fakeCallContext.getCredentials()).isInstanceOf(ApiKeyCredentials.class); + } } diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index 3e99f7471c..95409da115 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -31,6 +31,7 @@ import static com.google.api.gax.util.TimeConversionTestUtils.testDurationMethod; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.when; import com.google.api.core.ApiClock; import com.google.api.core.NanoClock; @@ -56,6 +57,9 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.function.Function; import java.util.function.Supplier; + +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -120,6 +124,7 @@ void testEmptyBuilder() throws Exception { Truth.assertThat(builder.getWatchdogCheckIntervalDuration()) .isGreaterThan(java.time.Duration.ZERO); Truth.assertThat(builder.getQuotaProjectId()).isNull(); + Truth.assertThat(builder.getApiKey()).isNull(); FakeClientSettings settings = builder.build(); Truth.assertThat(settings.getExecutorProvider()) @@ -150,6 +155,7 @@ void testEmptyBuilder() throws Exception { Truth.assertThat(settingsString).contains("watchdogProvider"); Truth.assertThat(settingsString).contains("watchdogCheckInterval"); Truth.assertThat(settingsString).contains(("quotaProjectId")); + Truth.assertThat(settingsString).contains(("apiKey")); } @Test @@ -165,6 +171,7 @@ void testBuilder() throws Exception { WatchdogProvider watchdogProvider = Mockito.mock(WatchdogProvider.class); java.time.Duration watchdogCheckInterval = java.time.Duration.ofSeconds(13); String quotaProjectId = "test_quota_project_id"; + String apiKey = "api_key"; builder.setExecutorProvider(executorProvider); builder.setTransportChannelProvider(transportProvider); @@ -175,6 +182,7 @@ void testBuilder() throws Exception { builder.setWatchdogProvider(watchdogProvider); builder.setWatchdogCheckIntervalDuration(watchdogCheckInterval); builder.setQuotaProjectId(quotaProjectId); + builder.setApiKey(apiKey); // For backward compatibility, backgroundExecutorProvider is set to executorProvider Truth.assertThat(builder.getExecutorProvider()).isSameInstanceAs(executorProvider); @@ -188,6 +196,7 @@ void testBuilder() throws Exception { Truth.assertThat(builder.getWatchdogCheckIntervalDuration()) .isSameInstanceAs(watchdogCheckInterval); Truth.assertThat(builder.getQuotaProjectId()).isEqualTo(quotaProjectId); + Truth.assertThat(builder.getApiKey()).isEqualTo(apiKey); String builderString = builder.toString(); Truth.assertThat(builderString).contains("executorProvider"); @@ -200,6 +209,7 @@ void testBuilder() throws Exception { Truth.assertThat(builderString).contains("watchdogProvider"); Truth.assertThat(builderString).contains("watchdogCheckInterval"); Truth.assertThat(builderString).contains("quotaProjectId"); + Truth.assertThat(builderString).contains("apiKey"); } @Test @@ -262,6 +272,7 @@ void testBuilderFromSettings() throws Exception { WatchdogProvider watchdogProvider = Mockito.mock(WatchdogProvider.class); java.time.Duration watchdogCheckInterval = java.time.Duration.ofSeconds(14); String quotaProjectId = "test_builder_from_settings_quotaProjectId"; + String apiKey = "api_key"; builder.setExecutorProvider(executorProvider); builder.setTransportChannelProvider(transportProvider); @@ -272,6 +283,7 @@ void testBuilderFromSettings() throws Exception { builder.setWatchdogProvider(watchdogProvider); builder.setWatchdogCheckIntervalDuration(watchdogCheckInterval); builder.setQuotaProjectId(quotaProjectId); + builder.setApiKey(apiKey); FakeClientSettings settings = builder.build(); FakeClientSettings.Builder newBuilder = new FakeClientSettings.Builder(settings); @@ -288,6 +300,7 @@ void testBuilderFromSettings() throws Exception { Truth.assertThat(newBuilder.getWatchdogCheckIntervalDuration()) .isEqualTo(watchdogCheckInterval); Truth.assertThat(newBuilder.getQuotaProjectId()).isEqualTo(quotaProjectId); + Truth.assertThat(newBuilder.getApiKey()).isEqualTo(apiKey); } @Test @@ -566,4 +579,36 @@ public void testWatchdogCheckInterval_backportMethodsBehaveCorrectly() { cs -> cs.getWatchdogCheckIntervalDuration(), cs -> cs.getWatchdogCheckInterval()); } + + @Test + void testClientSettingsBuilder_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exception { + FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); + CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); + when(credentialsProvider.getCredentials()).thenReturn( Mockito.mock(Credentials.class)); + builder.setCredentialsProvider(credentialsProvider); + builder.setApiKey("api_key"); + + try { + builder.build(); + fail("No exception raised"); + } catch (IllegalArgumentException e) { + Assertions.assertTrue(e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); + } + } + + @Test + void testEmptyApiKeyClientSettingsBuild_isTreatedAsNull() throws Exception { + FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); + CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); + Credentials credentials = Mockito.mock(Credentials.class); + when(credentialsProvider.getCredentials()).thenReturn(credentials); + builder.setCredentialsProvider(credentialsProvider); + builder.setApiKey(""); + + + FakeClientSettings fakeClientSettings = builder.build(); + + Assertions.assertEquals(fakeClientSettings.getCredentialsProvider().getCredentials(), credentials); + Assertions.assertNull(fakeClientSettings.getApiKey()); + } } From f6afbef19b331af2a6a4c3d679d484073b0800ef Mon Sep 17 00:00:00 2001 From: detmerl Date: Wed, 28 Aug 2024 10:40:58 -0400 Subject: [PATCH 03/14] fixed formatting --- .../com/google/api/gax/rpc/ClientContext.java | 3 +- .../google/api/gax/rpc/ClientSettings.java | 1 - .../com/google/api/gax/rpc/StubSettings.java | 33 ++++++++++++++----- .../google/api/gax/rpc/ClientContextTest.java | 9 ++--- .../api/gax/rpc/ClientSettingsTest.java | 2 -- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index d66406c0ad..c410a31cb2 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -243,7 +243,8 @@ public static ClientContext create(StubSettings settings) throws IOException { defaultCallContext = defaultCallContext.withCredentials(credentials); } if (settings.getApiKey() != null) { - defaultCallContext = defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); + defaultCallContext = + defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); } defaultCallContext = defaultCallContext.withEndpointContext(endpointContext); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index 6e06cb0727..ee08b66c8d 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -38,7 +38,6 @@ import com.google.api.gax.core.ExecutorProvider; import com.google.common.base.MoreObjects; import java.io.IOException; -import java.lang.IllegalArgumentException; import java.util.concurrent.Executor; import javax.annotation.Nonnull; import javax.annotation.Nullable; diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 40b581868a..1b88cf552a 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -132,7 +132,9 @@ private EndpointContext buildEndpointContext(Builder builder) { } } - /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ + /** + * @deprecated Please use {@link #getBackgroundExecutorProvider()}. + */ @Deprecated public final ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; @@ -174,17 +176,23 @@ protected String getServiceName() { return ""; } - /** @return the fully resolved universe domain used by the client */ + /** + * @return the fully resolved universe domain used by the client + */ public final String getUniverseDomain() { return endpointContext.resolvedUniverseDomain(); } - /** @return the fully resolved endpoint used by the client */ + /** + * @return the fully resolved endpoint used by the client + */ public String getEndpoint() { return endpointContext.resolvedEndpoint(); } - /** @return the newly created EndpointContext */ + /** + * @return the newly created EndpointContext + */ @InternalApi final EndpointContext getEndpointContext() { return endpointContext; @@ -236,7 +244,10 @@ public final String getGdchApiAudience() { return gdchApiAudience; } - /** Gets the ApiKey that should be used for authentication. If an empty string was provided it will return null */ + /** + * Gets the ApiKey that should be used for authentication. If an empty string was provided it will + * return null + */ public final String getApiKey() { return (apiKey == null || apiKey.isEmpty()) ? null : apiKey; } @@ -586,14 +597,17 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { } /** - * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in [CallContext]. + * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in + * [CallContext]. */ public B setApiKey(String apiKey) { this.apiKey = apiKey; return self(); } - /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ + /** + * @deprecated Please use {@link #getBackgroundExecutorProvider()}. + */ @Deprecated public ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; @@ -635,7 +649,10 @@ public ApiClock getClock() { return clock; } - /** Gets the ApiKey that was previously set on this Builder. If an empty string was provided it will return null */ + /** + * Gets the ApiKey that was previously set on this Builder. If an empty string was provided it + * will return null + */ public final String getApiKey() { return (apiKey == null || apiKey.isEmpty()) ? null : apiKey; } diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 39f716a879..2f7f59bd21 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -1080,13 +1080,8 @@ public void testSetApiKey_createsApiCredentials() throws IOException { InterceptingExecutor executor = new InterceptingExecutor(1); FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); FakeTransportProvider transportProvider = - new FakeTransportProvider( - transportChannel, - executor, - true, - ImmutableMap.of(), - null, - DEFAULT_ENDPOINT); + new FakeTransportProvider( + transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index 95409da115..f7098c15f2 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -57,8 +57,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.function.Function; import java.util.function.Supplier; - -import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; From a516d950d39a64ab3510072ba8a43e37faaa6de6 Mon Sep 17 00:00:00 2001 From: detmerl Date: Wed, 28 Aug 2024 10:42:29 -0400 Subject: [PATCH 04/14] fixed formatting --- .../google/api/gax/rpc/ClientSettings.java | 22 +++++++++++++------ .../api/gax/rpc/ClientSettingsTest.java | 9 ++++---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index ee08b66c8d..28f1e1e552 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -58,8 +58,10 @@ public abstract class ClientSettings /** Constructs an instance of ClientSettings. */ protected ClientSettings(Builder builder) throws IOException { - if (builder.stubSettings.getApiKey() != null && builder.stubSettings.getCredentialsProvider().getCredentials() != null) { - throw new IllegalArgumentException("You can not provide both ApiKey and Credentials for a client."); + if (builder.stubSettings.getApiKey() != null + && builder.stubSettings.getCredentialsProvider().getCredentials() != null) { + throw new IllegalArgumentException( + "You can not provide both ApiKey and Credentials for a client."); } this.stubSettings = builder.stubSettings.build(); } @@ -68,7 +70,9 @@ public final StubSettings getStubSettings() { return stubSettings; } - /** @deprecated Please use {@link #getBackgroundExecutorProvider()} */ + /** + * @deprecated Please use {@link #getBackgroundExecutorProvider()} + */ @Deprecated public final ExecutorProvider getExecutorProvider() { return stubSettings.getExecutorProvider(); @@ -115,7 +119,9 @@ public final WatchdogProvider getWatchdogProvider() { return stubSettings.getStreamWatchdogProvider(); } - public final String getApiKey() { return stubSettings.getApiKey(); } + public final String getApiKey() { + return stubSettings.getApiKey(); + } /** This method is obsolete. Use {@link #getWatchdogCheckIntervalDuration()} instead. */ @Nonnull @@ -134,7 +140,6 @@ public final String getGdchApiAudience() { return stubSettings.getGdchApiAudience(); } - public String toString() { return MoreObjects.toStringHelper(this) .add("executorProvider", getExecutorProvider()) @@ -310,7 +315,8 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { } /** - * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in [CallContext]. + * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in + * [CallContext]. */ public B setApiKey(String apiKey) { stubSettings.setApiKey(apiKey); @@ -380,7 +386,9 @@ public WatchdogProvider getWatchdogProvider() { } /** Gets the ApiKey that was previously set on this Builder. */ - public String getApiKey() { return stubSettings.getApiKey(); } + public String getApiKey() { + return stubSettings.getApiKey(); + } /** This method is obsolete. Use {@link #getWatchdogCheckIntervalDuration()} instead */ @Nullable diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index f7098c15f2..7d208a9e29 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -582,7 +582,7 @@ public void testWatchdogCheckInterval_backportMethodsBehaveCorrectly() { void testClientSettingsBuilder_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exception { FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); - when(credentialsProvider.getCredentials()).thenReturn( Mockito.mock(Credentials.class)); + when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); builder.setCredentialsProvider(credentialsProvider); builder.setApiKey("api_key"); @@ -590,7 +590,8 @@ void testClientSettingsBuilder_throwsErrorIfApiKeyAndCredentialsAreProvided() th builder.build(); fail("No exception raised"); } catch (IllegalArgumentException e) { - Assertions.assertTrue(e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); + Assertions.assertTrue( + e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); } } @@ -603,10 +604,10 @@ void testEmptyApiKeyClientSettingsBuild_isTreatedAsNull() throws Exception { builder.setCredentialsProvider(credentialsProvider); builder.setApiKey(""); - FakeClientSettings fakeClientSettings = builder.build(); - Assertions.assertEquals(fakeClientSettings.getCredentialsProvider().getCredentials(), credentials); + Assertions.assertEquals( + fakeClientSettings.getCredentialsProvider().getCredentials(), credentials); Assertions.assertNull(fakeClientSettings.getApiKey()); } } From f7ec0faf8481c6066732bffb680ad745b3a2af85 Mon Sep 17 00:00:00 2001 From: detmerl Date: Thu, 12 Sep 2024 16:12:01 -0400 Subject: [PATCH 05/14] wip --- .../com/google/api/gax/rpc/ClientContext.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index c410a31cb2..96caf9e745 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -177,8 +177,16 @@ public static ClientContext create(StubSettings settings) throws IOException { EndpointContext endpointContext = settings.getEndpointContext(); String endpoint = endpointContext.resolvedEndpoint(); - String settingsGdchApiAudience = settings.getGdchApiAudience(); + String apiKey = settings.getApiKey(); + Credentials credentials = settings.getCredentialsProvider().getCredentials(); + + if (apiKey != null && credentials != null) { + throw new Exception(); + } + + String settingsGdchApiAudience = settings.getGdchApiAudience(); + boolean usingGDCH = credentials instanceof GdchCredentials; if (usingGDCH) { // Can only determine if the GDC-H is being used via the Credentials. The Credentials object @@ -217,6 +225,10 @@ public static ClientContext create(StubSettings settings) throws IOException { credentials = new QuotaProjectIdHidingCredentials(credentials); } + if (apiKey != null) { + credentials = ApiKeyCredentials.create(settings.getApiKey()); + } + TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider(); // After needsExecutor and StubSettings#setExecutorProvider are deprecated, transport channel // executor can only be set from TransportChannelProvider#withExecutor directly, and a provider @@ -242,10 +254,6 @@ public static ClientContext create(StubSettings settings) throws IOException { if (credentials != null) { defaultCallContext = defaultCallContext.withCredentials(credentials); } - if (settings.getApiKey() != null) { - defaultCallContext = - defaultCallContext.withCredentials(ApiKeyCredentials.create(settings.getApiKey())); - } defaultCallContext = defaultCallContext.withEndpointContext(endpointContext); WatchdogProvider watchdogProvider = settings.getStreamWatchdogProvider(); From db1674b560545fb1ac5e3633df305407e107ac0f Mon Sep 17 00:00:00 2001 From: detmerl Date: Mon, 16 Sep 2024 12:58:10 -0400 Subject: [PATCH 06/14] wip --- .../com/google/api/gax/rpc/ClientContext.java | 77 +++++++++---------- .../google/api/gax/rpc/ClientSettings.java | 5 -- .../google/api/gax/rpc/ClientContextTest.java | 35 +++++++-- .../api/gax/rpc/ClientSettingsTest.java | 17 ---- 4 files changed, 67 insertions(+), 67 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 96caf9e745..738ab47d7a 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -176,59 +176,58 @@ public static ClientContext create(StubSettings settings) throws IOException { // A valid EndpointContext should have been created in the StubSettings EndpointContext endpointContext = settings.getEndpointContext(); String endpoint = endpointContext.resolvedEndpoint(); - String apiKey = settings.getApiKey(); - Credentials credentials = settings.getCredentialsProvider().getCredentials(); - if (apiKey != null && credentials != null) { - throw new Exception(); + throw new IllegalArgumentException( + "You can not provide both ApiKey and Credentials for a client."); } - - String settingsGdchApiAudience = settings.getGdchApiAudience(); - - boolean usingGDCH = credentials instanceof GdchCredentials; - if (usingGDCH) { - // Can only determine if the GDC-H is being used via the Credentials. The Credentials object - // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the - // endpointContext only on GDC-H flows. - endpointContext = endpointContext.withGDCH(); - // Resolve the new endpoint with the GDC-H flow - endpoint = endpointContext.resolvedEndpoint(); - // We recompute the GdchCredentials with the audience - String audienceString; - if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - audienceString = settingsGdchApiAudience; - } else if (!Strings.isNullOrEmpty(endpoint)) { - audienceString = endpoint; - } else { - throw new IllegalArgumentException("Could not infer GDCH api audience from settings"); - } - - URI gdchAudienceUri; - try { - gdchAudienceUri = URI.create(audienceString); - } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string - throw new IllegalArgumentException("The GDC-H API audience string is not a valid URI", ex); + if (apiKey != null) { + credentials = ApiKeyCredentials.create(settings.getApiKey()); + } else { + // check if need to adjust credentials for Gdch audience + String settingsGdchApiAudience = settings.getGdchApiAudience(); + boolean usingGDCH = credentials instanceof GdchCredentials; + if (usingGDCH) { + // Can only determine if the GDC-H is being used via the Credentials. The Credentials object + // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the + // endpointContext only on GDC-H flows. + endpointContext = endpointContext.withGDCH(); + // Resolve the new endpoint with the GDC-H flow + endpoint = endpointContext.resolvedEndpoint(); + // We recompute the GdchCredentials with the audience + String audienceString; + if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + audienceString = settingsGdchApiAudience; + } else if (!Strings.isNullOrEmpty(endpoint)) { + audienceString = endpoint; + } else { + throw new IllegalArgumentException("Could not infer GDCH api audience from settings"); + } + + URI gdchAudienceUri; + try { + gdchAudienceUri = URI.create(audienceString); + } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string + throw new IllegalArgumentException( + "The GDC-H API audience string is not a valid URI", ex); + } + credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); + } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + throw new IllegalArgumentException( + "GDC-H API audience can only be set when using GdchCredentials"); } - credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); - } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - throw new IllegalArgumentException( - "GDC-H API audience can only be set when using GdchCredentials"); } if (settings.getQuotaProjectId() != null && credentials != null) { // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as // QuotaProjectIdHidingCredentials. - // Ensure that a custom set quota project id takes priority over one detected by credentials. + // Ensure that a custom set quota project id takes priority over one detected by + // credentials. // Avoid the backend receiving possibly conflict values of quotaProjectId credentials = new QuotaProjectIdHidingCredentials(credentials); } - if (apiKey != null) { - credentials = ApiKeyCredentials.create(settings.getApiKey()); - } - TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider(); // After needsExecutor and StubSettings#setExecutorProvider are deprecated, transport channel // executor can only be set from TransportChannelProvider#withExecutor directly, and a provider diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index 28f1e1e552..69437b830f 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -58,11 +58,6 @@ public abstract class ClientSettings /** Constructs an instance of ClientSettings. */ protected ClientSettings(Builder builder) throws IOException { - if (builder.stubSettings.getApiKey() != null - && builder.stubSettings.getCredentialsProvider().getCredentials() != null) { - throw new IllegalArgumentException( - "You can not provide both ApiKey and Credentials for a client."); - } this.stubSettings = builder.stubSettings.build(); } diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 2f7f59bd21..294f0a6432 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -31,12 +31,7 @@ import static com.google.api.gax.util.TimeConversionTestUtils.testDurationMethod; import static com.google.common.truth.Truth.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -67,6 +62,8 @@ import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; + +import org.apache.http.impl.client.SystemDefaultCredentialsProvider; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -1094,4 +1091,30 @@ public void testSetApiKey_createsApiCredentials() throws IOException { FakeCallContext fakeCallContext = (FakeCallContext) context.getDefaultCallContext(); assertThat(fakeCallContext.getCredentials()).isInstanceOf(ApiKeyCredentials.class); } + + @Test + void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exception { + String apiKey = "key"; + FakeStubSettings.Builder builder = new FakeStubSettings.Builder(); + InterceptingExecutor executor = new InterceptingExecutor(1); + FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); + FakeTransportProvider transportProvider = + new FakeTransportProvider( + transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); + builder.setTransportChannelProvider(transportProvider); + + HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); + Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); + builder.setHeaderProvider(headerProvider); + builder.setApiKey(apiKey); + builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); + + try { + ClientContext context = ClientContext.create(builder.build()); + fail("No exception raised"); + } catch (IllegalArgumentException e) { + assert( + e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); + } + } } diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index 7d208a9e29..79a9818fe9 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -578,23 +578,6 @@ public void testWatchdogCheckInterval_backportMethodsBehaveCorrectly() { cs -> cs.getWatchdogCheckInterval()); } - @Test - void testClientSettingsBuilder_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exception { - FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); - CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); - when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); - builder.setCredentialsProvider(credentialsProvider); - builder.setApiKey("api_key"); - - try { - builder.build(); - fail("No exception raised"); - } catch (IllegalArgumentException e) { - Assertions.assertTrue( - e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); - } - } - @Test void testEmptyApiKeyClientSettingsBuild_isTreatedAsNull() throws Exception { FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); From 9a139519b2ff398d4fccfca5fa5deb954e3249d3 Mon Sep 17 00:00:00 2001 From: detmerl Date: Mon, 16 Sep 2024 15:59:21 -0400 Subject: [PATCH 07/14] wip --- .../com/google/api/gax/rpc/ClientContext.java | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 738ab47d7a..7602e34173 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -183,40 +183,25 @@ public static ClientContext create(StubSettings settings) throws IOException { "You can not provide both ApiKey and Credentials for a client."); } if (apiKey != null) { + // if API key exists it becomes the default credential credentials = ApiKeyCredentials.create(settings.getApiKey()); - } else { - // check if need to adjust credentials for Gdch audience - String settingsGdchApiAudience = settings.getGdchApiAudience(); - boolean usingGDCH = credentials instanceof GdchCredentials; - if (usingGDCH) { - // Can only determine if the GDC-H is being used via the Credentials. The Credentials object - // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the - // endpointContext only on GDC-H flows. - endpointContext = endpointContext.withGDCH(); - // Resolve the new endpoint with the GDC-H flow - endpoint = endpointContext.resolvedEndpoint(); - // We recompute the GdchCredentials with the audience - String audienceString; - if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - audienceString = settingsGdchApiAudience; - } else if (!Strings.isNullOrEmpty(endpoint)) { - audienceString = endpoint; - } else { - throw new IllegalArgumentException("Could not infer GDCH api audience from settings"); - } - - URI gdchAudienceUri; - try { - gdchAudienceUri = URI.create(audienceString); - } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string - throw new IllegalArgumentException( - "The GDC-H API audience string is not a valid URI", ex); - } - credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); - } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - throw new IllegalArgumentException( - "GDC-H API audience can only be set when using GdchCredentials"); - } + } + + // check if need to adjust credentials/endpoint/endpointContext for GDC-H + String settingsGdchApiAudience = settings.getGdchApiAudience(); + boolean usingGDCH = credentials instanceof GdchCredentials; + if (usingGDCH) { + // Can only determine if the GDC-H is being used via the Credentials. The Credentials object + // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the + // endpointContext only on GDC-H flows. + endpointContext = endpointContext.withGDCH(); + // Resolve the new endpoint with the GDC-H flow + endpoint = endpointContext.resolvedEndpoint(); + // We recompute the GdchCredentials with the audience + credentials = getGdchCredentials(settingsGdchApiAudience, endpoint, credentials); + } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + throw new IllegalArgumentException( + "GDC-H API audience can only be set when using GdchCredentials"); } if (settings.getQuotaProjectId() != null && credentials != null) { @@ -304,6 +289,27 @@ public static ClientContext create(StubSettings settings) throws IOException { .build(); } + private static Credentials getGdchCredentials(String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { + String audienceString; + if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + audienceString = settingsGdchApiAudience; + } else if (!Strings.isNullOrEmpty(endpoint)) { + audienceString = endpoint; + } else { + throw new IllegalArgumentException("Could not infer GDCH api audience from settings"); + } + + URI gdchAudienceUri; + try { + gdchAudienceUri = URI.create(audienceString); + } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string + throw new IllegalArgumentException( + "The GDC-H API audience string is not a valid URI", ex); + } + credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); + return credentials; + } + /** * Getting a header map from HeaderProvider and InternalHeaderProvider from settings with Quota * Project Id. From 62a39562891bdd0be14b340a933bdd2685b940d4 Mon Sep 17 00:00:00 2001 From: detmerl Date: Tue, 17 Sep 2024 10:57:04 -0400 Subject: [PATCH 08/14] clean up --- gapic-generator-java-pom-parent/pom.xml | 2 +- .../java/com/google/api/gax/rpc/ClientContext.java | 7 +++++-- .../com/google/api/gax/rpc/ClientSettings.java | 6 +++--- .../com/google/api/gax/rpc/ClientContextTest.java | 4 +--- gax-java/pom.xml | 14 +++++++------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/gapic-generator-java-pom-parent/pom.xml b/gapic-generator-java-pom-parent/pom.xml index 1f242ed128..f41632fe49 100644 --- a/gapic-generator-java-pom-parent/pom.xml +++ b/gapic-generator-java-pom-parent/pom.xml @@ -27,7 +27,7 @@ consistent across modules in this repository --> 1.3.2 1.66.0 - 1.24.1-SNAPSHOT + 1.25.1-SNAPSHOT 1.44.2 2.11.0 33.2.1-jre diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 7602e34173..40b84bbe33 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -234,7 +234,6 @@ public static ClientContext create(StubSettings settings) throws IOException { ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel); - if (credentials != null) { defaultCallContext = defaultCallContext.withCredentials(credentials); } @@ -289,7 +288,11 @@ public static ClientContext create(StubSettings settings) throws IOException { .build(); } - private static Credentials getGdchCredentials(String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { + /** + * Constructs a new {@link Credentials} object based on credentials provided with a GDC-H audience + */ + private static Credentials getGdchCredentials( + String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { String audienceString; if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { audienceString = settingsGdchApiAudience; diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index 69437b830f..13aa1229b7 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -65,9 +65,7 @@ public final StubSettings getStubSettings() { return stubSettings; } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()} - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()} */ @Deprecated public final ExecutorProvider getExecutorProvider() { return stubSettings.getExecutorProvider(); @@ -312,6 +310,8 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { /** * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in * [CallContext]. + * + * Note: you can not set an API key and credentials object in the same Settings. It will fail when creating the client. */ public B setApiKey(String apiKey) { stubSettings.setApiKey(apiKey); diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 294f0a6432..e3312a0dff 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -1080,7 +1080,6 @@ public void testSetApiKey_createsApiCredentials() throws IOException { new FakeTransportProvider( transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); - HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); @@ -1102,7 +1101,6 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce new FakeTransportProvider( transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); - HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); @@ -1110,7 +1108,7 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); try { - ClientContext context = ClientContext.create(builder.build()); + ClientContext.create(builder.build()); fail("No exception raised"); } catch (IllegalArgumentException e) { assert( diff --git a/gax-java/pom.xml b/gax-java/pom.xml index 99025fc608..e8014aa2fd 100644 --- a/gax-java/pom.xml +++ b/gax-java/pom.xml @@ -4,14 +4,14 @@ com.google.api gax-parent pom - 2.52.1-SNAPSHOT + 2.53.1-SNAPSHOT GAX (Google Api eXtensions) for Java (Parent) Google Api eXtensions for Java (Parent) com.google.api gapic-generator-java-pom-parent - 2.44.1-SNAPSHOT + 2.45.1-SNAPSHOT ../gapic-generator-java-pom-parent @@ -50,7 +50,7 @@ com.google.api api-common - 2.35.1-SNAPSHOT + 2.36.1-SNAPSHOT com.google.auth @@ -98,24 +98,24 @@ com.google.api gax - 2.52.1-SNAPSHOT + 2.53.1-SNAPSHOT com.google.api gax - 2.52.1-SNAPSHOT + 2.53.1-SNAPSHOT test-jar testlib com.google.api.grpc proto-google-common-protos - 2.43.1-SNAPSHOT + 2.44.1-SNAPSHOT com.google.api.grpc grpc-google-common-protos - 2.43.1-SNAPSHOT + 2.44.1-SNAPSHOT io.grpc From fa251cf0e38947edf5e7f9aafa4a3d33552adb6c Mon Sep 17 00:00:00 2001 From: detmerl Date: Tue, 17 Sep 2024 11:16:29 -0400 Subject: [PATCH 09/14] clean up --- .../com/google/api/gax/rpc/ClientContext.java | 8 +++----- .../google/api/gax/rpc/ClientSettings.java | 3 ++- .../com/google/api/gax/rpc/StubSettings.java | 20 +++++-------------- .../google/api/gax/rpc/ClientContextTest.java | 18 ++++++++++------- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 40b84bbe33..974b99d0c1 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -180,7 +180,7 @@ public static ClientContext create(StubSettings settings) throws IOException { Credentials credentials = settings.getCredentialsProvider().getCredentials(); if (apiKey != null && credentials != null) { throw new IllegalArgumentException( - "You can not provide both ApiKey and Credentials for a client."); + "You can not provide both ApiKey and Credentials for a client."); } if (apiKey != null) { // if API key exists it becomes the default credential @@ -207,8 +207,7 @@ public static ClientContext create(StubSettings settings) throws IOException { if (settings.getQuotaProjectId() != null && credentials != null) { // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as // QuotaProjectIdHidingCredentials. - // Ensure that a custom set quota project id takes priority over one detected by - // credentials. + // Ensure that a custom set quota project id takes priority over one detected by credentials. // Avoid the backend receiving possibly conflict values of quotaProjectId credentials = new QuotaProjectIdHidingCredentials(credentials); } @@ -306,8 +305,7 @@ private static Credentials getGdchCredentials( try { gdchAudienceUri = URI.create(audienceString); } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string - throw new IllegalArgumentException( - "The GDC-H API audience string is not a valid URI", ex); + throw new IllegalArgumentException("The GDC-H API audience string is not a valid URI", ex); } credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); return credentials; diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index 13aa1229b7..01c8c3748c 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -311,7 +311,8 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in * [CallContext]. * - * Note: you can not set an API key and credentials object in the same Settings. It will fail when creating the client. + *

Note: you can not set an API key and credentials object in the same Settings. It will fail + * when creating the client. */ public B setApiKey(String apiKey) { stubSettings.setApiKey(apiKey); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 1b88cf552a..0876f35346 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -132,9 +132,7 @@ private EndpointContext buildEndpointContext(Builder builder) { } } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()}. - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public final ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; @@ -176,23 +174,17 @@ protected String getServiceName() { return ""; } - /** - * @return the fully resolved universe domain used by the client - */ + /** @return the fully resolved universe domain used by the client */ public final String getUniverseDomain() { return endpointContext.resolvedUniverseDomain(); } - /** - * @return the fully resolved endpoint used by the client - */ + /** @return the fully resolved endpoint used by the client */ public String getEndpoint() { return endpointContext.resolvedEndpoint(); } - /** - * @return the newly created EndpointContext - */ + /** @return the newly created EndpointContext */ @InternalApi final EndpointContext getEndpointContext() { return endpointContext; @@ -605,9 +597,7 @@ public B setApiKey(String apiKey) { return self(); } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()}. - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index e3312a0dff..771a73b061 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -31,7 +31,13 @@ import static com.google.api.gax.util.TimeConversionTestUtils.testDurationMethod; import static com.google.common.truth.Truth.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -62,8 +68,6 @@ import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; - -import org.apache.http.impl.client.SystemDefaultCredentialsProvider; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -1098,8 +1102,8 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce InterceptingExecutor executor = new InterceptingExecutor(1); FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); FakeTransportProvider transportProvider = - new FakeTransportProvider( - transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); + new FakeTransportProvider( + transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); @@ -1111,8 +1115,8 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce ClientContext.create(builder.build()); fail("No exception raised"); } catch (IllegalArgumentException e) { - assert( - e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); + assert (e.getMessage() + .contains("You can not provide both ApiKey and Credentials for a client.")); } } } From 334a4e81a65bfe9a08a876078ceb0460331b92fc Mon Sep 17 00:00:00 2001 From: detmerl Date: Thu, 19 Sep 2024 11:24:07 -0400 Subject: [PATCH 10/14] cleaned up tests/logic --- .../com/google/api/gax/rpc/ClientContext.java | 34 +++++++++---------- .../com/google/api/gax/rpc/StubSettings.java | 4 +-- .../google/api/gax/rpc/ClientContextTest.java | 4 ++- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 974b99d0c1..eb3485258f 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -185,23 +185,23 @@ public static ClientContext create(StubSettings settings) throws IOException { if (apiKey != null) { // if API key exists it becomes the default credential credentials = ApiKeyCredentials.create(settings.getApiKey()); - } - - // check if need to adjust credentials/endpoint/endpointContext for GDC-H - String settingsGdchApiAudience = settings.getGdchApiAudience(); - boolean usingGDCH = credentials instanceof GdchCredentials; - if (usingGDCH) { - // Can only determine if the GDC-H is being used via the Credentials. The Credentials object - // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the - // endpointContext only on GDC-H flows. - endpointContext = endpointContext.withGDCH(); - // Resolve the new endpoint with the GDC-H flow - endpoint = endpointContext.resolvedEndpoint(); - // We recompute the GdchCredentials with the audience - credentials = getGdchCredentials(settingsGdchApiAudience, endpoint, credentials); - } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - throw new IllegalArgumentException( - "GDC-H API audience can only be set when using GdchCredentials"); + } else { + // check if need to adjust credentials/endpoint/endpointContext for GDC-H + String settingsGdchApiAudience = settings.getGdchApiAudience(); + boolean usingGDCH = credentials instanceof GdchCredentials; + if (usingGDCH) { + // Can only determine if the GDC-H is being used via the Credentials. The Credentials object + // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the + // endpointContext only on GDC-H flows. + endpointContext = endpointContext.withGDCH(); + // Resolve the new endpoint with the GDC-H flow + endpoint = endpointContext.resolvedEndpoint(); + // We recompute the GdchCredentials with the audience + credentials = getGdchCredentials(settingsGdchApiAudience, endpoint, credentials); + } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + throw new IllegalArgumentException( + "GDC-H API audience can only be set when using GdchCredentials"); + } } if (settings.getQuotaProjectId() != null && credentials != null) { diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 0876f35346..800dbb9c1a 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -589,8 +589,8 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { } /** - * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in - * [CallContext]. + * Sets the API key. The API key will get translated to an {@link + * com.google.auth.ApiKeyCredentials} and stored in {@link ClientContext}. */ public B setApiKey(String apiKey) { this.apiKey = apiKey; diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 771a73b061..7f6a3e86f5 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -1109,7 +1109,9 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); builder.setApiKey(apiKey); - builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); + CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); + Mockito.when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); + builder.setCredentialsProvider(credentialsProvider); try { ClientContext.create(builder.build()); From edb658feb50a48413f00b3e260e02a6a110c2c8f Mon Sep 17 00:00:00 2001 From: detmerl Date: Thu, 19 Sep 2024 11:26:36 -0400 Subject: [PATCH 11/14] cleaned up formatting --- .../src/main/java/com/google/api/gax/rpc/ClientContext.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index eb3485258f..e3fa86efcc 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -288,7 +288,8 @@ public static ClientContext create(StubSettings settings) throws IOException { } /** - * Constructs a new {@link Credentials} object based on credentials provided with a GDC-H audience + * Constructs a new {@link com.google.auth.Credentials} object based on credentials provided with + * a GDC-H audience */ private static Credentials getGdchCredentials( String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { From 33f64a101812d8173ae2271805a995b005b21b8a Mon Sep 17 00:00:00 2001 From: detmerl Date: Thu, 19 Sep 2024 11:44:18 -0400 Subject: [PATCH 12/14] updated to use assertThrows --- .../com/google/api/gax/rpc/ClientContextTest.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 7f6a3e86f5..b41556d580 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -37,7 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -68,6 +67,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; +import org.junit.*; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -1113,12 +1113,7 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce Mockito.when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); builder.setCredentialsProvider(credentialsProvider); - try { - ClientContext.create(builder.build()); - fail("No exception raised"); - } catch (IllegalArgumentException e) { - assert (e.getMessage() - .contains("You can not provide both ApiKey and Credentials for a client.")); - } + Assert.assertThrows( + IllegalArgumentException.class, () -> ClientContext.create(builder.build())); } } From ae0f28103f58fa838605b75cf6b960a22f8a3d4a Mon Sep 17 00:00:00 2001 From: detmerl Date: Mon, 23 Sep 2024 10:01:46 -0400 Subject: [PATCH 13/14] fixed imports --- .../test/java/com/google/api/gax/rpc/ClientContextTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index b41556d580..1c11709bd3 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -67,7 +67,6 @@ import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; -import org.junit.*; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -1113,7 +1112,6 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce Mockito.when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); builder.setCredentialsProvider(credentialsProvider); - Assert.assertThrows( - IllegalArgumentException.class, () -> ClientContext.create(builder.build())); + assertThrows(IllegalArgumentException.class, () -> ClientContext.create(builder.build())); } } From 5e346e6a763aec46317809fe1f6aabf19c02f100 Mon Sep 17 00:00:00 2001 From: detmerl Date: Mon, 23 Sep 2024 10:09:01 -0400 Subject: [PATCH 14/14] fixed imports --- .../java/com/google/api/gax/rpc/ClientSettingsTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index 79a9818fe9..c2d4627d0f 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -30,6 +30,8 @@ package com.google.api.gax.rpc; import static com.google.api.gax.util.TimeConversionTestUtils.testDurationMethod; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.when; @@ -57,7 +59,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.function.Function; import java.util.function.Supplier; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -588,9 +589,7 @@ void testEmptyApiKeyClientSettingsBuild_isTreatedAsNull() throws Exception { builder.setApiKey(""); FakeClientSettings fakeClientSettings = builder.build(); - - Assertions.assertEquals( - fakeClientSettings.getCredentialsProvider().getCredentials(), credentials); - Assertions.assertNull(fakeClientSettings.getApiKey()); + assertEquals(fakeClientSettings.getCredentialsProvider().getCredentials(), credentials); + assertNull(fakeClientSettings.getApiKey()); } }