use of org.eclipse.hono.adapter.mqtt.MqttContext in project hono by eclipse.
the class KuraProtocolAdapterTest method testMapTopicMapsKuraControlMessagesToEventApi.
/**
* Verifies that the adapter maps control messages with QoS 1 published from a Kura gateway to
* the Event endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicMapsKuraControlMessagesToEventApi(final TestContext ctx) {
// GIVEN an adapter configured to use the standard topic.control-prefix $EDC
// WHEN a message is published to a topic with the Kura $EDC prefix as endpoint
final MqttContext context = newContext(MqttQoS.AT_LEAST_ONCE, "$EDC/my-scope/4711");
final Async determineAddressSuccess = ctx.async();
final Future<ResourceIdentifier> addressTracker = adapter.mapTopic(context).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is mapped to the event API
determineAddressSuccess.await();
assertAddress(addressTracker.result(), EventConstants.EVENT_ENDPOINT, "my-scope", "4711");
// and has the control message content type
assertThat(context.contentType(), is(config.getCtrlMsgContentType()));
}
use of org.eclipse.hono.adapter.mqtt.MqttContext in project hono by eclipse.
the class KuraProtocolAdapterTest method testMapTopicMapsKuraDataMessagesToTelemetryApi.
/**
* Verifies that the adapter forwards data messages with QoS 0 published from a Kura gateway to
* the Telemetry endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicMapsKuraDataMessagesToTelemetryApi(final TestContext ctx) {
// GIVEN an adapter configured with a custom data message content type
config.setDataMsgContentType("data-msg");
// WHEN a message is published to an application topic with QoS 0
final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, "my-scope/4711");
final Async determineAddressSuccess = ctx.async();
final Future<ResourceIdentifier> addressTracker = adapter.mapTopic(context).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is mapped to the telemetry API
determineAddressSuccess.await();
assertAddress(addressTracker.result(), TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
// and has the configured data message content type
assertThat(context.contentType(), is(config.getDataMsgContentType()));
}
use of org.eclipse.hono.adapter.mqtt.MqttContext 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.adapter.mqtt.MqttContext in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testOnPublishedAuthenticatedMessageFailsForMissingDeviceId.
/**
* Verifies that the adapter fails to map a topic without a device ID received from an authenticated device.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testOnPublishedAuthenticatedMessageFailsForMissingDeviceId(final TestContext ctx) {
givenAnAdapter();
// WHEN an authenticated device publishes a message to a topic that does not contain a device ID
final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT + "/my-tenant", new Device("my-tenant", "device"));
adapter.onPublishedMessage(context).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message cannot be mapped to an address
}));
}
Aggregations