Search in sources :

Example 1 with MongoClient

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);
}
Also used : MongoClient(io.vertx.ext.mongo.MongoClient) JsonObject(io.vertx.core.json.JsonObject) MongoAuth(io.vertx.ext.auth.mongo.MongoAuth)

Example 2 with MongoClient

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);
}
Also used : MongoClient(io.vertx.ext.mongo.MongoClient) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with MongoClient

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Timeout(io.vertx.junit5.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) Device(org.eclipse.hono.service.management.device.Device) MongoClient(io.vertx.ext.mongo.MongoClient) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ChronoUnit(java.time.temporal.ChronoUnit) Optional(java.util.Optional) FindOptions(io.vertx.ext.mongo.FindOptions) NoopSpan(io.opentracing.noop.NoopSpan) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Device(org.eclipse.hono.service.management.device.Device) JsonObject(io.vertx.core.json.JsonObject) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Test(org.junit.jupiter.api.Test)

Example 4 with MongoClient

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();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(io.vertx.core.json.JsonObject) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) MongoClient(io.vertx.ext.mongo.MongoClient) MongoDbBasedRegistrationConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedRegistrationConfigProperties) UUID(java.util.UUID) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) List(java.util.List) SpringBasedHonoPasswordEncoder(org.eclipse.hono.auth.SpringBasedHonoPasswordEncoder) Optional(java.util.Optional) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) OperationResult(org.eclipse.hono.service.management.OperationResult) Checkpoint(io.vertx.junit5.Checkpoint) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Mockito.mock(org.mockito.Mockito.mock) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) Credentials(org.eclipse.hono.service.management.credentials.Credentials) CredentialsServiceTestBase(org.eclipse.hono.service.credentials.CredentialsServiceTestBase) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) CompositeFuture(io.vertx.core.CompositeFuture) MongoDbBasedCredentialsDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedCredentialsDao) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MongoDbBasedCredentialsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedCredentialsConfigProperties) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) MongoDbBasedDeviceDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedDeviceDao) NoopSpan(io.opentracing.noop.NoopSpan) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MongoClient(io.vertx.ext.mongo.MongoClient) Promise(io.vertx.core.Promise) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 5 with MongoClient

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) TenantDto(org.eclipse.hono.service.management.tenant.TenantDto) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Vertx(io.vertx.core.Vertx) MongoClient(io.vertx.ext.mongo.MongoClient) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ChronoUnit(java.time.temporal.ChronoUnit) BaseDto(org.eclipse.hono.service.management.BaseDto) Buffer(io.vertx.core.buffer.Buffer) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) FindOptions(io.vertx.ext.mongo.FindOptions) NoopSpan(io.opentracing.noop.NoopSpan) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Tenant(org.eclipse.hono.service.management.tenant.Tenant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)13 MongoClient (io.vertx.ext.mongo.MongoClient)13 Truth.assertThat (com.google.common.truth.Truth.assertThat)7 NoopSpan (io.opentracing.noop.NoopSpan)7 Future (io.vertx.core.Future)7 Timeout (io.vertx.junit5.Timeout)7 VertxExtension (io.vertx.junit5.VertxExtension)7 VertxTestContext (io.vertx.junit5.VertxTestContext)7 Optional (java.util.Optional)7 TimeUnit (java.util.concurrent.TimeUnit)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Test (org.junit.jupiter.api.Test)7 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)7 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.when (org.mockito.Mockito.when)7 FindOptions (io.vertx.ext.mongo.FindOptions)6 UpdateOptions (io.vertx.ext.mongo.UpdateOptions)6 Instant (java.time.Instant)6