use of io.vertx.ext.unit.TestContext 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);
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testDoStartLoadsCredentials.
/**
* Verifies that credentials are successfully loaded from file during startup.
*
* @param ctx The test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartLoadsCredentials(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);
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();
}));
svc.doStart(startFuture);
// THEN the credentials from the file are loaded
startup.await(2000);
assertRegistered(svc, Constants.DEFAULT_TENANT, "sensor1", CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, ctx);
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testDoStartIgnoresMalformedJson.
/**
* Verifies that the credentials service successfully starts up even if
* the file to read credentials from contains malformed JSON.
*
* @param ctx The vert.x context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartIgnoresMalformedJson(final TestContext ctx) {
// GIVEN a registration 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));
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
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
startup.complete();
}));
svc.doStart(startupTracker);
// THEN startup succeeds
startup.await(2000);
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedRegistrationServiceTest method testDoStartIgnoresMalformedJson.
/**
* Verifies that the registration service successfully starts up even if
* the file to read device information from contains malformed JSON.
*
* @param ctx The vert.x context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartIgnoresMalformedJson(final TestContext ctx) {
// GIVEN a registration service configured to read data from a file
// that contains malformed JSON
when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
doAnswer(invocation -> {
final Buffer data = mock(Buffer.class);
when(data.getBytes()).thenReturn("NO JSON".getBytes(StandardCharsets.UTF_8));
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
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
startup.complete();
}));
registrationService.doStart(startupTracker);
// THEN startup succeeds
startup.await();
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class FileBasedRegistrationServiceTest method testDoStartCreatesFile.
/**
* Verifies that the registration service creates a file for persisting device registration
* 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 registration service configured to persist data to a not yet existing file
props.setSaveToFile(true);
when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.FALSE);
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.succeededFuture());
return null;
}).when(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
doAnswer(invocation -> {
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
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
startup.complete();
}));
registrationService.doStart(startupTracker);
// THEN the file gets created
startup.await();
verify(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
}
Aggregations