/** * Verifies that the <em>onCommandConnectionLost</em> and * <em>onCommandConnectionEstablished</em> hooks are invoked * when the command connection is re-established. * * @param ctx The vert.x test context. */ @SuppressWarnings("unchecked") @Test public void testCallbacksInvokedOnReconnect(final TestContext ctx) { // GIVEN an adapter connected to Hono services final Handler<Void> commandConnectionEstablishedHandler = mock(Handler.class); final Handler<Void> commandConnectionLostHandler = mock(Handler.class); givenAnAdapterConfiguredWithServiceClients(mock(Handler.class), commandConnectionEstablishedHandler, commandConnectionLostHandler); adapter.startInternal().setHandler(ctx.asyncAssertSuccess(ok -> { final ArgumentCaptor<Handler<ProtonConnection>> disconnectHandlerCaptor = ArgumentCaptor.forClass(Handler.class); verify(commandConnection).connect(disconnectHandlerCaptor.capture()); // WHEN the command connection is lost disconnectHandlerCaptor.getValue().handle(mock(ProtonConnection.class)); // THEN the onCommandConnectionLost hook is being invoked, verify(commandConnectionLostHandler).handle(null); // the connection is re-established and verify(commandConnection, times(2)).connect(any(Handler.class)); // the onCommandConnectionEstablished hook is being invoked verify(commandConnectionEstablishedHandler, times(2)).handle(null); })); }
/** * Verifies that the adapter connects to required services during * startup and invokes the <em>onCommandConnectionEstablished</em> and * <em>doStart</em> methods. * * @param ctx The vert.x test context. */ @SuppressWarnings("unchecked") @Test public void testStartInternalConnectsToServices(final TestContext ctx) { // GIVEN an adapter configured with service clients // that can connect to the corresponding services final Handler<Void> startupHandler = mock(Handler.class); final Handler<Void> commandConnectionHandler = mock(Handler.class); final Handler<Void> commandConnectionLostHandler = mock(Handler.class); givenAnAdapterConfiguredWithServiceClients(startupHandler, commandConnectionHandler, commandConnectionLostHandler); // WHEN starting the adapter adapter.startInternal().setHandler(ctx.asyncAssertSuccess(ok -> { // THEN the service clients have connected verify(tenantService).connect(any(Handler.class)); verify(registrationService).connect(any(Handler.class)); verify(messagingService).connect(any(Handler.class)); verify(credentialsService).connect(any(Handler.class)); verify(commandConnection).connect(any(Handler.class)); verify(startupHandler).handle(null); verify(commandConnectionHandler).handle(null); verify(commandConnectionLostHandler, never()).handle(null); })); }