Search in sources :

Example 11 with ResourceIdentifier

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()));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 12 with ResourceIdentifier

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()));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 13 with ResourceIdentifier

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()));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 14 with ResourceIdentifier

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;
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 15 with ResourceIdentifier

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) MessagingType(org.eclipse.hono.util.MessagingType) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) AuthHandler(org.eclipse.hono.adapter.auth.device.AuthHandler) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) QoS(org.eclipse.hono.util.QoS) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) ClientErrorException(org.eclipse.hono.client.ClientErrorException) OptionalInt(java.util.OptionalInt) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Commands(org.eclipse.hono.client.command.Commands) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) MqttServer(io.vertx.mqtt.MqttServer) ProtocolAdapterTestSupport(org.eclipse.hono.adapter.test.ProtocolAdapterTestSupport) SSLSession(javax.net.ssl.SSLSession) ArgumentCaptor(org.mockito.ArgumentCaptor) MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) BiConsumer(java.util.function.BiConsumer) HttpUtils(org.eclipse.hono.service.http.HttpUtils) AsyncResult(io.vertx.core.AsyncResult) CommandConstants(org.eclipse.hono.util.CommandConstants) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Promise(io.vertx.core.Promise) MqttSubscribeMessage(io.vertx.mqtt.messages.MqttSubscribeMessage) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Adapter(org.eclipse.hono.util.Adapter) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) MqttAuth(io.vertx.mqtt.MqttAuth) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) Handler(io.vertx.core.Handler) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)82 Message (org.apache.qpid.proton.message.Message)30 Future (io.vertx.core.Future)24 HttpURLConnection (java.net.HttpURLConnection)22 MessageHelper (org.eclipse.hono.util.MessageHelper)22 ClientErrorException (org.eclipse.hono.client.ClientErrorException)20 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 Handler (io.vertx.core.Handler)18 Map (java.util.Map)18 Span (io.opentracing.Span)17 Buffer (io.vertx.core.buffer.Buffer)17 SpanContext (io.opentracing.SpanContext)16 Constants (org.eclipse.hono.util.Constants)16 Promise (io.vertx.core.Promise)15 Objects (java.util.Objects)14 AsyncResult (io.vertx.core.AsyncResult)13 Vertx (io.vertx.core.Vertx)13 ProtonConnection (io.vertx.proton.ProtonConnection)13 ProtonReceiver (io.vertx.proton.ProtonReceiver)13