16
16
17
17
package org .springframework .cloud .gateway .tests .grpc ;
18
18
19
+ import static io .grpc .Status .FAILED_PRECONDITION ;
20
+ import static io .grpc .Status .RESOURCE_EXHAUSTED ;
21
+ import static io .grpc .netty .NegotiationType .TLS ;
22
+
19
23
import java .security .cert .X509Certificate ;
24
+ import java .util .Iterator ;
20
25
21
26
import javax .net .ssl .SSLException ;
22
27
import javax .net .ssl .TrustManager ;
23
28
import javax .net .ssl .X509TrustManager ;
24
29
25
- import io .grpc .ManagedChannel ;
26
- import io .grpc .StatusRuntimeException ;
27
- import io .grpc .netty .GrpcSslContexts ;
28
- import io .grpc .netty .NettyChannelBuilder ;
29
30
import org .assertj .core .api .Assertions ;
30
31
import org .junit .jupiter .api .BeforeEach ;
31
32
import org .junit .jupiter .api .Test ;
32
-
33
33
import org .springframework .boot .SpringApplication ;
34
34
import org .springframework .boot .test .context .SpringBootTest ;
35
35
import org .springframework .boot .test .context .SpringBootTest .WebEnvironment ;
36
36
import org .springframework .boot .web .server .test .LocalServerPort ;
37
37
import org .springframework .test .annotation .DirtiesContext ;
38
38
39
- import static io .grpc .Status .FAILED_PRECONDITION ;
40
- import static io .grpc .Status .RESOURCE_EXHAUSTED ;
41
- import static io .grpc .netty .NegotiationType .TLS ;
39
+ import io .grpc .ManagedChannel ;
40
+ import io .grpc .StatusRuntimeException ;
41
+ import io .grpc .netty .GrpcSslContexts ;
42
+ import io .grpc .netty .NettyChannelBuilder ;
42
43
43
44
/**
44
45
* @author Alberto C. Ríos
45
46
*/
46
- @ SpringBootTest (classes = org .springframework .cloud .gateway .tests .grpc .GRPCApplication .class ,
47
- webEnvironment = WebEnvironment .RANDOM_PORT )
47
+ @ SpringBootTest (classes = org .springframework .cloud .gateway .tests .grpc .GRPCApplication .class , webEnvironment = WebEnvironment .RANDOM_PORT )
48
48
@ DirtiesContext
49
49
public class GRPCApplicationTests {
50
50
@@ -67,19 +67,31 @@ public void gRPCUnaryCallShouldReturnResponse() throws SSLException {
67
67
ManagedChannel channel = createSecuredChannel (gatewayPort );
68
68
69
69
final HelloResponse response = HelloServiceGrpc .newBlockingStub (channel )
70
- .hello (HelloRequest .newBuilder ().setFirstName ("Sir" ).setLastName ("FromClient" ).build ());
70
+ .hello (HelloRequest .newBuilder ().setFirstName ("Sir" ).setLastName ("FromClient" ).build ());
71
71
72
72
Assertions .assertThat (response .getGreeting ()).isEqualTo ("Hello, Sir FromClient" );
73
73
}
74
74
75
+ @ Test
76
+ public void gRPCStreamingCallShouldReturnResponse () throws SSLException {
77
+ ManagedChannel channel = createSecuredChannel (gatewayPort );
78
+
79
+ final Iterator <HelloResponse > response = StreamServiceGrpc .newBlockingStub (channel )
80
+ .more (HelloRequest .newBuilder ().setFirstName ("Sir" ).setLastName ("FromClient" ).build ());
81
+
82
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(0) ==> Sir" );
83
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(1) ==> Sir" );
84
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(2) ==> Sir" );
85
+ }
86
+
75
87
private ManagedChannel createSecuredChannel (int port ) throws SSLException {
76
88
TrustManager [] trustAllCerts = createTrustAllTrustManager ();
77
89
78
90
return NettyChannelBuilder .forAddress ("localhost" , port )
79
- .useTransportSecurity ()
80
- .sslContext (GrpcSslContexts .forClient ().trustManager (trustAllCerts [0 ]).build ())
81
- .negotiationType (TLS )
82
- .build ();
91
+ .useTransportSecurity ()
92
+ .sslContext (GrpcSslContexts .forClient ().trustManager (trustAllCerts [0 ]).build ())
93
+ .negotiationType (TLS )
94
+ .build ();
83
95
}
84
96
85
97
@ Test
@@ -88,24 +100,40 @@ public void gRPCUnaryCallShouldHandleRuntimeException() throws SSLException {
88
100
89
101
try {
90
102
HelloServiceGrpc .newBlockingStub (channel )
91
- .hello (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeException!" ).build ());
92
- }
93
- catch (StatusRuntimeException e ) {
103
+ .hello (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeException!" ).build ());
104
+ } catch (StatusRuntimeException e ) {
94
105
Assertions .assertThat (FAILED_PRECONDITION .getCode ()).isEqualTo (e .getStatus ().getCode ());
95
106
Assertions .assertThat ("Invalid firstName" ).isEqualTo (e .getStatus ().getDescription ());
96
107
}
97
108
}
98
109
99
110
@ Test
100
- public void gRPCUnaryCallShouldHandleRuntimeException2 () throws SSLException {
111
+ public void gRPCUnaryCallShouldHandleRuntimeExceptionAfterData () throws SSLException {
101
112
ManagedChannel channel = createSecuredChannel (gatewayPort );
102
113
boolean thrown = false ;
103
114
try {
104
115
HelloServiceGrpc .newBlockingStub (channel )
105
116
.hello (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeExceptionAfterData!" ).build ())
106
117
.getGreeting ();
118
+ } catch (StatusRuntimeException e ) {
119
+ thrown = true ;
120
+ Assertions .assertThat (e .getStatus ().getCode ()).isEqualTo (RESOURCE_EXHAUSTED .getCode ());
121
+ Assertions .assertThat (e .getStatus ().getDescription ()).isEqualTo ("Too long firstNames?" );
107
122
}
108
- catch (StatusRuntimeException e ) {
123
+ Assertions .assertThat (thrown ).withFailMessage ("Expected exception not thrown!" ).isTrue ();
124
+ }
125
+
126
+ @ Test
127
+ public void gRPCStreamingCallShouldHandleRuntimeExceptionAfterData () throws SSLException {
128
+ ManagedChannel channel = createSecuredChannel (gatewayPort );
129
+ boolean thrown = false ;
130
+ final Iterator <HelloResponse > response = StreamServiceGrpc .newBlockingStub (channel )
131
+ .more (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeExceptionAfterData!" ).setLastName ("FromClient" ).build ());
132
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(0) ==> failWithRuntimeExceptionAfterData!" );
133
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(1) ==> failWithRuntimeExceptionAfterData!" );
134
+ try {
135
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(2) ==> failWithRuntimeExceptionAfterData!" );
136
+ } catch (StatusRuntimeException e ) {
109
137
thrown = true ;
110
138
Assertions .assertThat (e .getStatus ().getCode ()).isEqualTo (RESOURCE_EXHAUSTED .getCode ());
111
139
Assertions .assertThat (e .getStatus ().getDescription ()).isEqualTo ("Too long firstNames?" );
0 commit comments