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