Search in sources :

Example 6 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project hono by eclipse.

the class AmqpServiceBaseTest method testServerCallsPublishEventOnClientDisconnect.

/**
 * Verifies that the service invokes the <em>publishConnectionClosedEvent</em>
 * method when a client disconnects.
 */
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testServerCallsPublishEventOnClientDisconnect() {
    // GIVEN a server to which a client is connected
    final Handler<ProtonConnection> publishConnectionClosedEvent = mock(Handler.class);
    final AmqpServiceBase<ServiceConfigProperties> server = createServer(null, publishConnectionClosedEvent);
    final ProtonConnection con = newConnection(Constants.PRINCIPAL_ANONYMOUS);
    server.onRemoteConnectionOpen(con);
    final ArgumentCaptor<Handler> closeHandlerCaptor = ArgumentCaptor.forClass(Handler.class);
    verify(con).disconnectHandler(closeHandlerCaptor.capture());
    // WHEN the client disconnects from the service
    closeHandlerCaptor.getValue().handle(con);
    // THEN the publishConnectionClosedEvent method is invoked
    verify(publishConnectionClosedEvent).handle(any(ProtonConnection.class));
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Handler(io.vertx.core.Handler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Test(org.junit.Test)

Example 7 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project hono by eclipse.

the class AmqpServiceBaseTest method newConnection.

private static ProtonConnection newConnection(final HonoUser user) {
    final Record attachments = new RecordImpl();
    attachments.set(Constants.KEY_CONNECTION_ID, String.class, CON_ID);
    attachments.set(Constants.KEY_CLIENT_PRINCIPAL, HonoUser.class, user);
    final ProtonConnection con = mock(ProtonConnection.class);
    when(con.attachments()).thenReturn(attachments);
    when(con.getRemoteContainer()).thenReturn("test-client");
    return con;
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Record(org.apache.qpid.proton.engine.Record) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl)

Example 8 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project hono by eclipse.

the class AbstractHonoClientTest method testCreateSenderFails.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void testCreateSenderFails(final Supplier<ErrorCondition> errorSupplier, final Predicate<Throwable> failureAssertion) {
    final Record attachments = new RecordImpl();
    final ProtonSender sender = mock(ProtonSender.class);
    when(sender.getRemoteCondition()).thenReturn(errorSupplier.get());
    when(sender.attachments()).thenReturn(attachments);
    final ProtonConnection con = mock(ProtonConnection.class);
    when(con.createSender(anyString())).thenReturn(sender);
    final Future<ProtonSender> result = AbstractHonoClient.createSender(context, props, con, "target", ProtonQoS.AT_LEAST_ONCE, null);
    final ArgumentCaptor<Handler> openHandler = ArgumentCaptor.forClass(Handler.class);
    verify(sender).openHandler(openHandler.capture());
    openHandler.getValue().handle(Future.failedFuture(new IllegalStateException()));
    assertTrue(result.failed());
    assertTrue(failureAssertion.test(result.cause()));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) ProtonConnection(io.vertx.proton.ProtonConnection) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl)

Example 9 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project hono by eclipse.

the class EventConsumerImplTest method testCreateRegistersBiConsumerAsMessageHandler.

/**
 * Verifies that the message delivery for a received event is forwarded to the
 * registered event consumer.
 *
 * @param ctx The test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCreateRegistersBiConsumerAsMessageHandler(final TestContext ctx) {
    // GIVEN an event consumer that releases all messages
    final Async consumerCreation = ctx.async();
    final BiConsumer<ProtonDelivery, Message> eventConsumer = (delivery, message) -> {
        ProtonHelper.released(delivery, true);
    };
    final RecordImpl attachments = new RecordImpl();
    final Source source = mock(Source.class);
    when(source.toString()).thenReturn("event/tenant");
    final ProtonReceiver receiver = mock(ProtonReceiver.class);
    when(receiver.getSource()).thenReturn(source);
    when(receiver.attachments()).thenReturn(attachments);
    when(receiver.getRemoteQoS()).thenReturn(ProtonQoS.AT_LEAST_ONCE);
    when(receiver.open()).then(answer -> {
        consumerCreation.complete();
        return receiver;
    });
    final ProtonConnection con = mock(ProtonConnection.class);
    when(con.createReceiver(anyString())).thenReturn(receiver);
    when(receiver.openHandler(any(Handler.class))).thenAnswer(invocation -> {
        final Handler handler = invocation.getArgument(0);
        handler.handle(Future.succeededFuture(receiver));
        return receiver;
    });
    final ArgumentCaptor<ProtonMessageHandler> messageHandler = ArgumentCaptor.forClass(ProtonMessageHandler.class);
    EventConsumerImpl.create(vertx.getOrCreateContext(), new ClientConfigProperties(), con, "tenant", eventConsumer, open -> {
    }, remoteDetach -> {
    });
    consumerCreation.await();
    verify(receiver).handler(messageHandler.capture());
    // WHEN an event is received
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    final Message msg = mock(Message.class);
    messageHandler.getValue().handle(delivery, msg);
    // THEN the message is released and settled
    verify(delivery).disposition(any(Released.class), eq(Boolean.TRUE));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) After(org.junit.After) BiConsumer(java.util.function.BiConsumer) Message(org.apache.qpid.proton.message.Message) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) Future(io.vertx.core.Future) Mockito(org.mockito.Mockito) Source(org.apache.qpid.proton.amqp.transport.Source) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) ProtonReceiver(io.vertx.proton.ProtonReceiver) Released(org.apache.qpid.proton.amqp.messaging.Released) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Handler(io.vertx.core.Handler) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Source(org.apache.qpid.proton.amqp.transport.Source) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Async(io.vertx.ext.unit.Async) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Test(org.junit.Test)

Example 10 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project hono by eclipse.

the class ConnectionFactoryImpl method handleConnectionAttemptResult.

private void handleConnectionAttemptResult(final AsyncResult<ProtonConnection> conAttempt, final ProtonClientOptions clientOptions, final Handler<AsyncResult<ProtonConnection>> closeHandler, final Handler<ProtonConnection> disconnectHandler, final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {
    if (conAttempt.failed()) {
        logger.debug("can't connect to AMQP 1.0 container [{}://{}:{}]: {}", clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), conAttempt.cause().getMessage());
        connectionResultHandler.handle(Future.failedFuture(conAttempt.cause()));
    } else {
        // at this point the SASL exchange has completed successfully
        logger.debug("connected to AMQP 1.0 container [{}://{}:{}], opening connection ...", clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
        ProtonConnection downstreamConnection = conAttempt.result();
        downstreamConnection.setContainer(String.format("%s-%s", config.getName(), UUID.randomUUID())).setHostname(config.getAmqpHostname()).openHandler(openCon -> {
            if (openCon.succeeded()) {
                logger.debug("connection to container [{}] at [{}://{}:{}] open", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
                downstreamConnection.disconnectHandler(disconnectHandler);
                downstreamConnection.closeHandler(closeHandler);
                connectionResultHandler.handle(Future.succeededFuture(downstreamConnection));
            } else {
                final ErrorCondition error = downstreamConnection.getRemoteCondition();
                if (error == null) {
                    logger.warn("can't open connection to container [{}] at [{}://{}:{}]", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), openCon.cause());
                } else {
                    logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {} -{}", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), error.getCondition(), error.getDescription());
                }
                connectionResultHandler.handle(Future.failedFuture(openCon.cause()));
            }
        }).open();
    }
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) Future(io.vertx.core.Future) Objects(java.util.Objects) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) TrustOptions(io.vertx.core.net.TrustOptions) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) KeyCertOptions(io.vertx.core.net.KeyCertOptions) ProtonSaslExternalImpl(io.vertx.proton.sasl.impl.ProtonSaslExternalImpl) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ProtonSaslPlainImpl(io.vertx.proton.sasl.impl.ProtonSaslPlainImpl) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Strings(org.eclipse.hono.util.Strings) ProtonConnection(io.vertx.proton.ProtonConnection) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition)

Aggregations

ProtonConnection (io.vertx.proton.ProtonConnection)63 ProtonClient (io.vertx.proton.ProtonClient)37 Message (org.apache.qpid.proton.message.Message)36 Handler (io.vertx.core.Handler)35 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)27 ProtonServer (io.vertx.proton.ProtonServer)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)25 AsyncResult (io.vertx.core.AsyncResult)24 TestContext (io.vertx.ext.unit.TestContext)24 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)24 RunWith (org.junit.runner.RunWith)24 Section (org.apache.qpid.proton.amqp.messaging.Section)23 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)21 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)21 ProtonStreams (io.vertx.proton.streams.ProtonStreams)20 Vertx (io.vertx.core.Vertx)19 Symbol (org.apache.qpid.proton.amqp.Symbol)19 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)19