use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.
the class ProtonBasedDeviceRegistrationClientTest method testDeviceChangeNotificationRemovesValueFromCache.
/**
* Verifies that the client removes registrations of a device from the cache if it receives a notification about a
* change in that device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDeviceChangeNotificationRemovesValueFromCache(final VertxTestContext ctx) {
final String tenantId = "the-tenant-id";
final String deviceId = "the-device-id";
givenAClient(cache);
final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(DeviceChangeNotification.TYPE);
final Set<AnnotatedCacheKey<?>> expectedCacheRemovals = new HashSet<>();
// GIVEN a client with a cache containing device registrations of two tenants
client.start().compose(v -> addResultToCache("other-tenant", deviceId, "gateway-id")).compose(v -> addResultToCache(tenantId, "other-device", "gateway-id")).compose(v -> addResultToCache(tenantId, deviceId, "gateway-id")).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, deviceId, "other-gateway")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
// WHEN receiving a notification about a change on the device
sendViaEventBusMock(new DeviceChangeNotification(LifecycleChange.UPDATE, tenantId, deviceId, Instant.now(), false), notificationHandlerCaptor.getValue());
// THEN the cache is invalidated for registrations of the changed device and not for other devices
ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
ctx.completeNow();
})));
}
use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.
the class ProtonBasedDeviceRegistrationClientTest method testDeviceChangeNotificationRemovesGatewaysFromCache.
/**
* Verifies that the client removes registrations of a gateway from the cache if it receives a notification about a
* change in that gateway.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDeviceChangeNotificationRemovesGatewaysFromCache(final VertxTestContext ctx) {
final String tenantId = "the-tenant-id";
final String deviceId = "the-device-id";
final String gatewayId = "the-device-id";
givenAClient(cache);
final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(DeviceChangeNotification.TYPE);
final Set<AnnotatedCacheKey<?>> expectedCacheRemovals = new HashSet<>();
// GIVEN a client with a cache containing device registrations of two tenants
client.start().compose(v -> addResultToCache("other-tenant", deviceId, "gateway-id")).compose(v -> addResultToCache(tenantId, "other-device", "gateway-id")).compose(v -> addResultToCache(tenantId, deviceId, gatewayId)).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, gatewayId, "other-gateway")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
// WHEN receiving a notification about a change on the gateway device
sendViaEventBusMock(new DeviceChangeNotification(LifecycleChange.UPDATE, tenantId, gatewayId, Instant.now(), false), notificationHandlerCaptor.getValue());
// THEN the cache is invalidated for registrations where the device id matches the gateway id or the
// device id and no other registrations
ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
ctx.completeNow();
})));
}
use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification 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.DeviceChangeNotification in project hono by eclipse.
the class KafkaBasedNotificationSenderTest method testProducerRecordForDeviceNotification.
/**
* Verifies that the expected Kafka record is created when publishing a {@link DeviceChangeNotification}.
*
* @param ctx The vert.x test context.
*/
@Test
public void testProducerRecordForDeviceNotification(final VertxTestContext ctx) {
final DeviceChangeNotification notification = new DeviceChangeNotification(CHANGE, TENANT_ID, DEVICE_ID, CREATION_TIME, ENABLED);
testProducerRecordForNotification(ctx, notification, DEVICE_ID);
}
use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification 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();
}
Aggregations