34
34
import org .eclipse .hono .adapter .MapperEndpoint ;
35
35
import org .eclipse .hono .adapter .mqtt .MqttContext ;
36
36
import org .eclipse .hono .adapter .mqtt .MqttProtocolAdapterProperties ;
37
+ import org .eclipse .hono .client .ClientErrorException ;
37
38
import org .eclipse .hono .client .ServerErrorException ;
38
39
import org .eclipse .hono .client .command .Command ;
39
40
import org .eclipse .hono .service .auth .DeviceUser ;
@@ -353,7 +354,7 @@ public void testMapCommandSucceeds(final VertxTestContext ctx) {
353
354
}
354
355
355
356
/**
356
- * Verifies that the upstream mapper returns a failed future with a ServerErrorException if the upstream mapper has been configured
357
+ * Verifies that the upstream mapper returns a failed future with a ClientErrorException if the upstream mapper has been configured
357
358
* for an adapter but the remote service returns a 403 status code indicating that the device payload cannot be mapped.
358
359
*
359
360
* @param ctx The Vert.x test context.
@@ -379,8 +380,8 @@ public void testMappingCommandFailsForWhenPayloadCannotMapped(final VertxTestCon
379
380
messageMapping .mapUpstreamMessage (assertion , command )
380
381
.onComplete (ctx .failing (t -> {
381
382
ctx .verify (() -> {
382
- assertThat (t ).isInstanceOf (ServerErrorException .class );
383
- assertThat ((((ServerErrorException ) t ).getErrorCode ())).isEqualTo (HttpURLConnection .HTTP_UNAVAILABLE );
383
+ assertThat (t ).isInstanceOf (ClientErrorException .class );
384
+ assertThat ((((ClientErrorException ) t ).getErrorCode ())).isEqualTo (HttpURLConnection .HTTP_FORBIDDEN );
384
385
});
385
386
ctx .completeNow ();
386
387
}));
@@ -389,4 +390,39 @@ public void testMappingCommandFailsForWhenPayloadCannotMapped(final VertxTestCon
389
390
verify (httpRequest ).sendBuffer (any (Buffer .class ), handleCaptor .capture ());
390
391
handleCaptor .getValue ().handle (Future .succeededFuture (httpResponse ));
391
392
}
393
+
394
+ /**
395
+ * Verifies that the upstream mapper returns a failed future with a ServerErrorException if the upstream mapper has been configured
396
+ * for an adapter but the remote service cannot be reached should return a 503.
397
+ *
398
+ * @param ctx The Vert.x test context.
399
+ */
400
+ @ Test
401
+ @ SuppressWarnings ("unchecked" )
402
+ public void testMappingCommandFailsForWhenMapperCannotBeReached (final VertxTestContext ctx ) {
403
+
404
+ config .setMapperEndpoints (Map .of ("mapper" , MapperEndpoint .from ("host" , 1234 , "/uri" , false )));
405
+ final HttpRequest <Buffer > httpRequest = mock (HttpRequest .class , withSettings ().defaultAnswer (RETURNS_SELF ));
406
+
407
+ final Buffer payload = Buffer .buffer ("payload" );
408
+
409
+ when (mapperWebClient .post (anyInt (), anyString (), anyString ())).thenReturn (httpRequest );
410
+
411
+ final Command command = mock (Command .class );
412
+ when (command .getPayload ()).thenReturn (payload );
413
+
414
+ final RegistrationAssertion assertion = new RegistrationAssertion ("gateway" ).setUpstreamMessageMapper ("mapper" );
415
+ messageMapping .mapUpstreamMessage (assertion , command )
416
+ .onComplete (ctx .failing (t -> {
417
+ ctx .verify (() -> {
418
+ assertThat (t ).isInstanceOf (ServerErrorException .class );
419
+ assertThat ((((ServerErrorException ) t ).getErrorCode ())).isEqualTo (HttpURLConnection .HTTP_UNAVAILABLE );
420
+ });
421
+ ctx .completeNow ();
422
+ }));
423
+
424
+ final ArgumentCaptor <Handler <AsyncResult <HttpResponse <Buffer >>>> handleCaptor = VertxMockSupport .argumentCaptorHandler ();
425
+ verify (httpRequest ).sendBuffer (any (Buffer .class ), handleCaptor .capture ());
426
+ handleCaptor .getValue ().handle (Future .failedFuture (new RuntimeException ("something went wrong" )));
427
+ }
392
428
}
0 commit comments