Search in sources :

Example 51 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class AbstractHonoClientTest method setUp.

/**
 * Sets up the fixture.
 */
@SuppressWarnings("unchecked")
@Before
public void setUp() {
    props = new ClientConfigProperties();
    context = mock(Context.class);
    doAnswer(invocation -> {
        final Handler<Void> handler = invocation.getArgument(0);
        handler.handle(null);
        return null;
    }).when(context).runOnContext(any(Handler.class));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) Handler(io.vertx.core.Handler) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before)

Example 52 with Handler

use of io.vertx.core.Handler 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 53 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsWithServerErrorExceptionIfSendQueueFull.

/**
 * Verifies that the client fails the handler for sending a request message
 * with a {@link ServerErrorException} if the link to the peer has no credit left.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateAndSendRequestFailsWithServerErrorExceptionIfSendQueueFull(final TestContext ctx) {
    // GIVEN a request-response client with a full send queue
    when(sender.sendQueueFull()).thenReturn(Boolean.TRUE);
    // WHEN sending a request message
    final Async sendFailure = ctx.async();
    client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
        ctx.assertTrue(ServerErrorException.class.isInstance(t));
        sendFailure.complete();
    }));
    // THEN the message is not sent and the request result handler is failed
    sendFailure.await();
    verify(sender, never()).send(any(Message.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Target(org.apache.qpid.proton.amqp.transport.Target) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Message(org.apache.qpid.proton.message.Message) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 54 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfReceiverIsNotOpen.

/**
 * Verifies that a response handler is immediately failed with a
 * {@link ServerErrorException} when the receiver link is not open (yet).
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateAndSendRequestFailsIfReceiverIsNotOpen(final TestContext ctx) {
    // GIVEN a client whose sender and receiver are not open
    when(receiver.isOpen()).thenReturn(Boolean.FALSE);
    // WHEN sending a request
    Async requestFailure = ctx.async();
    client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
        ctx.assertTrue(ServerErrorException.class.isInstance(t));
        requestFailure.complete();
    }));
    // THEN the request fails immediately
    requestFailure.await();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Target(org.apache.qpid.proton.amqp.transport.Target) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 55 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfSenderIsNotOpen.

/**
 * Verifies that a response handler is immediately failed with a
 * {@link ServerErrorException} when the sender link is not open (yet).
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateAndSendRequestFailsIfSenderIsNotOpen(final TestContext ctx) {
    // GIVEN a client whose sender and receiver are not open
    when(sender.isOpen()).thenReturn(Boolean.FALSE);
    // WHEN sending a request
    Async requestFailure = ctx.async();
    client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
        ctx.assertTrue(ServerErrorException.class.isInstance(t));
        requestFailure.complete();
    }));
    // THEN the request fails immediately
    requestFailure.await();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Target(org.apache.qpid.proton.amqp.transport.Target) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Handler (io.vertx.core.Handler)119 Test (org.junit.Test)78 Future (io.vertx.core.Future)67 Vertx (io.vertx.core.Vertx)59 AsyncResult (io.vertx.core.AsyncResult)57 Context (io.vertx.core.Context)52 Buffer (io.vertx.core.buffer.Buffer)48 Async (io.vertx.ext.unit.Async)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)39 Map (java.util.Map)38 TestContext (io.vertx.ext.unit.TestContext)37 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)36 RunWith (org.junit.runner.RunWith)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 Collections (java.util.Collections)33 JsonObject (io.vertx.core.json.JsonObject)31 HttpURLConnection (java.net.HttpURLConnection)31 Before (org.junit.Before)31 StandardCharsets (java.nio.charset.StandardCharsets)29 TimeUnit (java.util.concurrent.TimeUnit)29