use of io.vertx.core.Context 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.core.Context 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());
}));
}
use of io.vertx.core.Context in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageFailsForDisabledTenant.
/**
* Verifies that the adapter does not forward a message published by a device
* if the device belongs to a tenant for which the adapter has been disabled.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryMessageFailsForDisabledTenant(final TestContext ctx) {
// GIVEN an adapter
final MqttServer server = getMqttServer(false);
// which is disabled for tenant "my-tenant"
final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
myTenantConfig.addAdapterConfiguration(new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, ADAPTER_TYPE).put(TenantConstants.FIELD_ENABLED, false));
when(tenantClient.get("my-tenant")).thenReturn(Future.succeededFuture(myTenantConfig));
final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
forceClientMocksToConnected();
final MessageSender sender = mock(MessageSender.class);
when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
// WHEN a device of "my-tenant" publishes a telemetry message
adapter.uploadTelemetryMessage(new MqttContext(mock(MqttPublishMessage.class), mock(MqttEndpoint.class)), "my-tenant", "the-device", Buffer.buffer("test")).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message has not been sent downstream
verify(sender, never()).send(any(Message.class));
// because the tenant is not enabled
ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ClientErrorException) t).getErrorCode());
}));
}
use of io.vertx.core.Context in project hono by eclipse.
the class AbstractHonoClientTest method setUp.
/**
* Sets up the fixture.
*/
@SuppressWarnings("unchecked")
@Before
public void setUp() {
props = new ClientConfigProperties();
context = mock(Context.class);
doAnswer(invocation -> {
final Handler<Void> handler = invocation.getArgument(0);
handler.handle(null);
return null;
}).when(context).runOnContext(any(Handler.class));
}
use of io.vertx.core.Context in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsWithServerErrorExceptionIfSendQueueFull.
/**
* Verifies that the client fails the handler for sending a request message
* with a {@link ServerErrorException} if the link to the peer has no credit left.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestFailsWithServerErrorExceptionIfSendQueueFull(final TestContext ctx) {
// GIVEN a request-response client with a full send queue
when(sender.sendQueueFull()).thenReturn(Boolean.TRUE);
// WHEN sending a request message
final Async sendFailure = ctx.async();
client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
sendFailure.complete();
}));
// THEN the message is not sent and the request result handler is failed
sendFailure.await();
verify(sender, never()).send(any(Message.class));
}
Aggregations