Search in sources :

Example 86 with Tenant

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

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

the class AmqpUploadTestBase method testAdapterRejectsBadInboundMessage.

/**
 * Verifies that a message containing a payload which has the <em>empty notification</em>
 * content type is rejected by the adapter.
 *
 * @param context The Vert.x context for running asynchronous tests.
 * @throws InterruptedException if test is interrupted while running.
 */
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testAdapterRejectsBadInboundMessage(final VertxTestContext context) throws InterruptedException {
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final VertxTestContext setup = new VertxTestContext();
    setupProtocolAdapter(tenantId, new Tenant(), deviceId, ProtonQoS.AT_LEAST_ONCE).map(s -> {
        setup.verify(() -> {
            final UnsignedLong maxMessageSize = s.getRemoteMaxMessageSize();
            assertWithMessage("max-message-size included in adapter's attach frame").that(maxMessageSize).isNotNull();
            assertWithMessage("max-message-size").that(maxMessageSize.longValue()).isGreaterThan(0);
        });
        sender = s;
        return s;
    }).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        context.failNow(setup.causeOfFailure());
        return;
    }
    final Message msg = ProtonHelper.message("some payload");
    msg.setContentType(EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION);
    msg.setAddress(getEndpointName());
    sender.send(msg, delivery -> {
        context.verify(() -> {
            assertThat(delivery.getRemoteState()).isInstanceOf(Rejected.class);
            final Rejected rejected = (Rejected) delivery.getRemoteState();
            final ErrorCondition error = rejected.getError();
            assertThat((Object) error.getCondition()).isEqualTo(Constants.AMQP_BAD_REQUEST);
        });
        context.completeNow();
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) AmqpErrorException(org.eclipse.hono.util.AmqpErrorException) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) Function(java.util.function.Function) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) Binary(org.apache.qpid.proton.amqp.Binary) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) MethodSource(org.junit.jupiter.params.provider.MethodSource) Data(org.apache.qpid.proton.amqp.messaging.Data) Device(org.eclipse.hono.service.management.device.Device) MessageContext(org.eclipse.hono.application.client.MessageContext) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) LinkError(org.apache.qpid.proton.amqp.transport.LinkError) Promise(io.vertx.core.Promise) ServerErrorException(org.eclipse.hono.client.ServerErrorException) DownstreamMessageAssertions(org.eclipse.hono.tests.DownstreamMessageAssertions) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Buffer(io.vertx.core.buffer.Buffer) Assertions(org.junit.jupiter.api.Assertions) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) QoS(org.eclipse.hono.util.QoS) Tenant(org.eclipse.hono.service.management.tenant.Tenant) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) Message(org.apache.qpid.proton.message.Message) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) VertxTestContext(io.vertx.junit5.VertxTestContext) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Timeout(io.vertx.junit5.Timeout)

Example 88 with Tenant

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

the class MongoDbBasedRegistrationServiceTest method testDeleteDeviceSucceedsForNonExistingTenant.

/**
 * Verifies that a request to delete a device succeeds even if the tenant that the device belongs to does not
 * exist (anymore).
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testDeleteDeviceSucceedsForNonExistingTenant(final VertxTestContext ctx) {
    when(tenantInformationService.tenantExists(eq(TENANT), any(Span.class))).thenReturn(// report that tenant exists when createDevice is invoked
    Future.succeededFuture(OperationResult.ok(HttpURLConnection.HTTP_OK, TenantKey.from(TENANT), Optional.empty(), Optional.empty())), // report that tenant does not exist afterwards
    Future.succeededFuture(OperationResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
    getDeviceManagementService().createDevice(TENANT, Optional.empty(), new Device(), NoopSpan.INSTANCE).compose(result -> getDeviceManagementService().deleteDevice(TENANT, result.getPayload().getId(), Optional.empty(), NoopSpan.INSTANCE)).onComplete(ctx.succeedingThenComplete());
}
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) Device(org.eclipse.hono.service.management.device.Device) Span(io.opentracing.Span) NoopSpan(io.opentracing.noop.NoopSpan) AbstractRegistrationServiceTest(org.eclipse.hono.service.registration.AbstractRegistrationServiceTest) Test(org.junit.jupiter.api.Test)

Example 89 with Tenant

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

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

the class MongoDbBasedTenantServiceTest method testAddTenantFailsForDuplicateAlias.

/**
 * Verifies that a tenant cannot be added if it uses an already registered
 * alias.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAddTenantFailsForDuplicateAlias(final VertxTestContext ctx) {
    final var tenantSpec = new Tenant().setAlias("the-alias");
    addTenant("tenant", tenantSpec).compose(ok -> getTenantManagementService().createTenant(Optional.of("other-tenant"), tenantSpec, NoopSpan.INSTANCE)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_CONFLICT));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) BeforeEach(org.junit.jupiter.api.BeforeEach) 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) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TenantService(org.eclipse.hono.service.tenant.TenantService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) MongoDbBasedTenantsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedTenantsConfigProperties) AfterEach(org.junit.jupiter.api.AfterEach) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) MongoDbBasedTenantDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedTenantDao) Tenant(org.eclipse.hono.service.management.tenant.Tenant) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) Test(org.junit.jupiter.api.Test)

Aggregations

Tenant (org.eclipse.hono.service.management.tenant.Tenant)165 Test (org.junit.jupiter.api.Test)138 VertxTestContext (io.vertx.junit5.VertxTestContext)137 HttpURLConnection (java.net.HttpURLConnection)122 Truth.assertThat (com.google.common.truth.Truth.assertThat)113 TimeUnit (java.util.concurrent.TimeUnit)109 JsonObject (io.vertx.core.json.JsonObject)108 Future (io.vertx.core.Future)107 Timeout (io.vertx.junit5.Timeout)99 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)98 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)97 Constants (org.eclipse.hono.util.Constants)95 Tenants (org.eclipse.hono.tests.Tenants)92 Optional (java.util.Optional)91 Promise (io.vertx.core.Promise)86 Device (org.eclipse.hono.service.management.device.Device)80 Adapter (org.eclipse.hono.util.Adapter)78 VertxExtension (io.vertx.junit5.VertxExtension)77 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)77 Logger (org.slf4j.Logger)74