Skip to content

Commit 8ec304a

Browse files
authored
chore: Add httpjson server-side streaming showcase tests (#1588)
1 parent 159ce50 commit 8ec304a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java

+57
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertThrows;
2121

22+
import com.google.api.client.http.javanet.NetHttpTransport;
2223
import com.google.api.gax.core.NoCredentialsProvider;
2324
import com.google.api.gax.rpc.CancelledException;
2425
import com.google.api.gax.rpc.ServerStream;
@@ -35,11 +36,13 @@
3536
import java.util.Iterator;
3637
import org.junit.After;
3738
import org.junit.Before;
39+
import org.junit.Ignore;
3840
import org.junit.Test;
3941

4042
public class ITServerSideStreaming {
4143

4244
private EchoClient grpcClient;
45+
private EchoClient httpjsonClient;
4346

4447
@Before
4548
public void createClients() throws IOException {
@@ -53,11 +56,24 @@ public void createClients() throws IOException {
5356
.build())
5457
.build();
5558
grpcClient = EchoClient.create(grpcEchoSettings);
59+
// Create Http JSON Echo Client
60+
EchoSettings httpJsonEchoSettings =
61+
EchoSettings.newHttpJsonBuilder()
62+
.setCredentialsProvider(NoCredentialsProvider.create())
63+
.setTransportChannelProvider(
64+
EchoSettings.defaultHttpJsonTransportProviderBuilder()
65+
.setHttpTransport(
66+
new NetHttpTransport.Builder().doNotValidateCertificate().build())
67+
.setEndpoint("http://localhost:7469")
68+
.build())
69+
.build();
70+
httpjsonClient = EchoClient.create(httpJsonEchoSettings);
5671
}
5772

5873
@After
5974
public void destroyClient() {
6075
grpcClient.close();
76+
httpjsonClient.close();
6177
}
6278

6379
@Test
@@ -96,4 +112,45 @@ public void testGrpc_serverError_receiveErrorAfterLastWordInStream() {
96112
assertThrows(CancelledException.class, echoResponseIterator::next);
97113
assertThat(cancelledException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED);
98114
}
115+
116+
@Test
117+
public void testHttpJson_receiveStreamedContent() {
118+
String content = "The rain in Spain stays mainly on the plain!";
119+
ServerStream<EchoResponse> responseStream =
120+
httpjsonClient
121+
.expandCallable()
122+
.call(ExpandRequest.newBuilder().setContent(content).build());
123+
ArrayList<String> responses = new ArrayList<>();
124+
for (EchoResponse response : responseStream) {
125+
responses.add(response.getContent());
126+
}
127+
128+
assertThat(responses)
129+
.containsExactlyElementsIn(
130+
ImmutableList.of(
131+
"The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!"))
132+
.inOrder();
133+
}
134+
135+
@Ignore(
136+
value = "Ignore until https://github.com/googleapis/gapic-showcase/issues/1286 is resolved")
137+
@Test
138+
public void testHttpJson_serverError_receiveErrorAfterLastWordInStream() {
139+
String content = "The rain in Spain";
140+
Status cancelledStatus =
141+
Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build();
142+
ServerStream<EchoResponse> responseStream =
143+
httpjsonClient
144+
.expandCallable()
145+
.call(ExpandRequest.newBuilder().setContent(content).setError(cancelledStatus).build());
146+
Iterator<EchoResponse> echoResponseIterator = responseStream.iterator();
147+
148+
assertThat(echoResponseIterator.next().getContent()).isEqualTo("The");
149+
assertThat(echoResponseIterator.next().getContent()).isEqualTo("rain");
150+
assertThat(echoResponseIterator.next().getContent()).isEqualTo("in");
151+
assertThat(echoResponseIterator.next().getContent()).isEqualTo("Spain");
152+
CancelledException cancelledException =
153+
assertThrows(CancelledException.class, echoResponseIterator::next);
154+
assertThat(cancelledException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED);
155+
}
99156
}

0 commit comments

Comments
 (0)