Search in sources :

Example 1 with ProtonBasedApplicationClient

use of org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient in project hono by eclipse.

the class ApplicationClientIT method testConnectFailsWithClientErrorForNoSASLMechanismException.

/**
 * Verifies that a connection attempt where no credentials are given fails after two retries with a
 * ClientErrorException with status code 401.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testConnectFailsWithClientErrorForNoSASLMechanismException(final VertxTestContext ctx) {
    // GIVEN a client that is configured with no username and password
    final ClientConfigProperties downstreamProps = new ClientConfigProperties();
    downstreamProps.setHost(IntegrationTestSupport.DOWNSTREAM_HOST);
    downstreamProps.setPort(IntegrationTestSupport.DOWNSTREAM_PORT);
    downstreamProps.setReconnectAttempts(2);
    client = new ProtonBasedApplicationClient(HonoConnection.newConnection(vertx, downstreamProps));
    // WHEN the client tries to connect
    client.connect().onComplete(ctx.failing(t -> {
        // THEN the connection attempt fails due to lack of authorization
        ctx.verify(() -> {
            assertThat(t).isInstanceOf(ClientErrorException.class);
            assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAUTHORIZED);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Vertx(io.vertx.core.Vertx) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) HonoConnection(org.eclipse.hono.client.HonoConnection) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Test(org.junit.jupiter.api.Test)

Example 2 with ProtonBasedApplicationClient

use of org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient in project hono by eclipse.

the class IntegrationTestSupport method init.

/**
 * Connects to the AMQP 1.0 Messaging Network.
 * <p>
 * Also creates an HTTP client for accessing the Device Registry.
 *
 * @param downstreamProps The properties for connecting to the AMQP Messaging
 *                           Network.
 * @return A future indicating the outcome of the operation.
 */
public Future<Void> init(final ClientConfigProperties downstreamProps) {
    initRegistryClient();
    protonBasedHonoConnection = HonoConnection.newConnection(vertx, downstreamProps);
    applicationClient = new ProtonBasedApplicationClient(protonBasedHonoConnection);
    return applicationClient.start().onSuccess(connected -> LOGGER.info("connected to AMQP Messaging Network [{}:{}]", downstreamProps.getHost(), downstreamProps.getPort()));
}
Also used : ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient)

Example 3 with ProtonBasedApplicationClient

use of org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient in project hono by eclipse.

the class HonoExampleApplicationBase method createAmqpApplicationClient.

/**
 * The consumer needs one connection to the AMQP 1.0 messaging network from which it can consume data.
 * <p>
 * The client for receiving data is instantiated here.
 * <p>
 * NB: if you want to integrate this code with your own software, it might be necessary to copy the trust
 * store to your project as well and adopt the file path.
 */
private ApplicationClient<? extends MessageContext> createAmqpApplicationClient() {
    final ClientConfigProperties props = new ClientConfigProperties();
    props.setLinkEstablishmentTimeout(5000L);
    props.setHost(HonoExampleConstants.HONO_MESSAGING_HOST);
    props.setPort(port);
    if (!USE_PLAIN_CONNECTION) {
        props.setUsername(HONO_CLIENT_USER);
        props.setPassword(HONO_CLIENT_PASSWORD);
        props.setTlsEnabled(true);
        props.setServerRole("AMQP Messaging Network");
        props.setTrustStorePath("target/config/hono-demo-certs-jar/trusted-certs.pem");
        props.setHostnameVerificationRequired(false);
    }
    return new ProtonBasedApplicationClient(HonoConnection.newConnection(vertx, props));
}
Also used : ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties)

Example 4 with ProtonBasedApplicationClient

use of org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient in project hono by eclipse.

the class ApplicationClientIT method testConnectFailsWithClientErrorIfTlsHandshakeFails.

/**
 * Verifies that a connection attempt where the TLS handshake cannot be finished successfully fails after two
 * retries with a ClientErrorException with status code 400.
 *
 * @param ctx The vert.x test context.
 */
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testConnectFailsWithClientErrorIfTlsHandshakeFails(final VertxTestContext ctx) {
    // GIVEN a client that is configured to try to connect using an unsupported TLS version
    final ClientConfigProperties downstreamProps = new ClientConfigProperties();
    downstreamProps.setHost(IntegrationTestSupport.DOWNSTREAM_HOST);
    downstreamProps.setPort(IntegrationTestSupport.DOWNSTREAM_PORT);
    downstreamProps.setTlsEnabled(true);
    downstreamProps.setSecureProtocols(List.of("TLSv1.1"));
    downstreamProps.setReconnectAttempts(2);
    client = new ProtonBasedApplicationClient(HonoConnection.newConnection(vertx, downstreamProps));
    // WHEN the client tries to connect
    client.connect().onComplete(ctx.failing(t -> {
        // THEN the connection attempt fails due to lack of authorization
        ctx.verify(() -> {
            assertThat(t).isInstanceOf(ClientErrorException.class);
            assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_BAD_REQUEST);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Vertx(io.vertx.core.Vertx) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) HonoConnection(org.eclipse.hono.client.HonoConnection) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Aggregations

ProtonBasedApplicationClient (org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient)4 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Vertx (io.vertx.core.Vertx)2 Timeout (io.vertx.junit5.Timeout)2 VertxExtension (io.vertx.junit5.VertxExtension)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 HttpURLConnection (java.net.HttpURLConnection)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 AmqpApplicationClient (org.eclipse.hono.application.client.amqp.AmqpApplicationClient)2 ClientErrorException (org.eclipse.hono.client.ClientErrorException)2 HonoConnection (org.eclipse.hono.client.HonoConnection)2 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Test (org.junit.jupiter.api.Test)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 CommonKafkaClientConfigProperties (org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties)1