use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class VertxBasedHttpProtocolAdapterTest method setup.
/**
* Sets up the protocol adapter.
*
* @param context
*/
@SuppressWarnings("unchecked")
@BeforeClass
public static final void setup(final TestContext context) {
vertx = Vertx.vertx();
final Async startup = context.async();
tenantClient = mock(HonoClient.class);
when(tenantClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(tenantClient));
messagingClient = mock(HonoClient.class);
when(messagingClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(messagingClient));
registrationClient = mock(HonoClient.class);
when(registrationClient.connect(any(Handler.class))).thenReturn(Future.succeededFuture(registrationClient));
credentialsAuthProvider = mock(HonoClientBasedAuthProvider.class);
when(credentialsAuthProvider.start()).thenReturn(Future.succeededFuture());
when(credentialsAuthProvider.stop()).thenReturn(Future.succeededFuture());
config = new HttpProtocolAdapterProperties();
config.setInsecurePort(0);
config.setAuthenticationRequired(true);
httpAdapter = new VertxBasedHttpProtocolAdapter();
httpAdapter.setConfig(config);
httpAdapter.setTenantServiceClient(tenantClient);
httpAdapter.setHonoMessagingClient(messagingClient);
httpAdapter.setRegistrationServiceClient(registrationClient);
httpAdapter.setCredentialsAuthProvider(credentialsAuthProvider);
vertx.deployVerticle(httpAdapter, context.asyncAssertSuccess(s -> {
startup.complete();
}));
startup.await(1000);
}
use of io.vertx.ext.unit.TestContext 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.ext.unit.TestContext in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageFailsForMissingDeviceId.
/**
* Verifies that the adapter fails to map a topic without a device ID received from an anonymous device.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testOnPublishedMessageFailsForMissingDeviceId(final TestContext ctx) {
givenAnAdapter();
// WHEN an anonymous device publishes a message to a topic that does not contain a device ID
final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT + "/my-tenant");
adapter.onPublishedMessage(context).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message cannot be mapped to an address
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class VertxBasedMqttProtocolAdapterTest method testOnPublishedMessageFailsForMissingTenant.
/**
* Verifies that the adapter fails to map a topic without a tenant ID received from an anonymous device.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testOnPublishedMessageFailsForMissingTenant(final TestContext ctx) {
givenAnAdapter();
// WHEN an anonymous device publishes a message to a topic that does not contain a tenant ID
final MqttContext context = newContext(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
adapter.onPublishedMessage(context).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the message cannot be mapped to an address
}));
}
use of io.vertx.ext.unit.TestContext 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