Search in sources :

Example 1 with ProtonClientOptions

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

the class StandaloneEventApiTest method prepareHonoServer.

/**
 * Sets up Hono Messaging service.
 *
 * @param ctx The test context.
 */
@BeforeClass
public static void prepareHonoServer(final TestContext ctx) {
    vertx = Vertx.vertx();
    downstreamAdapter = new MessageDiscardingDownstreamAdapter(vertx);
    final HonoMessagingConfigProperties configProperties = new HonoMessagingConfigProperties();
    configProperties.setInsecurePort(0);
    final EventEndpoint eventEndpoint = new EventEndpoint(vertx);
    eventEndpoint.setMetrics(mock(MessagingMetrics.class));
    eventEndpoint.setEventAdapter(downstreamAdapter);
    eventEndpoint.setRegistrationAssertionValidator(assertionHelper);
    eventEndpoint.setConfiguration(configProperties);
    server = new HonoMessaging();
    server.setSaslAuthenticatorFactory(new HonoSaslAuthenticatorFactory(TestSupport.createAuthenticationService(createUser())));
    server.setConfig(configProperties);
    server.addEndpoint(eventEndpoint);
    Future<String> serverTracker = Future.future();
    vertx.deployVerticle(server, serverTracker.completer());
    serverTracker.compose(s -> {
        final ClientConfigProperties clientProps = new ClientConfigProperties();
        clientProps.setName("test");
        clientProps.setHost(server.getInsecurePortBindAddress());
        clientProps.setPort(server.getInsecurePort());
        clientProps.setUsername(USER);
        clientProps.setPassword(PWD);
        client = new HonoClientImpl(vertx, clientProps);
        return client.connect(new ProtonClientOptions());
    }).setHandler(ctx.asyncAssertSuccess());
}
Also used : EventEndpoint(org.eclipse.hono.event.impl.EventEndpoint) HonoSaslAuthenticatorFactory(org.eclipse.hono.service.auth.HonoSaslAuthenticatorFactory) TestContext(io.vertx.ext.unit.TestContext) AuthoritiesImpl(org.eclipse.hono.auth.AuthoritiesImpl) HonoUserAdapter(org.eclipse.hono.auth.HonoUserAdapter) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) HonoUser(org.eclipse.hono.auth.HonoUser) Constants(org.eclipse.hono.util.Constants) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) MessageSender(org.eclipse.hono.client.MessageSender) Message(org.apache.qpid.proton.message.Message) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Activity(org.eclipse.hono.auth.Activity) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TestSupport(org.eclipse.hono.TestSupport) Authorities(org.eclipse.hono.auth.Authorities) HonoSaslAuthenticatorFactory(org.eclipse.hono.service.auth.HonoSaslAuthenticatorFactory) Mockito.mock(org.mockito.Mockito.mock) EventEndpoint(org.eclipse.hono.event.impl.EventEndpoint) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) BeforeClass(org.junit.BeforeClass)

Example 2 with ProtonClientOptions

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

the class ClientTestBase method connect.

/**
 * Sets up the environment:
 * <ol>
 * <li>connect to the AMQP messaging network</li>
 * <li>connects to the Hono Server</li>
 * <li>connects to the Hono Device Registry</li>
 * <li>creates a RegistrationClient for TEST_TENANT_ID</li>
 * <li>creates a MessageSender for TEST_TENANT_ID</li>
 * </ul>
 *
 * @param ctx The test context
 */
