use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class DeviceAndGatewayAutoProvisionerTest method provisionAndVerifySuccessfulResult.
@SuppressWarnings("unchecked")
private void provisionAndVerifySuccessfulResult(final VertxTestContext ctx, final boolean isGateway, final String expectedDeviceId) throws CertificateEncodingException {
final JsonObject clientContext = new JsonObject().put(CredentialsConstants.FIELD_CLIENT_CERT, cert.getEncoded());
// WHEN provisioning a device/gateway from a certificate
deviceAndGatewayAutoProvisioner.provisionIfEnabled(tenantId, tenant, subjectDn, clientContext, NoopSpan.INSTANCE).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
// VERIFY that that the device/gateway has been registered.
final ArgumentCaptor<Device> deviceCaptor = ArgumentCaptor.forClass(Device.class);
verify(deviceManagementService).createDevice(eq(tenantId), any(), deviceCaptor.capture(), any());
if (isGateway) {
// VERIFY that a gateway has been provisioned by checking the relevant property
assertThat(deviceCaptor.getValue().getAuthorities()).contains(RegistryManagementConstants.AUTHORITY_AUTO_PROVISIONING_ENABLED);
}
// VERIFY that the correct credentials are stored
final ArgumentCaptor<List<CommonCredential>> credentialsCaptor = ArgumentCaptor.forClass(List.class);
verify(credentialsManagementService).updateCredentials(eq(tenantId), eq(expectedDeviceId), credentialsCaptor.capture(), any(), any());
final List<CommonCredential> credentialsCaptorValue = credentialsCaptor.getValue();
assertThat(credentialsCaptorValue.size()).isEqualTo(1);
assertThat(credentialsCaptorValue.get(0).getType()).isEqualTo(RegistryManagementConstants.SECRETS_TYPE_X509_CERT);
assertThat(credentialsCaptorValue.get(0).getAuthId()).isEqualTo(subjectDn);
// VERIFY the returned credentials result after successful auto-provisioning
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_CREATED);
final JsonObject returnedCredential = result.getPayload();
assertThat(returnedCredential.getString(RegistryManagementConstants.FIELD_PAYLOAD_DEVICE_ID)).isEqualTo(expectedDeviceId);
assertThat(returnedCredential.getString(RegistryManagementConstants.FIELD_AUTH_ID)).isEqualTo(subjectDn);
assertThat(returnedCredential.getString(RegistryManagementConstants.FIELD_TYPE)).isEqualTo(RegistryManagementConstants.SECRETS_TYPE_X509_CERT);
// VERIFY that a auto-provisioning notification has been sent
final ArgumentCaptor<Map<String, Object>> messagePropertiesArgumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(sender).sendEvent(argThat(tenant -> tenant.getTenantId().equals(tenantId)), argThat(assertion -> assertion.getDeviceId().equals(expectedDeviceId)), eq(EventConstants.CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION), any(), messagePropertiesArgumentCaptor.capture(), any());
final Map<String, Object> eventProperties = messagePropertiesArgumentCaptor.getValue();
assertThat(eventProperties.get(MessageHelper.APP_PROPERTY_REGISTRATION_STATUS)).isEqualTo(EventConstants.RegistrationStatus.NEW.name());
assertThat(eventProperties.get(MessageHelper.APP_PROPERTY_TENANT_ID)).isEqualTo(tenantId);
// VERIFY that the device registration has been updated as the auto-provisioning event has been
// successfully sent
verify(deviceManagementService).updateDevice(eq(tenantId), eq(expectedDeviceId), deviceCaptor.capture(), any(), any());
final DeviceStatus deviceStatus = deviceCaptor.getValue().getStatus();
assertThat(deviceStatus.isAutoProvisioned()).isTrue();
assertThat(deviceStatus.isAutoProvisioningNotificationSent()).isTrue();
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class AbstractRegistrationServiceTest method testAssertRegistrationContainsDeviceInfo.
/**
* Verifies that the payload returned for a request to assert the registration
* of an existing device contains the information registered for the device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAssertRegistrationContainsDeviceInfo(final VertxTestContext ctx) {
final JsonObject registeredDevice = new JsonObject().put(RegistrationConstants.FIELD_DOWNSTREAM_MESSAGE_MAPPER, "mapping-service").put(RegistrationConstants.FIELD_PAYLOAD_DEFAULTS, new JsonObject().put("foo", "bar")).put(RegistrationConstants.FIELD_VIA, new JsonArray().add("gw1").add("gw2")).put("ext", new JsonObject().put("key", "value"));
when(service.getRegistrationInformation(any(DeviceKey.class), any(Span.class))).thenReturn(Future.succeededFuture(RegistrationResult.from(HttpURLConnection.HTTP_OK, new JsonObject().put(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID, "device").put(RegistrationConstants.FIELD_DATA, registeredDevice))));
service.assertRegistration(Constants.DEFAULT_TENANT, "device", span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(result.getPayload().getString(RegistrationConstants.FIELD_DOWNSTREAM_MESSAGE_MAPPER)).isEqualTo("mapping-service");
assertThat(result.getPayload().getJsonArray(RegistrationConstants.FIELD_VIA)).containsExactly("gw1", "gw2");
assertThat(result.getPayload().getJsonObject(RegistrationConstants.FIELD_PAYLOAD_DEFAULTS)).containsExactly(Map.entry("foo", "bar"));
assertThat(result.getPayload().containsKey("ext")).isFalse();
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class AbstractTenantManagementServiceTest method testNotificationOnCreateTenant.
/**
* Verifies that {@link AbstractTenantManagementService#createTenant(Optional, Tenant, Span)} publishes the expected
* notification.
*
* @param context The vert.x test context.
*/
@Test
public void testNotificationOnCreateTenant(final VertxTestContext context) {
final var notificationArgumentCaptor = ArgumentCaptor.forClass(TenantChangeNotification.class);
tenantManagementService.createTenant(Optional.of(DEFAULT_TENANT_ID), new Tenant().setEnabled(false), SPAN).onComplete(context.succeeding(result -> {
context.verify(() -> {
verify(eventBus).publish(eq(NotificationEventBusSupport.getEventBusAddress(TenantChangeNotification.TYPE)), notificationArgumentCaptor.capture(), any());
assertThat(notificationArgumentCaptor.getAllValues().size()).isEqualTo(1);
final var notification = notificationArgumentCaptor.getValue();
assertThat(notification).isNotNull();
assertThat(notification.getChange()).isEqualTo(LifecycleChange.CREATE);
assertThat(notification.getTenantId()).isEqualTo(DEFAULT_TENANT_ID);
assertThat(notification.getCreationTime()).isNotNull();
assertThat(notification.isEnabled()).isFalse();
});
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class AbstractDeviceManagementServiceTest method testNotificationOnDeleteDevice.
/**
* Verifies that {@link AbstractDeviceManagementService#deleteDevice(String, String, Optional, Span)} publishes the
* expected notification.
*
* @param context The vert.x test context.
*/
@Test
public void testNotificationOnDeleteDevice(final VertxTestContext context) {
final var notificationArgumentCaptor = ArgumentCaptor.forClass(DeviceChangeNotification.class);
deviceManagementService.createDevice(DEFAULT_TENANT_ID, Optional.of(DEFAULT_DEVICE_ID), new Device(), SPAN).compose(result -> deviceManagementService.deleteDevice(DEFAULT_TENANT_ID, DEFAULT_DEVICE_ID, Optional.empty(), SPAN)).onComplete(context.succeeding(result -> {
context.verify(() -> {
verify(eventBus, times(2)).publish(eq(NotificationEventBusSupport.getEventBusAddress(DeviceChangeNotification.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.getDeviceId()).isEqualTo(DEFAULT_DEVICE_ID);
assertThat(notification.getCreationTime()).isNotNull();
assertThat(notification.isEnabled()).isFalse();
});
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class AbstractTenantManagementService method createTenant.
@Override
public final Future<OperationResult<Id>> createTenant(final Optional<String> tenantId, final Tenant tenantObj, final Span span) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(tenantObj);
Objects.requireNonNull(span);
final Promise<Void> tenantCheck = Promise.promise();
try {
tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
tenantCheck.complete();
} catch (final IllegalStateException e) {
log.debug("error creating tenant", e);
TracingHelper.logError(span, e);
tenantCheck.fail(new ClientErrorException(tenantId.orElse("N/A"), HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
}
final String tenantIdValue = tenantId.orElseGet(this::createId);
return tenantCheck.future().compose(ok -> processCreateTenant(tenantIdValue, tenantObj, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.CREATE, tenantIdValue, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId.get()));
}
Aggregations