use of org.eclipse.hono.auth.Device 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.auth.Device in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionLimitFailsIfConnectionDurationLimitIsReached.
/**
* Verifies that the connection limit check fails if the tenant's connection duration limit has been reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionLimitFailsIfConnectionDurationLimitIsReached(final VertxTestContext ctx) {
// GIVEN a tenant for which the connection duration limit has been reached
final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
adapter.setResourceLimitChecks(checks);
// WHEN a device tries to connect
adapter.checkConnectionLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// THEN the connection limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(ConnectionDurationExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(ConnectionDurationExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device 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.auth.Device in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionLimitFailsIfConnectionLimitIsReached.
/**
* Verifies that the connection limit check fails if the maximum number of connections
* for a tenant have been reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionLimitFailsIfConnectionLimitIsReached(final VertxTestContext ctx) {
// GIVEN a tenant for which the maximum number of connections has been reached
final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
when(checks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
adapter.setResourceLimitChecks(checks);
// WHEN a device tries to connect
adapter.checkConnectionLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// THEN the connection limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(TenantConnectionsExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(TenantConnectionsExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testValidateAddressUsesDeviceIdentityForAddressWithoutTenant.
/**
* Verifies that the adapter uses an authenticated device's identity when validating an
* address without a tenant ID.
*
* @param ctx The vert.x test context.
*/
@Test
public void testValidateAddressUsesDeviceIdentityForAddressWithoutTenant(final VertxTestContext ctx) {
// WHEN an authenticated device publishes a message to an address that does not contain a tenant ID
final Device authenticatedDevice = new Device("my-tenant", "4711");
final ResourceIdentifier address = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, "", "4712");
adapter.validateAddress(address, authenticatedDevice).onComplete(ctx.succeeding(r -> ctx.verify(() -> {
// THEN the validated address contains the authenticated device's tenant and device ID
assertEquals("my-tenant", r.getTenantId());
assertEquals("4712", r.getResourceId());
ctx.completeNow();
})));
}
Aggregations