use of io.vertx.core.Future in project hono by eclipse.
the class AbstractCredentialsServiceTest method testGetCredentialsPreservesOriginalErrorStatus.
/**
* Verifies that when the <em>processGet</em> method returns an error result, its status
* is adopted for the <em>get</em> method result.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsPreservesOriginalErrorStatus(final VertxTestContext ctx) {
final AbstractCredentialsService credentialsService = new AbstractCredentialsService() {
@Override
protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
return Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_BAD_GATEWAY));
}
};
final String tenantId = "tenant";
final String type = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD;
final String authId = UUID.randomUUID().toString();
final NoopSpan span = NoopSpan.INSTANCE;
credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult -> {
ctx.verify(() -> {
assertThat(getCredentialsResult.getCacheDirective()).isNotNull();
assertThat(getCredentialsResult.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
assertThat(getCredentialsResult.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
});
// another test with auto-provisioning enabled
credentialsService.setDeviceAndGatewayAutoProvisioner(getDeviceAndGatewayAutoProvisionerMock());
credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult2 -> {
ctx.verify(() -> {
assertThat(getCredentialsResult2.getCacheDirective()).isNotNull();
assertThat(getCredentialsResult2.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
assertThat(getCredentialsResult2.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
});
ctx.completeNow();
}));
}));
}
use of io.vertx.core.Future in project hono by eclipse.
the class DelegatingRegistrationAmqpEndpoint method processAssertRequest.
private Future<Message> processAssertRequest(final Message request, final ResourceIdentifier targetAddress, final SpanContext spanContext) {
final String tenantId = targetAddress.getTenantId();
final String deviceId = MessageHelper.getDeviceId(request);
final String gatewayId = MessageHelper.getGatewayId(request);
final Span span = TracingHelper.buildServerChildSpan(tracer, spanContext, SPAN_NAME_ASSERT_DEVICE_REGISTRATION, getClass().getSimpleName()).start();
TracingHelper.setDeviceTags(span, tenantId, deviceId);
final Future<Message> resultFuture;
if (tenantId == null || deviceId == null) {
TracingHelper.logError(span, "missing tenant and/or device");
resultFuture = Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
final Future<RegistrationResult> result;
if (gatewayId == null) {
logger.debug("asserting registration of device [tenant: {}, device-id: {}]", tenantId, deviceId);
result = getService().assertRegistration(tenantId, deviceId, span);
} else {
TracingHelper.TAG_GATEWAY_ID.set(span, gatewayId);
logger.debug("asserting registration of device [tenant: {}, device-id: {}] for gateway [{}]", tenantId, deviceId, gatewayId);
result = getService().assertRegistration(tenantId, deviceId, gatewayId, span);
}
resultFuture = result.map(res -> RegistrationConstants.getAmqpReply(RegistrationConstants.REGISTRATION_ENDPOINT, tenantId, request, res));
}
return finishSpanOnFutureCompletion(span, resultFuture);
}
use of io.vertx.core.Future in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doSearchDevices.
private void doSearchDevices(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_SEARCH_DEVICES, getClass().getSimpleName()).start();
final String tenantId = getTenantParam(ctx);
final Future<Integer> pageSize = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_SIZE, DEFAULT_PAGE_SIZE, CONVERTER_INT, value -> value >= MIN_PAGE_SIZE && value <= MAX_PAGE_SIZE);
final Future<Integer> pageOffset = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_OFFSET, DEFAULT_PAGE_OFFSET, CONVERTER_INT, value -> value >= MIN_PAGE_OFFSET);
final Future<List<Filter>> filters = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_FILTER_JSON, Filter.class);
final Future<List<Sort>> sortOptions = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_SORT_JSON, Sort.class);
CompositeFuture.all(pageSize, pageOffset, filters, sortOptions).onSuccess(ok -> TracingHelper.TAG_TENANT_ID.set(span, tenantId)).compose(ok -> getService().searchDevices(tenantId, pageSize.result(), pageOffset.result(), filters.result(), sortOptions.result(), span)).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of io.vertx.core.Future in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doCreateDevice.
private void doCreateDevice(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_DEVICE, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), true));
final Future<Device> device = fromPayload(ctx);
CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
final Optional<String> did = Optional.ofNullable(deviceId.result());
TracingHelper.TAG_TENANT_ID.set(span, tenantId.result());
did.ifPresent(s -> TracingHelper.TAG_DEVICE_ID.set(span, s));
logger.debug("creating device [tenant: {}, device-id: {}]", tenantId.result(), did.orElse("<auto>"));
return getService().createDevice(tenantId.result(), did, device.result(), span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> responseHeaders.set(HttpHeaders.LOCATION, String.format("/%s/%s/%s", getName(), tenantId.result(), id)));
}, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of io.vertx.core.Future in project hono by eclipse.
the class DelegatingTenantManagementHttpEndpoint method searchTenants.
private void searchTenants(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_SEARCH_TENANT, getClass().getSimpleName()).start();
final Future<Integer> pageSize = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_SIZE, DEFAULT_PAGE_SIZE, CONVERTER_INT, value -> value >= MIN_PAGE_SIZE && value <= MAX_PAGE_SIZE);
final Future<Integer> pageOffset = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_OFFSET, DEFAULT_PAGE_OFFSET, CONVERTER_INT, value -> value >= MIN_PAGE_OFFSET);
final Future<List<Filter>> filters = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_FILTER_JSON, Filter.class);
final Future<List<Sort>> sortOptions = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_SORT_JSON, Sort.class);
CompositeFuture.all(pageSize, pageOffset, filters, sortOptions).compose(ok -> getService().searchTenants(pageSize.result(), pageOffset.result(), filters.result(), sortOptions.result(), span)).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Aggregations