Search in sources :

Example 46 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier 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()));
}
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 47 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier 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()));
}
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 48 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 valid endpoint names.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadMessageSupportsShortAndLongEndpointNames(final TestContext ctx) {
    // GIVEN an adapter with a downstream event consumer
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    givenATelemetrySenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
    givenAnEventSenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
    // WHEN a device publishes an event
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    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);
    final MqttContext context = new MqttContext(messageFromDevice, endpoint);
    ResourceIdentifier resourceId = ResourceIdentifier.from("telemetry", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("event", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("t", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("e", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("unknown", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertFailure());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer) Test(org.junit.Test)

Example 49 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier 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"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) RunWith(org.junit.runner.RunWith) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) EndpointType(org.eclipse.hono.util.EndpointType) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Assert.assertThat(org.junit.Assert.assertThat) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Timeout(org.junit.rules.Timeout) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Device(org.eclipse.hono.service.auth.device.Device) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Device(org.eclipse.hono.service.auth.device.Device) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 50 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class HonoMessagingMessageFilterTest method testVerifyDetectsDeviceIdMismatch.

@Test
public void testVerifyDetectsDeviceIdMismatch() {
    // GIVEN a valid telemetry message with device id not matching the link target
    final Message msg = givenAMessageHavingProperties(MY_DEVICE + "_1", MY_TENANT);
    // WHEN receiving the message via a link with mismatching tenant
    final ResourceIdentifier linkTarget = getResourceIdentifier(MY_TENANT, MY_DEVICE);
    // THEN message validation fails
    assertFalse(HonoMessagingMessageFilter.verify(linkTarget, msg));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Message(org.apache.qpid.proton.message.Message) Test(org.junit.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