Search in sources :

Example 61 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class DeviceRegistrationApiTests method testAssertRegistrationFailsForDisabledGateway.

/**
 * Verifies that the registry fails a disabled gateway's request to assert a device's registration status with a 403
 * error code.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testAssertRegistrationFailsForDisabledGateway(final VertxTestContext ctx) {
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final String gatewayId = getHelper().getRandomDeviceId(tenantId);
    final Device gateway = new Device();
    gateway.setEnabled(false);
    final Device device = new Device();
    device.setVia(Collections.singletonList(gatewayId));
    getHelper().registry.registerDevice(tenantId, gatewayId, gateway).compose(ok -> getHelper().registry.registerDevice(tenantId, deviceId, device)).compose(r -> getClient().assertRegistration(tenantId, deviceId, gatewayId, NoopSpan.INSTANCE.context())).onComplete(ctx.failing(t -> {
        assertErrorCode(t, HttpURLConnection.HTTP_FORBIDDEN);
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Device(org.eclipse.hono.service.management.device.Device) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) JsonObject(io.vertx.core.json.JsonObject) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) Device(org.eclipse.hono.service.management.device.Device) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 62 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class DeviceRegistrationApiTests method testAssertRegistrationSucceedsForDeviceViaGateway.

/**
 * Verifies that the registry succeeds a request to assert the registration status of a device that connects via an
 * authorized gateway.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testAssertRegistrationSucceedsForDeviceViaGateway(final VertxTestContext ctx) {
    final String gatewayId = getHelper().getRandomDeviceId(tenantId);
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final List<String> via = List.of(gatewayId, "another-gateway");
    final Device device = new Device();
    device.setVia(via);
    getHelper().registry.registerDevice(tenantId, gatewayId).compose(ok -> getHelper().registry.registerDevice(tenantId, deviceId, device)).compose(ok -> getClient().assertRegistration(tenantId, deviceId, gatewayId, NoopSpan.INSTANCE.context())).onComplete(ctx.succeeding(resp -> {
        ctx.verify(() -> {
            assertThat(resp.getDeviceId()).isEqualTo(deviceId);
            assertThat(resp.getAuthorizedGateways()).containsExactlyElementsIn(via);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Device(org.eclipse.hono.service.management.device.Device) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) JsonObject(io.vertx.core.json.JsonObject) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) Device(org.eclipse.hono.service.management.device.Device) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 63 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class TelemetryMqttQoS1IT method testUploadMessagesWithNoConsumerSendsErrors.

/**
 * Verifies that sending a number of telemetry messages to Hono's MQTT adapter while there is no consumer causes
 * corresponding error messages to be published to the client if it is subscribed on the error topic.
 *
 * @param ctx The test context.
 * @throws InterruptedException if the test fails.
 */
@Test
@AssumeMessagingSystem(type = MessagingType.amqp)
public void testUploadMessagesWithNoConsumerSendsErrors(final VertxTestContext ctx) throws InterruptedException {
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final Tenant tenant = new Tenant();
    final VertxTestContext setup = new VertxTestContext();
    helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    // timeout value should be higher than the "hono.messaging.flowLatency" MQTT adapter configuration value
    // so that no timeout doesn't while the MQTT adapter waits for credit after having created the first downstream
    // sender link
    final long publishCompletionTimeout = 1500;
    doTestUploadMessages(ctx, tenantId, connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password), (payload) -> send(tenantId, deviceId, payload, true, null, publishCompletionTimeout).map(String::valueOf), // no consumer created here (future succeeded with null value)
    (messageHandler) -> Future.succeededFuture(), (msg) -> {
        final JsonObject payload = new JsonObject(msg.payload());
        final String correlationId = payload.getString(MessageHelper.SYS_PROPERTY_CORRELATION_ID);
        ctx.verify(() -> {
            assertThat(payload.getInteger("code")).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
            // error message should be the localized NoConsumerException message
            assertThat(payload.getString("message")).isEqualTo(ServiceInvocationException.getLocalizedMessage(NoConsumerException.CLIENT_FACING_MESSAGE_KEY));
            // validate topic segments; example: error//myDeviceId/telemetry/4/503
            final String[] topicSegments = msg.topicName().split("/");
            assertThat(topicSegments.length).isEqualTo(6);
            assertThat(topicSegments[0]).isEqualTo("e");
            assertThat(topicSegments[1]).isEqualTo(tenantId);
            // device
            assertThat(topicSegments[2]).isEmpty();
            assertThat(topicSegments[3]).isEqualTo(TelemetryConstants.TELEMETRY_ENDPOINT_SHORT);
            assertThat(topicSegments[4]).isEqualTo(correlationId);
            assertThat(topicSegments[5]).isEqualTo(Integer.toString(HttpURLConnection.HTTP_UNAVAILABLE));
        });
        return Future.succeededFuture(correlationId);
    }, String.format("e/%s//#", tenantId));
}
Also used : Tenant(org.eclipse.hono.service.management.tenant.Tenant) VertxTestContext(io.vertx.junit5.VertxTestContext) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem)

