Search in sources :

Example 1 with ConnectionEventProducer

use of org.eclipse.hono.adapter.monitoring.ConnectionEventProducer in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method testConnectionEventWithTenantConfiguredMessaging.

/**
 * Verifies that when a tenant is configured to use Kafka-based messaging, the connection event is forwarded to
 * Kafka.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testConnectionEventWithTenantConfiguredMessaging(final VertxTestContext ctx) {
    // GIVEN a protocol adapter configured to send connection events
    final ConnectionEventProducer connectionEventProducer = new HonoEventConnectionEventProducer();
    adapter.setConnectionEventProducer(connectionEventProducer);
    when(kafkaEventSender.sendEvent(any(TenantObject.class), any(RegistrationAssertion.class), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
    // WHEN a device of a tenant that is configured to use Kafka-based messaging connects to such an adapter
    final Device authenticatedDevice = new Device(Constants.DEFAULT_TENANT, "4711");
    final TenantObject tenantObject = TenantObject.from(Constants.DEFAULT_TENANT, true);
    tenantObject.setProperty(TenantConstants.FIELD_EXT, Map.of(TenantConstants.FIELD_EXT_MESSAGING_TYPE, MessagingType.kafka.name()));
    when(tenantClient.get(eq(Constants.DEFAULT_TENANT), any())).thenReturn(Future.succeededFuture(tenantObject));
    // THEN the adapter forwards the connection event message downstream
    adapter.sendConnectedEvent("remote-id", authenticatedDevice, null).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            verify(kafkaEventSender).sendEvent(eq(tenantObject), argThat(assertion -> assertion.getDeviceId().equals("4711")), eq(EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE), any(Buffer.class), any(), any());
        });
        ctx.completeNow();
    }));
}
Also used : ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TenantConstants(org.eclipse.hono.util.TenantConstants) Context(io.vertx.core.Context) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) MessagingType(org.eclipse.hono.util.MessagingType) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) EventSender(org.eclipse.hono.client.telemetry.EventSender) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) TenantClient(org.eclipse.hono.client.registry.TenantClient) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Commands(org.eclipse.hono.client.command.Commands) Constants(org.eclipse.hono.util.Constants) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) HttpUtils(org.eclipse.hono.service.http.HttpUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessagingClient(org.eclipse.hono.util.MessagingClient) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) CommandConsumerFactory(org.eclipse.hono.client.command.CommandConsumerFactory) Mockito.never(org.mockito.Mockito.never) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionEventProducer

use of org.eclipse.hono.adapter.monitoring.ConnectionEventProducer in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method testConnectionEventGetsSent.

/**
 * Verifies that the (default) ConnectionEvent API configured for a protocol adapter
 * forwards the message to downstream applications.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testConnectionEventGetsSent(final VertxTestContext ctx) {
    // GIVEN a protocol adapter configured to send connection events
    final ConnectionEventProducer connectionEventProducer = new HonoEventConnectionEventProducer();
    adapter.setConnectionEventProducer(connectionEventProducer);
    when(amqpEventSender.sendEvent(any(TenantObject.class), any(RegistrationAssertion.class), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
    // WHEN a device connects to such an adapter
    final Device authenticatedDevice = new Device(Constants.DEFAULT_TENANT, "4711");
    final TenantObject tenantObject = TenantObject.from(Constants.DEFAULT_TENANT, true);
    when(tenantClient.get(eq(Constants.DEFAULT_TENANT), any())).thenReturn(Future.succeededFuture(tenantObject));
    // THEN the adapter forwards the connection event message downstream
    adapter.sendConnectedEvent("remote-id", authenticatedDevice, null).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            verify(amqpEventSender).sendEvent(eq(tenantObject), argThat(assertion -> assertion.getDeviceId().equals("4711")), eq(EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE), any(Buffer.class), any(), any());
        });
        ctx.completeNow();
    }));
}
Also used : ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TenantConstants(org.eclipse.hono.util.TenantConstants) Context(io.vertx.core.Context) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) MessagingType(org.eclipse.hono.util.MessagingType) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) EventSender(org.eclipse.hono.client.telemetry.EventSender) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) TenantClient(org.eclipse.hono.client.registry.TenantClient) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Commands(org.eclipse.hono.client.command.Commands) Constants(org.eclipse.hono.util.Constants) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) HttpUtils(org.eclipse.hono.service.http.HttpUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessagingClient(org.eclipse.hono.util.MessagingClient) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) CommandConsumerFactory(org.eclipse.hono.client.command.CommandConsumerFactory) Mockito.never(org.mockito.Mockito.never) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionEventProducer

use of org.eclipse.hono.adapter.monitoring.ConnectionEventProducer in project hono by eclipse.

the class VertxBasedAmqpProtocolAdapterTest method testConnectionCount.

/**
 * Verifies that the adapter increments the connection count when
 * a device connects and decrement the count when the device disconnects.
 */
