Search in sources :

Example 6 with ResourceLimits

use of org.eclipse.hono.util.ResourceLimits in project hono by eclipse.

the class ProtonBasedCommandResponseSenderTest method testCommandResponseMessageHasCreationTimeAndTtl.

/**
 * Verifies that command response messages being sent downstream contain a creation-time
 * and a time-to-live as defined at the tenant level.
 */
@Test
public void testCommandResponseMessageHasCreationTimeAndTtl() {
    final var now = Instant.now();
    final TenantObject tenant = TenantObject.from(TENANT_ID);
    tenant.setResourceLimits(new ResourceLimits().setMaxTtlCommandResponse(10L));
    when(protonSender.sendQueueFull()).thenReturn(Boolean.FALSE);
    when(protonSender.send(any(Message.class), VertxMockSupport.anyHandler())).thenReturn(mock(ProtonDelivery.class));
    // WHEN sending a command response message
    final CommandResponse commandResponse = CommandResponse.fromRequestId(Commands.encodeRequestIdParameters(CORRELATION_ID, REPLY_TO_ID, DEVICE_ID, MessagingType.amqp), TENANT_ID, DEVICE_ID, null, null, HttpURLConnection.HTTP_OK);
    sender.sendCommandResponse(tenant, new RegistrationAssertion(DEVICE_ID), commandResponse, span.context());
    final ArgumentCaptor<Message> downstreamMessage = ArgumentCaptor.forClass(Message.class);
    verify(protonSender).send(downstreamMessage.capture(), VertxMockSupport.anyHandler());
    // THEN the message being sent contains a creation-time
    assertThat(downstreamMessage.getValue().getCreationTime()).isAtLeast(now.toEpochMilli());
    // and a TTL
    assertThat(downstreamMessage.getValue().getTtl()).isEqualTo(10_000L);
    // and a 200 status code
    assertThat(MessageHelper.getStatus(downstreamMessage.getValue())).isEqualTo(HttpURLConnection.HTTP_OK);
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) ResourceLimits(org.eclipse.hono.util.ResourceLimits) CommandResponse(org.eclipse.hono.client.command.CommandResponse) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceLimits

use of org.eclipse.hono.util.ResourceLimits in project hono by eclipse.

the class CommandAndControlAmqpIT method createRandomTenantAndInitDeviceId.

/**
 * Creates a random tenant.
 * <p>
 * The tenant will be configured with a max TTL for command responses.
 *
 * @param ctx The vert.x test context.
 */
@BeforeEach
public void createRandomTenantAndInitDeviceId(final VertxTestContext ctx) {
    tenantId = helper.getRandomTenantId();
    deviceId = helper.getRandomDeviceId(tenantId);
    final Tenant tenantConfig = new Tenant().setResourceLimits(new ResourceLimits().setMaxTtlCommandResponse(TTL_COMMAND_RESPONSE.toSeconds()));
    helper.registry.addTenant(tenantId, tenantConfig).onComplete(ctx.succeedingThenComplete());
}
Also used : Tenant(org.eclipse.hono.service.management.tenant.Tenant) ResourceLimits(org.eclipse.hono.util.ResourceLimits) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with ResourceLimits

use of org.eclipse.hono.util.ResourceLimits in project hono by eclipse.

the class TenantTest method testDecodeResourceLimits.

/**
 * Decode "resource-limits" section.
 */
