Search in sources :

Example 1 with MqttConnAckMessage

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

the class TelemetryMqttIT method testConnectFailsForDisabledTenant.

/**
 * Verifies that the adapter rejects connection attempts from devices
 * that belong to a tenant for which the adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testConnectFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final String password = "secret";
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_MQTT).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> {
        final Future<MqttConnAckMessage> result = Future.future();
        final MqttClientOptions options = new MqttClientOptions().setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
        mqttClient = MqttClient.create(VERTX, options);
        // WHEN a device that belongs to the tenant tries to connect to the adapter
        mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
        return result;
    }).setHandler(ctx.asyncAssertFailure(t -> {
    // THEN the connection attempt gets rejected
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) MessageConsumer(org.eclipse.hono.client.MessageConsumer) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Consumer(java.util.function.Consumer) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) Buffer(io.vertx.core.buffer.Buffer) MqttClient(io.vertx.mqtt.MqttClient) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) TenantObject(org.eclipse.hono.util.TenantObject) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) Test(org.junit.Test)

Example 2 with MqttConnAckMessage

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

the class MqttTestBase method doTestUploadMessages.

/**
 * Verifies that a number of messages uploaded to Hono's HTTP adapter can be successfully
 * consumed via the AMQP Messaging Network.
 *
 * @param ctx The test context.
 * @param useShortTopicName Whether to use standard or short topic names
 * @throws InterruptedException if the test fails.
 */
public void doTestUploadMessages(final TestContext ctx, boolean useShortTopicName) throws InterruptedException {
    final int messagesToSend = 200;
    final CountDownLatch received = new CountDownLatch(messagesToSend);
    final Async setup = ctx.async();
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final String password = "secret";
    final TenantObject tenant = TenantObject.from(tenantId, true);
    helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> createConsumer(tenantId, msg -> {
        LOGGER.trace("received {}", msg);
        assertMessageProperties(ctx, msg);
        assertAdditionalMessageProperties(ctx, msg);
        received.countDown();
        if (received.getCount() % 40 == 0) {
            LOGGER.info("messages received: {}", messagesToSend - received.getCount());
        }
    })).compose(ok -> {
        final Future<MqttConnAckMessage> result = Future.future();
        final MqttClientOptions options = new MqttClientOptions().setMaxInflightQueue(200).setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
        mqttClient = MqttClient.create(VERTX, options);
        mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
        return result;
    }).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
    setup.await();
    final long start = System.currentTimeMillis();
    final AtomicInteger messageCount = new AtomicInteger(0);
    final AtomicReference<Async> sendResult = new AtomicReference<>();
    mqttClient.publishCompletionHandler(packetId -> {
        if (pendingMessages.remove(packetId)) {
            sendResult.get().complete();
        } else {
            LOGGER.info("received PUBACK for unexpected message [id: {}]", packetId);
        }
    });
    while (messageCount.get() < messagesToSend) {
        sendResult.set(ctx.async());
        send(tenantId, deviceId, Buffer.buffer("hello " + messageCount.getAndIncrement()), useShortTopicName, sendAttempt -> {
            if (sendAttempt.failed()) {
                LOGGER.debug("error sending message {}", messageCount.get(), sendAttempt.cause());
            } else {
                pendingMessages.add(sendAttempt.result());
            }
        });
        if (messageCount.get() % 40 == 0) {
            LOGGER.info("messages sent: " + messageCount.get());
        }
        sendResult.get().await();
    }
    long timeToWait = Math.max(TEST_TIMEOUT - 1000, Math.round(messagesToSend * 1.2));
    if (!received.await(timeToWait, TimeUnit.MILLISECONDS)) {
        LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
        ctx.fail("did not receive all messages sent");
    } else {
        LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) MqttClient(io.vertx.mqtt.MqttClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Set(java.util.Set) Test(org.junit.Test) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) Handler(io.vertx.core.Handler) TenantObject(org.eclipse.hono.util.TenantObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) Future(io.vertx.core.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 TestContext (io.vertx.ext.unit.TestContext)2 MqttClient (io.vertx.mqtt.MqttClient)2 MqttClientOptions (io.vertx.mqtt.MqttClientOptions)2 MqttConnAckMessage (io.vertx.mqtt.messages.MqttConnAckMessage)2 Consumer (java.util.function.Consumer)2 Message (org.apache.qpid.proton.message.Message)2 MessageConsumer (org.eclipse.hono.client.MessageConsumer)2 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)2 TenantObject (org.eclipse.hono.util.TenantObject)2 Test (org.junit.Test)2 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 Vertx (io.vertx.core.Vertx)1 JsonObject (io.vertx.core.json.JsonObject)1 Async (io.vertx.ext.unit.Async)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 HashSet (java.util.HashSet)1