Example 64 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsJmsIT method testRequestFailsForMissingSubject.

/**
 * Verifies that the service responds with a 400 status to a request that
 * has no subject.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testRequestFailsForMissingSubject(final VertxTestContext ctx) {
    final JsonObject searchCriteria = CredentialsConstants.getSearchCriteria(CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, "device");
    getJmsBasedClient().sendRequest(tenantId, null, null, searchCriteria.toBuffer()).onComplete(ctx.failing(t -> {
        assertErrorCode(t, HttpURLConnection.HTTP_BAD_REQUEST);
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) JmsBasedCredentialsClient(org.eclipse.hono.tests.jms.JmsBasedCredentialsClient) JmsBasedHonoConnection(org.eclipse.hono.tests.jms.JmsBasedHonoConnection) VertxExtension(io.vertx.junit5.VertxExtension) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Buffer(io.vertx.core.buffer.Buffer) BeforeAll(org.junit.jupiter.api.BeforeAll) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) Checkpoint(io.vertx.junit5.Checkpoint) CredentialsAction(org.eclipse.hono.util.CredentialsConstants.CredentialsAction) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 65 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsWithGatewayAutoProvisioning.

/**
 * Verifies when no credentials are found, the properties related to auto-provisioning of gateways are enabled
 * in the corresponding tenant's CA entry and the client context contains a serialized X.509 certificate then
 * a gateway is auto-provisioned. (i.e A gateway is registered and it's corresponding credentials are stored).
 * <p>
 * It also verifies that an auto-provisioning event is successfully sent and the device registration
 * is updated accordingly.
 *
 * @param ctx The vert.x test context.
 * @throws CertificateException if the self signed certificate cannot be created.
 * @throws FileNotFoundException if the self signed certificate cannot be read.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsWithGatewayAutoProvisioning(final VertxTestContext ctx) throws CertificateException, FileNotFoundException {
    // GIVEN a tenant's trusted CA entry with auto-provisioning and auto-provision as gateway enabled
    // while device has not been registered and no credentials are stored yet
    final X509Certificate cert = createCertificate();
    final var tenant = Tenants.createTenantForTrustAnchor(cert);
    tenant.getTrustedCertificateAuthorities().get(0).setAutoProvisioningEnabled(true).setAutoProvisioningAsGatewayEnabled(true);
    testAutoProvisioningSucceeds(ctx, tenant, cert, true, null);
}
Also used : X509Certificate(java.security.cert.X509Certificate) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Aggregations

Timeout (io.vertx.junit5.Timeout)73 VertxTestContext (io.vertx.junit5.VertxTestContext)73 Test (org.junit.jupiter.api.Test)71 TimeUnit (java.util.concurrent.TimeUnit)67 Truth.assertThat (com.google.common.truth.Truth.assertThat)65 Future (io.vertx.core.Future)54 HttpURLConnection (java.net.HttpURLConnection)52 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)49 Handler (io.vertx.core.Handler)47 Buffer (io.vertx.core.buffer.Buffer)46 BeforeEach (org.junit.jupiter.api.BeforeEach)46 Promise (io.vertx.core.Promise)45 Vertx (io.vertx.core.Vertx)37 JsonObject (io.vertx.core.json.JsonObject)37 VertxExtension (io.vertx.junit5.VertxExtension)37 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)37 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)37 Tenant (org.eclipse.hono.service.management.tenant.Tenant)36 MethodSource (org.junit.jupiter.params.provider.MethodSource)35