Search in sources :

Example 1 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method testDeviceChangeNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes credentials 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 credentials of two tenants
    client.start().compose(v -> addResultToCache("other-tenant", deviceId, "other")).compose(v -> addResultToCache(tenantId, "other-device", "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 about a change on the device
        sendViaEventBusMock(new DeviceChangeNotification(LifecycleChange.UPDATE, tenantId, deviceId, Instant.now(), false), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all credentials of the changed device and not for other devices
        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) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method addResultToCache.

private Future<AnnotatedCacheKey<?>> addResultToCache(final String tenantId, final String deviceId, final String authId) {
    final String credentialsType = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD;
    final CredentialsResult<CredentialsObject> credentialsResult = CredentialsResult.from(HttpURLConnection.HTTP_OK, new CredentialsObject(deviceId, authId, credentialsType));
    @SuppressWarnings("unchecked") final ArgumentCaptor<AnnotatedCacheKey<?>> responseCacheKey = ArgumentCaptor.forClass(AnnotatedCacheKey.class);
    when(cache.getIfPresent(responseCacheKey.capture())).thenReturn(credentialsResult);
    return client.get(tenantId, credentialsType, authId, new JsonObject(), null).map(credentialsObject -> {
        final AnnotatedCacheKey<?> cacheKey = responseCacheKey.getValue();
        cacheKey.putAttribute("device-id", deviceId);
        cacheBackingMap.put(cacheKey, credentialsResult);
        return cacheKey;
    });
}
Also used : CredentialsObject(org.eclipse.hono.util.CredentialsObject) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey)

Example 3 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedDeviceRegistrationClientTest method addResultToCache.

private Future<AnnotatedCacheKey<?>> addResultToCache(final String tenantId, final String deviceId, final String gatewayId) {
    final JsonObject registrationAssertion = newRegistrationAssertionResult(deviceId);
    final RegistrationResult registrationResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion);
    when(cache.getIfPresent(any())).thenReturn(registrationResult);
    @SuppressWarnings("unchecked") final ArgumentCaptor<AnnotatedCacheKey<?>> responseCacheKey = ArgumentCaptor.forClass(AnnotatedCacheKey.class);
    when(cache.getIfPresent(responseCacheKey.capture())).thenReturn(registrationResult);
    return client.assertRegistration(tenantId, deviceId, gatewayId, null).map(assertion -> {
        final AnnotatedCacheKey<?> cacheKey = responseCacheKey.getValue();
        cacheBackingMap.put(cacheKey, registrationResult);
        return cacheKey;
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) RegistrationResult(org.eclipse.hono.util.RegistrationResult) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey)

Example 4 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method testCredentialsChangeNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes credentials of a device from the cache if it receives a notification about a
 * change in the credentials of that device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCredentialsChangeNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    final String deviceId = "the-device-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(CredentialsChangeNotification.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 about a change on the credentials
        sendViaEventBusMock(new CredentialsChangeNotification(tenantId, deviceId, Instant.now()), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all credentials of the changed device and not for other devices
        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) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) 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 AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey 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

AnnotatedCacheKey (org.eclipse.hono.client.util.AnnotatedCacheKey)14 JsonObject (io.vertx.core.json.JsonObject)12 Span (io.opentracing.Span)10 Cache (com.github.benmanes.caffeine.cache.Cache)8 Future (io.vertx.core.Future)8 HttpURLConnection (java.net.HttpURLConnection)8 HonoConnection (org.eclipse.hono.client.HonoConnection)8 SendMessageSampler (org.eclipse.hono.client.SendMessageSampler)8 NotificationEventBusSupport (org.eclipse.hono.notification.NotificationEventBusSupport)8 AllDevicesOfTenantDeletedNotification (org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)8 DeviceChangeNotification (org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)7 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)7 CacheDirective (org.eclipse.hono.util.CacheDirective)7 Constants (org.eclipse.hono.util.Constants)7 MessageHelper (org.eclipse.hono.util.MessageHelper)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 Tracer (io.opentracing.Tracer)6 Handler (io.vertx.core.Handler)6 Vertx (io.vertx.core.Vertx)6 EventBus (io.vertx.core.eventbus.EventBus)6