Search in sources :

Example 6 with MongoClient

use of io.vertx.ext.mongo.MongoClient 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();
    }));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) 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) IndexOptions(io.vertx.ext.mongo.IndexOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 7 with MongoClient

use of io.vertx.ext.mongo.MongoClient in project hono by eclipse.

the class MongoDbBasedDeviceDaoTest method testUpdateSetsLastUpdate.

/**
 * Verifies that the DAO sets the new version and last update time but also keeps the original
 * creation date when updating a device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateSetsLastUpdate(final VertxTestContext ctx) {
    final var existingRecord = DeviceDto.forRead(DeviceDto::new, "tenantId", "deviceId", new Device(), false, false, Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS), null, "initial-version");
    when(mongoClient.findOneAndReplaceWithOptions(anyString(), any(JsonObject.class), any(JsonObject.class), any(FindOptions.class), any(UpdateOptions.class))).thenReturn(Future.succeededFuture(new JsonObject().put(DeviceDto.FIELD_VERSION, "new-version")));
    final var dto = DeviceDto.forUpdate(() -> existingRecord, "tenantId", "deviceId", new Device(), "new-version");
    dao.update(dto, Optional.of(existingRecord.getVersion()), NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(newVersion -> {
        ctx.verify(() -> {
            final var document = ArgumentCaptor.forClass(JsonObject.class);
            verify(mongoClient).findOneAndReplaceWithOptions(eq("devices"), any(JsonObject.class), document.capture(), any(FindOptions.class), any(UpdateOptions.class));
            MongoDbBasedTenantDaoTest.assertUpdateDocumentContainsStatusProperties(document.getValue(), "new-version", existingRecord.getCreationTime());
        });
        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) FindOptions(io.vertx.ext.mongo.FindOptions) Device(org.eclipse.hono.service.management.device.Device) JsonObject(io.vertx.core.json.JsonObject) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) Test(org.junit.jupiter.api.Test)

Example 8 with MongoClient

use of io.vertx.ext.mongo.MongoClient in project hono by eclipse.

the class MongoDbBasedCredentialsDaoTest method testUpdateSetsLastUpdate.

/**
 * Verifies that the DAO sets the new version and last update time but also keeps the original
 * creation date when updating credentials.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateSetsLastUpdate(final VertxTestContext ctx) {
    final var existingRecord = CredentialsDto.forRead("tenantId", "deviceId", List.of(), Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS), null, "initial-version");
    when(mongoClient.findOneAndReplaceWithOptions(anyString(), any(JsonObject.class), any(JsonObject.class), any(FindOptions.class), any(UpdateOptions.class))).thenReturn(Future.succeededFuture(new JsonObject().put(DeviceDto.FIELD_VERSION, "new-version")));
    final var dto = CredentialsDto.forUpdate(() -> existingRecord, List.of(), "new-version");
    dao.update(dto, Optional.of(existingRecord.getVersion()), NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(newVersion -> {
        ctx.verify(() -> {
            final var document = ArgumentCaptor.forClass(JsonObject.class);
            verify(mongoClient).findOneAndReplaceWithOptions(eq("credentials"), any(JsonObject.class), document.capture(), any(FindOptions.class), any(UpdateOptions.class));
            MongoDbBasedTenantDaoTest.assertUpdateDocumentContainsStatusProperties(document.getValue(), "new-version", existingRecord.getCreationTime());
        });
        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) 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) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) 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) FindOptions(io.vertx.ext.mongo.FindOptions) JsonObject(io.vertx.core.json.JsonObject) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) Test(org.junit.jupiter.api.Test)

Example 9 with MongoClient

use of io.vertx.ext.mongo.MongoClient in project hono by eclipse.

the class MongoDbBasedTenantDaoTest method testUpdateSetsLastUpdate.

/**
 * Verifies that the DAO sets the new version and last update time but also keeps the original
 * creation date when updating a tenant.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateSetsLastUpdate(final VertxTestContext ctx) {
    final var existingRecord = TenantDto.forRead("tenantId", new Tenant(), Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS), null, "initial-version");
    when(mongoClient.findOneAndReplaceWithOptions(anyString(), any(JsonObject.class), any(JsonObject.class), any(FindOptions.class), any(UpdateOptions.class))).thenReturn(Future.succeededFuture(new JsonObject().put(TenantDto.FIELD_VERSION, "new-version")));
    final var dto = TenantDto.forUpdate(() -> existingRecord, new Tenant(), "new-version");
    dao.update(dto, Optional.of(existingRecord.getVersion()), NoopSpan.INSTANCE.context()).onComplete(ctx.succeeding(newVersion -> {
        ctx.verify(() -> {
            assertThat(newVersion).isEqualTo("new-version");
            final var document = ArgumentCaptor.forClass(JsonObject.class);
            verify(mongoClient).findOneAndReplaceWithOptions(eq("tenants"), any(JsonObject.class), document.capture(), any(FindOptions.class), any(UpdateOptions.class));
            MongoDbBasedTenantDaoTest.assertUpdateDocumentContainsStatusProperties(document.getValue(), "new-version", existingRecord.getCreationTime());
        });
        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) FindOptions(io.vertx.ext.mongo.FindOptions) Tenant(org.eclipse.hono.service.management.tenant.Tenant) JsonObject(io.vertx.core.json.JsonObject) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) Test(org.junit.jupiter.api.Test)

Example 10 with MongoClient

use of io.vertx.ext.mongo.MongoClient in project vertx-auth by vert-x3.

the class AuthMongoExamples method example5.

public void example5(Vertx vertx, JsonObject mongoClientConfig) {
    MongoClient client = MongoClient.createShared(vertx, mongoClientConfig);
    JsonObject authProperties = new JsonObject();
    MongoAuth authProvider = MongoAuth.create(client, authProperties);
    authProvider.setHashAlgorithm(HashAlgorithm.PBKDF2);
}
Also used : MongoClient(io.vertx.ext.mongo.MongoClient) JsonObject(io.vertx.core.json.JsonObject) MongoAuth(io.vertx.ext.auth.mongo.MongoAuth)

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