Search in sources :

Example 6 with Sort

use of org.eclipse.hono.service.management.Sort in project hono by eclipse.

the class AbstractDeviceManagementSearchDevicesTest method testSearchDevicesWithCardToMatchSingleCharacter.

/**
 * Verifies that a request to search devices with filters containing the wildcard character '?'
 * and matching devices are found.
 *
 * @param ctx The vert.x test context.
 */
@Test
default void testSearchDevicesWithCardToMatchSingleCharacter(final VertxTestContext ctx) {
    final String tenantId = DeviceRegistryUtils.getUniqueIdentifier();
    final int pageSize = 10;
    final int pageOffset = 0;
    final Filter filter1 = new Filter("/id", "testDevice-?");
    final Filter filter2 = new Filter("/ext/value", "test$?Value");
    final Sort sortOption = new Sort("/id");
    createDevices(tenantId, Map.of("testDevice-x", new Device().setExtensions(Map.of("value", "test$Value")), "testDevice-1", new Device().setExtensions(Map.of("value", "test$1Value")), "testDevice-2", new Device().setExtensions(Map.of("value", "test$2Value")))).compose(ok -> getDeviceManagementService().searchDevices(tenantId, pageSize, pageOffset, List.of(filter1, filter2), List.of(sortOption), NoopSpan.INSTANCE).onComplete(ctx.succeeding(s -> {
        ctx.verify(() -> {
            assertThat(s.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
            final SearchResult<DeviceWithId> searchResult = s.getPayload();
            assertThat(searchResult.getTotal()).isEqualTo(2);
            assertThat(searchResult.getResult()).hasSize(2);
            assertThat(searchResult.getResult().get(0).getId()).isEqualTo("testDevice-1");
            assertThat(searchResult.getResult().get(1).getId()).isEqualTo("testDevice-2");
        });
        ctx.completeNow();
    })));
}
Also used : Filter(org.eclipse.hono.service.management.Filter) Sort(org.eclipse.hono.service.management.Sort) Test(org.junit.jupiter.api.Test)

Example 7 with Sort

use of org.eclipse.hono.service.management.Sort in project hono by eclipse.

the class AbstractDeviceManagementSearchDevicesTest method testSearchDevicesWithSortOption.

/**
 * Verifies that a request to search devices with a sort option succeeds and the result is in accordance with the
 * specified sort option.
 *
 * @param ctx The vert.x test context.
 */
@Test
default void testSearchDevicesWithSortOption(final VertxTestContext ctx) {
    final String tenantId = DeviceRegistryUtils.getUniqueIdentifier();
    final int pageSize = 1;
    final int pageOffset = 0;
    final Filter filter = new Filter("/enabled", true);
    final Sort sortOption = new Sort("/ext/id");
    sortOption.setDirection(Sort.Direction.DESC);
    createDevices(tenantId, Map.of("testDevice1", new Device().setEnabled(true).setExtensions(Map.of("id", "aaa")), "testDevice2", new Device().setEnabled(true).setExtensions(Map.of("id", "bbb")))).compose(ok -> getDeviceManagementService().searchDevices(tenantId, pageSize, pageOffset, List.of(filter), List.of(sortOption), NoopSpan.INSTANCE)).onComplete(ctx.succeeding(s -> {
        ctx.verify(() -> {
            assertThat(s.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
            final SearchResult<DeviceWithId> searchResult = s.getPayload();
            assertThat(searchResult.getTotal()).isEqualTo(2);
            assertThat(searchResult.getResult()).hasSize(1);
            assertThat(searchResult.getResult().get(0).getId()).isEqualTo("testDevice2");
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Filter(org.eclipse.hono.service.management.Filter) Truth.assertThat(com.google.common.truth.Truth.assertThat) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) List(java.util.List) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Filter(org.eclipse.hono.service.management.Filter) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Test(org.junit.jupiter.api.Test)

Example 8 with Sort

use of org.eclipse.hono.service.management.Sort in project hono by eclipse.

the class AbstractTenantManagementSearchTenantsTest method testSearchTenantsWithWildCardToMatchMultipleCharacters.

/**
 * Verifies that a request to search tenants with filters containing the wildcard character '*'
 * succeeds and matching tenants are found.
 *
 * @param ctx The vert.x test context.
 */
@Test
default void testSearchTenantsWithWildCardToMatchMultipleCharacters(final VertxTestContext ctx) {
    final String tenantId1 = DeviceRegistryUtils.getUniqueIdentifier();
    final String tenantId2 = DeviceRegistryUtils.getUniqueIdentifier();
    final String tenantId3 = DeviceRegistryUtils.getUniqueIdentifier();
    final int pageSize = 10;
    final int pageOffset = 0;
    final Filter filter1 = new Filter("/ext/id", "tenant*-*");
    final Filter filter2 = new Filter("/ext/value", "test$2*e");
    final Sort sortOption = new Sort("/id");
    createTenants(Map.of(tenantId1, new Tenant().setEnabled(false).setExtensions(Map.of("id", "tenant1-id")), tenantId2, new Tenant().setEnabled(true).setExtensions(Map.of("id", "tenant2-id", "value", "test$2Value")), tenantId3, new Tenant().setEnabled(true).setExtensions(Map.of("id", "tenant3-id", "value", "test$3Value")))).onFailure(ctx::failNow).compose(ok -> getTenantManagementService().searchTenants(pageSize, pageOffset, List.of(filter1, filter2), List.of(sortOption), NoopSpan.INSTANCE)).onComplete(ctx.succeeding(s -> {
        ctx.verify(() -> {
            assertThat(s.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
            final SearchResult<TenantWithId> searchResult = s.getPayload();
            assertThat(searchResult.getTotal()).isEqualTo(1);
            assertThat(searchResult.getResult()).hasSize(1);
            assertThat(searchResult.getResult().get(0).getId()).isEqualTo(tenantId2);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Filter(org.eclipse.hono.service.management.Filter) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) Sort(org.eclipse.hono.service.management.Sort) Adapter(org.eclipse.hono.util.Adapter) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Optional(java.util.Optional) NoopSpan(io.opentracing.noop.NoopSpan) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Filter(org.eclipse.hono.service.management.Filter) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Test(org.junit.jupiter.api.Test)

Example 9 with Sort

use of org.eclipse.hono.service.management.Sort in project hono by eclipse.

the class AbstractTenantManagementSearchTenantsTest method testSearchTenantsWithPageOffset.

/**
 * Verifies that a request to search tenants with valid page offset succeeds and the result is in accordance with
 * the specified page offset.
 *
 * @param ctx The vert.x test context.
 */
@Test
default void testSearchTenantsWithPageOffset(final VertxTestContext ctx) {
    final String tenantId1 = DeviceRegistryUtils.getUniqueIdentifier();
    final String tenantId2 = DeviceRegistryUtils.getUniqueIdentifier();
    final int pageSize = 1;
    final int pageOffset = 1;
    final Filter filter = new Filter("/enabled", true);
    final Sort sortOption = new Sort("/ext/id");
    sortOption.setDirection(Sort.Direction.DESC);
    createTenants(Map.of(tenantId1, new Tenant().setEnabled(true).setExtensions(Map.of("id", "1")), tenantId2, new Tenant().setEnabled(true).setExtensions(Map.of("id", "2")))).onFailure(ctx::failNow).compose(ok -> getTenantManagementService().searchTenants(pageSize, pageOffset, List.of(filter), List.of(sortOption), NoopSpan.INSTANCE)).onComplete(ctx.succeeding(s -> {
        ctx.verify(() -> {
            assertThat(s.getStatus()).isEqualTo(HttpURLConnection.HTTP_OK);
            final SearchResult<TenantWithId> searchResult = s.getPayload();
            assertThat(searchResult.getTotal()).isEqualTo(2);
            assertThat(searchResult.getResult()).hasSize(1);
            assertThat(searchResult.getResult().get(0).getId()).isEqualTo(tenantId1);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Filter(org.eclipse.hono.service.management.Filter) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) Sort(org.eclipse.hono.service.management.Sort) Adapter(org.eclipse.hono.util.Adapter) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Optional(java.util.Optional) NoopSpan(io.opentracing.noop.NoopSpan) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Filter(org.eclipse.hono.service.management.Filter) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Test(org.junit.jupiter.api.Test)

Example 10 with Sort

use of org.eclipse.hono.service.management.Sort in project hono by eclipse.

the class MongoDbBasedDeviceDao method find.

/**
 * {@inheritDoc}
 */
@Override
public Future<SearchResult<DeviceWithId>> find(final String tenantId, final int pageSize, final int pageOffset, final List<Filter> filters, final List<Sort> sortOptions, final SpanContext tracingContext) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(filters);
    Objects.requireNonNull(sortOptions);
    if (pageSize <= 0) {
        throw new IllegalArgumentException("page size must be a positive integer");
    }
    if (pageOffset < 0) {
        throw new IllegalArgumentException("page offset must not be negative");
    }
    final Span span = tracer.buildSpan("find Devices").addReference(References.CHILD_OF, tracingContext).start();
    final JsonObject filterDocument = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withDeviceFilters(filters).document();
    final JsonObject sortDocument = MongoDbDocumentBuilder.builder().withDeviceSortOptions(sortOptions).document();
    return processSearchResource(pageSize, pageOffset, filterDocument, sortDocument, MongoDbBasedDeviceDao::getDevicesWithId).onFailure(t -> TracingHelper.logError(span, "error finding devices", t)).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) Set(java.util.Set) MongoClient(io.vertx.ext.mongo.MongoClient) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DeviceDto(org.eclipse.hono.service.management.device.DeviceDto) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span)

Aggregations

Filter (org.eclipse.hono.service.management.Filter)13 Sort (org.eclipse.hono.service.management.Sort)13 Future (io.vertx.core.Future)10 HttpURLConnection (java.net.HttpURLConnection)10 List (java.util.List)10 Optional (java.util.Optional)10 Test (org.junit.jupiter.api.Test)9 SearchResult (org.eclipse.hono.service.management.SearchResult)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 CompositeFuture (io.vertx.core.CompositeFuture)6 Collectors (java.util.stream.Collectors)6 Span (io.opentracing.Span)5 NoopSpan (io.opentracing.noop.NoopSpan)5 JsonObject (io.vertx.core.json.JsonObject)5 VertxTestContext (io.vertx.junit5.VertxTestContext)5 Map (java.util.Map)5 DeviceRegistryUtils (org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils)5 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)5 Promise (io.vertx.core.Promise)4 Objects (java.util.Objects)4