use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class AbstractTenantManagementService method updateTenant.
@Override
public final Future<OperationResult<Void>> updateTenant(final String tenantId, final Tenant tenantObj, final Optional<String> resourceVersion, final Span span) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(tenantObj);
Objects.requireNonNull(resourceVersion);
Objects.requireNonNull(span);
final Promise<Void> tenantCheck = Promise.promise();
try {
tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
tenantCheck.complete();
} catch (final IllegalStateException e) {
log.debug("error updating tenant", e);
TracingHelper.logError(span, e);
tenantCheck.fail(new ClientErrorException(tenantId, HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
}
return tenantCheck.future().compose(ok -> processUpdateTenant(tenantId, tenantObj, resourceVersion, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.UPDATE, tenantId, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class NotificationEventBusSupportTest method testPublishedNotificationIsReceived.
/**
* Verifies that a notification published on the vert.x event bus is getting received by consumers.
*
* @param ctx The vert.x test context.
* @param vertx The vert.x instance.
*/
@Test
public void testPublishedNotificationIsReceived(final VertxTestContext ctx, final Vertx vertx) {
final TenantChangeNotification notification = new TenantChangeNotification(LifecycleChange.CREATE, "my-tenant", Instant.parse("2007-12-03T10:15:30Z"), false);
final int consumerCount = 2;
final Checkpoint checkpoint = ctx.checkpoint(consumerCount);
for (int i = 0; i < consumerCount; i++) {
NotificationEventBusSupport.registerConsumer(vertx, notification.getType(), receivedNotification -> {
ctx.verify(() -> assertThat(receivedNotification).isEqualTo(notification));
checkpoint.flag();
});
}
NotificationEventBusSupport.sendNotification(vertx, notification);
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class ProtonBasedNotificationReceiverTest 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");
final TenantChangeNotification tenantChangeNotification = new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false);
final Message tenantChangeNotificationMsg = ProtonHelper.message();
MessageHelper.setJsonPayload(tenantChangeNotificationMsg, JsonObject.mapFrom(tenantChangeNotification));
final DeviceChangeNotification deviceChangeNotification = new DeviceChangeNotification(LifecycleChange.CREATE, tenantId, deviceId, creationTime, false);
final Message deviceChangeNotificationMsg = ProtonHelper.message();
MessageHelper.setJsonPayload(deviceChangeNotificationMsg, JsonObject.mapFrom(deviceChangeNotification));
final CredentialsChangeNotification credentialsChangeNotification = new CredentialsChangeNotification(tenantId, deviceId, creationTime);
final Message credentialsChangeNotificationMsg = ProtonHelper.message();
MessageHelper.setJsonPayload(credentialsChangeNotificationMsg, JsonObject.mapFrom(credentialsChangeNotification));
final AllDevicesOfTenantDeletedNotification allDevicesOfTenantDeletedChangeNotification = new AllDevicesOfTenantDeletedNotification(tenantId, creationTime);
final Message allDevicesOfTenantDeletedChangeNotificationMsg = ProtonHelper.message();
MessageHelper.setJsonPayload(allDevicesOfTenantDeletedChangeNotificationMsg, JsonObject.mapFrom(allDevicesOfTenantDeletedChangeNotification));
final Checkpoint handlerInvokedCheckpoint = ctx.checkpoint(4);
client.registerConsumer(TenantChangeNotification.TYPE, notification -> ctx.verify(() -> {
assertThat(notification).isInstanceOf(TenantChangeNotification.class);
handlerInvokedCheckpoint.flag();
}));
client.registerConsumer(DeviceChangeNotification.TYPE, notification -> ctx.verify(() -> {
assertThat(notification).isInstanceOf(DeviceChangeNotification.class);
handlerInvokedCheckpoint.flag();
}));
client.registerConsumer(CredentialsChangeNotification.TYPE, notification -> ctx.verify(() -> {
assertThat(notification).isInstanceOf(CredentialsChangeNotification.class);
handlerInvokedCheckpoint.flag();
}));
client.registerConsumer(AllDevicesOfTenantDeletedNotification.TYPE, notification -> ctx.verify(() -> {
assertThat(notification).isInstanceOf(AllDevicesOfTenantDeletedNotification.class);
handlerInvokedCheckpoint.flag();
}));
// WHEN starting the client
client.start().onComplete(ctx.succeeding(v -> ctx.verify(() -> {
// THEN the receiver links got created
final Map<String, ProtonMessageHandler> receiverMsgHandlersPerAddress = assertReceiverLinkCreated(connection);
assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(TenantChangeNotification.TYPE));
assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(DeviceChangeNotification.TYPE));
assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(CredentialsChangeNotification.TYPE));
assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(AllDevicesOfTenantDeletedNotification.TYPE));
final ProtonMessageHandler tenantChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(TenantChangeNotification.TYPE));
final ProtonMessageHandler deviceChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(DeviceChangeNotification.TYPE));
final ProtonMessageHandler credentialsChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(CredentialsChangeNotification.TYPE));
final ProtonMessageHandler allDevicesOfTenantDeletedChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(AllDevicesOfTenantDeletedNotification.TYPE));
// and sending notifications on the links
tenantChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), tenantChangeNotificationMsg);
deviceChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), deviceChangeNotificationMsg);
credentialsChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), credentialsChangeNotificationMsg);
allDevicesOfTenantDeletedChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), allDevicesOfTenantDeletedChangeNotificationMsg);
// causes the client to receive the notifications and the handlerInvokedCheckpoint to get flagged
})));
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class ProtonBasedNotificationReceiverTest method testRegisterCommandConsumer.
/**
* Verifies that the client with a registered consumer receives notification messages.
*
* @param ctx The vert.x test context.
*/
@Test
public void testRegisterCommandConsumer(final VertxTestContext ctx) {
final String tenantId = "my-tenant";
final Instant creationTime = Instant.parse("2007-12-03T10:15:30Z");
final TenantChangeNotification notification = new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false);
final Message notificationMessage = ProtonHelper.message();
MessageHelper.setJsonPayload(notificationMessage, JsonObject.mapFrom(notification));
// GIVEN a client where a TenantChangeNotification consumer gets registered
final AtomicReference<TenantChangeNotification> receivedNotificationRef = new AtomicReference<>();
client.registerConsumer(TenantChangeNotification.TYPE, receivedNotificationRef::set);
// WHEN starting the client
client.start().onComplete(ctx.succeeding(v -> {
ctx.verify(() -> {
// THEN a receiver link got created
final ProtonMessageHandler receiverMessageHandler = AmqpClientUnitTestHelper.assertReceiverLinkCreated(connection);
// and sending a notification on the link
receiverMessageHandler.handle(mock(ProtonDelivery.class), notificationMessage);
// causes the client to receive the notification
assertThat(receivedNotificationRef.get()).isNotNull();
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class KafkaBasedNotificationSenderTest method testProducerRecordForTenantNotification.
/**
* Verifies that the expected Kafka record is created when publishing a {@link TenantChangeNotification}.
*
* @param ctx The vert.x test context.
*/
@Test
public void testProducerRecordForTenantNotification(final VertxTestContext ctx) {
final TenantChangeNotification notification = new TenantChangeNotification(CHANGE, TENANT_ID, CREATION_TIME, ENABLED);
testProducerRecordForNotification(ctx, notification, TENANT_ID);
}
Aggregations