Search in sources :

Example 1 with ProtocolAdapterProperties

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

the class AbstractProtocolAdapterBaseTest method setup.

/**
 * Sets up the fixture.
 */
@Before
public void setup() {
    registrationClient = mock(RegistrationClient.class);
    HonoClient honoClient = mock(HonoClient.class);
    when(honoClient.getOrCreateRegistrationClient(anyString())).thenReturn(Future.succeededFuture(registrationClient));
    properties = new ProtocolAdapterProperties();
    adapter = newProtocolAdapter(properties);
    adapter.setRegistrationServiceClient(honoClient);
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) HonoClient(org.eclipse.hono.client.HonoClient) RegistrationClient(org.eclipse.hono.client.RegistrationClient) Before(org.junit.Before)

Example 2 with ProtocolAdapterProperties

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

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

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

the class AbstractVertxBasedMqttProtocolAdapterTest method setup.

/**
 * Creates clients for the needed micro services and sets the configuration to enable the insecure port.
 */
@Before
@SuppressWarnings("unchecked")
public void setup() {
    config = new ProtocolAdapterProperties();
    config.setInsecurePortEnabled(true);
    regClient = mock(RegistrationClient.class);
    final JsonObject result = new JsonObject().put(RegistrationConstants.FIELD_ASSERTION, "token");
    when(regClient.assertRegistration(anyString(), any())).thenReturn(Future.succeededFuture(result));
    tenantClient = mock(TenantClient.class);
    when(tenantClient.get(anyString())).thenAnswer(invocation -> {
        return Future.succeededFuture(TenantObject.from(invocation.getArgument(0), true));
    });
    tenantServiceClient = mock(HonoClient.class);
    when(tenantServiceClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(tenantServiceClient));
    when(tenantServiceClient.getOrCreateTenantClient()).thenReturn(Future.succeededFuture(tenantClient));
    messagingClient = mock(HonoClient.class);
    when(messagingClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(messagingClient));
    deviceRegistrationServiceClient = mock(HonoClient.class);
    when(deviceRegistrationServiceClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(deviceRegistrationServiceClient));
    when(deviceRegistrationServiceClient.getOrCreateRegistrationClient(anyString())).thenReturn(Future.succeededFuture(regClient));
    credentialsAuthProvider = mock(HonoClientBasedAuthProvider.class);
    when(credentialsAuthProvider.start()).thenReturn(Future.succeededFuture());
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) HonoClient(org.eclipse.hono.client.HonoClient) RegistrationClient(org.eclipse.hono.client.RegistrationClient) TenantClient(org.eclipse.hono.client.TenantClient) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Before(org.junit.Before)

Example 5 with ProtocolAdapterProperties

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

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