Search in sources :

Example 1 with AllDevicesOfTenantDeletedNotification

use of org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnAllDevicesOfTenantDeletedNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * about the deletion of all device data of the tenant of that device has been received.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnAllDevicesOfTenantDeletedNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new AllDevicesOfTenantDeletedNotification("tenant", Instant.now()));
}
Also used : AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Example 2 with AllDevicesOfTenantDeletedNotification

use of org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification in project hono by eclipse.

the class KafkaBasedNotificationSenderTest method testProducerRecordForAllDevicesOfTenantDeletedNotification.

/**
 * Verifies that the expected Kafka record is created when publishing a {@link AllDevicesOfTenantDeletedNotification}.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testProducerRecordForAllDevicesOfTenantDeletedNotification(final VertxTestContext ctx) {
    final AllDevicesOfTenantDeletedNotification notification = new AllDevicesOfTenantDeletedNotification(TENANT_ID, CREATION_TIME);
    testProducerRecordForNotification(ctx, notification, TENANT_ID);
}
Also used : AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Test(org.junit.jupiter.api.Test)

Example 3 with AllDevicesOfTenantDeletedNotification

use of org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification in project hono by eclipse.

the class AbstractDeviceManagementService method deleteDevicesOfTenant.

@Override
public final Future<Result<Void>> deleteDevicesOfTenant(final String tenantId, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(span);
    return this.tenantInformationService.tenantExists(tenantId, span).otherwise(t -> Result.from(ServiceInvocationException.extractStatusCode(t))).compose(result -> {
        switch(result.getStatus()) {
            case HttpURLConnection.HTTP_OK:
                break;
            case HttpURLConnection.HTTP_NOT_FOUND:
                span.log("tenant does not exist (anymore)");
                LOG.info("trying to delete devices of non-existing tenant [tenant-id: {}]", tenantId);
                break;
            default:
                span.log(Map.of(Fields.EVENT, "could not determine tenant status", Tags.HTTP_STATUS.getKey(), result.getStatus()));
                LOG.info("could not determine tenant status [tenant-id: {}, code: {}]", tenantId, result.getStatus());
        }
        return processDeleteDevicesOfTenant(tenantId, span);
    }).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new AllDevicesOfTenantDeletedNotification(tenantId, Instant.now()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Tags(io.opentracing.tag.Tags) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Fields(io.opentracing.log.Fields) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Device(org.eclipse.hono.service.management.device.Device) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) UUID(java.util.UUID) 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) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)

Example 4 with AllDevicesOfTenantDeletedNotification

use of org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification in project hono by eclipse.

the class ProtonBasedDeviceRegistrationClientTest method testAllDevicesOfTenantDeletedNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes all registrations of a tenant from the cache if it receives a notification that
 * tenant all devices of the tenant have been deleted.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAllDevicesOfTenantDeletedNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(AllDevicesOfTenantDeletedNotification.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", "device-id1", "gateway-id")).compose(v -> addResultToCache(tenantId, "device-id1", "gateway-id")).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, "device-id2", "gateway-id")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
        // WHEN receiving a notification that all devices of a tenant have been deleted
        sendViaEventBusMock(new AllDevicesOfTenantDeletedNotification(tenantId, Instant.now()), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all registrations of the tenant
        ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
        ctx.completeNow();
    })));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Timeout(io.vertx.junit5.Timeout) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) RegistrationResult(org.eclipse.hono.util.RegistrationResult) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) Cache(com.github.benmanes.caffeine.cache.Cache) Constants(org.eclipse.hono.util.Constants) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Message(org.apache.qpid.proton.message.Message) HonoConnection(org.eclipse.hono.client.HonoConnection) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) ProtonHelper(io.vertx.proton.ProtonHelper) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with AllDevicesOfTenantDeletedNotification

use of org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method testAllDevicesOfTenantDeletedNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes all credentials of a tenant from the cache if it receives a notification that
 * tenant all devices of the tenant have been deleted.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAllDevicesOfTenantDeletedNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    final String deviceId = "the-device-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(AllDevicesOfTenantDeletedNotification.TYPE);
    final Set<AnnotatedCacheKey<?>> expectedCacheRemovals = new HashSet<>();
    // GIVEN a client with a cache containing credentials of two tenants
    client.start().compose(v -> addResultToCache("other-tenant", deviceId, "other")).compose(v -> addResultToCache(tenantId, deviceId, "auth-id1")).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, deviceId, "auth-id2")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
        // WHEN receiving a notification that all devices of a tenant have been deleted
        sendViaEventBusMock(new AllDevicesOfTenantDeletedNotification(tenantId, Instant.now()), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all credentials of the tenant and not for other tenants
        ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
        ctx.completeNow();
    })));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Tags(io.opentracing.tag.Tags) Timeout(io.vertx.junit5.Timeout) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsResult(org.eclipse.hono.util.CredentialsResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Cache(com.github.benmanes.caffeine.cache.Cache) Constants(org.eclipse.hono.util.Constants) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Message(org.apache.qpid.proton.message.Message) HonoConnection(org.eclipse.hono.client.HonoConnection) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

AllDevicesOfTenantDeletedNotification (org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)7 Test (org.junit.jupiter.api.Test)6 Instant (java.time.Instant)5 DeviceChangeNotification (org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)5 Future (io.vertx.core.Future)4 Vertx (io.vertx.core.Vertx)4 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 Span (io.opentracing.Span)3 Tracer (io.opentracing.Tracer)3 EventBus (io.vertx.core.eventbus.EventBus)3 JsonObject (io.vertx.core.json.JsonObject)3 Timeout (io.vertx.junit5.Timeout)3 VertxExtension (io.vertx.junit5.VertxExtension)3 VertxTestContext (io.vertx.junit5.VertxTestContext)3 ProtonDelivery (io.vertx.proton.ProtonDelivery)3 ProtonHelper (io.vertx.proton.ProtonHelper)3 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)3 ProtonQoS (io.vertx.proton.ProtonQoS)3 ProtonReceiver (io.vertx.proton.ProtonReceiver)3