use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class MqttTestBase method doTestUploadMessages.
/**
* Verifies that a number of messages uploaded to Hono's HTTP adapter can be successfully
* consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @param useShortTopicName Whether to use standard or short topic names
* @throws InterruptedException if the test fails.
*/
public void doTestUploadMessages(final TestContext ctx, boolean useShortTopicName) throws InterruptedException {
final int messagesToSend = 200;
final CountDownLatch received = new CountDownLatch(messagesToSend);
final Async setup = ctx.async();
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final TenantObject tenant = TenantObject.from(tenantId, true);
helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> createConsumer(tenantId, msg -> {
LOGGER.trace("received {}", msg);
assertMessageProperties(ctx, msg);
assertAdditionalMessageProperties(ctx, msg);
received.countDown();
if (received.getCount() % 40 == 0) {
LOGGER.info("messages received: {}", messagesToSend - received.getCount());
}
})).compose(ok -> {
final Future<MqttConnAckMessage> result = Future.future();
final MqttClientOptions options = new MqttClientOptions().setMaxInflightQueue(200).setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
mqttClient = MqttClient.create(VERTX, options);
mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
return result;
}).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
setup.await();
final long start = System.currentTimeMillis();
final AtomicInteger messageCount = new AtomicInteger(0);
final AtomicReference<Async> sendResult = new AtomicReference<>();
mqttClient.publishCompletionHandler(packetId -> {
if (pendingMessages.remove(packetId)) {
sendResult.get().complete();
} else {
LOGGER.info("received PUBACK for unexpected message [id: {}]", packetId);
}
});
while (messageCount.get() < messagesToSend) {
sendResult.set(ctx.async());
send(tenantId, deviceId, Buffer.buffer("hello " + messageCount.getAndIncrement()), useShortTopicName, sendAttempt -> {
if (sendAttempt.failed()) {
LOGGER.debug("error sending message {}", messageCount.get(), sendAttempt.cause());
} else {
pendingMessages.add(sendAttempt.result());
}
});
if (messageCount.get() % 40 == 0) {
LOGGER.info("messages sent: " + messageCount.get());
}
sendResult.get().await();
}
long timeToWait = Math.max(TEST_TIMEOUT - 1000, Math.round(messagesToSend * 1.2));
if (!received.await(timeToWait, TimeUnit.MILLISECONDS)) {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
ctx.fail("did not receive all messages sent");
} else {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
}
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsAmqpIT method prepareDeviceRegistry.
/**
* Starts the device registry and connects a client.
*
* @param ctx The vert.x test context.
*/
@BeforeClass
public static void prepareDeviceRegistry(final TestContext ctx) {
client = DeviceRegistryAmqpTestSupport.prepareDeviceRegistryClient(vertx);
client.connect(new ProtonClientOptions()).compose(c -> c.getOrCreateCredentialsClient(Constants.DEFAULT_TENANT)).setHandler(ctx.asyncAssertSuccess(r -> {
credentialsClient = r;
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsHttpIT method testUpdateCredentialsSucceeds.
/**
* Verifies that the service accepts an update credentials request for existing credentials.
*
* @param context The vert.x test context.
*/
@Test
public void testUpdateCredentialsSucceeds(final TestContext context) {
final JsonObject altered = hashedPasswordCredentials.copy();
altered.put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, "other-device");
registry.addCredentials(TENANT, hashedPasswordCredentials).compose(ar -> registry.updateCredentials(TENANT, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, altered)).compose(ur -> registry.getCredentials(TENANT, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD)).setHandler(context.asyncAssertSuccess(gr -> {
context.assertEquals("other-device", gr.toJsonObject().getString(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID));
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class CredentialsHttpIT method testUpdateCredentialsFailsForNonMatchingTypeInPayload.
/**
* Verifies that the service rejects an update request for credentials containing a different type.
*
* @param context The vert.x test context.
*/
@Test
public void testUpdateCredentialsFailsForNonMatchingTypeInPayload(final TestContext context) {
final JsonObject altered = hashedPasswordCredentials.copy().put(CredentialsConstants.FIELD_TYPE, "non-matching-type");
registry.addCredentials(TENANT, hashedPasswordCredentials).compose(ar -> {
return registry.updateCredentials(TENANT, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, altered, HttpURLConnection.HTTP_BAD_REQUEST);
}).setHandler(context.asyncAssertSuccess());
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class DeviceRegistrationHttpIT method testGetDeviceContainsRegisteredInfo.
/**
* Verifies that the information that has been registered for a device
* is contained in the result when retrieving registration information
* for the device.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetDeviceContainsRegisteredInfo(final TestContext ctx) {
final JsonObject data = new JsonObject().put("testString", "testValue").put("testBoolean", Boolean.FALSE).put(RegistrationConstants.FIELD_ENABLED, Boolean.TRUE);
registry.registerDevice(TENANT, deviceId, data).compose(ok -> registry.getRegistrationInfo(TENANT, deviceId)).compose(info -> {
assertRegistrationInformation(ctx, info.toJsonObject(), deviceId, data);
return Future.succeededFuture();
}).setHandler(ctx.asyncAssertSuccess());
}
Aggregations