From dd1f819a12b2beafd8ecedde343959600a231e20 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 6 May 2024 09:46:51 +0000 Subject: [PATCH 1/9] test: add gapic-showcase test to verify api-version is sent correctly via headers. --- .../v1beta1/it/ITApiVersionHeaders.java | 209 ++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java new file mode 100644 index 0000000000..5bcb45fa99 --- /dev/null +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -0,0 +1,209 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.showcase.v1beta1.it; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.httpjson.*; +import com.google.common.collect.ImmutableList; +import com.google.showcase.v1beta1.*; +import com.google.showcase.v1beta1.it.util.TestClientInitializer; +import io.grpc.*; +import java.util.ArrayList; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ITApiVersionHeaders { + private static final String SPLIT_TOKEN = "&"; + private static final String API_VERSION_HEADER_STRING = "x-goog-api-version"; + private static final String HTTP_RESPONSE_HEADER_STRING = + "x-showcase-request-" + API_VERSION_HEADER_STRING; + private static final Metadata.Key API_VERSION_HEADER_KEY = + Metadata.Key.of(API_VERSION_HEADER_STRING, Metadata.ASCII_STRING_MARSHALLER); + + // Implement a client interceptor to retrieve the trailing metadata from response. + private static class GrpcCapturingClientInterceptor implements ClientInterceptor { + private Metadata metadata; + + @Override + public ClientCall interceptCall( + MethodDescriptor method, final CallOptions callOptions, Channel next) { + ClientCall call = next.newCall(method, callOptions); + return new ForwardingClientCall.SimpleForwardingClientCall(call) { + @Override + public void start(Listener responseListener, Metadata headers) { + Listener wrappedListener = + new SimpleForwardingClientCallListener(responseListener) { + @Override + public void onClose(Status status, Metadata trailers) { + if (status.isOk()) { + metadata = trailers; + } + super.onClose(status, trailers); + } + }; + + super.start(wrappedListener, headers); + } + }; + } + } + + private static class SimpleForwardingClientCallListener + extends ClientCall.Listener { + private final ClientCall.Listener delegate; + + SimpleForwardingClientCallListener(ClientCall.Listener delegate) { + this.delegate = delegate; + } + + @Override + public void onHeaders(Metadata headers) { + delegate.onHeaders(headers); + } + + @Override + public void onMessage(RespT message) { + delegate.onMessage(message); + } + + @Override + public void onClose(Status status, Metadata trailers) { + delegate.onClose(status, trailers); + } + + @Override + public void onReady() { + delegate.onReady(); + } + } + // Implement a client interceptor to retrieve the response headers + private static class HttpJsonCapturingClientInterceptor implements HttpJsonClientInterceptor { + private HttpJsonMetadata metadata; + + @Override + public HttpJsonClientCall interceptCall( + ApiMethodDescriptor method, + HttpJsonCallOptions callOptions, + HttpJsonChannel next) { + HttpJsonClientCall call = next.newCall(method, callOptions); + return new ForwardingHttpJsonClientCall.SimpleForwardingHttpJsonClientCall< + RequestT, ResponseT>(call) { + @Override + public void start(Listener responseListener, HttpJsonMetadata requestHeaders) { + Listener forwardingResponseListener = + new ForwardingHttpJsonClientCallListener.SimpleForwardingHttpJsonClientCallListener< + ResponseT>(responseListener) { + @Override + public void onHeaders(HttpJsonMetadata responseHeaders) { + metadata = responseHeaders; + super.onHeaders(responseHeaders); + } + + @Override + public void onMessage(ResponseT message) { + super.onMessage(message); + } + + @Override + public void onClose(int statusCode, HttpJsonMetadata trailers) { + super.onClose(statusCode, trailers); + } + }; + + super.start(forwardingResponseListener, requestHeaders); + } + }; + } + } + + private HttpJsonCapturingClientInterceptor httpJsonInterceptor; + private GrpcCapturingClientInterceptor grpcInterceptor; + private HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor; + private GrpcCapturingClientInterceptor grpcComplianceInterceptor; + private EchoClient grpcClient; + private EchoClient httpJsonClient; + private ComplianceClient grpcComplianceClient; + private ComplianceClient httpJsonComplianceClient; + + @Before + public void createClients() throws Exception { + // Create gRPC Interceptor and Client + grpcInterceptor = new GrpcCapturingClientInterceptor(); + grpcClient = TestClientInitializer.createGrpcEchoClient(ImmutableList.of(grpcInterceptor)); + + // Create HttpJson Interceptor and Client + httpJsonInterceptor = new HttpJsonCapturingClientInterceptor(); + httpJsonClient = + TestClientInitializer.createHttpJsonEchoClient(ImmutableList.of(httpJsonInterceptor)); + + // Create gRPC ComplianceClient and Interceptor + // Creating a compliance client to test case where api version is not set + grpcComplianceInterceptor = new GrpcCapturingClientInterceptor(); + grpcComplianceClient = + TestClientInitializer.createGrpcComplianceClient( + ImmutableList.of(grpcComplianceInterceptor)); + + // Create HttpJson ComplianceClient and Interceptor + httpJsonComplianceInterceptor = new HttpJsonCapturingClientInterceptor(); + httpJsonComplianceClient = + TestClientInitializer.createHttpJsonComplianceClient( + ImmutableList.of(httpJsonComplianceInterceptor)); + } + + @After + public void destroyClient() { + grpcClient.close(); + httpJsonClient.close(); + grpcComplianceClient.close(); + httpJsonComplianceClient.close(); + } + + @Test + public void testGrpc_matchesApiVersion() { + grpcClient.echo(EchoRequest.newBuilder().build()); + String headerValue = grpcInterceptor.metadata.get(API_VERSION_HEADER_KEY); + assertThat(headerValue).isEqualTo("v1_20240408"); + } + + @Test + public void testHttpJson_matchesHeaderName() { + httpJsonClient.echo(EchoRequest.newBuilder().build()); + ArrayList headerValues = + (ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING); + String headerValue = (String) headerValues.get(0); + assertThat(headerValue).isEqualTo("v1_20240408"); + } + + @Test + public void testGrpc_noApiVersion() { + RepeatRequest request = + RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); + grpcComplianceClient.repeatDataSimplePath(request); + assertThat(API_VERSION_HEADER_KEY).isNotIn(grpcComplianceInterceptor.metadata.keys()); + } + + @Test + public void testHttpJson_noApiVersion() { + RepeatRequest request = + RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); + httpJsonComplianceClient.repeatDataSimplePath(request); + assertThat(API_VERSION_HEADER_KEY) + .isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet()); + } + +} From 39dcec2eba97fb51bf62283183d1f7e9099eb060 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 6 May 2024 10:00:27 +0000 Subject: [PATCH 2/9] test: update showcase version. --- showcase/gapic-showcase/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/showcase/gapic-showcase/pom.xml b/showcase/gapic-showcase/pom.xml index 5bdc31a945..9e1b331ceb 100644 --- a/showcase/gapic-showcase/pom.xml +++ b/showcase/gapic-showcase/pom.xml @@ -19,7 +19,7 @@ - 0.33.0 + 0.35.0 From 219c8fdb150a33a9f2e44c442f4e91a586000ead Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 6 May 2024 10:10:12 +0000 Subject: [PATCH 3/9] fix: fmt. --- .../google/showcase/v1beta1/it/ITApiVersionHeaders.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 5bcb45fa99..49e3a044ce 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -192,7 +192,7 @@ public void testHttpJson_matchesHeaderName() { @Test public void testGrpc_noApiVersion() { RepeatRequest request = - RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); + RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); grpcComplianceClient.repeatDataSimplePath(request); assertThat(API_VERSION_HEADER_KEY).isNotIn(grpcComplianceInterceptor.metadata.keys()); } @@ -200,10 +200,9 @@ public void testGrpc_noApiVersion() { @Test public void testHttpJson_noApiVersion() { RepeatRequest request = - RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); + RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build(); httpJsonComplianceClient.repeatDataSimplePath(request); assertThat(API_VERSION_HEADER_KEY) - .isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet()); + .isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet()); } - } From 5c7cfca9ed1413809bbfa0f65922af444a7eef40 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 7 May 2024 02:52:01 +0000 Subject: [PATCH 4/9] fix: minor fixes and added todo comment. --- .../google/showcase/v1beta1/it/ITApiVersionHeaders.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 49e3a044ce..b50662fc73 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -27,14 +27,17 @@ import org.junit.Before; import org.junit.Test; +// TODO: add testing on error responses once feat is implemented in showcase. +// https://github.com/googleapis/gapic-showcase/pull/1456 public class ITApiVersionHeaders { - private static final String SPLIT_TOKEN = "&"; private static final String API_VERSION_HEADER_STRING = "x-goog-api-version"; private static final String HTTP_RESPONSE_HEADER_STRING = "x-showcase-request-" + API_VERSION_HEADER_STRING; private static final Metadata.Key API_VERSION_HEADER_KEY = Metadata.Key.of(API_VERSION_HEADER_STRING, Metadata.ASCII_STRING_MARSHALLER); + private static final String EXPECTED_ECHO_API_VERSION = "v1_20240408"; + // Implement a client interceptor to retrieve the trailing metadata from response. private static class GrpcCapturingClientInterceptor implements ClientInterceptor { private Metadata metadata; @@ -177,7 +180,7 @@ public void destroyClient() { public void testGrpc_matchesApiVersion() { grpcClient.echo(EchoRequest.newBuilder().build()); String headerValue = grpcInterceptor.metadata.get(API_VERSION_HEADER_KEY); - assertThat(headerValue).isEqualTo("v1_20240408"); + assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION); } @Test @@ -186,7 +189,7 @@ public void testHttpJson_matchesHeaderName() { ArrayList headerValues = (ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING); String headerValue = (String) headerValues.get(0); - assertThat(headerValue).isEqualTo("v1_20240408"); + assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION); } @Test From c9d8ded1a8af3d7cbb9a647cb802a71412c8d7d7 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 7 May 2024 02:59:06 +0000 Subject: [PATCH 5/9] fix: expose ApiClientHeaderProvider.API_VERSION_HEADER_KEY as public for testing. --- .../com/google/api/gax/rpc/ApiClientHeaderProvider.java | 2 +- .../google/showcase/v1beta1/it/ITApiVersionHeaders.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiClientHeaderProvider.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiClientHeaderProvider.java index ae27404335..a5e80e10b1 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiClientHeaderProvider.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiClientHeaderProvider.java @@ -42,7 +42,7 @@ public class ApiClientHeaderProvider implements HeaderProvider, Serializable { private static final long serialVersionUID = -8876627296793342119L; static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project"; - static final String API_VERSION_HEADER_KEY = "x-goog-api-version"; + public static final String API_VERSION_HEADER_KEY = "x-goog-api-version"; private final Map headers; diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index b50662fc73..218e1e0e17 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.api.gax.httpjson.*; +import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.common.collect.ImmutableList; import com.google.showcase.v1beta1.*; import com.google.showcase.v1beta1.it.util.TestClientInitializer; @@ -30,11 +31,11 @@ // TODO: add testing on error responses once feat is implemented in showcase. // https://github.com/googleapis/gapic-showcase/pull/1456 public class ITApiVersionHeaders { - private static final String API_VERSION_HEADER_STRING = "x-goog-api-version"; private static final String HTTP_RESPONSE_HEADER_STRING = - "x-showcase-request-" + API_VERSION_HEADER_STRING; + "x-showcase-request-" + ApiClientHeaderProvider.API_VERSION_HEADER_KEY; private static final Metadata.Key API_VERSION_HEADER_KEY = - Metadata.Key.of(API_VERSION_HEADER_STRING, Metadata.ASCII_STRING_MARSHALLER); + Metadata.Key.of( + ApiClientHeaderProvider.API_VERSION_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER); private static final String EXPECTED_ECHO_API_VERSION = "v1_20240408"; From f30f325cfcc859375e751eb9f8c19a53fe60efea Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 7 May 2024 03:01:46 +0000 Subject: [PATCH 6/9] add additional todo comment. --- .../com/google/showcase/v1beta1/it/ITApiVersionHeaders.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 218e1e0e17..9af6d2cd0e 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -30,6 +30,8 @@ // TODO: add testing on error responses once feat is implemented in showcase. // https://github.com/googleapis/gapic-showcase/pull/1456 +// TODO: watch for showcase gRPC trailer changes suggested in +// https://github.com/googleapis/gapic-showcase/pull/1509#issuecomment-2089147103 public class ITApiVersionHeaders { private static final String HTTP_RESPONSE_HEADER_STRING = "x-showcase-request-" + ApiClientHeaderProvider.API_VERSION_HEADER_KEY; From bc0cf7c6dae5575b735d3775002f96101d26560b Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Wed, 8 May 2024 03:49:46 +0000 Subject: [PATCH 7/9] test: added test cases for attempting add user api version headers. --- .../v1beta1/it/ITApiVersionHeaders.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 9af6d2cd0e..912859babd 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -19,10 +19,15 @@ import com.google.api.gax.httpjson.*; import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.FixedHeaderProvider; +import com.google.api.gax.rpc.StubSettings; import com.google.common.collect.ImmutableList; import com.google.showcase.v1beta1.*; import com.google.showcase.v1beta1.it.util.TestClientInitializer; +import com.google.showcase.v1beta1.stub.ComplianceStubSettings; +import com.google.showcase.v1beta1.stub.EchoStubSettings; import io.grpc.*; +import java.io.IOException; import java.util.ArrayList; import org.junit.After; import org.junit.Before; @@ -40,6 +45,7 @@ public class ITApiVersionHeaders { ApiClientHeaderProvider.API_VERSION_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER); private static final String EXPECTED_ECHO_API_VERSION = "v1_20240408"; + private static final String CUSTOM_API_VERSION = "user-supplied-version"; // Implement a client interceptor to retrieve the trailing metadata from response. private static class GrpcCapturingClientInterceptor implements ClientInterceptor { @@ -211,4 +217,95 @@ public void testHttpJson_noApiVersion() { assertThat(API_VERSION_HEADER_KEY) .isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet()); } + + @Test(expected = IllegalArgumentException.class) + public void testGrpcEcho_userApiVersionThrowsException() throws IOException { + StubSettings stubSettings = + grpcClient + .getSettings() + .getStubSettings() + .toBuilder() + .setHeaderProvider( + FixedHeaderProvider.create( + ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) + .build(); + + try (EchoClient echo = + EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) { + assertThat(true).isFalse(); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException { + StubSettings stubSettings = + httpJsonClient + .getSettings() + .getStubSettings() + .toBuilder() + .setHeaderProvider( + FixedHeaderProvider.create( + ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) + .build(); + + try (EchoClient echo = + EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) { + assertThat(true).isFalse(); + } + } + + @Test + public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException { + StubSettings stubSettingsWithApiVersionHeader = + grpcComplianceClient + .getSettings() + .getStubSettings() + .toBuilder() + .setHeaderProvider( + FixedHeaderProvider.create( + ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) + .build(); + try (ComplianceClient customComplianceClient = + ComplianceClient.create( + ComplianceSettings.create((ComplianceStubSettings) stubSettingsWithApiVersionHeader))) { + + RepeatRequest request = + RepeatRequest.newBuilder() + .setInfo(ComplianceData.newBuilder().setFString("test")) + .build(); + customComplianceClient.repeatDataSimplePath(request); + String headerValue = grpcComplianceInterceptor.metadata.get(API_VERSION_HEADER_KEY); + assertThat(headerValue).isEqualTo(CUSTOM_API_VERSION); + } + } + + @Test + public void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException { + StubSettings httpJsonStubSettingsWithApiVersionHeader = + httpJsonComplianceClient + .getSettings() + .getStubSettings() + .toBuilder() + .setHeaderProvider( + FixedHeaderProvider.create( + ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) + .build(); + try (ComplianceClient customComplianceClient = + ComplianceClient.create( + ComplianceSettings.create( + (ComplianceStubSettings) httpJsonStubSettingsWithApiVersionHeader))) { + + RepeatRequest request = + RepeatRequest.newBuilder() + .setInfo(ComplianceData.newBuilder().setFString("test")) + .build(); + customComplianceClient.repeatDataSimplePath(request); + + ArrayList headerValues = + (ArrayList) + httpJsonComplianceInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING); + String headerValue = (String) headerValues.get(0); + assertThat(headerValue).isEqualTo(CUSTOM_API_VERSION); + } + } } From 64ac5a7aa6d03e0f34c6c0ffc2292f551a64c583 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Thu, 9 May 2024 02:07:05 +0000 Subject: [PATCH 8/9] test: switch to assertThrows to capture exception. --- .../v1beta1/it/ITApiVersionHeaders.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 912859babd..34ca48e985 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -16,6 +16,7 @@ package com.google.showcase.v1beta1.it; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.api.gax.httpjson.*; import com.google.api.gax.rpc.ApiClientHeaderProvider; @@ -46,6 +47,9 @@ public class ITApiVersionHeaders { private static final String EXPECTED_ECHO_API_VERSION = "v1_20240408"; private static final String CUSTOM_API_VERSION = "user-supplied-version"; + private static final String EXPECTED_EXCEPTION_MESSAGE = + "Header provider can't override the header: " + + ApiClientHeaderProvider.API_VERSION_HEADER_KEY; // Implement a client interceptor to retrieve the trailing metadata from response. private static class GrpcCapturingClientInterceptor implements ClientInterceptor { @@ -218,7 +222,7 @@ public void testHttpJson_noApiVersion() { .isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet()); } - @Test(expected = IllegalArgumentException.class) + @Test public void testGrpcEcho_userApiVersionThrowsException() throws IOException { StubSettings stubSettings = grpcClient @@ -230,13 +234,14 @@ public void testGrpcEcho_userApiVersionThrowsException() throws IOException { ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) .build(); - try (EchoClient echo = - EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) { - assertThat(true).isFalse(); - } + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))); + assertThat(exception.getMessage()).isEqualTo(EXPECTED_EXCEPTION_MESSAGE); } - @Test(expected = IllegalArgumentException.class) + @Test public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException { StubSettings stubSettings = httpJsonClient @@ -248,10 +253,11 @@ public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION)) .build(); - try (EchoClient echo = - EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) { - assertThat(true).isFalse(); - } + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))); + assertThat(exception.getMessage()).isEqualTo(EXPECTED_EXCEPTION_MESSAGE); } @Test From 428f5cea3df6e4af640f07d26e4bffcfb176a561 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Fri, 10 May 2024 09:50:40 +0000 Subject: [PATCH 9/9] fix: add awaitTermination for test cleanup. --- .../google/showcase/v1beta1/it/ITApiVersionHeaders.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index 34ca48e985..7ef4019975 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -30,6 +30,7 @@ import io.grpc.*; import java.io.IOException; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,6 +51,7 @@ public class ITApiVersionHeaders { private static final String EXPECTED_EXCEPTION_MESSAGE = "Header provider can't override the header: " + ApiClientHeaderProvider.API_VERSION_HEADER_KEY; + private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10; // Implement a client interceptor to retrieve the trailing metadata from response. private static class GrpcCapturingClientInterceptor implements ClientInterceptor { @@ -182,11 +184,16 @@ public void createClients() throws Exception { } @After - public void destroyClient() { + public void destroyClient() throws InterruptedException { grpcClient.close(); httpJsonClient.close(); grpcComplianceClient.close(); httpJsonComplianceClient.close(); + + grpcClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS); + httpJsonClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS); + grpcComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS); + httpJsonComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS); } @Test