Search in sources :

Example 1 with MqttPublishMessage

use of io.vertx.mqtt.messages.MqttPublishMessage in project hono by eclipse.

the class KuraProtocolAdapterTest method newMessage.

private static MqttPublishMessage newMessage(final MqttQoS qosLevel, final String topic, final Buffer payload) {
    final MqttPublishMessage message = mock(MqttPublishMessage.class);
    when(message.qosLevel()).thenReturn(qosLevel);
    when(message.topicName()).thenReturn(topic);
    when(message.payload()).thenReturn(payload);
    return message;
}
Also used : MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage)

Example 2 with MqttPublishMessage

use of io.vertx.mqtt.messages.MqttPublishMessage in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadQoS1MessageSendsPubAckOnSuccess.

private void testUploadQoS1MessageSendsPubAckOnSuccess(final Future<ProtonDelivery> outcome, final BiConsumer<AbstractVertxBasedMqttProtocolAdapter<?>, MqttContext> upload) {
    // GIVEN an adapter with a downstream event consumer
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    // 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);
    upload.accept(adapter, context);
    // THEN the device does not receive a PUBACK
    verify(endpoint, never()).publishAcknowledge(anyInt());
    // until the event has been settled and accepted
    outcome.complete(mock(ProtonDelivery.class));
    verify(endpoint).publishAcknowledge(5555555);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ProtonDelivery(io.vertx.proton.ProtonDelivery) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer)

Example 3 with MqttPublishMessage

use of io.vertx.mqtt.messages.MqttPublishMessage in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testOnUnauthenticatedMessageDoesNotSendPubAckOnFailure.

/**
 * Verifies that the adapter does not send a PUBACK package to the device if
 * an event message has not been accepted by the peer.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testOnUnauthenticatedMessageDoesNotSendPubAckOnFailure(final TestContext ctx) {
    // GIVEN an adapter with a downstream event consumer
    final Future<ProtonDelivery> outcome = Future.future();
    givenAnEventSenderForOutcome(outcome);
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    // WHEN a device publishes an event
    final Buffer payload = Buffer.buffer("some payload");
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
    final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
    when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
    when(messageFromDevice.messageId()).thenReturn(5555555);
    final MqttContext context = new MqttContext(messageFromDevice, endpoint);
    adapter.uploadEventMessage(context, "my-tenant", "4712", payload).setHandler(ctx.asyncAssertFailure());
    // and the peer rejects the message
    outcome.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    // THEN the device has not received a PUBACK
    verify(endpoint, never()).publishAcknowledge(anyInt());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ProtonDelivery(io.vertx.proton.ProtonDelivery) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 4 with MqttPublishMessage

use of io.vertx.mqtt.messages.MqttPublishMessage in project hono by eclipse.

the class VertxBasedMqttProtocolAdapterTest method testMapTopicSupportsShortAndLongTopicNames.

/**
 * Verifies that the adapter supports all required topic names.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testMapTopicSupportsShortAndLongTopicNames(final TestContext ctx) {
    givenAnAdapter();
    MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, EventConstants.EVENT_ENDPOINT);
    adapter.mapTopic(message).setHandler(ctx.asyncAssertSuccess());
    message = newMessage(MqttQoS.AT_LEAST_ONCE, EventConstants.EVENT_ENDPOINT);
    adapter.mapTopic(message).setHandler(address -> assertEquals(EndpointType.EVENT, EndpointType.fromString(address.result().getEndpoint())));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
    adapter.mapTopic(message).setHandler(address -> assertEquals(EndpointType.TELEMETRY, EndpointType.fromString(address.result().getEndpoint())));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, EventConstants.EVENT_ENDPOINT_SHORT);
    adapter.mapTopic(message).setHandler(address -> assertEquals(EndpointType.EVENT, EndpointType.fromString(address.result().getEndpoint())));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT_SHORT);
    adapter.mapTopic(message).setHandler(address -> assertEquals(EndpointType.TELEMETRY, EndpointType.fromString(address.result().getEndpoint())));
    message = newMessage(MqttQoS.AT_LEAST_ONCE, "unknown");
    adapter.mapTopic(message).setHandler(ctx.asyncAssertFailure());
}
Also used : MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Test(org.junit.Test)

Example 5 with MqttPublishMessage

use of io.vertx.mqtt.messages.MqttPublishMessage 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)

Aggregations

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