19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static org .junit .Assert .assertThrows ;
21
21
22
+ import com .google .api .client .http .javanet .NetHttpTransport ;
22
23
import com .google .api .gax .core .NoCredentialsProvider ;
23
24
import com .google .api .gax .rpc .CancelledException ;
24
25
import com .google .api .gax .rpc .ServerStream ;
35
36
import java .util .Iterator ;
36
37
import org .junit .After ;
37
38
import org .junit .Before ;
39
+ import org .junit .Ignore ;
38
40
import org .junit .Test ;
39
41
40
42
public class ITServerSideStreaming {
41
43
42
44
private EchoClient grpcClient ;
45
+ private EchoClient httpjsonClient ;
43
46
44
47
@ Before
45
48
public void createClients () throws IOException {
@@ -53,11 +56,24 @@ public void createClients() throws IOException {
53
56
.build ())
54
57
.build ();
55
58
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 );
56
71
}
57
72
58
73
@ After
59
74
public void destroyClient () {
60
75
grpcClient .close ();
76
+ httpjsonClient .close ();
61
77
}
62
78
63
79
@ Test
@@ -96,4 +112,45 @@ public void testGrpc_serverError_receiveErrorAfterLastWordInStream() {
96
112
assertThrows (CancelledException .class , echoResponseIterator ::next );
97
113
assertThat (cancelledException .getStatusCode ().getCode ()).isEqualTo (StatusCode .Code .CANCELLED );
98
114
}
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
+ }
99
156
}
0 commit comments