use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedTenantServiceTest method testUpdateTenantsFailsIfModificationIsDisabled.
/**
* Verifies that the <em>modificationEnabled</em> property prevents updating an existing entry.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUpdateTenantsFailsIfModificationIsDisabled(final TestContext ctx) {
// GIVEN a service containing a set of tenants
// that has been configured to not allow modification of entries
props.setModificationEnabled(false);
// WHEN trying to update the tenant
svc.update("tenant", new JsonObject(), ctx.asyncAssertSuccess(s -> {
// THEN the update fails
ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, s.getStatus());
}));
}
use of io.vertx.ext.unit.TestContext 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);
}
use of io.vertx.ext.unit.TestContext 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);
}
use of io.vertx.ext.unit.TestContext 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));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedTenantServiceTest method testAddTenantRefusesDuplicates.
/**
* Verifies that tenants cannot be added several times.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAddTenantRefusesDuplicates(final TestContext ctx) {
final Async countDown = ctx.async();
addTenant(svc, ctx, countDown, "tenant");
countDown.await();
svc.add("tenant", buildTenantPayload("tenant"), ctx.asyncAssertSuccess(s -> {
ctx.assertEquals(HttpURLConnection.HTTP_CONFLICT, s.getStatus());
}));
}
Aggregations