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));
}
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);
}
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));
}
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);
}
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());
}
Aggregations