use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedRegistrationServiceTest method testDoStartLoadsDeviceIdentities.
/**
* Verifies that device identities are successfully loaded from file during startup.
*
* @param ctx The test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartLoadsDeviceIdentities(final TestContext ctx) {
// GIVEN a service configured with a file name
when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
doAnswer(invocation -> {
final Buffer data = DeviceRegistryTestUtils.readFile(FILE_NAME);
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
Async startup = ctx.async();
Future<Void> startFuture = Future.future();
startFuture.setHandler(ctx.asyncAssertSuccess(s -> {
startup.complete();
}));
registrationService.doStart(startFuture);
// THEN the device identities from the file are loaded
startup.await();
registrationService.getDevice(TENANT, DEVICE, ctx.asyncAssertSuccess());
registrationService.getDevice(TENANT, "4712", ctx.asyncAssertSuccess(result -> {
final JsonObject data = result.getPayload().getJsonObject(RegistrationConstants.FIELD_DATA);
ctx.assertEquals(data.getString(FileBasedRegistrationService.PROPERTY_VIA), GW);
}));
registrationService.getDevice(TENANT, GW, ctx.asyncAssertSuccess());
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedRegistrationServiceTest method testContentsNotSavedOnShutdownIfSavingIfDisabled.
/**
* 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 during shutdown.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testContentsNotSavedOnShutdownIfSavingIfDisabled(final TestContext ctx) {
// GIVEN a registration service configured to not persist data
props.setSaveToFile(false);
when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.failedFuture("malformed data"));
return null;
}).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
startup.complete();
}));
registrationService.doStart(startupTracker);
startup.await();
// WHEN adding a device
registrationService.addDevice(TENANT, DEVICE, new JsonObject());
// and shutting down the service
Async shutdown = ctx.async();
Future<Void> shutdownTracker = Future.future();
shutdownTracker.setHandler(ctx.asyncAssertSuccess(done -> {
shutdown.complete();
}));
registrationService.doStop(shutdownTracker);
// THEN no data has been written to the file system
shutdown.await();
verify(fileSystem, never()).createFile(eq(props.getFilename()), any(Handler.class));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedRegistrationServiceTest method testDuplicateRegistrationFails.
/**
* Verifies that the registry returns 409 when trying to register a device twice.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDuplicateRegistrationFails(final TestContext ctx) {
final EventBusMessage createRequest = newRequest(RegistrationConstants.ACTION_REGISTER, TENANT);
registrationService.processRequest(createRequest).map(response -> {
ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus());
return response;
}).compose(ok -> registrationService.processRequest(createRequest)).setHandler(ctx.asyncAssertSuccess(response -> {
ctx.assertEquals(HttpURLConnection.HTTP_CONFLICT, response.getStatus());
}));
}
use of io.vertx.ext.unit.TestContext 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));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedTenantServiceTest method testRemoveTenantsSucceeds.
/**
* Verifies that the service removes tenants for a given tenantId.
*
* @param ctx The vert.x test context.
*/
@Test
public void testRemoveTenantsSucceeds(final TestContext ctx) {
final Async add = ctx.async();
addTenant(svc, ctx, add, "tenant");
add.await();
svc.remove("tenant", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
assertTenantDoesNotExist(svc, "tenant", ctx);
}));
}
Aggregations