use of io.vertx.core.Promise in project hono by eclipse.
the class KafkaClientUnitTestHelper method newKafkaProducer.
/**
* Returns a new {@link KafkaProducer}.
*
* @param producer The mock producer to wrap.
* @param <K> The type of the key.
* @param <V> The type of the value.
* @return The new Kafka producer.
*/
public static <K, V> KafkaProducer<K, V> newKafkaProducer(final MockProducer<K, V> producer) {
final VertxInternal vertxMock = mock(VertxInternal.class);
final ContextInternal context = VertxMockSupport.mockContextInternal(vertxMock);
doAnswer(invocation -> Promise.promise()).when(context).promise();
doAnswer(invocation -> {
final Promise<RecordMetadata> result = Promise.promise();
final Handler<Promise<RecordMetadata>> handler = invocation.getArgument(0);
handler.handle(result);
return result.future();
}).when(context).executeBlocking(VertxMockSupport.anyHandler());
VertxMockSupport.executeBlockingCodeImmediately(vertxMock, context);
return KafkaProducer.create(vertxMock, producer);
}
use of io.vertx.core.Promise in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapter method checkAuthorizationAndResourceLimits.
private Future<Void> checkAuthorizationAndResourceLimits(final Device authenticatedDevice, final ProtonConnection con, final Span span) {
final Promise<Void> connectAuthorizationCheck = Promise.promise();
if (getConfig().isAuthenticationRequired()) {
if (authenticatedDevice == null) {
connectAuthorizationCheck.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "anonymous devices not supported"));
} else {
log.trace("received connection request from {}", authenticatedDevice);
// the SASL handshake will already have authenticated the device
// we still need to verify that
// the adapter is enabled for the tenant,
// the device/gateway exists and is enabled and
// that the connection limit for the tenant is not exceeded.
CompositeFuture.all(checkDeviceRegistration(authenticatedDevice, span.context()), getTenantConfiguration(authenticatedDevice.getTenantId(), span.context()).compose(tenantConfig -> CompositeFuture.all(isAdapterEnabled(tenantConfig), checkConnectionLimit(tenantConfig, span.context())))).map(ok -> {
log.debug("{} is registered and enabled", authenticatedDevice);
span.log(String.format("device [%s] is registered and enabled", authenticatedDevice));
return (Void) null;
}).onComplete(connectAuthorizationCheck);
}
} else {
log.trace("received connection request from anonymous device [container: {}]", con.getRemoteContainer());
connectAuthorizationCheck.complete();
}
return connectAuthorizationCheck.future();
}
use of io.vertx.core.Promise in project hono by eclipse.
the class ConfigBasedCoapEndpointFactory method getSecureEndpoint.
/**
* {@inheritDoc}
*/
@Override
public Future<Endpoint> getSecureEndpoint() {
final Promise<Endpoint> result = Promise.promise();
if (isSecurePortEnabled()) {
final int securePort = config.getPort(CoAP.DEFAULT_COAP_SECURE_PORT);
final int insecurePort = config.isInsecurePortEnabled() ? config.getInsecurePort(CoAP.DEFAULT_COAP_PORT) : Constants.PORT_UNCONFIGURED;
if (config.isInsecurePortEnabled() && insecurePort == securePort) {
LOG.error("secure and insecure ports must be configured to bind to different port numbers");
result.fail("secure and insecure ports configured to bind to same port number");
} else {
getSecureNetworkConfig().compose(secureNetworkConfig -> createSecureEndpoint(securePort, secureNetworkConfig)).onComplete(result);
}
} else if (!config.isInsecurePortEnabled()) {
result.fail(new IllegalStateException("neither secure nor insecure port configured"));
} else {
LOG.info("neither key/cert nor secure port are configured, won't create secure endpoint");
result.fail(new IllegalStateException("neither key/cert nor secure port are configured"));
}
return result.future();
}
use of io.vertx.core.Promise in project hono by eclipse.
the class AbstractVertxBasedCoapAdapterTest method testStartRegistersResources.
/**
* Verifies that the adapter registers resources as part of the start-up process.
*
* @param ctx The helper to use for running async tests on vertx.
*/
@Test
public void testStartRegistersResources(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
// and a set of resources
final Resource resource = mock(Resource.class);
adapter.addResources(Set.of(resource));
// WHEN starting the adapter
final Promise<Void> startupTracker = Promise.promise();
startupTracker.future().onComplete(ctx.succeeding(s -> {
// THEN the resources have been registered with the server
final ArgumentCaptor<VertxCoapResource> resourceCaptor = ArgumentCaptor.forClass(VertxCoapResource.class);
ctx.verify(() -> {
verify(server).add(resourceCaptor.capture());
assertThat(resourceCaptor.getValue().getWrappedResource()).isEqualTo(resource);
});
ctx.completeNow();
}));
adapter.start(startupTracker);
}
use of io.vertx.core.Promise in project hono by eclipse.
the class AbstractVertxBasedCoapAdapterTest method testResourcesAreRunOnVertxContext.
/**
* Verifies that the resources registered with the adapter are always
* executed on the adapter's vert.x context.
*
* @param ctx The helper to use for running async tests on vertx.
*/
@Test
public void testResourcesAreRunOnVertxContext(final VertxTestContext ctx) {
// GIVEN an adapter
final Context context = vertx.getOrCreateContext();
givenAnAdapter(properties);
// with a resource
final Promise<Void> resourceInvocation = Promise.promise();
final Resource resource = new CoapResource("test") {
@Override
public void handleGET(final CoapExchange exchange) {
ctx.verify(() -> assertThat(Vertx.currentContext()).isEqualTo(context));
resourceInvocation.complete();
}
};
adapter.addResources(Set.of(resource));
adapter.init(vertx, context);
final Promise<Void> startupTracker = Promise.promise();
adapter.start(startupTracker);
startupTracker.future().compose(ok -> {
// WHEN the resource receives a GET request
final Request request = new Request(Code.GET);
final Exchange getExchange = new Exchange(request, Origin.REMOTE, mock(Executor.class));
final ArgumentCaptor<VertxCoapResource> resourceCaptor = ArgumentCaptor.forClass(VertxCoapResource.class);
verify(server).add(resourceCaptor.capture());
resourceCaptor.getValue().handleRequest(getExchange);
// THEN the resource's handler has been run on the adapter's vert.x event loop
return resourceInvocation.future();
}).onComplete(ctx.succeedingThenComplete());
}
Aggregations