Skip to content

Commit

Permalink
cleaned up tests/logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ldetmer committed Sep 19, 2024
1 parent fa251cf commit 334a4e8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 334a4e8

Please sign in to comment.