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