use of io.vertx.mqtt.impl.MqttTopicSubscriptionImpl in project hono by eclipse.
the class CommandSubscriptionTest method testSubscriptionSucceedsForAuthenticatedDevice.
/**
* Verifies that an authenticated device can successfully subscribe for commands
* targeted at itself using all variants of topic names.
*
* @param endpointName The endpoint name used in the topic.
* @param reqPartName The request part name used in the topic.
* @param qos The requested QoS.
*/
@ParameterizedTest
@MethodSource("endpointAndReqNamesWithQoS")
public void testSubscriptionSucceedsForAuthenticatedDevice(final String endpointName, final String reqPartName, final MqttQoS qos) {
final Command command = mock(Command.class);
when(command.isTargetedAtGateway()).thenReturn(false);
when(command.getTenant()).thenReturn(device.getTenantId());
when(command.getGatewayOrDeviceId()).thenReturn(device.getDeviceId());
when(command.getRequestId()).thenReturn("requestId");
when(command.getName()).thenReturn("doSomething");
// WHEN subscribing to commands using explicit topic
MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/tenant/device/%s/#", endpointName, reqPartName), qos);
CommandSubscription subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getRequestPart()).isEqualTo(reqPartName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the command topic does include both the tenant and device ID
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s/%s/%s/%s/requestId/doSomething", endpointName, device.getTenantId(), device.getDeviceId(), reqPartName));
// WHEN subscribing to commands including tenant only
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/tenant//%s/#", endpointName, reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getRequestPart()).isEqualTo(reqPartName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the command topic does include the tenant as well
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s/%s//%s/requestId/doSomething", endpointName, device.getTenantId(), reqPartName));
// WHEN subscribing to commands including device ID only
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//device/%s/#", endpointName, reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getRequestPart()).isEqualTo(reqPartName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the command topic does include the device ID as well
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s//%s/%s/requestId/doSomething", endpointName, device.getDeviceId(), reqPartName));
// WHEN subscribing to commands using implicit topic
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s///%s/#", endpointName, reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getRequestPart()).isEqualTo(reqPartName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the command topic does not include tenant nor device ID
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s///%s/requestId/doSomething", endpointName, reqPartName));
// using a tenant other than the tenant that the device belongs to should fail
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/otherTenant/device/%s/#", endpointName, reqPartName), qos);
assertThat(CommandSubscription.fromTopic(mqttTopicSubscription, device)).isNull();
}
use of io.vertx.mqtt.impl.MqttTopicSubscriptionImpl in project hono by eclipse.
the class CommandSubscriptionTest method testSubscriptionSucceedsForAuthenticatedGateway.
/**
* Verifies that an authenticated gateway can successfully subscribe for commands
* targeted at one of devices that it is authorized to act on behalf of.
*
* @param endpointName The endpoint name used in the topic.
* @param reqPartName The request part name used in the topic.
* @param qos The requested QoS.
*/
@ParameterizedTest
@MethodSource("endpointAndReqNamesWithQoS")
public void testSubscriptionSucceedsForAuthenticatedGateway(final String endpointName, final String reqPartName, final MqttQoS qos) {
final String gatewayManagedDeviceId = "gatewayManagedDevice";
final Command command = mock(Command.class);
when(command.isTargetedAtGateway()).thenReturn(true);
when(command.getTenant()).thenReturn(gw.getTenantId());
when(command.getGatewayId()).thenReturn(gw.getDeviceId());
when(command.getGatewayOrDeviceId()).thenReturn(gw.getDeviceId());
when(command.getDeviceId()).thenReturn(gatewayManagedDeviceId);
when(command.getRequestId()).thenReturn("requestId");
when(command.getName()).thenReturn("doSomething");
// WHEN subscribing to commands for a specific device omitting tenant
MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//%s/%s/#", endpointName, gatewayManagedDeviceId, reqPartName), qos);
CommandSubscription subscription = CommandSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gatewayManagedDeviceId);
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(true);
// THEN the command topic does not include the tenant either
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s//%s/%s/requestId/doSomething", endpointName, gatewayManagedDeviceId, reqPartName));
// WHEN subscribing to commands for a specific device including the tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/%s/%s/%s/#", endpointName, device.getTenantId(), gatewayManagedDeviceId, reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gatewayManagedDeviceId);
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(true);
// THEN the command topic does include the tenant as well
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s/%s/%s/%s/requestId/doSomething", endpointName, gw.getTenantId(), gatewayManagedDeviceId, reqPartName));
// WHEN subscribing to commands for all devices omitting tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//+/%s/#", endpointName, reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(false);
// THEN the command topic does not include the tenant either
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s//%s/%s/requestId/doSomething", endpointName, gatewayManagedDeviceId, reqPartName));
// WHEN subscribing to commands for all devices including the tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/%s/+/%s/#", endpointName, device.getTenantId(), reqPartName), qos);
subscription = CommandSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(false);
// THEN the command topic does include the tenant as well
assertThat(subscription.getCommandPublishTopic(command)).isEqualTo(String.format("%s/%s/%s/%s/requestId/doSomething", endpointName, gw.getTenantId(), gatewayManagedDeviceId, reqPartName));
// using a tenant other than the tenant that the gateway belongs to should fail
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/otherTenant/+/%s/#", endpointName, reqPartName), qos);
assertThat(CommandSubscription.fromTopic(mqttTopicSubscription, gw)).isNull();
}
use of io.vertx.mqtt.impl.MqttTopicSubscriptionImpl in project hono by eclipse.
the class CommandSubscriptionTest method testSubscriptionSucceedsForUnauthenticatedDevice.
/**
* Verifies that an unauthenticated device can successfully subscribe for commands
* using the default topic.
*
* @param endpointName The endpoint name used in the topic.
* @param reqPartName The request part name used in the topic.
* @param qos The requested QoS.
*/
@ParameterizedTest
@MethodSource("endpointAndReqNamesWithQoS")
public void testSubscriptionSucceedsForUnauthenticatedDevice(final String endpointName, final String reqPartName, final MqttQoS qos) {
final MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/tenant1/deviceA/%s/#", endpointName, reqPartName), qos);
final CommandSubscription subscription = CommandSubscription.fromTopic(mqttTopicSubscription, null);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant1");
assertThat(subscription.getDeviceId()).isEqualTo("deviceA");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getRequestPart()).isEqualTo(reqPartName);
assertThat(subscription.getQos()).isEqualTo(qos);
}
use of io.vertx.mqtt.impl.MqttTopicSubscriptionImpl in project hono by eclipse.
the class ErrorSubscriptionTest method testSubscriptionSucceedsForAuthenticatedGateway.
/**
* Verifies that an authenticated gateway can successfully subscribe for errors
* targeted at one of devices that it is authorized to act on behalf of.
*
* @param endpointName The endpoint name used in the topic.
* @param qos The requested QoS.
*/
@ParameterizedTest
@MethodSource("endpointNamesWithQoS")
public void testSubscriptionSucceedsForAuthenticatedGateway(final String endpointName, final MqttQoS qos) {
final String gatewayManagedDeviceId = "gatewayManagedDevice";
final Integer messageId = 123;
final int errorCode = 503;
final String errorCausingMsgEndpoint = TelemetryConstants.TELEMETRY_ENDPOINT;
final MqttPublishMessage message = mock(MqttPublishMessage.class);
when(message.messageId()).thenReturn(messageId);
when(message.topicName()).thenReturn(ResourceIdentifier.from(errorCausingMsgEndpoint, gw.getTenantId(), gatewayManagedDeviceId).toString());
when(message.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
final MqttContext context = MqttContext.fromPublishPacket(message, mock(MqttEndpoint.class), mock(Span.class), gw);
// WHEN subscribing to errors for a specific device omitting tenant
MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//%s/#", endpointName, gatewayManagedDeviceId), qos);
ErrorSubscription subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gatewayManagedDeviceId);
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(true);
// THEN the error topic does not include the tenant either
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s//%s/%s/%s/%s", endpointName, gatewayManagedDeviceId, errorCausingMsgEndpoint, messageId, errorCode));
// WHEN subscribing to errors for a specific device including the tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/%s/%s/#", endpointName, device.getTenantId(), gatewayManagedDeviceId), qos);
subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gatewayManagedDeviceId);
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(true);
// THEN the error topic does include the tenant as well
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s/%s/%s/%s/%s/%s", endpointName, gw.getTenantId(), gatewayManagedDeviceId, errorCausingMsgEndpoint, messageId, errorCode));
// WHEN subscribing to errors for all devices omitting tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//+/#", endpointName), qos);
subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(false);
// THEN the error topic does not include the tenant either
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s//%s/%s/%s/%s", endpointName, gatewayManagedDeviceId, errorCausingMsgEndpoint, messageId, errorCode));
// WHEN subscribing to errors for all devices including the tenant
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/%s/+/#", endpointName, device.getTenantId()), qos);
subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, gw);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo(gw.getTenantId());
assertThat(subscription.getDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(gw.getDeviceId());
assertThat(subscription.getQos()).isEqualTo(qos);
assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(false);
// THEN the error topic does include the tenant as well
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s/%s/%s/%s/%s/%s", endpointName, gw.getTenantId(), gatewayManagedDeviceId, errorCausingMsgEndpoint, messageId, errorCode));
// using a tenant other than the tenant that the gateway belongs to should fail
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/otherTenant/+/#", endpointName), qos);
assertThat(ErrorSubscription.fromTopic(mqttTopicSubscription, gw)).isNull();
}
use of io.vertx.mqtt.impl.MqttTopicSubscriptionImpl in project hono by eclipse.
the class ErrorSubscriptionTest method testSubscriptionSucceedsForAuthenticatedDevice.
/**
* Verifies that an authenticated device can successfully subscribe for errors
* targeted at itself using all variants of topic names.
*
* @param endpointName The endpoint name used in the topic.
* @param qos The requested QoS.
*/
@ParameterizedTest
@MethodSource("endpointNamesWithQoS")
public void testSubscriptionSucceedsForAuthenticatedDevice(final String endpointName, final MqttQoS qos) {
final Integer messageId = 123;
final int errorCode = 503;
final String errorCausingMsgEndpoint = TelemetryConstants.TELEMETRY_ENDPOINT;
final MqttPublishMessage message = mock(MqttPublishMessage.class);
when(message.messageId()).thenReturn(messageId);
when(message.topicName()).thenReturn(errorCausingMsgEndpoint);
when(message.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
final MqttContext context = MqttContext.fromPublishPacket(message, mock(MqttEndpoint.class), mock(Span.class), device);
// WHEN subscribing to errors using explicit topic
MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/tenant/device/#", endpointName), qos);
ErrorSubscription subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the error topic does include both the tenant and device ID
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s/%s/%s/%s/%s/%s", endpointName, device.getTenantId(), device.getDeviceId(), errorCausingMsgEndpoint, messageId, errorCode));
// WHEN subscribing to errors including tenant only
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/tenant//#", endpointName), qos);
subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the error topic does include the tenant as well
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s/%s//%s/%s/%s", endpointName, device.getTenantId(), errorCausingMsgEndpoint, messageId, errorCode));
// WHEN subscribing to errors including device ID only
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s//device/#", endpointName), qos);
subscription = ErrorSubscription.fromTopic(mqttTopicSubscription, device);
assertThat(subscription).isNotNull();
assertThat(subscription.getTenant()).isEqualTo("tenant");
assertThat(subscription.getDeviceId()).isEqualTo("device");
assertThat(subscription.getEndpoint()).isEqualTo(endpointName);
assertThat(subscription.getQos()).isEqualTo(qos);
// THEN the error topic does include the device ID as well
assertThat(subscription.getErrorPublishTopic(context, errorCode)).isEqualTo(String.format("%s//%s/%s/%s/%s", endpointName, device.getDeviceId(), errorCausingMsgEndpoint, messageId, errorCode));
// using a tenant other than the tenant that the device belongs to should fail
mqttTopicSubscription = new MqttTopicSubscriptionImpl(String.format("%s/otherTenant/device/#", endpointName), qos);
assertThat(ErrorSubscription.fromTopic(mqttTopicSubscription, device)).isNull();
}
Aggregations