use of io.vertx.ext.mongo.MongoClient in project vertx-auth by vert-x3.
the class AuthMongoExamples method example1.
public void example1(Vertx vertx, JsonObject mongoClientConfig) {
MongoClient client = MongoClient.createShared(vertx, mongoClientConfig);
JsonObject authProperties = new JsonObject();
MongoAuth authProvider = MongoAuth.create(client, authProperties);
}
use of io.vertx.ext.mongo.MongoClient in project vertx-auth by vert-x3.
the class MongoAuthOptions method createProvider.
@Override
public MongoAuth createProvider(Vertx vertx) {
MongoClient client;
if (shared) {
if (datasourceName != null) {
client = MongoClient.createShared(vertx, config, datasourceName);
} else {
client = MongoClient.createShared(vertx, config);
}
} else {
client = MongoClient.createNonShared(vertx, config);
}
JsonObject authConfig = new JsonObject();
MongoAuthOptionsConverter.toJson(this, authConfig);
return MongoAuth.create(client, authConfig);
}
use of io.vertx.ext.mongo.MongoClient in project hono by eclipse.
the class MongoDbBasedDeviceDaoTest method testCreateSetsCreationDate.
/**
* Verifies that the DAO sets the initial version and creation date when creating a device.
*
* @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 = DeviceDto.forCreation(DeviceDto::new, "tenantId", "deviceId", new Device(), "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("devices"), document.capture());
MongoDbBasedTenantDaoTest.assertCreationDocumentContainsStatusProperties(document.getValue(), "initial-version");
});
ctx.completeNow();
}));
}
use of io.vertx.ext.mongo.MongoClient in project hono by eclipse.
the class MongoDbBasedCredentialServiceTest method testCredentialsDaoUsesIndex.
/**
* Verifies that the credentials DAO uses a proper index to retrieve credentials by auth-id and type.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCredentialsDaoUsesIndex(final VertxTestContext ctx) {
final var tenantId = UUID.randomUUID().toString();
final MongoClient mongoClient = MongoDbTestUtils.getMongoClient(vertx, DB_NAME);
final var dto1 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device1a", "secret"), Credentials.createPSKCredential("device1b", "shared-secret")), UUID.randomUUID().toString());
final var dto2 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device2a", "secret"), Credentials.createPSKCredential("device2b", "shared-secret")), UUID.randomUUID().toString());
final var dto3 = CredentialsDto.forCreation(tenantId, UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device3a", "secret"), Credentials.createPSKCredential("device3b", "shared-secret")), UUID.randomUUID().toString());
final var dto4 = CredentialsDto.forCreation(UUID.randomUUID().toString(), UUID.randomUUID().toString(), List.of(Credentials.createPasswordCredential("device1a", "secret"), Credentials.createPSKCredential("device1b", "shared-secret")), UUID.randomUUID().toString());
credentialsDao.create(dto1, NoopSpan.INSTANCE.context()).compose(ok -> credentialsDao.create(dto2, NoopSpan.INSTANCE.context())).compose(ok -> credentialsDao.create(dto3, NoopSpan.INSTANCE.context())).compose(ok -> credentialsDao.create(dto4, NoopSpan.INSTANCE.context())).compose(ok -> {
final Promise<JsonObject> resultHandler = Promise.promise();
final var filter = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withAuthId("device1a").withType(CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD).document();
final var commandRight = new JsonObject().put("find", "credentials").put("batchSize", 1).put("singleBatch", true).put("filter", filter).put("projection", MongoDbBasedCredentialsDao.PROJECTION_CREDS_BY_TYPE_AND_AUTH_ID);
final var explain = new JsonObject().put("explain", commandRight).put("verbosity", "executionStats");
mongoClient.runCommand("explain", explain, resultHandler);
return resultHandler.future();
}).onComplete(ctx.succeeding(result -> {
if (LOG.isTraceEnabled()) {
LOG.trace("result:{}{}", System.lineSeparator(), result.encodePrettily());
}
ctx.verify(() -> {
final var indexScan = (JsonObject) JsonPointer.from("/queryPlanner/winningPlan/inputStage/inputStage").queryJson(result);
assertThat(indexScan.getString("indexName")).isEqualTo(MongoDbBasedCredentialsDao.IDX_CREDENTIALS_TYPE_AND_AUTH_ID);
final var executionStats = result.getJsonObject("executionStats", new JsonObject());
// there are two credentials with auth-id "device1a" and type "hashed-password"
assertThat(executionStats.getInteger("totalKeysExamined")).isEqualTo(2);
assertThat(executionStats.getInteger("totalDocsExamined")).isEqualTo(2);
});
ctx.completeNow();
}));
}
use of io.vertx.ext.mongo.MongoClient 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();
}));
}
Aggregations