Search in sources :

Example 6 with TenantChangeNotification

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));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) ClientErrorException(org.eclipse.hono.client.ClientErrorException) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification)

Example 7 with TenantChangeNotification

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

Example 8 with TenantChangeNotification

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
    })));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Map(java.util.Map) Message(org.apache.qpid.proton.message.Message) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) HonoConnection(org.eclipse.hono.client.HonoConnection) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Vertx(io.vertx.core.Vertx) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Checkpoint(io.vertx.junit5.Checkpoint) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Checkpoint(io.vertx.junit5.Checkpoint) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Message(org.apache.qpid.proton.message.Message) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) ProtonDelivery(io.vertx.proton.ProtonDelivery) Instant(java.time.Instant) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 9 with TenantChangeNotification

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Map(java.util.Map) Message(org.apache.qpid.proton.message.Message) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) HonoConnection(org.eclipse.hono.client.HonoConnection) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Vertx(io.vertx.core.Vertx) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Checkpoint(io.vertx.junit5.Checkpoint) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Message(org.apache.qpid.proton.message.Message) Instant(java.time.Instant) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 10 with TenantChangeNotification

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);
}
Also used : TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) 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