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();
})));
}
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;
});
}
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;
});
}
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();
})));
}
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();
})));
}
Aggregations