From 97ea5d80be716fee748eb6a29a90d97c32b8886e Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Thu, 30 Mar 2023 14:17:43 -0400 Subject: [PATCH] chore: Add httpjson server-side streaming showcase tests --- .../v1beta1/it/ITServerSideStreaming.java | 57 +++++++++++++++++++ 1 file changed, 57 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 107f4f9380..41ec4ae081 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,6 +19,7 @@ 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; @@ -37,11 +38,13 @@ import java.util.Iterator; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class ITServerSideStreaming { private EchoClient grpcClient; + private EchoClient httpjsonClient; @Before public void createClients() throws IOException, GeneralSecurityException { @@ -55,11 +58,24 @@ 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 @@ -98,4 +114,45 @@ public void testGrpc_serverError_receiveErrorAfterLastWordInStream() { assertThrows(CancelledException.class, echoResponseIterator::next); assertThat(cancelledException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED); } + + @Test + public void testHttpJson_receiveStreamedContent() { + String content = "The rain in Spain stays mainly on the plain!"; + ServerStream responseStream = + httpjsonClient + .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", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!")) + .inOrder(); + } + + @Ignore( + value = "Ignore until https://github.com/googleapis/gapic-showcase/issues/1286 is resolved") + @Test + public void testHttpJson_serverError_receiveErrorAfterLastWordInStream() { + String content = "The rain in Spain"; + Status cancelledStatus = + Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); + ServerStream responseStream = + httpjsonClient + .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); + } }