use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification.
private void testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(final VertxTestContext ctx, final Device device, final AbstractNotification notification) {
// GIVEN a device connected to an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(true);
final Promise<Void> startPromise = Promise.promise();
adapter.doStart(startPromise);
assertThat(startPromise.future().succeeded()).isTrue();
adapter.createMqttDeviceEndpoint(endpoint, device, OptionalInt.empty());
final Promise<Void> endpointClosedPromise = Promise.promise();
doAnswer(invocation -> {
endpointClosedPromise.complete();
return null;
}).when(endpoint).close();
// WHEN a notification is sent about the tenant/device having been deleted or disabled
NotificationEventBusSupport.sendNotification(vertx, notification);
// THEN the MQTT endpoint representing the device connection is closed
endpointClosedPromise.future().onComplete(ctx.succeeding(v -> {
ctx.verify(() -> {
// and the adapter didn't send an empty notification downstream
assertEmptyNotificationHasNotBeenSentDownstream(device.getTenantId(), device.getDeviceId(), 0);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadEmptyCommandResponseSucceeds.
/**
* Verifies that the adapter accepts a command response message with an empty body.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadEmptyCommandResponseSucceeds(final VertxTestContext ctx) {
// GIVEN an adapter with a command response consumer
final CommandResponseSender sender = givenACommandResponseSenderForAnyTenant();
// WHEN forwarding a command response that has been published
givenAnAdapter(properties);
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_MOST_ONCE);
when(messageFromDevice.messageId()).thenReturn(5555555);
when(messageFromDevice.topicName()).thenReturn("command/my-tenant/4712/res/1010f8ab0b53-bd96-4d99-9d9c-56b868474a6a/200");
// ... with an empty payload
when(messageFromDevice.payload()).thenReturn(null);
final ResourceIdentifier address = ResourceIdentifier.fromString("command/my-tenant/4712/res/1010f8ab0b53-bd96-4d99-9d9c-56b868474a6a/200");
adapter.uploadCommandResponseMessage(newMqttContext(messageFromDevice, endpoint, span), address).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
verify(sender).sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), any());
// then it is forwarded successfully
verify(metrics).reportCommand(eq(MetricsTags.Direction.RESPONSE), eq("my-tenant"), any(TenantObject.class), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(0), any());
ctx.completeNow();
});
}));
}
Aggregations