use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class KafkaBasedEventSenderIT method testProducerTopicMetricsGetRemovedOnTenantDeletion.
/**
* Verifies that the event sender causes topic-specific metrics in its underlying Kafka producer to be removed
* when a tenant-deletion notification is sent via the vert.x event bus.
*
* @param ctx The vert.x text context.
* @throws InterruptedException if test execution gets interrupted.
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testProducerTopicMetricsGetRemovedOnTenantDeletion(final VertxTestContext ctx) throws InterruptedException {
final String tenantId = "MetricsRemovalTestTenant";
final String tenantTopicName = new HonoTopic(HonoTopic.Type.EVENT, tenantId).toString();
final VertxTestContext setup = new VertxTestContext();
createTopic(tenantTopicName).compose(v -> KafkaBasedEventSender.start()).compose(v -> sendEvent(tenantId, "myDeviceId", "test")).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
// GIVEN a started event sender that has already sent an event message
// and the underlying Kafka producer having filled corresponding topic-specific metrics
final var producerOptional = producerFactory.getProducer(EventConstants.EVENT_ENDPOINT);
ctx.verify(() -> {
assertThat(producerOptional.isPresent()).isTrue();
assertThat(getTopicRelatedMetrics(producerOptional.get(), tenantTopicName)).isNotEmpty();
});
// WHEN sending a tenant-deleted notification for that tenant
NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.DELETE, tenantId, Instant.now(), false));
vertx.runOnContext(v -> {
// THEN the metrics of the underlying producer don't contain any metrics regarding that topic
ctx.verify(() -> assertThat(getTopicRelatedMetrics(producerOptional.get(), tenantTopicName)).isEmpty());
ctx.completeNow();
});
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnTenantDeletedNotification.
/**
* Verifies that the adapter closes the connection to an authenticated device when a notification
* about the deletion of the tenant of that device has been received.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDeviceConnectionIsClosedOnTenantDeletedNotification(final VertxTestContext ctx) {
final Device device = new Device("tenant", "deviceId");
testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new TenantChangeNotification(LifecycleChange.DELETE, "tenant", Instant.now(), true));
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnTenantDisabledNotification.
/**
* Verifies that the adapter closes the connection to an authenticated device when a notification
* has been received that the tenant of the device has been disabled.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDeviceConnectionIsClosedOnTenantDisabledNotification(final VertxTestContext ctx) {
final Device device = new Device("tenant", "deviceId");
testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new TenantChangeNotification(LifecycleChange.UPDATE, "tenant", Instant.now(), false));
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class KafkaBasedNotificationSenderTest method testSendFailsWithTheExpectedError.
/**
* Verifies that the send method returns the underlying error wrapped in a {@link ServerErrorException}.
*
* @param ctx The vert.x test context.
*/
@Test
public void testSendFailsWithTheExpectedError(final VertxTestContext ctx) {
// GIVEN a sender sending a message
final RuntimeException expectedError = new RuntimeException("boom");
final MockProducer<String, JsonObject> mockProducer = new MockProducer<>(false, new StringSerializer(), new JsonObjectSerializer());
final KafkaBasedNotificationSender sender = newSender(mockProducer);
sender.publish(new TenantChangeNotification(CHANGE, TENANT_ID, CREATION_TIME, ENABLED)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN it fails with the expected error
assertThat(t).isInstanceOf(ServerErrorException.class);
assertThat(((ServerErrorException) t).getErrorCode()).isEqualTo(503);
assertThat(t.getCause()).isEqualTo(expectedError);
});
ctx.completeNow();
}));
// WHEN the send operation fails
mockProducer.errorNext(expectedError);
}
use of org.eclipse.hono.notification.deviceregistry.TenantChangeNotification in project hono by eclipse.
the class ProtonBasedNotificationSenderTest method testPublishNotificationSucceeds.
/**
* Verifies that the sender sends a notification message with the expected format.
*
* @param ctx The vert.x test context.
*/
@Test
public void testPublishNotificationSucceeds(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 ArgumentCaptor<Handler<ProtonDelivery>> dispositionHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
// WHEN publishing a notification
final Future<Void> publishNotificationFuture = notificationSender.publish(notification).onComplete(ctx.succeedingThenComplete());
// VERIFY that the message is being sent
verify(sender).send(messageCaptor.capture(), dispositionHandlerCaptor.capture());
// VERIFY that the future waits for the disposition to be updated by the peer
assertThat(publishNotificationFuture.isComplete()).isFalse();
// THEN the disposition is updated and the peer accepts the message
dispositionHandlerCaptor.getValue().handle(protonDelivery);
// VERIFY that the message is as expected
final Message message = messageCaptor.getValue();
assertThat(message.getAddress()).isEqualTo(NotificationAddressHelper.getAddress(notification.getType()));
assertThat(message.getContentType()).isEqualTo(NotificationConstants.CONTENT_TYPE);
final AbstractNotification decodedNotification = Json.decodeValue(MessageHelper.getPayload(message), AbstractNotification.class);
assertThat(decodedNotification.getClass()).isEqualTo(notification.getClass());
assertThat(publishNotificationFuture.isComplete()).isTrue();
}
Aggregations