@Before
public void connect(final TestContext ctx) {
    final ClientConfigProperties downstreamProps = new ClientConfigProperties();
    downstreamProps.setHost(IntegrationTestSupport.DOWNSTREAM_HOST);
    downstreamProps.setPort(IntegrationTestSupport.DOWNSTREAM_PORT);
    downstreamProps.setPathSeparator(IntegrationTestSupport.PATH_SEPARATOR);
    downstreamProps.setUsername(IntegrationTestSupport.RESTRICTED_CONSUMER_NAME);
    downstreamProps.setPassword(IntegrationTestSupport.RESTRICTED_CONSUMER_PWD);
    downstreamClient = new HonoClientImpl(vertx, ConnectionFactoryBuilder.newBuilder(downstreamProps).vertx(vertx).build(), downstreamProps);
    final ClientConfigProperties honoProps = new ClientConfigProperties();
    honoProps.setHost(IntegrationTestSupport.HONO_HOST);
    honoProps.setPort(IntegrationTestSupport.HONO_PORT);
    honoProps.setUsername(IntegrationTestSupport.HONO_USER);
    honoProps.setPassword(IntegrationTestSupport.HONO_PWD);
    honoClient = new HonoClientImpl(vertx, ConnectionFactoryBuilder.newBuilder(honoProps).vertx(vertx).build(), honoProps);
    final ClientConfigProperties registryProps = new ClientConfigProperties();
    registryProps.setHost(IntegrationTestSupport.HONO_DEVICEREGISTRY_HOST);
    registryProps.setPort(IntegrationTestSupport.HONO_DEVICEREGISTRY_AMQP_PORT);
    registryProps.setUsername(IntegrationTestSupport.HONO_USER);
    registryProps.setPassword(IntegrationTestSupport.HONO_PWD);
    honoDeviceRegistryClient = new HonoClientImpl(vertx, ConnectionFactoryBuilder.newBuilder(registryProps).vertx(vertx).build(), registryProps);
    final ProtonClientOptions options = new ProtonClientOptions();
    // connect to AMQP messaging network
    final Future<HonoClient> downstreamTracker = downstreamClient.connect(options);
    // create sender
    final Future<MessageSender> senderTracker = honoClient.connect(options).compose(connectedClient -> createProducer(TEST_TENANT_ID)).map(s -> {
        sender = s;
        return s;
    });
    // create registration client
    final Future<RegistrationClient> registrationClientTracker = honoDeviceRegistryClient.connect(options).compose(connectedClient -> connectedClient.getOrCreateRegistrationClient(TEST_TENANT_ID)).map(c -> {
        registrationClient = c;
        return c;
    });
    CompositeFuture.all(downstreamTracker, senderTracker, registrationClientTracker).setHandler(ctx.asyncAssertSuccess(s -> {
        LOGGER.info("connections to Hono server, Hono device registry and AMQP messaging network established");
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) CompositeFuture(io.vertx.core.CompositeFuture) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectionFactoryBuilder(org.eclipse.hono.connection.ConnectionFactoryImpl.ConnectionFactoryBuilder) MessageSender(org.eclipse.hono.client.MessageSender) After(org.junit.After) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) HonoClient(org.eclipse.hono.client.HonoClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) HonoClient(org.eclipse.hono.client.HonoClient) MessageSender(org.eclipse.hono.client.MessageSender) RegistrationClient(org.eclipse.hono.client.RegistrationClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Before(org.junit.Before)

Example 3 with ProtonClientOptions

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

the class TenantAmqpIT method prepareDeviceRegistry.

/**
 * Starts the device registry, connects a client and provides a tenant API client.
 *
 * @param ctx The vert.x test context.
 */
@BeforeClass
public static void prepareDeviceRegistry(final TestContext ctx) {
    client = DeviceRegistryAmqpTestSupport.prepareDeviceRegistryClient(vertx);
    client.connect(new ProtonClientOptions()).compose(c -> c.getOrCreateTenantClient()).setHandler(ctx.asyncAssertSuccess(r -> {
        tenantClient = r;
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) TenantClient(org.eclipse.hono.client.TenantClient) Timeout(org.junit.rules.Timeout) HonoClient(org.eclipse.hono.client.HonoClient) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) BeforeClass(org.junit.BeforeClass)

Example 4 with ProtonClientOptions

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

the class HonoClientImplTest method testClientDoesNotTriggerReconnectionAfterShutdown.

/**
 * Verifies that the client does not try to re-connect to a server instance if the client was shutdown.
 *
 * @param ctx The test context.
 */
@Test
public void testClientDoesNotTriggerReconnectionAfterShutdown(final TestContext ctx) {
    // GIVEN a client that tries to connect to a server but does not succeed
    final Async connectionHandlerInvocation = ctx.async();
    connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con, 0, Integer.MAX_VALUE);
    client = new HonoClientImpl(vertx, connectionFactory, props);
    client.connect(new ProtonClientOptions().setReconnectAttempts(1)).setHandler(ctx.asyncAssertFailure(cause -> connectionHandlerInvocation.complete()));
    // WHEN client gets shutdown
    client.shutdown();
    // THEN reconnect gets stopped, i.e. connection fails
    connectionHandlerInvocation.await();
}
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 5 with ProtonClientOptions

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

the class HonoClientImplTest method testGetOrCreateRequestResponseClientFailsIfInvokedConcurrently.

/**
 * Verifies that a concurrent request to create a request-response client fails the given
 * future for tracking the attempt.
 *
 * @param ctx The helper to use for running async tests.
 */
@Test
public void testGetOrCreateRequestResponseClientFailsIfInvokedConcurrently(final TestContext ctx) {
    // GIVEN a client that already tries to create a registration client for "tenant"
    final Async connected = ctx.async();
    client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
    connected.await();
    client.getOrCreateRequestResponseClient("registration/tenant", () -> Future.future(), result -> {
    });
    // WHEN an additional, concurrent attempt is made to create a client for "tenant"
    client.getOrCreateRequestResponseClient("registration/tenant", () -> {
        ctx.fail("should not create concurrent client");
        return Future.succeededFuture(mock(RegistrationClient.class));
    }, ctx.<RequestResponseClient>asyncAssertFailure(t -> {
        // THEN the concurrent attempt fails without any attempt being made to create another client
        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) RegistrationClient(org.eclipse.hono.client.RegistrationClient) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Aggregations

ProtonClientOptions (io.vertx.proton.ProtonClientOptions)27 Test (org.junit.Test)20 Vertx (io.vertx.core.Vertx)19 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)19 Handler (io.vertx.core.Handler)17 Future (io.vertx.core.Future)16 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 Constants (org.eclipse.hono.util.Constants)15 RunWith (org.junit.runner.RunWith)15 TimeUnit (java.util.concurrent.TimeUnit)14 MessageSender (org.eclipse.hono.client.MessageSender)14 BeforeClass (org.junit.BeforeClass)14 ProtonConnection (io.vertx.proton.ProtonConnection)13 HttpURLConnection (java.net.HttpURLConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AsyncResult (io.vertx.core.AsyncResult)12 Async (io.vertx.ext.unit.Async)12 ClientErrorException (org.eclipse.hono.client.ClientErrorException)12 RegistrationClient (org.eclipse.hono.client.RegistrationClient)12