Search in sources :

Example 6 with ProtocolAdapterProperties

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

the class AbstractVertxBasedMqttProtocolAdapterTest method getAdapter.

private AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> getAdapter(final MqttServer server) {
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = new AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties>() {

        @Override
        protected String getTypeName() {
            return ADAPTER_TYPE;
        }

        @Override
        protected Future<Void> onPublishedMessage(final MqttContext ctx) {
            final ResourceIdentifier topic = ResourceIdentifier.fromString(ctx.message().topicName());
            return uploadTelemetryMessage(ctx, topic.getTenantId(), topic.getResourceId(), ctx.message().payload());
        }
    };
    adapter.setConfig(config);
    adapter.setMetrics(new MqttAdapterMetrics());
    adapter.setTenantServiceClient(tenantServiceClient);
    adapter.setHonoMessagingClient(messagingClient);
    adapter.setRegistrationServiceClient(deviceRegistrationServiceClient);
    adapter.setCredentialsAuthProvider(credentialsAuthProvider);
    if (server != null) {
        adapter.setMqttInsecureServer(server);
        adapter.init(vertx, mock(Context.class));
    }
    return adapter;
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 7 with ProtocolAdapterProperties

use of org.eclipse.hono.config.ProtocolAdapterProperties 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)

Example 8 with ProtocolAdapterProperties

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

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageFailsForDisabledTenant.

/**
 * Verifies that the adapter does not forward a message published by a device
 * if the device belongs to a tenant for which the adapter has been disabled.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadTelemetryMessageFailsForDisabledTenant(final TestContext ctx) {
    // 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();
    final MessageSender sender = mock(MessageSender.class);
    when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
    // WHEN a device of "my-tenant" publishes a telemetry message
    adapter.uploadTelemetryMessage(new MqttContext(mock(MqttPublishMessage.class), mock(MqttEndpoint.class)), "my-tenant", "the-device", Buffer.buffer("test")).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the message has not been sent downstream
        verify(sender, never()).send(any(Message.class));
        // because the tenant is not enabled
        ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((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) TenantObject(org.eclipse.hono.util.TenantObject) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MessageSender(org.eclipse.hono.client.MessageSender) MqttServer(io.vertx.mqtt.MqttServer) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 9 with ProtocolAdapterProperties

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

the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerRetrievesCredentialsOnRecord.

/**
 * Verifies that an adapter retrieves credentials on record for a device connecting to the adapter.
 */
@SuppressWarnings("unchecked")
@Test
public void testEndpointHandlerRetrievesCredentialsOnRecord() {
    // GIVEN an adapter requiring devices to authenticate endpoint
    final MqttServer server = getMqttServer(false);
    config.setAuthenticationRequired(true);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    forceClientMocksToConnected();
    final MqttEndpoint endpoint = getMqttEndpointAuthenticated();
    adapter.handleEndpointConnection(endpoint);
    verify(credentialsAuthProvider).authenticate(any(UsernamePasswordCredentials.class), any(Handler.class));
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttServer(io.vertx.mqtt.MqttServer) Handler(io.vertx.core.Handler) UsernamePasswordCredentials(org.eclipse.hono.service.auth.device.UsernamePasswordCredentials) Test(org.junit.Test)

Example 10 with ProtocolAdapterProperties

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

the class VertxBasedMqttProtocolAdapterTest method givenAnAdapter.

private void givenAnAdapter() {
    config = new ProtocolAdapterProperties();
    adapter = new VertxBasedMqttProtocolAdapter();
    adapter.setConfig(config);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties)

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