private AbstractProtocolAdapterBase<ProtocolAdapterProperties> newProtocolAdapter( final ProtocolAdapterProperties props, final String typeName, final Handler<Void> startupHandler) { return newProtocolAdapter(props, typeName, startupHandler, null, null); }
private static JsonObject newRegistrationAssertionResult(final String token) { return newRegistrationAssertionResult(token, null); }
/** * 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); })); }
private AbstractProtocolAdapterBase<ProtocolAdapterProperties> newProtocolAdapter(final ProtocolAdapterProperties props) { return newProtocolAdapter(props, ADAPTER_NAME); }
/** * Verifies that the adapter successfully retrieves a registration assertion * for an existing device. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionSucceedsForExistingDevice(final TestContext ctx) { // GIVEN an adapter connected to a registration service final JsonObject assertionResult = newRegistrationAssertionResult("token"); when(registrationClient.assertRegistration(eq("device"), any())).thenReturn(Future.succeededFuture(assertionResult)); when(registrationClient.assertRegistration(eq("device"), any(), any())).thenReturn(Future.succeededFuture(assertionResult)); // WHEN an assertion for the device is retrieved adapter.getRegistrationAssertion("tenant", "device", null).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); adapter.getRegistrationAssertion("tenant", "device", null, mock(SpanContext.class)).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); }
/** * Verifies that any non empty message without a content type is approved. * * @param ctx The vert.x test context. */ @Test public void testNonEmptyMessageWithoutContentType(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN an event without content type and a non empty payload is approved, no error message must be returned final Buffer payload = Buffer.buffer("test"); // arbitrary content-type needs non empty payload ctx.assertTrue(adapter.isPayloadOfIndicatedType(payload, null)); }
/** * Verifies that the registration assertion is set on a downstream message. */ @Test public void testAddPropertiesAddsRegistrationAssertion() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); }
/** * Verifies that any general message with a payload is approved. * * @param ctx The vert.x test context. */ @Test public void testNonEmptyGeneralMessage(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN an non empty event with a non empty payload is approved, no error message must be returned final Buffer payload = Buffer.buffer("test"); final String arbitraryContentType = "bum/lux"; // arbitrary content-type needs non empty payload ctx.assertTrue(adapter.isPayloadOfIndicatedType(payload, arbitraryContentType)); }
/** * Verifies that the fall back content type is set on a downstream message * if no default has been configured for the device. */ @Test public void testAddPropertiesAddsFallbackContentType() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is(AbstractProtocolAdapterBase.CONTENT_TYPE_OCTET_STREAM)); }
/** * Verifies that any empty notification with a payload is an error. * * @param ctx The vert.x test context. */ @Test public void testEmptyNotificationWithPayload(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN an empty event with a non empty payload is approved, an error message must be returned final Buffer payload = Buffer.buffer("test"); final String contentType = EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION; ctx.assertFalse(adapter.isPayloadOfIndicatedType(payload, contentType)); }
/** * Verifies that the registered default content type is set on a downstream message. */ @Test public void testAddPropertiesAddsDefaultContentType() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token", "application/hono")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is("application/hono")); }
adapter = newProtocolAdapter(properties); adapter.setTenantServiceClient(tenantService); adapter.setRegistrationServiceClient(registrationService);
/** * Verifies that the registered default content type is not set on a downstream message * that already contains a content type. */ @Test public void testAddPropertiesDoesNotAddDefaultContentType() { final Message message = ProtonHelper.message(); message.setContentType("application/existing"); adapter.addProperties(message, newRegistrationAssertionResult("token", "application/hono")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is("application/existing")); }
/** * Verifies that the helper approves empty notification without payload. * * @param ctx The vert.x test context. */ @Test public void testEmptyNotificationWithoutPayload(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN an empty event with an empty payload is approved, no error message must be returned final Buffer payload = null; final String contentType = EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION; ctx.assertTrue(adapter.isPayloadOfIndicatedType(payload, contentType)); }
/** * Verifies that the adapter's name is set on a downstream message. */ @Test public void testAddPropertiesAddsAdapterName() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat( MessageHelper.getApplicationProperty( message.getApplicationProperties(), MessageHelper.APP_PROPERTY_ORIG_ADAPTER, String.class), is(ADAPTER_NAME)); }
/** * Verifies that any empty general message is an error. * * @param ctx The vert.x test context. */ @Test public void testEmptyGeneralMessage(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN an event with content type and an empty payload is approved, an error message must be returned final Buffer payload = null; final String arbitraryContentType = "bum/lux"; // arbitrary content-type needs non empty payload ctx.assertFalse(adapter.isPayloadOfIndicatedType(payload, arbitraryContentType)); } }
/** * Verifies that the registration assertion is not set on a downstream message * if the downstream peer does not require it. */ @Test public void testAddPropertiesOmitsRegistrationAssertion() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token"), false); assertNull(MessageHelper.getRegistrationAssertion(message)); }
/** * Verifies that an adapter that does not define a type name * cannot be started. * * @param ctx The vert.x test context. */ @Test public void testStartInternalFailsIfNoTypeNameIsDefined(final TestContext ctx) { // GIVEN an adapter that does not define a type name adapter = newProtocolAdapter(properties, null); adapter.setRegistrationServiceClient(mock(HonoClient.class)); // WHEN starting the adapter // THEN startup fails adapter.startInternal().setHandler(ctx.asyncAssertFailure()); }
private void givenAnAdapterConfiguredWithServiceClients( final Handler<Void> startupHandler, final Handler<Void> commandConnectionEstablishedHandler, final Handler<Void> commandConnectionLostHandler) { adapter = newProtocolAdapter(properties, "test", startupHandler, commandConnectionEstablishedHandler, commandConnectionLostHandler); adapter.setCredentialsServiceClient(credentialsService); adapter.setHonoMessagingClient(messagingService); adapter.setRegistrationServiceClient(registrationService); adapter.setTenantServiceClient(tenantService); adapter.setCommandConnection(commandConnection); }