Search in sources :

Example 1 with MqttEndpoint

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);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttServer(io.vertx.mqtt.MqttServer) Test(org.junit.Test)

Example 2 with MqttEndpoint

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;
}
Also used : MqttAuth(io.vertx.mqtt.MqttAuth) MqttEndpoint(io.vertx.mqtt.MqttEndpoint)

Example 3 with MqttEndpoint

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);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) TenantObject(org.eclipse.hono.util.TenantObject) MqttAuth(io.vertx.mqtt.MqttAuth) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttServer(io.vertx.mqtt.MqttServer) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with MqttEndpoint

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);
}
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 5 with MqttEndpoint

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

Aggregations

MqttEndpoint (io.vertx.mqtt.MqttEndpoint)12 MqttServer (io.vertx.mqtt.MqttServer)11 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)11 Test (org.junit.Test)9 Buffer (io.vertx.core.buffer.Buffer)4 Handler (io.vertx.core.Handler)3 MqttAuth (io.vertx.mqtt.MqttAuth)3 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)3 DeviceCredentials (org.eclipse.hono.service.auth.device.DeviceCredentials)3 JsonObject (io.vertx.core.json.JsonObject)2 ProtonDelivery (io.vertx.proton.ProtonDelivery)2 ClientErrorException (org.eclipse.hono.client.ClientErrorException)2 Device (org.eclipse.hono.service.auth.device.Device)2 UsernamePasswordCredentials (org.eclipse.hono.service.auth.device.UsernamePasswordCredentials)2 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)2 TenantObject (org.eclipse.hono.util.TenantObject)2 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 AsyncResult (io.vertx.core.AsyncResult)1 CompositeFuture (io.vertx.core.CompositeFuture)1