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;
}
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);
}
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());
}
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());
}
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());
}
Aggregations