@@ -138,14 +138,7 @@ public void testSuccessfulUnaryResponse() throws ExecutionException, Interrupted
138138
139139 Field request ;
140140 Field expectedResponse ;
141- request =
142- expectedResponse =
143- Field .newBuilder () // "echo" service
144- .setName ("imTheBestField" )
145- .setNumber (2 )
146- .setCardinality (Cardinality .CARDINALITY_OPTIONAL )
147- .setDefaultValue ("blah" )
148- .build ();
141+ request = expectedResponse = createTestMessage ();
149142
150143 MOCK_SERVICE .addResponse (expectedResponse );
151144
@@ -164,27 +157,65 @@ public void testErrorUnaryResponse() throws InterruptedException {
164157
165158 HttpJsonCallContext callContext = HttpJsonCallContext .createDefault ().withChannel (channel );
166159
167- Field request ;
168- request =
169- Field .newBuilder () // "echo" service
170- .setName ("imTheBestField" )
171- .setNumber (2 )
172- .setCardinality (Cardinality .CARDINALITY_OPTIONAL )
173- .setDefaultValue ("blah" )
174- .build ();
175-
176160 ApiException exception =
177161 ApiExceptionFactory .createException (
178162 new Exception (), FakeStatusCode .of (Code .NOT_FOUND ), false );
179163 MOCK_SERVICE .addException (exception );
180164
181165 try {
182- callable .futureCall (request , callContext ).get ();
166+ callable .futureCall (createTestMessage () , callContext ).get ();
183167 Assert .fail ("No exception raised" );
184168 } catch (ExecutionException e ) {
185169 HttpResponseException respExp = (HttpResponseException ) e .getCause ();
186170 assertThat (respExp .getStatusCode ()).isEqualTo (400 );
187171 assertThat (respExp .getContent ()).isEqualTo (exception .toString ());
188172 }
189173 }
174+
175+ @ Test
176+ public void testErrorNullContentSuccessfulResponse () throws InterruptedException {
177+ HttpJsonDirectCallable <Field , Field > callable =
178+ new HttpJsonDirectCallable <>(FAKE_METHOD_DESCRIPTOR );
179+
180+ HttpJsonCallContext callContext = HttpJsonCallContext .createDefault ().withChannel (channel );
181+
182+ MOCK_SERVICE .addNullResponse ();
183+
184+ try {
185+ callable .futureCall (createTestMessage (), callContext ).get ();
186+ Assert .fail ("No exception raised" );
187+ } catch (ExecutionException e ) {
188+ HttpJsonStatusRuntimeException respExp = (HttpJsonStatusRuntimeException ) e .getCause ();
189+ assertThat (respExp .getStatusCode ()).isEqualTo (200 );
190+ assertThat (respExp .getCause ().getMessage ())
191+ .isEqualTo ("Both response message and response exception were null" );
192+ }
193+ }
194+
195+ @ Test
196+ public void testErrorNullContentFailedResponse () throws InterruptedException {
197+ HttpJsonDirectCallable <Field , Field > callable =
198+ new HttpJsonDirectCallable <>(FAKE_METHOD_DESCRIPTOR );
199+
200+ HttpJsonCallContext callContext = HttpJsonCallContext .createDefault ().withChannel (channel );
201+ MOCK_SERVICE .addNullResponse (400 );
202+
203+ try {
204+ callable .futureCall (createTestMessage (), callContext ).get ();
205+ Assert .fail ("No exception raised" );
206+ } catch (ExecutionException e ) {
207+ HttpResponseException respExp = (HttpResponseException ) e .getCause ();
208+ assertThat (respExp .getStatusCode ()).isEqualTo (400 );
209+ assertThat (respExp .getContent ()).isNull ();
210+ }
211+ }
212+
213+ private Field createTestMessage () {
214+ return Field .newBuilder () // "echo" service
215+ .setName ("imTheBestField" )
216+ .setNumber (2 )
217+ .setCardinality (Cardinality .CARDINALITY_OPTIONAL )
218+ .setDefaultValue ("blah" )
219+ .build ();
220+ }
190221}
0 commit comments