Search in sources :

Example 6 with ResourceLimitChecks

use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method testCheckMessageLimitFailsIfMessageLimitIsReached.

/**
 * Verifies that the message limit check fails, if the maximum number of messages
 * for a tenant have been reached. Also verifies that the payload size of the incoming
 * message is calculated based on the configured minimum message size.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCheckMessageLimitFailsIfMessageLimitIsReached(final VertxTestContext ctx) {
    // GIVEN a tenant with a minimum message size of 4kb configured
    final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
    tenant.setMinimumMessageSize(4096);
    final ArgumentCaptor<Long> payloadSizeCaptor = ArgumentCaptor.forClass(Long.class);
    // And for that tenant, the maximum messages limit has been already reached
    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), payloadSizeCaptor.capture(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
    when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
    adapter.setResourceLimitChecks(checks);
    // WHEN a device sends a message with a payload size of 5000 bytes
    adapter.checkMessageLimit(tenant, 5000, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
        // THEN the payload size used for the message limit checks is calculated based on the minimum message size.
        // In this case it should be 8kb
        assertEquals(8 * 1024, payloadSizeCaptor.getValue());
        // THEN the message limit check fails
        ctx.verify(() -> assertThat(ServiceInvocationException.extractStatusCode(t)).isEqualTo(HttpUtils.HTTP_TOO_MANY_REQUESTS));
        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) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) Test(org.junit.jupiter.api.Test)

Aggregations

Future (io.vertx.core.Future)6 Buffer (io.vertx.core.buffer.Buffer)6 List (java.util.List)6 Optional (java.util.Optional)6 ConnectionEventProducer (org.eclipse.hono.adapter.monitoring.ConnectionEventProducer)6 HonoEventConnectionEventProducer (org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer)6 ResourceLimitChecks (org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks)6 CommandResponseSender (org.eclipse.hono.client.command.CommandResponseSender)6 CommandRouterClient (org.eclipse.hono.client.command.CommandRouterClient)6 CredentialsClient (org.eclipse.hono.client.registry.CredentialsClient)6 DeviceRegistrationClient (org.eclipse.hono.client.registry.DeviceRegistrationClient)6 TenantClient (org.eclipse.hono.client.registry.TenantClient)6 EventSender (org.eclipse.hono.client.telemetry.EventSender)6 TelemetrySender (org.eclipse.hono.client.telemetry.TelemetrySender)6 MessagingClientProvider (org.eclipse.hono.client.util.MessagingClientProvider)6 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)6 MessagingType (org.eclipse.hono.util.MessagingType)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 SpanContext (io.opentracing.SpanContext)5 Context (io.vertx.core.Context)5