Search in sources :

Example 11 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testStartup.

/**
 * Verifies that an MQTT server is bound to the insecure port during startup and connections
 * to required services have been established.
 *
 * @param ctx The helper to use for running async tests on vertx.
 */
@SuppressWarnings("unchecked")
@Test
public void testStartup(final TestContext ctx) {
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    final Async startup = ctx.async();
    final Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(s -> {
        startup.complete();
    }));
    adapter.start(startupTracker);
    startup.await();
    verify(server).listen(any(Handler.class));
    verify(server).endpointHandler(any(Handler.class));
}
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) Async(io.vertx.ext.unit.Async) MqttServer(io.vertx.mqtt.MqttServer) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 12 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerFailsWithoutConnect.

// TODO: startup fail test
/**
 * Verifies that a connection attempt from a device is refused if the adapter is not
 * connected to all of the services it depends on.
 */
@Test
public void testEndpointHandlerFailsWithoutConnect() {
    // GIVEN an endpoint
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    adapter.handleEndpointConnection(endpoint);
    verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttServer(io.vertx.mqtt.MqttServer) Test(org.junit.Test)

Example 13 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testAuthenticatedMqttAdapterCreatesMessageHandlersForAuthenticatedDevices.

/**
 * Verifies that on successful authentication the adapter sets appropriate message and close
 * handlers on the client endpoint.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testAuthenticatedMqttAdapterCreatesMessageHandlersForAuthenticatedDevices() {
    // GIVEN an adapter
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    forceClientMocksToConnected();
    doAnswer(invocation -> {
        Handler<AsyncResult<Device>> resultHandler = invocation.getArgument(1);
        resultHandler.handle(Future.succeededFuture(new Device("DEFAULT_TENANT", "4711")));
        return null;
    }).when(credentialsAuthProvider).authenticate(any(DeviceCredentials.class), any(Handler.class));
    // WHEN a device tries to connect with valid credentials
    final MqttEndpoint endpoint = getMqttEndpointAuthenticated();
    adapter.handleEndpointConnection(endpoint);
    // THEN the device's logical ID is successfully established and corresponding handlers
    // are registered
    final ArgumentCaptor<DeviceCredentials> credentialsCaptor = ArgumentCaptor.forClass(DeviceCredentials.class);
    verify(credentialsAuthProvider).authenticate(credentialsCaptor.capture(), any(Handler.class));
    assertThat(credentialsCaptor.getValue().getAuthId(), is("sensor1"));
    verify(endpoint).accept(false);
    verify(endpoint).publishHandler(any(Handler.class));
    verify(endpoint).closeHandler(any(Handler.class));
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) Device(org.eclipse.hono.service.auth.device.Device) MqttServer(io.vertx.mqtt.MqttServer) Handler(io.vertx.core.Handler) DeviceCredentials(org.eclipse.hono.service.auth.device.DeviceCredentials) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.Test)

Example 14 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerRejectsUnauthenticatedDevices.

/**
 * Verifies that an adapter that is configured to require devices to authenticate,
 * rejects connections from devices not providing any credentials.
 */
@Test
public void testEndpointHandlerRejectsUnauthenticatedDevices() {
    // GIVEN an adapter that does require devices to authenticate
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    forceClientMocksToConnected();
    // WHEN a device connects without providing any credentials
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    adapter.handleEndpointConnection(endpoint);
    // THEN the connection is refused
    verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttServer(io.vertx.mqtt.MqttServer) Test(org.junit.Test)

Example 15 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadMessageSupportsShortAndLongEndpointNames.

/**
 * Verifies that the adapter will accept uploading messages to valid endpoint names.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadMessageSupportsShortAndLongEndpointNames(final TestContext ctx) {
    // GIVEN an adapter with a downstream event consumer
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    givenATelemetrySenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
    givenAnEventSenderForOutcome(Future.succeededFuture(mock(ProtonDelivery.class)));
    // 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);
    ResourceIdentifier resourceId = ResourceIdentifier.from("telemetry", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("event", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("t", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("e", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertSuccess());
    resourceId = ResourceIdentifier.from("unknown", "my-tenant", "4712");
    adapter.uploadMessage(context, resourceId, payload).setHandler(ctx.asyncAssertFailure());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer) Test(org.junit.Test)

Aggregations

ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)17 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)13 MqttServer (io.vertx.mqtt.MqttServer)13 Test (org.junit.Test)12 Handler (io.vertx.core.Handler)7 Buffer (io.vertx.core.buffer.Buffer)6 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)6 JsonObject (io.vertx.core.json.JsonObject)5 ProtonDelivery (io.vertx.proton.ProtonDelivery)5 HonoClient (org.eclipse.hono.client.HonoClient)5 RegistrationClient (org.eclipse.hono.client.RegistrationClient)5 DeviceCredentials (org.eclipse.hono.service.auth.device.DeviceCredentials)5 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)5 AsyncResult (io.vertx.core.AsyncResult)4 Context (io.vertx.core.Context)4 TestContext (io.vertx.ext.unit.TestContext)4 MqttAuth (io.vertx.mqtt.MqttAuth)4 ClientErrorException (org.eclipse.hono.client.ClientErrorException)4 TenantClient (org.eclipse.hono.client.TenantClient)4 Device (org.eclipse.hono.service.auth.device.Device)4