Search in sources :

Example 11 with MqttServer

use of io.vertx.mqtt.MqttServer in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageFailsForUnknownDevice.

/**
 * Verifies that the adapter does not forward a message published by a device
 * if the device's registration status cannot be asserted.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadTelemetryMessageFailsForUnknownDevice(final TestContext ctx) {
    // GIVEN an adapter
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    givenATelemetrySenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
    // WHEN an unknown device publishes a telemetry message
    when(regClient.assertRegistration(eq("unknown"), any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
    final MessageSender sender = mock(MessageSender.class);
    when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
    adapter.uploadTelemetryMessage(new MqttContext(mock(MqttPublishMessage.class), mock(MqttEndpoint.class)), "my-tenant", "unknown", Buffer.buffer("test")).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the message has not been sent downstream
        verify(sender, never()).send(any(Message.class));
        // because the device's registration status could not be asserted
        ctx.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ((ClientErrorException) t).getErrorCode());
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) ArgumentMatchers(org.mockito.ArgumentMatchers) ProtonDelivery(io.vertx.proton.ProtonDelivery) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Context(io.vertx.core.Context) MqttServer(io.vertx.mqtt.MqttServer) Assert.assertThat(org.junit.Assert.assertThat) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) TenantClient(org.eclipse.hono.client.TenantClient) MessageSender(org.eclipse.hono.client.MessageSender) BiConsumer(java.util.function.BiConsumer) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) AfterClass(org.junit.AfterClass) UsernamePasswordCredentials(org.eclipse.hono.service.auth.device.UsernamePasswordCredentials) DeviceCredentials(org.eclipse.hono.service.auth.device.DeviceCredentials) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) MqttAuth(io.vertx.mqtt.MqttAuth) Device(org.eclipse.hono.service.auth.device.Device) Handler(io.vertx.core.Handler) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MessageSender(org.eclipse.hono.client.MessageSender) MqttServer(io.vertx.mqtt.MqttServer) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 12 with MqttServer

use of io.vertx.mqtt.MqttServer in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method getMqttServer.

private static MqttServer getMqttServer(final boolean startupShouldFail) {
    final MqttServer server = mock(MqttServer.class);
    when(server.actualPort()).thenReturn(0, 1883);
    when(server.endpointHandler(VertxMockSupport.anyHandler())).thenReturn(server);
    when(server.listen(VertxMockSupport.anyHandler())).then(invocation -> {
        final Handler<AsyncResult<MqttServer>> handler = invocation.getArgument(0);
        if (startupShouldFail) {
            handler.handle(Future.failedFuture("MQTT server intentionally failed to start"));
        } else {
            handler.handle(Future.succeededFuture(server));
        }
        return server;
    });
    return server;
}
Also used : MqttServer(io.vertx.mqtt.MqttServer) AsyncResult(io.vertx.core.AsyncResult)

Example 13 with MqttServer

use of io.vertx.mqtt.MqttServer in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    MqttServerOptions options = new MqttServerOptions().setPort(8883).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")).setSsl(true);
    MqttServer mqttServer = MqttServer.create(vertx, options);
    mqttServer.endpointHandler(endpoint -> {
        // shows main connect info
        System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, " + "clean session = " + endpoint.isCleanSession());
        // accept connection from the remote client
        endpoint.accept(false);
    }).listen(ar -> {
        if (ar.succeeded()) {
            System.out.println("MQTT server is listening on port " + mqttServer.actualPort());
        } else {
            System.err.println("Error on starting the server" + ar.cause().getMessage());
        }
    });
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) Runner(io.vertx.example.mqtt.util.Runner) MqttServer(io.vertx.mqtt.MqttServer) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) MqttServer(io.vertx.mqtt.MqttServer)

Aggregations

MqttServer (io.vertx.mqtt.MqttServer)13 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)8 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)8 Buffer (io.vertx.core.buffer.Buffer)7 Test (org.junit.Test)7 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)6 ProtonDelivery (io.vertx.proton.ProtonDelivery)5 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)4 AsyncResult (io.vertx.core.AsyncResult)4 Handler (io.vertx.core.Handler)4 ClientErrorException (org.eclipse.hono.client.ClientErrorException)4 UsernamePasswordCredentials (org.eclipse.hono.service.auth.device.UsernamePasswordCredentials)4 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)3 Context (io.vertx.core.Context)3 Future (io.vertx.core.Future)3 Vertx (io.vertx.core.Vertx)3 JsonObject (io.vertx.core.json.JsonObject)3 Async (io.vertx.ext.unit.Async)3 TestContext (io.vertx.ext.unit.TestContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3