use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class EdgeDeviceAutoProvisionerTest method verifySuccessfulAutoProvisioning.
@SuppressWarnings("unchecked")
private void verifySuccessfulAutoProvisioning() {
final ArgumentCaptor<Device> registeredDeviceArgumentCaptor = ArgumentCaptor.forClass(Device.class);
verify(deviceManagementService).createDevice(eq(Constants.DEFAULT_TENANT), eq(Optional.of(DEVICE_ID)), registeredDeviceArgumentCaptor.capture(), any());
final Device registeredDevice = registeredDeviceArgumentCaptor.getValue();
assertThat(registeredDevice).isEqualTo(NEW_EDGE_DEVICE);
final ArgumentCaptor<Map<String, Object>> messageArgumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(sender).sendEvent(argThat(tenant -> tenant.getTenantId().equals(Constants.DEFAULT_TENANT)), argThat(assertion -> assertion.getDeviceId().equals(DEVICE_ID)), eq(EventConstants.CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION), any(), messageArgumentCaptor.capture(), any());
verify(deviceManagementService).updateDevice(eq(Constants.DEFAULT_TENANT), eq(DEVICE_ID), argThat(device -> device.getStatus().isAutoProvisioningNotificationSent()), any(Optional.class), any(Span.class));
final Map<String, Object> applicationProperties = messageArgumentCaptor.getValue();
verifyApplicationProperties(GATEWAY_ID, DEVICE_ID, applicationProperties);
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class AbstractTenantManagementServiceTest method testNotificationOnUpdateTenant.
/**
* Verifies that {@link AbstractTenantManagementService#updateTenant(String, Tenant, Optional, Span)} publishes the
* expected notification.
*
* @param context The vert.x test context.
*/
@Test
public void testNotificationOnUpdateTenant(final VertxTestContext context) {
final var notificationArgumentCaptor = ArgumentCaptor.forClass(TenantChangeNotification.class);
tenantManagementService.createTenant(Optional.of(DEFAULT_TENANT_ID), new Tenant(), SPAN).compose(result -> tenantManagementService.updateTenant(DEFAULT_TENANT_ID, new Tenant().setEnabled(false), Optional.empty(), SPAN)).onComplete(context.succeeding(result -> {
context.verify(() -> {
verify(eventBus, times(2)).publish(eq(NotificationEventBusSupport.getEventBusAddress(TenantChangeNotification.TYPE)), notificationArgumentCaptor.capture(), any());
assertThat(notificationArgumentCaptor.getAllValues().size()).isEqualTo(2);
final var notification = notificationArgumentCaptor.getValue();
assertThat(notification).isNotNull();
assertThat(notification.getChange()).isEqualTo(LifecycleChange.UPDATE);
assertThat(notification.getTenantId()).isEqualTo(DEFAULT_TENANT_ID);
assertThat(notification.getCreationTime()).isNotNull();
assertThat(notification.isEnabled()).isFalse();
});
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class AbstractTenantManagementServiceTest method testNotificationOnDeleteTenant.
/**
* Verifies that {@link AbstractTenantManagementService#deleteTenant(String, Optional, Span)} publishes the expected
* notification.
*
* @param context The vert.x test context.
*/
@Test
public void testNotificationOnDeleteTenant(final VertxTestContext context) {
final var notificationArgumentCaptor = ArgumentCaptor.forClass(TenantChangeNotification.class);
tenantManagementService.createTenant(Optional.of(DEFAULT_TENANT_ID), new Tenant(), SPAN).compose(result -> tenantManagementService.deleteTenant(DEFAULT_TENANT_ID, Optional.empty(), SPAN)).onComplete(context.succeeding(result -> {
context.verify(() -> {
verify(eventBus, times(2)).publish(eq(NotificationEventBusSupport.getEventBusAddress(TenantChangeNotification.TYPE)), notificationArgumentCaptor.capture(), any());
assertThat(notificationArgumentCaptor.getAllValues().size()).isEqualTo(2);
final var notification = notificationArgumentCaptor.getValue();
assertThat(notification).isNotNull();
assertThat(notification.getChange()).isEqualTo(LifecycleChange.DELETE);
assertThat(notification.getTenantId()).isEqualTo(DEFAULT_TENANT_ID);
assertThat(notification.getCreationTime()).isNotNull();
assertThat(notification.isEnabled()).isFalse();
});
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class DeviceRegistryUtils method convertTenant.
/**
* Converts a {@link Tenant} instance to a {@link TenantObject}.
*
* @param tenantId The identifier of the tenant.
* @param source The source tenant object.
* @param filterAuthorities {@code true} if CAs which are not valid at this point in time should be filtered out.
* @return The converted tenant object.
* @throws NullPointerException if tenantId or source are {@code null}.
*/
public static JsonObject convertTenant(final String tenantId, final Tenant source, final boolean filterAuthorities) {
final Instant now = Instant.now();
Objects.requireNonNull(tenantId);
Objects.requireNonNull(source);
final TenantObject target = TenantObject.from(tenantId, source.isEnabled());
target.setResourceLimits(source.getResourceLimits());
target.setTracingConfig(source.getTracing());
Optional.ofNullable(source.getMinimumMessageSize()).ifPresent(size -> target.setMinimumMessageSize(size));
Optional.ofNullable(source.getDefaults()).map(JsonObject::new).ifPresent(defaults -> target.setDefaults(defaults));
Optional.ofNullable(source.getAdapters()).filter(adapters -> !adapters.isEmpty()).map(adapters -> adapters.stream().map(adapter -> JsonObject.mapFrom(adapter)).map(json -> json.mapTo(org.eclipse.hono.util.Adapter.class)).collect(Collectors.toList())).ifPresent(adapters -> target.setAdapters(adapters));
Optional.ofNullable(source.getExtensions()).map(JsonObject::new).ifPresent(extensions -> target.setProperty(RegistryManagementConstants.FIELD_EXT, extensions));
Optional.ofNullable(source.getTrustedCertificateAuthorities()).map(list -> list.stream().filter(ca -> {
if (filterAuthorities) {
// filter out CAs which are not valid at this point in time
return !now.isBefore(ca.getNotBefore()) && !now.isAfter(ca.getNotAfter());
} else {
return true;
}
}).map(ca -> JsonObject.mapFrom(ca)).map(json -> {
// validity period is not included in TenantObject
json.remove(RegistryManagementConstants.FIELD_SECRETS_NOT_BEFORE);
json.remove(RegistryManagementConstants.FIELD_SECRETS_NOT_AFTER);
// remove the attributes that are internal to the device registry
// and not to be exposed to the adapters
json.remove(RegistryManagementConstants.FIELD_AUTO_PROVISION_AS_GATEWAY);
json.remove(RegistryManagementConstants.FIELD_AUTO_PROVISIONING_DEVICE_ID_TEMPLATE);
return json;
}).collect(JsonArray::new, JsonArray::add, JsonArray::addAll)).ifPresent(authorities -> target.setProperty(TenantConstants.FIELD_PAYLOAD_TRUSTED_CA, authorities));
return JsonObject.mapFrom(target);
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class AmqpConnectionIT method testConnectFailsAfterDeviceDeleted.
/**
* Verifies that after a device has already connected successfully to the adapter, the deletion of device
* registration data causes the adapter to refuse any following connection attempts.
* <p>
* This test relies upon the registration client cache data in the adapter getting deleted when the device is
* deleted (triggered via a corresponding notification from the device registry).
*
* @param ctx The test context.
*/
@Test
public void testConnectFailsAfterDeviceDeleted(final VertxTestContext ctx) {
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final Tenant tenant = new Tenant();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).compose(con -> {
// first connection attempt successful
con.close();
// now remove device
return helper.registry.deregisterDevice(tenantId, deviceId);
}).compose(ok -> {
final Promise<Void> resultPromise = Promise.promise();
// device deleted, now wait a bit for the device registry notifications to trigger registration cache invalidation
vertx.setTimer(500, tid -> resultPromise.complete());
return resultPromise.future();
}).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(t).isInstanceOf(AuthenticationException.class));
ctx.completeNow();
}));
}
Aggregations