Search in sources :

Example 41 with Device

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();
    }));
}
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 42 with Device

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();
    }));
}
Also used : 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) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) Test(org.junit.jupiter.api.Test)

Example 43 with Device

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();
    }));
}
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 44 with Device

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();
    }));
}
Also used : 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) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) Test(org.junit.jupiter.api.Test)

Example 45 with Device

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();
    })));
}
Also used : 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) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Aggregations

Device (org.eclipse.hono.auth.Device)115 HttpURLConnection (java.net.HttpURLConnection)74 Test (org.junit.jupiter.api.Test)72 Future (io.vertx.core.Future)69 ClientErrorException (org.eclipse.hono.client.ClientErrorException)67 Buffer (io.vertx.core.buffer.Buffer)66 Handler (io.vertx.core.Handler)63 TenantObject (org.eclipse.hono.util.TenantObject)63 Promise (io.vertx.core.Promise)59 Constants (org.eclipse.hono.util.Constants)58 Span (io.opentracing.Span)55 RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)55 SpanContext (io.opentracing.SpanContext)53 VertxTestContext (io.vertx.junit5.VertxTestContext)52 VertxExtension (io.vertx.junit5.VertxExtension)51 MessageHelper (org.eclipse.hono.util.MessageHelper)51 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)51 Mockito.when (org.mockito.Mockito.when)51 Truth.assertThat (com.google.common.truth.Truth.assertThat)50 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)47