Search in sources :

Example 26 with Async

use of io.vertx.ext.unit.Async 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 27 with Async

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

the class CredentialsHttpIT method removeCredentials.

/**
 * Removes the credentials that have been added by the test.
 *
 * @param ctx The vert.x test context.
 */
@After
public void removeCredentials(final TestContext ctx) {
    final Async deletion = ctx.async();
    registry.removeAllCredentials(TENANT, deviceId, HttpURLConnection.HTTP_NO_CONTENT).setHandler(attempt -> deletion.complete());
    deletion.await();
}
Also used : Async(io.vertx.ext.unit.Async) After(org.junit.After)

Example 28 with Async

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

the class DeviceRegistrationHttpIT method removeDevice.

/**
 * Removes the device that has been added by the test.
 *
 * @param ctx The vert.x test context.
 */
@After
public void removeDevice(final TestContext ctx) {
    final Async deletion = ctx.async();
    registry.deregisterDevice(TENANT, deviceId).setHandler(attempt -> deletion.complete());
    deletion.await();
}
Also used : Async(io.vertx.ext.unit.Async) After(org.junit.After)

Example 29 with Async

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

the class TenantHttpIT method removeTenant.

/**
 * Removes the credentials that have been added by the test.
 *
 * @param ctx The vert.x test context.
 */
@After
public void removeTenant(final TestContext ctx) {
    final Async deletion = ctx.async();
    registry.removeTenant(tenantId, HttpURLConnection.HTTP_NO_CONTENT).setHandler(attempt -> deletion.complete());
    deletion.await();
}
Also used : Async(io.vertx.ext.unit.Async) After(org.junit.After)

Example 30 with Async

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

the class FileBasedCredentialsServiceTest method testGetCredentialsSucceedsForExistingCredentials.

/**
 * Verifies that the service returns existing credentials.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) {
    register(svc, "tenant", "device", "myId", "myType", new JsonArray(), ctx);
    final Async get = ctx.async();
    svc.get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> {
        assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK));
        assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId"));
        assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType"));
        get.complete();
    }));
    get.await(2000);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Async (io.vertx.ext.unit.Async)156 Test (org.junit.Test)143 TestContext (io.vertx.ext.unit.TestContext)89 Handler (io.vertx.core.Handler)87 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)83 RunWith (org.junit.runner.RunWith)83 Future (io.vertx.core.Future)70 Vertx (io.vertx.core.Vertx)69 Before (org.junit.Before)69 JsonObject (io.vertx.core.json.JsonObject)65 HttpURLConnection (java.net.HttpURLConnection)55 Mockito (org.mockito.Mockito)55 Context (io.vertx.core.Context)44 Message (org.apache.qpid.proton.message.Message)44 Constants (org.eclipse.hono.util.Constants)44 Rule (org.junit.Rule)44 Timeout (org.junit.rules.Timeout)43 Assert.assertThat (org.junit.Assert.assertThat)40 Buffer (io.vertx.core.buffer.Buffer)39 AsyncResult (io.vertx.core.AsyncResult)37