use of io.vertx.mqtt.MqttEndpoint in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerAcceptsUnauthenticatedDevices.
/**
* Verifies that an adapter that is configured to not require devices to authenticate,
* accepts connections from devices not providing any credentials.
*/
@Test
public void testEndpointHandlerAcceptsUnauthenticatedDevices() {
// GIVEN an adapter that does not require devices to authenticate
config.setAuthenticationRequired(false);
final MqttServer server = getMqttServer(false);
final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
forceClientMocksToConnected();
// WHEN a device connects without providing credentials
final MqttEndpoint endpoint = mock(MqttEndpoint.class);
adapter.handleEndpointConnection(endpoint);
// THEN the connection is established
verify(endpoint).accept(false);
}
use of io.vertx.mqtt.MqttEndpoint in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method getMqttEndpointAuthenticated.
private MqttEndpoint getMqttEndpointAuthenticated() {
final MqttEndpoint endpoint = mock(MqttEndpoint.class);
when(endpoint.auth()).thenReturn(new MqttAuth("sensor1@DEFAULT_TENANT", "test"));
return endpoint;
}
use of io.vertx.mqtt.MqttEndpoint in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerRejectsDeviceOfDisabledTenant.
/**
* Verifies that an adapter rejects a connection attempt from a device that
* belongs to a tenant for which the adapter is disabled.
*/
@Test
public void testEndpointHandlerRejectsDeviceOfDisabledTenant() {
// GIVEN an adapter
final MqttServer server = getMqttServer(false);
// which is disabled for tenant "my-tenant"
final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
myTenantConfig.addAdapterConfiguration(new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, ADAPTER_TYPE).put(TenantConstants.FIELD_ENABLED, false));
when(tenantClient.get("my-tenant")).thenReturn(Future.succeededFuture(myTenantConfig));
final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
forceClientMocksToConnected();
// WHEN a device of "my-tenant" tries to connect
final MqttAuth deviceCredentials = new MqttAuth("device@my-tenant", "irrelevant");
final MqttEndpoint endpoint = mock(MqttEndpoint.class);
when(endpoint.auth()).thenReturn(deviceCredentials);
adapter.handleEndpointConnection(endpoint);
// THEN the connection is not established
verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
}
use of io.vertx.mqtt.MqttEndpoint 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.MqttEndpoint 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());
}
Aggregations