Search in sources :

Example 1 with TenantChangeNotification

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();
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) KafkaBasedEventSender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedEventSender) LoggerFactory(org.slf4j.LoggerFactory) KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) MessagingType(org.eclipse.hono.util.MessagingType) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) MetricName(org.apache.kafka.common.MetricName) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) NewTopic(io.vertx.kafka.admin.NewTopic) Logger(org.slf4j.Logger) Collection(java.util.Collection) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) Promise(io.vertx.core.Promise) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Collectors(java.util.stream.Collectors) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Buffer(io.vertx.core.buffer.Buffer) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) KafkaAdminClient(io.vertx.kafka.admin.KafkaAdminClient) VertxTestContext(io.vertx.junit5.VertxTestContext) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 2 with TenantChangeNotification

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));
}
Also used : Device(org.eclipse.hono.auth.Device) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 3 with TenantChangeNotification

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));
}
Also used : Device(org.eclipse.hono.auth.Device) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 4 with TenantChangeNotification

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);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JsonObjectSerializer(io.vertx.kafka.client.serialization.JsonObjectSerializer) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) JsonObject(io.vertx.core.json.JsonObject) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) NotificationConstants(org.eclipse.hono.notification.NotificationConstants) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) KafkaProducerFactory(org.eclipse.hono.client.kafka.producer.KafkaProducerFactory) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) MockProducer(org.apache.kafka.clients.producer.MockProducer) JsonObjectSerializer(io.vertx.kafka.client.serialization.JsonObjectSerializer) JsonObject(io.vertx.core.json.JsonObject) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) ServerErrorException(org.eclipse.hono.client.ServerErrorException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.jupiter.api.Test)

Example 5 with TenantChangeNotification

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();
}
Also used : Message(org.apache.qpid.proton.message.Message) Instant(java.time.Instant) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) Test(org.junit.jupiter.api.Test)

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