Search in sources :

Example 56 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class HonoClientImplTest method testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently.

/**
 * Verifies that a concurrent request to create a sender fails the given future for tracking the attempt.
 *
 * @param ctx The helper to use for running async tests.
 */
@Test
public void testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently(final TestContext ctx) {
    // GIVEN a client that already tries to create a telemetry sender for "tenant"
    final Async connected = ctx.async();
    client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
    connected.await();
    client.getOrCreateSender("telemetry/tenant", () -> Future.future());
    // WHEN an additional, concurrent attempt is made to create a telemetry sender for "tenant"
    client.getOrCreateSender("telemetry/tenant", () -> {
        ctx.fail("should not create concurrent client");
        return Future.succeededFuture(mock(MessageSender.class));
    }).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the concurrent attempt fails without any attempt being made to create another sender
        ctx.assertTrue(ServerErrorException.class.isInstance(t));
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RequestResponseClient(org.eclipse.hono.client.RequestResponseClient) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) InetAddress(java.net.InetAddress) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) RegistrationClient(org.eclipse.hono.client.RegistrationClient) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Assert.assertTrue(org.junit.Assert.assertTrue) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Example 57 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class RegistrationClientImplTest method testGetRegistrationInfoReturnsValueFromCache.

/**
 * Verifies that registration information is taken from cache.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetRegistrationInfoReturnsValueFromCache(final TestContext ctx) {
    // GIVEN an adapter with a cache containing a registration assertion
    // response for "device"
    client.setResponseCache(cache);
    final JsonObject registrationAssertion = newRegistrationAssertionResult();
    final RegistrationResult regResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion);
    when(cache.get(eq(TriTuple.of("assert", "device", "gateway")))).thenReturn(regResult);
    // WHEN getting registration information
    client.assertRegistration("device", "gateway").setHandler(ctx.asyncAssertSuccess(result -> {
        // THEN the registration information is read from the cache
        ctx.assertEquals(registrationAssertion, result);
        verify(sender, never()).send(any(Message.class));
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) 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) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) 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) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Date(java.sql.Date) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Jwts(io.jsonwebtoken.Jwts) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 58 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class RegistrationClientImplTest method testAssertRegistrationAddsResponseToCacheOnCacheMiss.

/**
 * Verifies that on a cache miss the adapter retrieves registration information
 * from the Device Registration service and puts it to the cache.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testAssertRegistrationAddsResponseToCacheOnCacheMiss(final TestContext ctx) {
    // GIVEN an adapter with an empty cache
    client.setResponseCache(cache);
    // WHEN getting registration information
    final Async assertion = ctx.async();
    client.assertRegistration("device").setHandler(ctx.asyncAssertSuccess(result -> assertion.complete()));
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    final JsonObject registrationAssertion = newRegistrationAssertionResult();
    final Message response = ProtonHelper.message(registrationAssertion.encode());
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
    MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60));
    response.setCorrelationId(messageCaptor.getValue().getMessageId());
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
    // THEN the registration information has been added to the cache
    verify(cache).put(eq(TriTuple.of("assert", "device", null)), any(RegistrationResult.class), any(Duration.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) 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) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) 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) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Date(java.sql.Date) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Jwts(io.jsonwebtoken.Jwts) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) Duration(java.time.Duration) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 59 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnfailureToConnect.

/**
 * Verifies that the given result handler is invoked if a connection attempt fails.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testConnectInvokesHandlerOnfailureToConnect(final TestContext ctx) {
    // GIVEN a factory configured to connect to a non-existing server
    ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    // WHEN trying to connect to the server
    final Async handlerInvocation = ctx.async();
    ProtonClientOptions options = new ProtonClientOptions().setConnectTimeout(100);
    factory.connect(options, null, null, ctx.asyncAssertFailure(t -> {
        handlerInvocation.complete();
    }));
    // THEN the connection attempt fails and the given handler is invoked
    handlerInvocation.await(2000);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Assert(org.junit.Assert) Handler(io.vertx.core.Handler) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Example 60 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeTest method testSendPing.

@Test
public void testSendPing(TestContext context) {
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    eventHandler = event -> {
        if (event.type() == BridgeEventType.SOCKET_PING) {
            async.complete();
        }
    };
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        socket.handler(buff -> {
        });
        FrameHelper.sendFrame("register", "echo", null, socket);
        FrameHelper.sendFrame("ping", socket);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

TestContext (io.vertx.ext.unit.TestContext)148 Test (org.junit.Test)147 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)141 RunWith (org.junit.runner.RunWith)141 Async (io.vertx.ext.unit.Async)123 Future (io.vertx.core.Future)121 Handler (io.vertx.core.Handler)112 Vertx (io.vertx.core.Vertx)103 HttpURLConnection (java.net.HttpURLConnection)100 Before (org.junit.Before)97 Timeout (org.junit.rules.Timeout)95 JsonObject (io.vertx.core.json.JsonObject)91 Rule (org.junit.Rule)83 Mockito (org.mockito.Mockito)74 Constants (org.eclipse.hono.util.Constants)68 Assert.assertThat (org.junit.Assert.assertThat)62 Context (io.vertx.core.Context)57 CoreMatchers.is (org.hamcrest.CoreMatchers.is)54 AsyncResult (io.vertx.core.AsyncResult)53 Buffer (io.vertx.core.buffer.Buffer)52