@Test
public void testDecodeResourceLimits() {
    final JsonObject tenantSpec = new JsonObject().put(RegistryManagementConstants.FIELD_RESOURCE_LIMITS, new JsonObject().put(RegistryManagementConstants.FIELD_MAX_CONNECTIONS, 100).put(RegistryManagementConstants.FIELD_MAX_TTL, 60).put(RegistryManagementConstants.FIELD_MAX_TTL_COMMAND_RESPONSE, 40).put(RegistryManagementConstants.FIELD_MAX_TTL_TELEMETRY_QOS0, 10).put(RegistryManagementConstants.FIELD_MAX_TTL_TELEMETRY_QOS1, 30).put(RegistryManagementConstants.FIELD_DATA_VOLUME, new JsonObject().put(RegistryManagementConstants.FIELD_MAX_BYTES, 20_000_000).put(RegistryManagementConstants.FIELD_EFFECTIVE_SINCE, "2019-04-25T14:30:00+02:00").put(RegistryManagementConstants.FIELD_PERIOD, new JsonObject().put(RegistryManagementConstants.FIELD_PERIOD_MODE, "days").put(RegistryManagementConstants.FIELD_PERIOD_NO_OF_DAYS, 90))).put(RegistryManagementConstants.FIELD_CONNECTION_DURATION, new JsonObject().put(RegistryManagementConstants.FIELD_MAX_MINUTES, 20_000_000).put(RegistryManagementConstants.FIELD_EFFECTIVE_SINCE, "2019-04-25T14:30:00+02:00").put(RegistryManagementConstants.FIELD_PERIOD, new JsonObject().put(RegistryManagementConstants.FIELD_PERIOD_MODE, "monthly"))));
    final Tenant tenant = tenantSpec.mapTo(Tenant.class);
    assertNotNull(tenant);
    assertTrue(tenant.isEnabled());
    final ResourceLimits limits = tenant.getResourceLimits();
    assertNotNull(limits);
    assertEquals(100, limits.getMaxConnections());
    assertEquals(60, limits.getMaxTtl());
    assertEquals(40, limits.getMaxTtlCommandResponse());
    assertEquals(10, limits.getMaxTtlTelemetryQoS0());
    assertEquals(30, limits.getMaxTtlTelemetryQoS1());
    assertNotNull(limits.getDataVolume());
    assertEquals(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2019-04-25T14:30:00+02:00", OffsetDateTime::from).toInstant(), limits.getDataVolume().getEffectiveSince());
    assertEquals(20_000_000, limits.getDataVolume().getMaxBytes());
    assertNotNull(limits.getDataVolume().getPeriod());
    assertEquals(PeriodMode.days, limits.getDataVolume().getPeriod().getMode());
    assertEquals(90, limits.getDataVolume().getPeriod().getNoOfDays());
    assertNotNull(limits.getConnectionDuration());
    assertEquals(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2019-04-25T14:30:00+02:00", OffsetDateTime::from).toInstant(), limits.getConnectionDuration().getEffectiveSince());
    assertEquals(20_000_000, limits.getConnectionDuration().getMaxMinutes());
    assertNotNull(limits.getConnectionDuration().getPeriod());
    assertEquals(PeriodMode.monthly, limits.getConnectionDuration().getPeriod().getMode());
}
Also used : OffsetDateTime(java.time.OffsetDateTime) ResourceLimits(org.eclipse.hono.util.ResourceLimits) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 9 with ResourceLimits

use of org.eclipse.hono.util.ResourceLimits in project hono by eclipse.

the class TenantTest method testEncodeResourceLimitsDoesNotIncludeDefaultValues.

/**
 * Encode "resource-limits" section.
 */
@Test
public void testEncodeResourceLimitsDoesNotIncludeDefaultValues() {
    final ResourceLimits limits = new ResourceLimits();
    final JsonObject json = JsonObject.mapFrom(limits);
    assertFalse(json.containsKey(RegistryManagementConstants.FIELD_MAX_CONNECTIONS));
    final ResourceLimits deserializedLimits = json.mapTo(ResourceLimits.class);
    assertThat(deserializedLimits.getMaxConnections()).isEqualTo(-1);
}
Also used : ResourceLimits(org.eclipse.hono.util.ResourceLimits) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 10 with ResourceLimits

use of org.eclipse.hono.util.ResourceLimits in project hono by eclipse.

the class TenantManagementIT method testGetTenantSucceeds.

/**
 * Verifies that a correctly added tenant record can be successfully looked up again.
 *
 * @param context The vert.x test context.
 */
@Test
public void testGetTenantSucceeds(final VertxTestContext context) {
    final var resourceLimits = new ResourceLimits();
    resourceLimits.setMaxConnections(1000);
    final var registrationLimits = new RegistrationLimits();
    registrationLimits.setMaxNumberOfDevices(100);
    registrationLimits.setMaxCredentialsPerDevice(5);
    final Tenant requestBody = buildTenantPayload();
    requestBody.setMinimumMessageSize(2048);
    requestBody.setResourceLimits(resourceLimits);
    requestBody.setRegistrationLimits(registrationLimits);
    final String tenantId = getHelper().getRandomTenantId();
    LOG.debug("registering tenant using Management API: {}", JsonObject.mapFrom(requestBody).encodePrettily());
    getHelper().registry.addTenant(tenantId, requestBody).compose(ar -> getHelper().registry.getTenant(tenantId)).onComplete(context.succeeding(httpResponse -> {
        final JsonObject json = httpResponse.bodyAsJsonObject();
        LOG.debug("retrieved tenant using Tenant API: {}", json.encodePrettily());
        context.verify(() -> {
            assertThat(json.containsKey(RegistryManagementConstants.FIELD_STATUS)).isFalse();
            assertTrue(IntegrationTestSupport.testJsonObjectToBeContained(json, JsonObject.mapFrom(requestBody)));
        });
        context.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Nested(org.junit.jupiter.api.Nested) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) CompositeFuture(io.vertx.core.CompositeFuture) Matcher(java.util.regex.Matcher) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SearchResult(org.eclipse.hono.service.management.SearchResult) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) EnabledIf(org.junit.jupiter.api.condition.EnabledIf) Device(org.eclipse.hono.service.management.device.Device) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Logger(org.slf4j.Logger) JacksonCodec(io.vertx.core.json.jackson.JacksonCodec) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) HttpHeaders(io.vertx.core.http.HttpHeaders) PublicKey(java.security.PublicKey) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Adapter(org.eclipse.hono.util.Adapter) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Tenant(org.eclipse.hono.service.management.tenant.Tenant) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) ResourceLimits(org.eclipse.hono.util.ResourceLimits) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceLimits (org.eclipse.hono.util.ResourceLimits)13 Test (org.junit.jupiter.api.Test)12 TenantObject (org.eclipse.hono.util.TenantObject)9 VertxTestContext (io.vertx.junit5.VertxTestContext)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 VertxExtension (io.vertx.junit5.VertxExtension)6 Map (java.util.Map)6 RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 JsonObject (io.vertx.core.json.JsonObject)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 Tracer (io.opentracing.Tracer)4 NoopSpan (io.opentracing.noop.NoopSpan)4 Vertx (io.vertx.core.Vertx)4 Buffer (io.vertx.core.buffer.Buffer)4 EventBus (io.vertx.core.eventbus.EventBus)4 HttpURLConnection (java.net.HttpURLConnection)4 Constants (org.eclipse.hono.util.Constants)4