@Test
public void testConnectionCount() {
    // GIVEN an AMQP adapter
    final ConnectionEventProducer connectionEventProducer = mock(ConnectionEventProducer.class);
    when(connectionEventProducer.connected(any(ConnectionEventProducer.Context.class), anyString(), anyString(), any(), any(), any())).thenReturn(Future.succeededFuture());
    when(connectionEventProducer.disconnected(any(ConnectionEventProducer.Context.class), anyString(), anyString(), any(), any(), any())).thenReturn(Future.succeededFuture());
    givenAnAdapter(properties);
    adapter.setConnectionEventProducer(connectionEventProducer);
    // with an enabled tenant
    givenAConfiguredTenant(TEST_TENANT_ID, true);
    // WHEN a device connects
    final Device authenticatedDevice = new Device(TEST_TENANT_ID, TEST_DEVICE);
    final Record record = new RecordImpl();
    record.set(AmqpAdapterConstants.KEY_CLIENT_DEVICE, Device.class, authenticatedDevice);
    final ProtonConnection deviceConnection = mock(ProtonConnection.class);
    when(deviceConnection.attachments()).thenReturn(record);
    when(deviceConnection.getRemoteContainer()).thenReturn("deviceContainer");
    adapter.onConnectRequest(deviceConnection);
    final ArgumentCaptor<Handler<AsyncResult<ProtonConnection>>> openHandler = VertxMockSupport.argumentCaptorHandler();
    verify(deviceConnection).openHandler(openHandler.capture());
    openHandler.getValue().handle(Future.succeededFuture(deviceConnection));
    // THEN the connection count is incremented
    verify(metrics).incrementConnections(TEST_TENANT_ID);
    // and a connected event has been fired
    verify(connectionEventProducer).connected(any(ConnectionEventProducer.Context.class), anyString(), eq(adapter.getTypeName()), eq(authenticatedDevice), any(), any());
    // WHEN the connection to the device is lost
    final ArgumentCaptor<Handler<ProtonConnection>> disconnectHandler = VertxMockSupport.argumentCaptorHandler();
    verify(deviceConnection).disconnectHandler(disconnectHandler.capture());
    disconnectHandler.getValue().handle(deviceConnection);
    // THEN the connection count is decremented
    verify(metrics).decrementConnections(TEST_TENANT_ID);
    // and a disconnected event has been fired
    verify(connectionEventProducer).disconnected(any(ConnectionEventProducer.Context.class), eq("deviceContainer"), eq(adapter.getTypeName()), eq(authenticatedDevice), any(), any());
    // WHEN the device closes its connection to the adapter
    final ArgumentCaptor<Handler<AsyncResult<ProtonConnection>>> closeHandler = VertxMockSupport.argumentCaptorHandler();
    verify(deviceConnection).closeHandler(closeHandler.capture());
    closeHandler.getValue().handle(Future.succeededFuture());
    // THEN the connection count is decremented
    verify(metrics, times(2)).decrementConnections(TEST_TENANT_ID);
    // and a disconnected event has been fired
    verify(connectionEventProducer, times(2)).disconnected(any(ConnectionEventProducer.Context.class), eq("deviceContainer"), eq(adapter.getTypeName()), eq(authenticatedDevice), any(), any());
}
Also used : ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) CommandContext(org.eclipse.hono.client.command.CommandContext) VertxTestContext(io.vertx.junit5.VertxTestContext) SpanContext(io.opentracing.SpanContext) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) Device(org.eclipse.hono.auth.Device) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Test(org.junit.jupiter.api.Test)

Aggregations

SpanContext (io.opentracing.SpanContext)3 Context (io.vertx.core.Context)3 Handler (io.vertx.core.Handler)3 VertxTestContext (io.vertx.junit5.VertxTestContext)3 ConnectionEventProducer (org.eclipse.hono.adapter.monitoring.ConnectionEventProducer)3 Device (org.eclipse.hono.auth.Device)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Future (io.vertx.core.Future)2 Promise (io.vertx.core.Promise)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 VertxExtension (io.vertx.junit5.VertxExtension)2 HttpURLConnection (java.net.HttpURLConnection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 HonoEventConnectionEventProducer (org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer)2 ResourceLimitChecks (org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks)2 ClientErrorException (org.eclipse.hono.client.ClientErrorException)2