Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add httpjson server-side streaming showcase tests #1588

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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<EchoResponse> responseStream =
httpjsonClient
.expandCallable()
.call(ExpandRequest.newBuilder().setContent(content).build());
ArrayList<String> 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")
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testHttpJson_serverError_receiveErrorAfterLastWordInStream() {
String content = "The rain in Spain";
Status cancelledStatus =
Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build();
ServerStream<EchoResponse> responseStream =
httpjsonClient
.expandCallable()
.call(ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build());
Iterator<EchoResponse> 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);
}
}