use of org.eclipse.hono.service.auth.device.Device in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageFailsForUnknownDevice.
/**
* Verifies that the adapter does not forward a message published by a device
* if the device's registration status cannot be asserted.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryMessageFailsForUnknownDevice(final TestContext ctx) {
// GIVEN an adapter
final MqttServer server = getMqttServer(false);
final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
givenATelemetrySenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
// WHEN an unknown device publishes a telemetry message
when(regClient.assertRegistration(eq("unknown"), any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
final MessageSender sender = mock(MessageSender.class);
when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
adapter.uploadTelemetryMessage(new MqttContext(mock(MqttPublishMessage.class), mock(MqttEndpoint.class)), "my-tenant", "unknown", Buffer.buffer("test")).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message has not been sent downstream
verify(sender, never()).send(any(Message.class));
// because the device's registration status could not be asserted
ctx.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ((ClientErrorException) t).getErrorCode());
}));
}
use of org.eclipse.hono.service.auth.device.Device in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageUsesDeviceIdentityForTopicWithoutTenant.
/**
* Verifies that the adapter uses an authenticated device's identity when mapping a topic without tenant ID.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testOnPublishedMessageUsesDeviceIdentityForTopicWithoutTenant(final TestContext ctx) {
givenAnAdapter();
// WHEN an authenticated device publishes a message to a topic that does not contain a tenant ID
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final MqttContext context = newContext(message, new Device("my-tenant", "4711"));
final Async addressCheck = ctx.async();
final Future<ResourceIdentifier> checkedAddress = adapter.mapTopic(message).compose(address -> adapter.checkAddress(context, address)).map(address -> {
addressCheck.complete();
return address;
});
// THEN the mapped address contains the authenticated device's tenant and device ID
addressCheck.await();
final ResourceIdentifier downstreamAddress = checkedAddress.result();
assertThat(downstreamAddress.getEndpoint(), is(TelemetryConstants.TELEMETRY_ENDPOINT));
assertThat(downstreamAddress.getTenantId(), is("my-tenant"));
}
use of org.eclipse.hono.service.auth.device.Device in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testMapTopicFailsForQoS2EventMessage.
/**
* Verifies that the adapter rejects QoS 2 messages published to the <em>event</em> endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicFailsForQoS2EventMessage(final TestContext ctx) {
givenAnAdapter();
// WHEN a device publishes a message with QoS 2 to an "event" topic
final MqttPublishMessage message = newMessage(MqttQoS.EXACTLY_ONCE, EventConstants.EVENT_ENDPOINT);
adapter.mapTopic(message).setHandler(ctx.asyncAssertFailure(t -> {
// THEN no downstream sender can be created for the message
}));
}
use of org.eclipse.hono.service.auth.device.Device in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testMapTopicFailsForQoS2TelemetryMessage.
/**
* Verifies that the adapter rejects QoS 2 messages published to the <em>telemetry</em> endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicFailsForQoS2TelemetryMessage(final TestContext ctx) {
givenAnAdapter();
// WHEN a device publishes a message with QoS 2 to a "telemetry" topic
final MqttPublishMessage message = newMessage(MqttQoS.EXACTLY_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
adapter.mapTopic(message).setHandler(ctx.asyncAssertFailure(t -> {
// THEN no downstream sender can be created for the message
}));
}
use of org.eclipse.hono.service.auth.device.Device in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testMapTopicFailsForUnknownEndpoint.
/**
* Verifies that the adapter rejects messages published to topics containing an endpoint
* other than <em>telemetry</em> or <em>event</em>.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicFailsForUnknownEndpoint(final TestContext ctx) {
givenAnAdapter();
// WHEN a device publishes a message to a topic with an unknown endpoint
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, "unknown");
adapter.mapTopic(message).setHandler(ctx.asyncAssertFailure(t -> {
// THEN no downstream sender can be created for the message
}));
}
Aggregations