use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMappingFailsForWhenPayloadCannotMapped.
/**
* Verifies that the downstream mapper returns a failed future with a ServerErrorException if the downstream mapper has been configured
* for an adapter but the remote service returns a 403 status code indicating that the device payload cannot be mapped.
*
* @param ctx The Vert.x test context.
*/
@Test
@SuppressWarnings("unchecked")
public void testMappingFailsForWhenPayloadCannotMapped(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, "gateway");
final HttpRequest<Buffer> httpRequest = mock(HttpRequest.class, withSettings().defaultAnswer(RETURNS_SELF));
final HttpResponse<Buffer> httpResponse = mock(HttpResponse.class);
when(httpResponse.statusCode()).thenReturn(HttpURLConnection.HTTP_FORBIDDEN);
when(mapperWebClient.post(anyInt(), anyString(), anyString())).thenReturn(httpRequest);
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, "mqtt-topic");
final MqttContext context = newContext(message, span, new Device(TEST_TENANT_ID, "gateway"));
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setDownstreamMessageMapper("mapper");
messageMapping.mapDownstreamMessage(context, targetAddress, assertion).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
assertThat(t).isInstanceOf(ServerErrorException.class);
assertThat((((ServerErrorException) t).getErrorCode())).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
});
ctx.completeNow();
}));
final ArgumentCaptor<Handler<AsyncResult<HttpResponse<Buffer>>>> handlerCaptor = VertxMockSupport.argumentCaptorHandler();
verify(httpRequest).sendBuffer(any(Buffer.class), handlerCaptor.capture());
handlerCaptor.getValue().handle(Future.succeededFuture(httpResponse));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMappingCommandFailsForWhenPayloadCannotMapped.
/**
* Verifies that the upstream mapper returns a failed future with a ServerErrorException if the upstream mapper has been configured
* for an adapter but the remote service returns a 403 status code indicating that the device payload cannot be mapped.
*
* @param ctx The Vert.x test context.
*/
@Test
@SuppressWarnings("unchecked")
public void testMappingCommandFailsForWhenPayloadCannotMapped(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final HttpRequest<Buffer> httpRequest = mock(HttpRequest.class, withSettings().defaultAnswer(RETURNS_SELF));
final Buffer payload = Buffer.buffer("payload");
final HttpResponse<Buffer> httpResponse = mock(HttpResponse.class);
when(httpResponse.statusCode()).thenReturn(HttpURLConnection.HTTP_FORBIDDEN);
when(mapperWebClient.post(anyInt(), anyString(), anyString())).thenReturn(httpRequest);
final Command command = mock(Command.class);
when(command.getPayload()).thenReturn(payload);
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setUpstreamMessageMapper("mapper");
messageMapping.mapUpstreamMessage(assertion, command).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
assertThat(t).isInstanceOf(ServerErrorException.class);
assertThat((((ServerErrorException) t).getErrorCode())).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
});
ctx.completeNow();
}));
final ArgumentCaptor<Handler<AsyncResult<HttpResponse<Buffer>>>> handleCaptor = VertxMockSupport.argumentCaptorHandler();
verify(httpRequest).sendBuffer(any(Buffer.class), handleCaptor.capture());
handleCaptor.getValue().handle(Future.succeededFuture(httpResponse));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testMessageLimitExceededForACommandResponseMessage.
/**
* Verifies that a command response message is rejected due to the limit exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testMessageLimitExceededForACommandResponseMessage(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
final CommandResponseSender sender = givenACommandResponseSenderForAnyTenant();
// WHEN the message limit exceeds
when(resourceLimitChecks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
// WHEN a device of "tenant" publishes a command response message
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("e/tenant/device");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_MOST_ONCE);
when(msg.payload()).thenReturn(Buffer.buffer("test"));
adapter.uploadMessage(newMqttContext(msg, mockEndpoint(), span), ResourceIdentifier.fromString(String.format("%s/tenant/device/res/%s/200", getCommandEndpoint(), Commands.encodeRequestIdParameters("cmd123", "to", "deviceId", MessagingType.amqp))), msg).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the request fails with a 429 error
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpUtils.HTTP_TOO_MANY_REQUESTS);
// AND the response is not being forwarded
verify(sender, never()).sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any());
// AND has reported the message as unprocessable
verify(metrics).reportCommand(eq(MetricsTags.Direction.RESPONSE), eq("tenant"), any(), eq(MetricsTags.ProcessingOutcome.UNPROCESSABLE), anyInt(), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method verifyEventMessageUsesTtlValueGivenInPropertyBag.
/**
* Verifies that the TTL for a downstream event is set to the given <em>time-to-live</em> value in the
* <em>property-bag</em>.
*
* @param ctx The vert.x test context.
*/
@Test
public void verifyEventMessageUsesTtlValueGivenInPropertyBag(final VertxTestContext ctx) {
// Given an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
// WHEN a "device" of "tenant" publishes an event message with a TTL value of 30 seconds.
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("e/tenant/device/?hono-ttl=30¶m2=value2");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
adapter.uploadEventMessage(newMqttContext(msg, mockEndpoint(), span), "tenant", "device", Buffer.buffer("test")).onComplete(ctx.succeeding(t -> {
ctx.verify(() -> {
// THEN the TTL value of the amqp message is 30 seconds.
assertEventHasBeenSentDownstream("tenant", "device", null, 30L);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnAllDevicesOfTenantDeletedNotification.
/**
* Verifies that the adapter closes the connection to an authenticated device when a notification
* about the deletion of all device data of the tenant of that device has been received.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDeviceConnectionIsClosedOnAllDevicesOfTenantDeletedNotification(final VertxTestContext ctx) {
final Device device = new Device("tenant", "deviceId");
testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new AllDevicesOfTenantDeletedNotification("tenant", Instant.now()));
}
Aggregations