Search in sources :

Example 16 with TestContext

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

the class HttpTestBase method testUploadMessageFailsForDisabledTenant.

/**
 * Verifies that the HTTP adapter rejects messages from a device
 * that belongs to a tenant for which the HTTP adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testUploadMessageFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_HTTP).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    final Async setup = ctx.async();
    helper.registry.addTenant(JsonObject.mapFrom(tenant)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
    setup.await();
    // WHEN a device that belongs to the tenant uploads a message
    send(tenantId, deviceId, Buffer.buffer("hello")).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the message gets rejected by the HTTP adapter
        ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) TenantObject(org.eclipse.hono.util.TenantObject) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Example 17 with TestContext

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

the class TelemetryMqttIT method testConnectFailsForDisabledTenant.

/**
 * Verifies that the adapter rejects connection attempts from devices
 * that belong to a tenant for which the adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testConnectFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final String password = "secret";
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_MQTT).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> {
        final Future<MqttConnAckMessage> result = Future.future();
        final MqttClientOptions options = new MqttClientOptions().setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
        mqttClient = MqttClient.create(VERTX, options);
        // WHEN a device that belongs to the tenant tries to connect to the adapter
        mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
        return result;
    }).setHandler(ctx.asyncAssertFailure(t -> {
    // THEN the connection attempt gets rejected
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) MessageConsumer(org.eclipse.hono.client.MessageConsumer) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Consumer(java.util.function.Consumer) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) Buffer(io.vertx.core.buffer.Buffer) MqttClient(io.vertx.mqtt.MqttClient) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) TenantObject(org.eclipse.hono.util.TenantObject) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) Test(org.junit.Test)

Example 18 with TestContext

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

the class CredentialsHttpIT method testGetCredentialsForDeviceRegardlessOfType.

/**
 * Verifies that the service returns all credentials registered for a given device regardless of type.
 * <p>
 * The returned JsonObject must consist of the total number of entries and contain all previously added credentials
 * in the provided JsonArray that is found under the key of the endpoint {@link CredentialsConstants#CREDENTIALS_ENDPOINT}.
 *
 * @param context The vert.x test context.
 * @throws InterruptedException if registration of credentials is interrupted.
 */
@Test
public void testGetCredentialsForDeviceRegardlessOfType(final TestContext context) throws InterruptedException {
    final String pskAuthId = getRandomAuthId(TEST_AUTH_ID);
    final List<JsonObject> credentialsToAdd = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        final JsonObject requestBody = newPskCredentials(deviceId, pskAuthId);
        requestBody.put(CredentialsConstants.FIELD_TYPE, "type" + i);
        credentialsToAdd.add(requestBody);
    }
    addMultipleCredentials(credentialsToAdd).compose(ar -> registry.getCredentials(TENANT, deviceId)).setHandler(context.asyncAssertSuccess(b -> {
        assertResponseBodyContainsAllCredentials(context, b.toJsonObject(), credentialsToAdd);
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) After(org.junit.After) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Rule(org.junit.Rule) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 19 with TestContext

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

the class CredentialsHttpIT method testUpdateCredentialsFailsForNonMatchingAuthIdInPayload.

/**
 * Verifies that the service rejects an update request for credentials containing a different authentication
 * identifier.
 *
 * @param context The vert.x test context.
 */
@Test
public void testUpdateCredentialsFailsForNonMatchingAuthIdInPayload(final TestContext context) {
    final JsonObject altered = hashedPasswordCredentials.copy().put(CredentialsConstants.FIELD_AUTH_ID, "non-matching-auth-id");
    registry.addCredentials(TENANT, hashedPasswordCredentials).compose(ar -> {
        return registry.updateCredentials(TENANT, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, altered, HttpURLConnection.HTTP_BAD_REQUEST);
    }).setHandler(context.asyncAssertSuccess());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) After(org.junit.After) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Rule(org.junit.Rule) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) CredentialsObject(org.eclipse.hono.util.CredentialsObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 20 with TestContext

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

the class DeviceRegistrationHttpIT method testUpdateDeviceSucceeds.

/**
 * Verifies that the registration information provided when updating
 * a device replaces the existing information.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateDeviceSucceeds(final TestContext ctx) {
    final JsonObject originalData = new JsonObject().put("key1", "value1").put("key2", "value2").put(RegistrationConstants.FIELD_ENABLED, Boolean.TRUE);
    final JsonObject updatedData = new JsonObject().put("newKey1", "newValue1").put(RegistrationConstants.FIELD_ENABLED, Boolean.FALSE);
    registry.registerDevice(TENANT, deviceId, originalData).compose(ok -> registry.updateDevice(TENANT, deviceId, updatedData)).compose(ok -> registry.getRegistrationInfo(TENANT, deviceId)).compose(info -> {
        assertRegistrationInformation(ctx, info.toJsonObject(), deviceId, updatedData);
        return Future.succeededFuture();
    }).setHandler(ctx.asyncAssertSuccess());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) Rule(org.junit.Rule) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) After(org.junit.After) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) JsonObject(io.vertx.core.json.JsonObject) 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