Search in sources :

Example 1 with MqttContext

use of org.eclipse.hono.adapter.mqtt.MqttContext 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 2 with MqttContext

use of org.eclipse.hono.adapter.mqtt.MqttContext 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 3 with MqttContext

use of org.eclipse.hono.adapter.mqtt.MqttContext 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 4 with MqttContext

use of org.eclipse.hono.adapter.mqtt.MqttContext in project hono by eclipse.

the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageFailsForMissingDeviceId.

/**
 * Verifies that the adapter fails to map a topic without a device ID received from an anonymous device.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testOnPublishedMessageFailsForMissingDeviceId(final TestContext ctx) {
    givenAnAdapter();
    // WHEN an anonymous 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");
    adapter.onPublishedMessage(context).setHandler(ctx.asyncAssertFailure(t -> {
    // THEN the message cannot be mapped to an address
    }));
}
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) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Test(org.junit.Test)

Example 5 with MqttContext

use of org.eclipse.hono.adapter.mqtt.MqttContext in project hono by eclipse.

the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageFailsForMissingTenant.

/**
 * Verifies that the adapter fails to map a topic without a tenant ID received from an anonymous device.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testOnPublishedMessageFailsForMissingTenant(final TestContext ctx) {
    givenAnAdapter();
    // WHEN an anonymous device publishes a message to a topic that does not contain a tenant ID
    final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
    adapter.onPublishedMessage(context).setHandler(ctx.asyncAssertFailure(t -> {
    // THEN the message cannot be mapped to an address
    }));
}
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) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) Test(org.junit.Test)

Aggregations

Async (io.vertx.ext.unit.Async)9 MqttContext (org.eclipse.hono.adapter.mqtt.MqttContext)9 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)9 Test (org.junit.Test)9 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)4 Future (io.vertx.core.Future)4 Buffer (io.vertx.core.buffer.Buffer)4 TestContext (io.vertx.ext.unit.TestContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)4 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)4 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)4 Device (org.eclipse.hono.service.auth.device.Device)4 EndpointType (org.eclipse.hono.util.EndpointType)4 EventConstants (org.eclipse.hono.util.EventConstants)4 TelemetryConstants (org.eclipse.hono.util.TelemetryConstants)4 CoreMatchers.is (org.hamcrest.CoreMatchers.is)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertThat (org.junit.Assert.assertThat)4 Rule (org.junit.Rule)4