use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class KuraProtocolAdapterTest method testMapTopicMapsKuraControlMessagesToTelemetryApi.
/**
* Verifies that the adapter maps control 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 testMapTopicMapsKuraControlMessagesToTelemetryApi(final TestContext ctx) {
// GIVEN an adapter configured to use the standard topic.control-prefix $EDC
// and a custom control message content type
config.setCtrlMsgContentType("control-msg");
// WHEN a message is published to a topic with the Kura $EDC prefix as endpoint
final MqttContext context = newContext(MqttQoS.AT_MOST_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 telemetry API
determineAddressSuccess.await();
assertAddress(addressTracker.result(), TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
// and has the custom control message content type
assertThat(context.contentType(), is(config.getCtrlMsgContentType()));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class KuraProtocolAdapterTest method testMapTopicMapsKuraDataMessagesToEventApi.
/**
* Verifies that the adapter forwards application 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 testMapTopicMapsKuraDataMessagesToEventApi(final TestContext ctx) {
// GIVEN an adapter
// WHEN a message is published to an application topic with QoS 1
final MqttContext context = newContext(MqttQoS.AT_LEAST_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 forwarded to the event API
determineAddressSuccess.await();
assertAddress(addressTracker.result(), EventConstants.EVENT_ENDPOINT, "my-scope", "4711");
// and is recognized as a data message
assertThat(context.contentType(), is(config.getDataMsgContentType()));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class KuraProtocolAdapterTest method testMapTopicRecognizesControlMessagesWithCustomControlPrefix.
/**
* Verifies that the adapter recognizes control messages published to a topic with a custom control prefix.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapTopicRecognizesControlMessagesWithCustomControlPrefix(final TestContext ctx) {
// GIVEN an adapter configured to use a custom topic.control-prefix
config.setControlPrefix("bumlux");
// WHEN a message is published to a topic with the custom prefix as endpoint
final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, "bumlux/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(), TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
// and is recognized as a control message
assertThat(context.contentType(), is(config.getCtrlMsgContentType()));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method getAdapter.
private AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> getAdapter(final MqttServer server) {
final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = new AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties>() {
@Override
protected String getTypeName() {
return ADAPTER_TYPE;
}
@Override
protected Future<Void> onPublishedMessage(final MqttContext ctx) {
final ResourceIdentifier topic = ResourceIdentifier.fromString(ctx.message().topicName());
return uploadTelemetryMessage(ctx, topic.getTenantId(), topic.getResourceId(), ctx.message().payload());
}
};
adapter.setConfig(config);
adapter.setMetrics(new MqttAdapterMetrics());
adapter.setTenantServiceClient(tenantServiceClient);
adapter.setHonoMessagingClient(messagingClient);
adapter.setRegistrationServiceClient(deviceRegistrationServiceClient);
adapter.setCredentialsAuthProvider(credentialsAuthProvider);
if (server != null) {
adapter.setMqttInsecureServer(server);
adapter.init(vertx, mock(Context.class));
}
return adapter;
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadMessageSupportsShortAndLongEndpointNames.
/**
* Verifies that the adapter will accept uploading messages to standard as well as shortened topic names.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadMessageSupportsShortAndLongEndpointNames(final VertxTestContext ctx) {
// GIVEN an adapter with downstream telemetry & event consumers
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
givenAnEventSenderForAnyTenant();
// WHEN a device publishes events and telemetry messages
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
final Buffer payload = Buffer.buffer("some payload");
final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
when(messageFromDevice.messageId()).thenReturn(5555555);
when(messageFromDevice.payload()).thenReturn(payload);
ResourceIdentifier resourceId = ResourceIdentifier.from("telemetry", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("event", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("t", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("e", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("unknown", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(t).isInstanceOf(ClientErrorException.class));
}));
ctx.completeNow();
}
Aggregations