Search in sources :

Example 36 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testPeriodicSafeJobIsNotScheduledIfSavingIfDisabled.

/**
 * Verifies that setting the <em>saveToFile</em> configuration property to <em>false</em> prevents
 * the registration service to write its content to the file system periodically.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPeriodicSafeJobIsNotScheduledIfSavingIfDisabled(final TestContext ctx) {
    props.setSaveToFile(false);
    when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
    doAnswer(invocation -> {
        Handler handler = invocation.getArgument(1);
        handler.handle(Future.failedFuture("malformed file"));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    Async startup = ctx.async();
    Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(done -> {
        startup.complete();
    }));
    registrationService.doStart(startupTracker);
    startup.await();
    verify(vertx, never()).setPeriodic(anyLong(), any(Handler.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) ArgumentMatchers(org.mockito.ArgumentMatchers) RunWith(org.junit.runner.RunWith) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) EventBus(io.vertx.core.eventbus.EventBus) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) Assert.assertFalse(org.junit.Assert.assertFalse) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 37 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class FileBasedTenantServiceTest method testLoadTenantsCanReadOutputOfSaveToFile.

/**
 * Verifies that the tenants file written by the registry when persisting the contents can
 * be loaded in again.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testLoadTenantsCanReadOutputOfSaveToFile(final TestContext ctx) {
    // GIVEN a service configured to persist tenants to file
    // that contains some tenants
    props.setFilename(FILE_NAME);
    props.setSaveToFile(true);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
    final Async countDown = ctx.async(2);
    addTenant(svc, ctx, countDown, Constants.DEFAULT_TENANT);
    addTenant(svc, ctx, countDown, "OTHER_TENANT");
    countDown.await();
    // WHEN saving the content to the file and clearing the tenant registry
    final Async write = ctx.async();
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(2);
        handler.handle(Future.succeededFuture());
        write.complete();
        return null;
    }).when(fileSystem).writeFile(eq(FILE_NAME), any(Buffer.class), any(Handler.class));
    svc.saveToFile();
    write.await();
    final ArgumentCaptor<Buffer> buffer = ArgumentCaptor.forClass(Buffer.class);
    verify(fileSystem).writeFile(eq(FILE_NAME), buffer.capture(), any(Handler.class));
    svc.clear();
    assertTenantDoesNotExist(svc, Constants.DEFAULT_TENANT, ctx);
    // THEN the tenants can be loaded back in from the file
    final Async read = ctx.async();
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(buffer.getValue()));
        read.complete();
        return null;
    }).when(fileSystem).readFile(eq(FILE_NAME), any(Handler.class));
    svc.loadTenantData();
    read.await();
    assertTenantExists(svc, Constants.DEFAULT_TENANT, ctx);
    assertTenantExists(svc, "OTHER_TENANT", ctx);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 38 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class FileBasedTenantServiceTest method testDoStartIgnoresMalformedJson.

/**
 * Verifies that the tenant service successfully starts up even if
 * the file to read tenants from contains malformed JSON.
 *
 * @param ctx The vert.x context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartIgnoresMalformedJson(final TestContext ctx) {
    // GIVEN a tenant service configured to read data from a file
    // that contains malformed JSON
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
    doAnswer(invocation -> {
        final Buffer data = mock(Buffer.class);
        when(data.getBytes()).thenReturn("NO JSON".getBytes(StandardCharsets.UTF_8));
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(data));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN starting the service
    final Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
    // THEN startup succeeds
    }));
    svc.doStart(startupTracker);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) TenantService(org.eclipse.hono.service.tenant.TenantService) Timeout(org.junit.rules.Timeout) 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) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 39 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class FileBasedTenantServiceTest method testDoStartLoadsTenants.

/**
 * Verifies that tenants are successfully loaded from file during startup.
 *
 * @param ctx The test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartLoadsTenants(final TestContext ctx) {
    // GIVEN a service configured with a file name
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
    doAnswer(invocation -> {
        final Buffer data = DeviceRegistryTestUtils.readFile(FILE_NAME);
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(data));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN the service is started
    final Async startup = ctx.async();
    final Future<Void> startFuture = Future.future();
    startFuture.setHandler(ctx.asyncAssertSuccess(s -> {
        startup.complete();
    }));
    svc.doStart(startFuture);
    // THEN the credentials from the file are loaded
    startup.await();
    assertTenantExists(svc, Constants.DEFAULT_TENANT, ctx);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) TenantService(org.eclipse.hono.service.tenant.TenantService) Timeout(org.junit.rules.Timeout) 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) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 40 with Handler

use of io.vertx.core.Handler in project hono by eclipse.

the class FileBasedTenantServiceTest method testDoStartCreatesFile.

/**
 * Verifies that the tenant service creates a file for persisting tenants
 * data if it does not exist yet during startup.
 *
 * @param ctx The vert.x context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartCreatesFile(final TestContext ctx) {
    // GIVEN a tenant service configured to persist data to a not yet existing file
    props.setSaveToFile(true);
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.FALSE);
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture());
        return null;
    }).when(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(1);
        handler.handle(Future.failedFuture("malformed file"));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN starting the service
    final Async startup = ctx.async();
    final Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
        startup.complete();
    }));
    svc.doStart(startupTracker);
    // THEN the file gets created
    startup.await();
    verify(fileSystem).createFile(eq(FILE_NAME), any(Handler.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) TenantService(org.eclipse.hono.service.tenant.TenantService) Timeout(org.junit.rules.Timeout) 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) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

Handler (io.vertx.core.Handler)119 Test (org.junit.Test)78 Future (io.vertx.core.Future)67 Vertx (io.vertx.core.Vertx)59 AsyncResult (io.vertx.core.AsyncResult)57 Context (io.vertx.core.Context)52 Buffer (io.vertx.core.buffer.Buffer)48 Async (io.vertx.ext.unit.Async)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)39 Map (java.util.Map)38 TestContext (io.vertx.ext.unit.TestContext)37 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)36 RunWith (org.junit.runner.RunWith)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 Collections (java.util.Collections)33 JsonObject (io.vertx.core.json.JsonObject)31 HttpURLConnection (java.net.HttpURLConnection)31 Before (org.junit.Before)31 StandardCharsets (java.nio.charset.StandardCharsets)29 TimeUnit (java.util.concurrent.TimeUnit)29