From eb55bfb79e76527a8d665d6e05f7cdf49af969aa Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Wed, 15 Mar 2023 12:48:01 -0400 Subject: [PATCH 01/14] chore: add simple unary callable showcase test for gRPC support --- .../showcase/v1beta1/it/ITFirstHttp.java | 68 ---------- .../showcase/v1beta1/it/ITFirstRpc.java | 75 ----------- .../showcase/v1beta1/it/ITUnaryCallable.java | 116 ++++++++++++++++++ 3 files changed, 116 insertions(+), 143 deletions(-) delete mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstHttp.java delete mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstRpc.java create mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstHttp.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstHttp.java deleted file mode 100644 index e1a66d65ed..0000000000 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstHttp.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2022 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 org.junit.Assert.assertEquals; - -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.gax.core.NoCredentialsProvider; -import com.google.showcase.v1beta1.EchoClient; -import com.google.showcase.v1beta1.EchoRequest; -import com.google.showcase.v1beta1.EchoResponse; -import com.google.showcase.v1beta1.EchoSettings; -import java.io.IOException; -import java.security.GeneralSecurityException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ITFirstHttp { - - private static EchoClient client; - - @BeforeClass - public static void createClient() throws IOException, GeneralSecurityException { - EchoSettings echoSettings = - EchoSettings.newHttpJsonBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultHttpJsonTransportProviderBuilder() - .setHttpTransport( - new NetHttpTransport.Builder().doNotValidateCertificate().build()) - .setEndpoint("http://localhost:7469") - .build()) - .build(); - - client = EchoClient.create(echoSettings); - } - - @AfterClass - public static void destroyClient() { - client.close(); - } - - @Test - public void testEcho() { - assertEquals("http-echo?", echo("http-echo?")); - assertEquals("http-echo!", echo("http-echo!")); - } - - private String echo(String value) { - EchoResponse response = client.echo(EchoRequest.newBuilder().setContent(value).build()); - return response.getContent(); - } -} \ No newline at end of file diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstRpc.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstRpc.java deleted file mode 100644 index 46e681bb86..0000000000 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITFirstRpc.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2022 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 org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import com.google.api.gax.core.NoCredentialsProvider; -import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; -import com.google.showcase.v1beta1.EchoClient; -import com.google.showcase.v1beta1.EchoRequest; -import com.google.showcase.v1beta1.EchoResponse; -import com.google.showcase.v1beta1.EchoSettings; -import io.grpc.ManagedChannelBuilder; -import java.io.IOException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class ITFirstRpc { - - private static EchoClient client; - - @Before - public void createClient() throws IOException { - EchoSettings echoSettings = - EchoSettings.newBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder() - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) - .build()) - .build(); - - client = EchoClient.create(echoSettings); - } - - @After - public void destroyClient() { - client.close(); - } - - @Test - public void testEcho() { - assertEquals("grpc-echo?", echo("grpc-echo?")); - assertEquals("grpc-echo!", echo("grpc-echo!")); - } - - @Test - public void testShutdown() { - assertFalse(client.isShutdown()); - client.shutdown(); - assertTrue(client.isShutdown()); - } - - private String echo(String value) { - EchoResponse response = client.echo(EchoRequest.newBuilder().setContent(value).build()); - return response.getContent(); - } -} \ No newline at end of file diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java new file mode 100644 index 0000000000..0d75af369f --- /dev/null +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java @@ -0,0 +1,116 @@ +/* + * Copyright 2022 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 com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GrpcStatusCode; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.rpc.CancelledException; +import com.google.api.gax.rpc.StatusCode; +import com.google.rpc.Status; +import com.google.showcase.v1beta1.EchoClient; +import com.google.showcase.v1beta1.EchoRequest; +import com.google.showcase.v1beta1.EchoResponse; +import com.google.showcase.v1beta1.EchoSettings; +import io.grpc.ManagedChannelBuilder; +import java.io.IOException; +import java.security.GeneralSecurityException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ITUnaryCallable { + + private static EchoClient grpcClient; + + private static EchoClient httpJsonClient; + + @Before + public void createClients() throws IOException, GeneralSecurityException { + // Create gRPC Echo Client + EchoSettings grpcEchoSettings = + EchoSettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + InstantiatingGrpcChannelProvider.newBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + grpcClient = EchoClient.create(grpcEchoSettings); + + // Create Http JSON Echo Client + EchoSettings httpJsonEchoSettings = + EchoSettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + httpJsonClient = EchoClient.create(httpJsonEchoSettings); + } + + @After + public void destroyClient() { + grpcClient.close(); + httpJsonClient.close(); + } + + @Test + public void testGrpc() { + assertEquals("grpc-echo?", echoGrpc("grpc-echo?")); + assertEquals("grpc-echo!", echoGrpc("grpc-echo!")); + } + + @Test + public void testGrpc_cancelledError() { + Status cancelledStatus = Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); + EchoRequest requestWithError = EchoRequest.newBuilder().setError(cancelledStatus).build(); + CancelledException exception = assertThrows(CancelledException.class, () -> grpcClient.echo(requestWithError)); + assertEquals(GrpcStatusCode.Code.CANCELLED, exception.getStatusCode().getCode()); + } + + @Test + public void testGrpc_shutdown() { + assertFalse(grpcClient.isShutdown()); + grpcClient.shutdown(); + assertTrue(grpcClient.isShutdown()); + } + + + @Test + public void testHttpJson() { + assertEquals("http-echo?", echoHttpJson("http-echo?")); + assertEquals("http-echo!", echoHttpJson("http-echo!")); + } + + private String echoGrpc(String value) { + EchoResponse response = grpcClient.echo(EchoRequest.newBuilder().setContent(value).build()); + return response.getContent(); + } + + private String echoHttpJson(String value) { + EchoResponse response = httpJsonClient.echo(EchoRequest.newBuilder().setContent(value).build()); + return response.getContent(); + } +} \ No newline at end of file From 5c07488704b6db3618a3de21b6477bae116bcc58 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Wed, 15 Mar 2023 17:08:54 -0400 Subject: [PATCH 02/14] install maven modules before calling bazelisk --- .github/workflows/ci-maven.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci-maven.yaml b/.github/workflows/ci-maven.yaml index e614dea160..f96d2e409b 100644 --- a/.github/workflows/ci-maven.yaml +++ b/.github/workflows/ci-maven.yaml @@ -25,6 +25,10 @@ jobs: mvn install --batch-mode --no-transfer-progress -Dcheckstyle.skip \ -Dfmt.skip - run: bazelisk version + - name: Install all modules + shell: bash + run: | + mvn -V -B -ntp clean install -DskipTests - name: Integration Tests run: | bazelisk --batch test //test/integration/... From 61c0ed565ad2a1ff1c04b882004ccada2fcc5766 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Wed, 15 Mar 2023 17:13:33 -0400 Subject: [PATCH 03/14] undo previous commit --- .github/workflows/ci-maven.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/ci-maven.yaml b/.github/workflows/ci-maven.yaml index f96d2e409b..e614dea160 100644 --- a/.github/workflows/ci-maven.yaml +++ b/.github/workflows/ci-maven.yaml @@ -25,10 +25,6 @@ jobs: mvn install --batch-mode --no-transfer-progress -Dcheckstyle.skip \ -Dfmt.skip - run: bazelisk version - - name: Install all modules - shell: bash - run: | - mvn -V -B -ntp clean install -DskipTests - name: Integration Tests run: | bazelisk --batch test //test/integration/... From 33f10a84fa04c1eb865f52c8fbe27fddd1c85afd Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Wed, 15 Mar 2023 22:15:24 -0400 Subject: [PATCH 04/14] use truth --- showcase/gapic-showcase/pom.xml | 7 ++++++ .../showcase/v1beta1/it/ITUnaryCallable.java | 25 ++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/showcase/gapic-showcase/pom.xml b/showcase/gapic-showcase/pom.xml index 8c3de921fd..7276f273cf 100644 --- a/showcase/gapic-showcase/pom.xml +++ b/showcase/gapic-showcase/pom.xml @@ -162,6 +162,13 @@ test + + com.google.truth + truth + 1.1.3 + test + + com.google.api.grpc grpc-gapic-showcase-v1beta1 diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java index 0d75af369f..7787e9d497 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package com.google.showcase.v1beta1.it; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.GrpcStatusCode; @@ -35,8 +38,6 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - public class ITUnaryCallable { private static EchoClient grpcClient; @@ -77,31 +78,31 @@ public void destroyClient() { } @Test - public void testGrpc() { - assertEquals("grpc-echo?", echoGrpc("grpc-echo?")); - assertEquals("grpc-echo!", echoGrpc("grpc-echo!")); + public void testGrpc_echoMessageBack() { + assertThat(echoGrpc("grpc-echo?")).isEqualTo("grpc-echo?"); + assertThat(echoGrpc("grpc-echo!")).isEqualTo("grpc-echo!"); } @Test - public void testGrpc_cancelledError() { + public void testGrpc_cancelledError_echoErrorBack() { Status cancelledStatus = Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); EchoRequest requestWithError = EchoRequest.newBuilder().setError(cancelledStatus).build(); CancelledException exception = assertThrows(CancelledException.class, () -> grpcClient.echo(requestWithError)); - assertEquals(GrpcStatusCode.Code.CANCELLED, exception.getStatusCode().getCode()); + assertThat(exception.getStatusCode().getCode()).isEqualTo(GrpcStatusCode.Code.CANCELLED); } @Test public void testGrpc_shutdown() { - assertFalse(grpcClient.isShutdown()); + assertThat(grpcClient.isShutdown()).isFalse(); grpcClient.shutdown(); - assertTrue(grpcClient.isShutdown()); + assertThat(grpcClient.isShutdown()).isTrue(); } @Test public void testHttpJson() { - assertEquals("http-echo?", echoHttpJson("http-echo?")); - assertEquals("http-echo!", echoHttpJson("http-echo!")); + assertThat(echoHttpJson("http-echo?")).isEqualTo("http-echo?"); + assertThat(echoHttpJson("http-echo!")).isEqualTo("http-echo!"); } private String echoGrpc(String value) { From 911d5464eba4e49032011aea4fc5ff351af4795c Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 16 Mar 2023 13:07:52 -0400 Subject: [PATCH 05/14] chore: add server side streaming showcase integration test for gRPC transport --- .../v1beta1/it/ITServerSideStreaming.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java new file mode 100644 index 0000000000..edc1418ec9 --- /dev/null +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -0,0 +1,83 @@ +package com.google.showcase.v1beta1.it; + +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.rpc.CancelledException; +import com.google.api.gax.rpc.ServerStream; +import com.google.api.gax.rpc.StatusCode; +import com.google.common.collect.ImmutableList; +import com.google.rpc.Status; +import com.google.showcase.v1beta1.*; +import io.grpc.ManagedChannelBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + +public class ITServerSideStreaming { + + private static EchoClient grpcClient; + + private static EchoClient httpJsonClient; + + @Before + public void createClients() throws IOException, GeneralSecurityException { + // Create gRPC Echo Client + EchoSettings grpcEchoSettings = + EchoSettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + InstantiatingGrpcChannelProvider.newBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + grpcClient = EchoClient.create(grpcEchoSettings); + + // Create Http JSON Echo Client + EchoSettings httpJsonEchoSettings = + EchoSettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + httpJsonClient = EchoClient.create(httpJsonEchoSettings); + } + + @After + public void destroyClient() { + grpcClient.close(); + httpJsonClient.close(); + } + + @Test + public void testGrpc_consumesAllResponses() { + String content = "The early bird catches the worm"; + ServerStream responseStream = grpcClient.expandCallable().call(ExpandRequest.newBuilder().setContent(content).build()); + ArrayList responses = new ArrayList<>(); + for (EchoResponse response : responseStream) { + responses.add(response.getContent()); + } + + assertThat(responses).containsExactlyElementsIn(ImmutableList.of("The", "early", "bird", "catches", "the", "worm")); + } + + @Test + public void testGrpc_errorHandling() { + String content = "The early bird catches the worm"; + Status cancelledStatus = Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); + ServerStream responseStream = grpcClient.expandCallable().call( + ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build()); + } + +} From 989eb78545759e4c46e36b81ff1323225d347944 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 16 Mar 2023 17:12:33 -0400 Subject: [PATCH 06/14] chore: add test for server-side streaming with gRPC transport --- .../v1beta1/it/ITServerSideStreaming.java | 126 ++++++++++-------- 1 file changed, 70 insertions(+), 56 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index edc1418ec9..22446cb538 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -1,5 +1,8 @@ package com.google.showcase.v1beta1.it; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; @@ -10,74 +13,85 @@ import com.google.rpc.Status; import com.google.showcase.v1beta1.*; import io.grpc.ManagedChannelBuilder; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - import java.io.IOException; import java.security.GeneralSecurityException; import java.util.ArrayList; - -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertThrows; +import java.util.Iterator; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class ITServerSideStreaming { - private static EchoClient grpcClient; + private static EchoClient grpcClient; - private static EchoClient httpJsonClient; + private static EchoClient httpJsonClient; - @Before - public void createClients() throws IOException, GeneralSecurityException { - // Create gRPC Echo Client - EchoSettings grpcEchoSettings = - EchoSettings.newBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder() - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) - .build()) - .build(); - grpcClient = EchoClient.create(grpcEchoSettings); + @Before + public void createClients() throws IOException, GeneralSecurityException { + // Create gRPC Echo Client + EchoSettings grpcEchoSettings = + EchoSettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + InstantiatingGrpcChannelProvider.newBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + grpcClient = EchoClient.create(grpcEchoSettings); - // Create Http JSON Echo Client - EchoSettings httpJsonEchoSettings = - EchoSettings.newHttpJsonBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultHttpJsonTransportProviderBuilder() - .setHttpTransport( - new NetHttpTransport.Builder().doNotValidateCertificate().build()) - .setEndpoint("http://localhost:7469") - .build()) - .build(); - httpJsonClient = EchoClient.create(httpJsonEchoSettings); - } - - @After - public void destroyClient() { - grpcClient.close(); - httpJsonClient.close(); - } + // Create Http JSON Echo Client + EchoSettings httpJsonEchoSettings = + EchoSettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + httpJsonClient = EchoClient.create(httpJsonEchoSettings); + } - @Test - public void testGrpc_consumesAllResponses() { - String content = "The early bird catches the worm"; - ServerStream responseStream = grpcClient.expandCallable().call(ExpandRequest.newBuilder().setContent(content).build()); - ArrayList responses = new ArrayList<>(); - for (EchoResponse response : responseStream) { - responses.add(response.getContent()); - } + @After + public void destroyClient() { + grpcClient.close(); + httpJsonClient.close(); + } - assertThat(responses).containsExactlyElementsIn(ImmutableList.of("The", "early", "bird", "catches", "the", "worm")); + @Test + public void testGrpc_receiveStreamedContent() { + String content = "The rain in Spain stays mainly on the plain!"; + ServerStream responseStream = + grpcClient.expandCallable().call(ExpandRequest.newBuilder().setContent(content).build()); + ArrayList responses = new ArrayList<>(); + for (EchoResponse response : responseStream) { + responses.add(response.getContent()); } - @Test - public void testGrpc_errorHandling() { - String content = "The early bird catches the worm"; - Status cancelledStatus = Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); - ServerStream responseStream = grpcClient.expandCallable().call( - ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build()); - } + assertThat(responses) + .containsExactlyElementsIn( + ImmutableList.of( + "The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")); + } + @Test + public void testGrpc_serverError_receiveErrorAfterLastWordInStream() { + String content = "The rain in Spain"; + Status cancelledStatus = + Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); + ServerStream responseStream = + grpcClient + .expandCallable() + .call(ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build()); + Iterator echoResponseIterator = responseStream.iterator(); + assertThat(echoResponseIterator.next().getContent()).isEqualTo("The"); + assertThat(echoResponseIterator.next().getContent()).isEqualTo("rain"); + assertThat(echoResponseIterator.next().getContent()).isEqualTo("in"); + assertThat(echoResponseIterator.next().getContent()).isEqualTo("Spain"); + CancelledException cancelledException = + assertThrows(CancelledException.class, echoResponseIterator::next); + assertThat(cancelledException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED); + } } From 039dedc67f808e1c1fd0775d4ca9f0c5c976a677 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Fri, 17 Mar 2023 09:51:40 -0400 Subject: [PATCH 07/14] and test for server error --- .../com/google/showcase/v1beta1/it/ITServerSideStreaming.java | 1 + 1 file changed, 1 insertion(+) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 22446cb538..9300193124 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -86,6 +86,7 @@ public void testGrpc_serverError_receiveErrorAfterLastWordInStream() { .expandCallable() .call(ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build()); Iterator echoResponseIterator = responseStream.iterator(); + assertThat(echoResponseIterator.next().getContent()).isEqualTo("The"); assertThat(echoResponseIterator.next().getContent()).isEqualTo("rain"); assertThat(echoResponseIterator.next().getContent()).isEqualTo("in"); From f945c0724eedae6d4470d770a0ec472128588fd6 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Mon, 20 Mar 2023 10:43:04 -0400 Subject: [PATCH 08/14] fix spacing --- .../com/google/showcase/v1beta1/it/ITServerSideStreaming.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 9300193124..01dd942c32 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -86,7 +86,7 @@ public void testGrpc_serverError_receiveErrorAfterLastWordInStream() { .expandCallable() .call(ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build()); Iterator echoResponseIterator = responseStream.iterator(); - + assertThat(echoResponseIterator.next().getContent()).isEqualTo("The"); assertThat(echoResponseIterator.next().getContent()).isEqualTo("rain"); assertThat(echoResponseIterator.next().getContent()).isEqualTo("in"); From 43ef73bfd6c588cc7a165d83b50817a1621f3ae3 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Wed, 22 Mar 2023 17:10:56 -0400 Subject: [PATCH 09/14] add header --- .../v1beta1/it/ITServerSideStreaming.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 01dd942c32..845d07f66b 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -1,3 +1,19 @@ +/* + * Copyright 2023 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; From 6932e88ebafec865f3199829fe9ffa35353fc87f Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 23 Mar 2023 12:08:54 -0400 Subject: [PATCH 10/14] remove http json setup code --- .../v1beta1/it/ITServerSideStreaming.java | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 845d07f66b..0271fd8926 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; -import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; import com.google.api.gax.rpc.CancelledException; @@ -40,9 +39,7 @@ public class ITServerSideStreaming { private static EchoClient grpcClient; - - private static EchoClient httpJsonClient; - + @Before public void createClients() throws IOException, GeneralSecurityException { // Create gRPC Echo Client @@ -55,25 +52,11 @@ public void createClients() throws IOException, GeneralSecurityException { .build()) .build(); grpcClient = EchoClient.create(grpcEchoSettings); - - // Create Http JSON Echo Client - EchoSettings httpJsonEchoSettings = - EchoSettings.newHttpJsonBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultHttpJsonTransportProviderBuilder() - .setHttpTransport( - new NetHttpTransport.Builder().doNotValidateCertificate().build()) - .setEndpoint("http://localhost:7469") - .build()) - .build(); - httpJsonClient = EchoClient.create(httpJsonEchoSettings); } @After public void destroyClient() { grpcClient.close(); - httpJsonClient.close(); } @Test From 2e534b7f5e553c3d83fac12de378b59f7c9e6250 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 23 Mar 2023 13:02:10 -0400 Subject: [PATCH 11/14] apply formatting --- .../google/showcase/v1beta1/it/ITServerSideStreaming.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 0271fd8926..2e3712e032 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -26,7 +26,9 @@ import com.google.api.gax.rpc.StatusCode; import com.google.common.collect.ImmutableList; import com.google.rpc.Status; -import com.google.showcase.v1beta1.*; +import com.google.showcase.v1beta1.EchoClient; +import com.google.showcase.v1beta1.EchoResponse; +import com.google.showcase.v1beta1.EchoSettings; import io.grpc.ManagedChannelBuilder; import java.io.IOException; import java.security.GeneralSecurityException; @@ -39,7 +41,7 @@ public class ITServerSideStreaming { private static EchoClient grpcClient; - + @Before public void createClients() throws IOException, GeneralSecurityException { // Create gRPC Echo Client From ec9255fab81f8ea85ed6045dd84d8c9f921f86f0 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 23 Mar 2023 15:01:56 -0400 Subject: [PATCH 12/14] fix compilation error --- .../com/google/showcase/v1beta1/it/ITServerSideStreaming.java | 1 + 1 file changed, 1 insertion(+) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index 2e3712e032..c43d6c4453 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -29,6 +29,7 @@ import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoResponse; import com.google.showcase.v1beta1.EchoSettings; +import com.google.showcase.v1beta1.ExpandRequest; import io.grpc.ManagedChannelBuilder; import java.io.IOException; import java.security.GeneralSecurityException; From fc7da8675aa39cac1cdf947d33cd5f61dcdbf80f Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 23 Mar 2023 16:19:10 -0400 Subject: [PATCH 13/14] use non-static and make assertion stricter --- .../com/google/showcase/v1beta1/it/ITServerSideStreaming.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index c43d6c4453..adb40b549f 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -41,7 +41,7 @@ public class ITServerSideStreaming { - private static EchoClient grpcClient; + private EchoClient grpcClient; @Before public void createClients() throws IOException, GeneralSecurityException { @@ -75,7 +75,7 @@ public void testGrpc_receiveStreamedContent() { assertThat(responses) .containsExactlyElementsIn( ImmutableList.of( - "The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")); + "The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")).inOrder(); } @Test From 01d81d7ae6ae46242246a9bfa55a1fd3eb19ac88 Mon Sep 17 00:00:00 2001 From: Mridula Peddada Date: Thu, 23 Mar 2023 16:46:48 -0400 Subject: [PATCH 14/14] formatter --- .../com/google/showcase/v1beta1/it/ITServerSideStreaming.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java index adb40b549f..107f4f9380 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java @@ -75,7 +75,8 @@ public void testGrpc_receiveStreamedContent() { assertThat(responses) .containsExactlyElementsIn( ImmutableList.of( - "The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")).inOrder(); + "The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")) + .inOrder(); } @Test