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