Search in sources :

Example 11 with TenantChangeNotification

use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.

the class KafkaBasedNotificationReceiverTest method testThatCorrectHandlerIsInvoked.

/**
 * Verifies that the receiver decodes the notifications it receives and invokes the correct handler.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testThatCorrectHandlerIsInvoked(final VertxTestContext ctx) {
    final String tenantId = "my-tenant";
    final String deviceId = "my-device";
    final Instant creationTime = Instant.parse("2007-12-03T10:15:30Z");
    mockConsumer.schedulePollTask(() -> {
        mockConsumer.addRecord(createKafkaRecord(new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false), 0L));
        mockConsumer.addRecord(createKafkaRecord(new DeviceChangeNotification(LifecycleChange.CREATE, tenantId, deviceId, creationTime, false), 0L));
        mockConsumer.addRecord(createKafkaRecord(new CredentialsChangeNotification(tenantId, deviceId, creationTime), 1L));
        mockConsumer.addRecord(createKafkaRecord(new AllDevicesOfTenantDeletedNotification(tenantId, creationTime), 2L));
    });
    final var receiver = createReceiver();
    final Checkpoint handlerInvokedCheckpoint = ctx.checkpoint(4);
    receiver.registerConsumer(TenantChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(TenantChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(DeviceChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(DeviceChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(CredentialsChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(CredentialsChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(AllDevicesOfTenantDeletedNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(AllDevicesOfTenantDeletedNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.start();
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Instant(java.time.Instant) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 12 with TenantChangeNotification

use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.

the class ProtonBasedTenantClientTest method testTenantChangeNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes all credentials of a tenant from the cache if it receives a notification about a
 * change in that tenant.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testTenantChangeNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(TenantChangeNotification.TYPE);
    // GIVEN a client with a cache containing two tenants
    client.start().compose(v -> addResultToCache("other-tenant")).compose(v -> addResultToCache(tenantId)).onComplete(ctx.succeeding(keyOfChangedTenant -> ctx.verify(() -> {
        // WHEN receiving a notification about a change on a tenant
        sendViaEventBusMock(new TenantChangeNotification(LifecycleChange.DELETE, tenantId, Instant.now(), false), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for the changed tenant
        ctx.verify(() -> verify(cache).invalidateAll(Set.of(keyOfChangedTenant)));
        ctx.completeNow();
    })));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TenantConstants(org.eclipse.hono.util.TenantConstants) Tags(io.opentracing.tag.Tags) Timeout(io.vertx.junit5.Timeout) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) Checkpoint(io.vertx.junit5.Checkpoint) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) X500Principal(javax.security.auth.x500.X500Principal) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cache(com.github.benmanes.caffeine.cache.Cache) ConcurrentMap(java.util.concurrent.ConcurrentMap) ArgumentCaptor(org.mockito.ArgumentCaptor) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) TenantResult(org.eclipse.hono.util.TenantResult) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 13 with TenantChangeNotification

use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.

the class AbstractTenantManagementService method createTenant.

@Override
public final Future<OperationResult<Id>> createTenant(final Optional<String> tenantId, final Tenant tenantObj, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(tenantObj);
    Objects.requireNonNull(span);
    final Promise<Void> tenantCheck = Promise.promise();
    try {
        tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
        tenantCheck.complete();
    } catch (final IllegalStateException e) {
        log.debug("error creating tenant", e);
        TracingHelper.logError(span, e);
        tenantCheck.fail(new ClientErrorException(tenantId.orElse("N/A"), HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
    }
    final String tenantIdValue = tenantId.orElseGet(this::createId);
    return tenantCheck.future().compose(ok -> processCreateTenant(tenantIdValue, tenantObj, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.CREATE, tenantIdValue, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId.get()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) ClientErrorException(org.eclipse.hono.client.ClientErrorException) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification)

Example 14 with TenantChangeNotification

use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.

the class AbstractTenantManagementService method deleteTenant.

@Override
public final Future<Result<Void>> deleteTenant(final String tenantId, final Optional<String> resourceVersion, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(resourceVersion);
    Objects.requireNonNull(span);
    return processDeleteTenant(tenantId, resourceVersion, span).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.DELETE, tenantId, Instant.now(), false))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification)

Aggregations

TenantChangeNotification (org.eclipse.hono.notification.deviceregistry.TenantChangeNotification)14 Test (org.junit.jupiter.api.Test)11 Instant (java.time.Instant)10 Vertx (io.vertx.core.Vertx)8 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)8 Future (io.vertx.core.Future)7 List (java.util.List)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 Promise (io.vertx.core.Promise)5 Checkpoint (io.vertx.junit5.Checkpoint)5 VertxExtension (io.vertx.junit5.VertxExtension)5 VertxTestContext (io.vertx.junit5.VertxTestContext)5 ServerErrorException (org.eclipse.hono.client.ServerErrorException)5 NotificationEventBusSupport (org.eclipse.hono.notification.NotificationEventBusSupport)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 Span (io.opentracing.Span)4 JsonObject (io.vertx.core.json.JsonObject)4 Timeout (io.vertx.junit5.Timeout)4 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)4