Search in sources :

Example 1 with RegistrationLimits

use of org.eclipse.hono.service.management.tenant.RegistrationLimits in project hono by eclipse.

the class MongoDbBasedRegistrationServiceTest method testCreateDeviceFailsIfTenantLevelDeviceLimitHasBeenReached.

/**
 * Verifies that a request to create more devices than the limit configured at the tenant level fails with a 403.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateDeviceFailsIfTenantLevelDeviceLimitHasBeenReached(final VertxTestContext ctx) {
    config.setMaxDevicesPerTenant(3);
    when(tenantInformationService.getTenant(anyString(), any())).thenReturn(Future.succeededFuture(new Tenant().setRegistrationLimits(new RegistrationLimits().setMaxNumberOfDevices(1))));
    getDeviceManagementService().createDevice(TENANT, Optional.empty(), new Device(), NoopSpan.INSTANCE).onFailure(ctx::failNow).compose(ok -> getDeviceManagementService().createDevice(TENANT, Optional.empty(), new Device(), NoopSpan.INSTANCE)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_FORBIDDEN));
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) AbstractRegistrationServiceTest(org.eclipse.hono.service.registration.AbstractRegistrationServiceTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) CompositeFuture(io.vertx.core.CompositeFuture) MessagingType(org.eclipse.hono.util.MessagingType) TestInstance(org.junit.jupiter.api.TestInstance) AutoProvisionerConfigProperties(org.eclipse.hono.deviceregistry.service.device.AutoProvisionerConfigProperties) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) MongoDbBasedCredentialsDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedCredentialsDao) Device(org.eclipse.hono.service.management.device.Device) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Logger(org.slf4j.Logger) EventSender(org.eclipse.hono.client.telemetry.EventSender) EdgeDeviceAutoProvisioner(org.eclipse.hono.deviceregistry.service.device.EdgeDeviceAutoProvisioner) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) MongoDbBasedRegistrationConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedRegistrationConfigProperties) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) RegistrationService(org.eclipse.hono.service.registration.RegistrationService) AfterEach(org.junit.jupiter.api.AfterEach) Optional(java.util.Optional) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) MongoDbBasedDeviceDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedDeviceDao) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Tenant(org.eclipse.hono.service.management.tenant.Tenant) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Device(org.eclipse.hono.service.management.device.Device) AbstractRegistrationServiceTest(org.eclipse.hono.service.registration.AbstractRegistrationServiceTest) Test(org.junit.jupiter.api.Test)

Example 2 with RegistrationLimits

use of org.eclipse.hono.service.management.tenant.RegistrationLimits in project hono by eclipse.

the class MongoDbBasedCredentialServiceTest method testUpdateCredentialsFailsForExceededCredentialsPerDeviceLimit.

/**
 * Verifies that a request to update credentials of a device fails with a 403 status code
 * if the number of credentials exceeds the tenant's configured limit.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateCredentialsFailsForExceededCredentialsPerDeviceLimit(final VertxTestContext ctx) {
    final var tenantId = UUID.randomUUID().toString();
    final var deviceId = UUID.randomUUID().toString();
    when(tenantInformationService.getTenant(anyString(), any())).thenReturn(Future.succeededFuture(new Tenant().setRegistrationLimits(new RegistrationLimits().setMaxCredentialsPerDevice(1))));
    credentialsManagementService.updateCredentials(tenantId, deviceId, List.of(Credentials.createPasswordCredential("device1", "secret"), Credentials.createPasswordCredential("device2", "secret")), Optional.empty(), NoopSpan.INSTANCE).onComplete(ctx.failing(t -> {
        ctx.verify(() -> {
            Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_FORBIDDEN);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(io.vertx.core.json.JsonObject) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) MongoClient(io.vertx.ext.mongo.MongoClient) MongoDbBasedRegistrationConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedRegistrationConfigProperties) UUID(java.util.UUID) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) List(java.util.List) SpringBasedHonoPasswordEncoder(org.eclipse.hono.auth.SpringBasedHonoPasswordEncoder) Optional(java.util.Optional) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) OperationResult(org.eclipse.hono.service.management.OperationResult) Checkpoint(io.vertx.junit5.Checkpoint) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Mockito.mock(org.mockito.Mockito.mock) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) Credentials(org.eclipse.hono.service.management.credentials.Credentials) CredentialsServiceTestBase(org.eclipse.hono.service.credentials.CredentialsServiceTestBase) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) CompositeFuture(io.vertx.core.CompositeFuture) MongoDbBasedCredentialsDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedCredentialsDao) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MongoDbBasedCredentialsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedCredentialsConfigProperties) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) MongoDbBasedDeviceDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedDeviceDao) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Tenant(org.eclipse.hono.service.management.tenant.Tenant) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Test(org.junit.jupiter.api.Test)

Example 3 with RegistrationLimits

use of org.eclipse.hono.service.management.tenant.RegistrationLimits in project hono by eclipse.

the class JdbcBasedCredentialsServiceTest method testUpdateCredentialsFailsForExceededCredentialsPerDeviceLimit.

/**
 * Verifies that a request to update credentials of a device fails with a 403 status code
 * if the number of credentials exceeds the tenant's configured limit.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateCredentialsFailsForExceededCredentialsPerDeviceLimit(final VertxTestContext ctx) {
    final var tenantId = UUID.randomUUID().toString();
    final var deviceId = UUID.randomUUID().toString();
    when(tenantInformationService.getTenant(anyString(), any())).thenReturn(Future.succeededFuture(new Tenant().setRegistrationLimits(new RegistrationLimits().setMaxCredentialsPerDevice(1))));
    getDeviceManagementService().createDevice(tenantId, Optional.of(deviceId), new Device(), NoopSpan.INSTANCE).onFailure(ctx::failNow).compose(ok -> getCredentialsManagementService().updateCredentials(tenantId, deviceId, List.of(Credentials.createPasswordCredential("device1", "secret"), Credentials.createPasswordCredential("device2", "secret")), Optional.empty(), NoopSpan.INSTANCE)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> {
            Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_FORBIDDEN);
        });
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Device(org.eclipse.hono.service.management.device.Device) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Credentials(org.eclipse.hono.service.management.credentials.Credentials) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) CredentialsServiceTestBase(org.eclipse.hono.service.credentials.CredentialsServiceTestBase) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test) List(java.util.List) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Tenant(org.eclipse.hono.service.management.tenant.Tenant) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Device(org.eclipse.hono.service.management.device.Device) Test(org.junit.jupiter.api.Test)

Example 4 with RegistrationLimits

use of org.eclipse.hono.service.management.tenant.RegistrationLimits in project hono by eclipse.

the class JdbcBasedRegistrationServiceTest method testCreateDeviceFailsIfTenantLevelDeviceLimitHasBeenReached.

/**
 * Verifies that a request to create more devices than the limit configured at the tenant level fails with a 403.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateDeviceFailsIfTenantLevelDeviceLimitHasBeenReached(final VertxTestContext ctx) {
    properties.setMaxDevicesPerTenant(3);
    when(tenantInformationService.getTenant(anyString(), any())).thenReturn(Future.succeededFuture(new Tenant().setRegistrationLimits(new RegistrationLimits().setMaxNumberOfDevices(1))));
    getDeviceManagementService().createDevice(TENANT, Optional.empty(), new Device(), NoopSpan.INSTANCE).onFailure(ctx::failNow).compose(ok -> getDeviceManagementService().createDevice(TENANT, Optional.empty(), new Device(), NoopSpan.INSTANCE)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_FORBIDDEN));
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Device(org.eclipse.hono.service.management.device.Device) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) AbstractRegistrationServiceTest(org.eclipse.hono.service.registration.AbstractRegistrationServiceTest) Mockito.when(org.mockito.Mockito.when) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Tenant(org.eclipse.hono.service.management.tenant.Tenant) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Device(org.eclipse.hono.service.management.device.Device) AbstractRegistrationServiceTest(org.eclipse.hono.service.registration.AbstractRegistrationServiceTest) Test(org.junit.jupiter.api.Test)

Example 5 with RegistrationLimits

use of org.eclipse.hono.service.management.tenant.RegistrationLimits 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

VertxTestContext (io.vertx.junit5.VertxTestContext)5 HttpURLConnection (java.net.HttpURLConnection)5 Optional (java.util.Optional)5 RegistrationLimits (org.eclipse.hono.service.management.tenant.RegistrationLimits)5 Tenant (org.eclipse.hono.service.management.tenant.Tenant)5 Test (org.junit.jupiter.api.Test)5 NoopSpan (io.opentracing.noop.NoopSpan)4 Future (io.vertx.core.Future)4 Assertions (org.eclipse.hono.deviceregistry.util.Assertions)4 Device (org.eclipse.hono.service.management.device.Device)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 CompositeFuture (io.vertx.core.CompositeFuture)3 Timeout (io.vertx.junit5.Timeout)3 VertxExtension (io.vertx.junit5.VertxExtension)3 List (java.util.List)3 TimeUnit (java.util.concurrent.TimeUnit)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Mockito.when (org.mockito.Mockito.when)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2