use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class MongoDbBasedTenantDaoTest method testCreateSetsCreationDate.
/**
* Verifies that the DAO sets the initial version and creation date when creating a tenant.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateSetsCreationDate(final VertxTestContext ctx) {
when(mongoClient.insert(anyString(), any(JsonObject.class))).thenReturn(Future.succeededFuture("initial-version"));
final var dto = TenantDto.forCreation("tenantId", new Tenant(), "initial-version");
dao.create(dto, NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(version -> {
ctx.verify(() -> {
assertThat(version).isEqualTo("initial-version");
final var document = ArgumentCaptor.forClass(JsonObject.class);
verify(mongoClient).insert(eq("tenants"), document.capture());
MongoDbBasedTenantDaoTest.assertCreationDocumentContainsStatusProperties(document.getValue(), "initial-version");
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class MongoDbBasedTenantDaoTest method testCreateIndicesDropsObsoleteIndex.
/**
* Verifies that when creating indices for the Tenant collection, an existing unique index on
* <em>tenant.trusted-ca.subject-dn</em> is getting dropped.
*
* @param ctx The vert.x test context.
* @param vertx The vert.x instance to run on.
*/
@Test
public void testCreateIndicesDropsObsoleteIndex(final VertxTestContext ctx, final Vertx vertx) {
// GIVEN a set of existing indices including a unique, partial index on tenant.trusted-ca.subject-dn
final Buffer existingIndices = vertx.fileSystem().readFileBlocking("target/test-classes/indexes.json");
when(mongoClient.createIndexWithOptions(anyString(), any(JsonObject.class), any(IndexOptions.class))).thenReturn(Future.succeededFuture());
when(mongoClient.dropIndex(anyString(), anyString())).thenReturn(Future.succeededFuture());
when(mongoClient.listIndexes(anyString())).thenReturn(Future.succeededFuture(existingIndices.toJsonArray()));
// WHEN creating indices for the tenant collection
dao.createIndices().onComplete(ctx.succeeding(ok -> {
ctx.verify(() -> {
// THEN the unique index is being dropped
verify(mongoClient).dropIndex(anyString(), eq("tenant.trusted-ca.subject-dn_1"));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class EventCoapIT method testEventMessageAlreadySentIsDeliveredWhenConsumerConnects.
/**
* Verifies that an event message from a device has been successfully sent and a north bound application,
* which connects after the event has been sent, can successfully receive those event message.
*
* @param ctx The vert.x test context.
* @throws InterruptedException if test execution gets interrupted.
*/
@Test
public void testEventMessageAlreadySentIsDeliveredWhenConsumerConnects(final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final String messagePayload = UUID.randomUUID().toString();
helper.registry.addDeviceForTenant(tenantId, new Tenant(), deviceId, SECRET).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
// WHEN a device that belongs to the tenant publishes an event
final Promise<OptionSet> sendEvent = Promise.promise();
final CoapClient client = getCoapClient();
final Request eventRequest = createCoapRequest(CoAP.Code.PUT, Type.CON, getPutResource(tenantId, deviceId), messagePayload.getBytes());
client.advanced(getHandler(sendEvent), eventRequest);
sendEvent.future().onSuccess(eventSent -> {
logger.debug("event message has been sent");
// THEN create a consumer once the event message has been successfully sent
logger.debug("opening event consumer for tenant [{}]", tenantId);
createConsumer(tenantId, msg -> {
// THEN verify that the event message has been received by the consumer
logger.debug("event message has been received by the consumer");
ctx.verify(() -> assertThat(msg.getPayload().toString()).isEqualTo(messagePayload));
ctx.completeNow();
});
}).onFailure(ctx::failNow);
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class TelemetryCoapIT method testUploadFailsForLargePayload.
/**
* Verifies that the upload of a telemetry message containing a payload that
* exceeds the CoAP adapter's configured max payload size fails with a 4.13
* response code.
*
* @param ctx The test context.
* @throws IOException if the CoAP request cannot be sent to the adapter.
* @throws ConnectorException if the CoAP request cannot be sent to the adapter.
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadFailsForLargePayload(final VertxTestContext ctx) throws ConnectorException, IOException {
final Tenant tenant = new Tenant();
helper.registry.addPskDeviceForTenant(tenantId, tenant, deviceId, SECRET).compose(ok -> {
final CoapClient client = getCoapsClient(deviceId, tenantId, SECRET);
final Request request = createCoapsRequest(Code.POST, Type.CON, getPostResource(), IntegrationTestSupport.getPayload(4096));
final Promise<OptionSet> result = Promise.promise();
client.advanced(getHandler(result, ResponseCode.REQUEST_ENTITY_TOO_LARGE), request);
return result.future();
}).onComplete(ctx.succeedingThenComplete());
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class TelemetryCoapIT method testUploadMessagesUsingClientCertificateWithAlias.
/**
* Verifies that a number of messages uploaded to Hono's CoAP adapter using TLS_ECDSA based authentication can be
* successfully consumed via the messaging infrastructure.
* <p>
* The device's tenant is being determined using the requested host name contained in the client's SNI TLS
* extension.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessagesUsingClientCertificateWithAlias(final VertxTestContext ctx) throws InterruptedException {
assumeTrue(IntegrationTestSupport.isTrustAnchorGroupsSupported(), "device registry does not support trust anchor groups");
assumeTrue(IntegrationTestSupport.isTenantAliasSupported(), "device registry does not support tenant aliases");
final var clientCertLoader = KeyLoader.fromFiles(vertx, PATH_DEVICE_KEY, PATH_DEVICE_CERT);
final var clientCert = (X509Certificate) clientCertLoader.getCertificateChain()[0];
final VertxTestContext setup = new VertxTestContext();
helper.getCertificate(PATH_CA_CERT).compose(caCert -> helper.registry.addTenant(helper.getRandomTenantId(), Tenants.createTenantForTrustAnchor(caCert).setTrustAnchorGroup("test-group")).map(caCert)).compose(caCert -> {
return helper.registry.addDeviceForTenant(tenantId, Tenants.createTenantForTrustAnchor(caCert).setTrustAnchorGroup("test-group").setAlias("test-alias"), deviceId, clientCert);
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
final CoapClient client = getCoapsClient(clientCertLoader);
testUploadMessages(ctx, tenantId, () -> warmUp(client, createCoapsRequest(Code.POST, getMessageType(), "test-alias." + IntegrationTestSupport.COAP_HOST, getPostResource(), "hello 0".getBytes(StandardCharsets.UTF_8))), count -> {
final Promise<OptionSet> result = Promise.promise();
final String payload = "hello " + count;
final Request request = createCoapsRequest(Code.POST, getMessageType(), "test-alias." + IntegrationTestSupport.COAP_HOST, getPostResource(), payload.getBytes(StandardCharsets.UTF_8));
client.advanced(getHandler(result), request);
return result.future();
});
}
Aggregations