Search in sources :

Example 11 with Sort

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

the class MongoDbBasedTenantDao method find.

/**
 * {@inheritDoc}
 */
@Override
public Future<SearchResult<TenantWithId>> find(final int pageSize, final int pageOffset, final List<Filter> filters, final List<Sort> sortOptions, final SpanContext tracingContext) {
    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 Tenants").addReference(References.CHILD_OF, tracingContext).start();
    final JsonObject filterDocument = MongoDbDocumentBuilder.builder().withTenantFilters(filters).document();
    final JsonObject sortDocument = MongoDbDocumentBuilder.builder().withTenantSortOptions(sortOptions).document();
    return processSearchResource(pageSize, pageOffset, filterDocument, sortDocument, MongoDbBasedTenantDao::getTenantsWithId).onFailure(t -> TracingHelper.logError(span, "error finding tenants", t)).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X500Principal(javax.security.auth.x500.X500Principal) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) ArrayList(java.util.ArrayList) 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) TenantDto(org.eclipse.hono.service.management.tenant.TenantDto) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) MongoClient(io.vertx.ext.mongo.MongoClient) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) 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)

Example 12 with Sort

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

the class DelegatingDeviceManagementHttpEndpointTest method testSearchDevicesSucceedsWithSearchCriteria.

/**
 * Verifies that the endpoint uses search criteria provided in a request's query parameters.
 */
@Test
public void testSearchDevicesSucceedsWithSearchCriteria() {
    final HttpServerResponse response = newResponse();
    requestParams.add(RegistryManagementConstants.PARAM_PAGE_SIZE, "10");
    requestParams.add(RegistryManagementConstants.PARAM_PAGE_OFFSET, "50");
    requestParams.add(RegistryManagementConstants.PARAM_FILTER_JSON, "{\"field\":\"/manufacturer\",\"value\":\"ACME*\"}");
    requestParams.add(RegistryManagementConstants.PARAM_SORT_JSON, "{\"field\":\"/manufacturer\",\"direction\":\"desc\"}");
    final HttpServerRequest request = newRequest(HttpMethod.GET, "/v1/devices/mytenant", requestHeaders, requestParams, response);
    router.handle(request);
    verify(response).setStatusCode(HttpURLConnection.HTTP_OK);
    verify(service).searchDevices(eq("mytenant"), eq(10), eq(50), argThat(filters -> {
        if (filters.isEmpty()) {
            return false;
        } else {
            final Filter filter = filters.get(0);
            return "/manufacturer".equals(filter.getField().toString()) && "ACME*".equals(filter.getValue()) && Operator.eq == filter.getOperator();
        }
    }), argThat(sortOptions -> {
        if (sortOptions.isEmpty()) {
            return false;
        } else {
            final Sort sortOption = sortOptions.get(0);
            return "/manufacturer".equals(sortOption.getField().toString()) && Direction.DESC == sortOption.getDirection();
        }
    }), any(Span.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Filter(org.eclipse.hono.service.management.Filter) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Sort(org.eclipse.hono.service.management.Sort) ArgumentCaptor(org.mockito.ArgumentCaptor) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) HttpServerRequestInternal(io.vertx.core.http.impl.HttpServerRequestInternal) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Operator(org.eclipse.hono.service.management.Filter.Operator) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) DefaultFailureHandler(org.eclipse.hono.service.http.DefaultFailureHandler) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) Handler(io.vertx.core.Handler) Id(org.eclipse.hono.service.management.Id) Direction(org.eclipse.hono.service.management.Sort.Direction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Filter(org.eclipse.hono.service.management.Filter) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Sort(org.eclipse.hono.service.management.Sort) Span(io.opentracing.Span) Test(org.junit.jupiter.api.Test)

Example 13 with Sort

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

the class AbstractDeviceManagementSearchDevicesTest method testSearchDevicesWithWildCardToMatchMultipleCharacters.

/**
 * Verifies that a request to search devices with filters containing the wildcard character '*'
 * succeeds and matching devices are found.
 *
 * @param ctx The vert.x test context.
 */
@Test
default void testSearchDevicesWithWildCardToMatchMultipleCharacters(final VertxTestContext ctx) {
    final String tenantId = DeviceRegistryUtils.getUniqueIdentifier();
    final int pageSize = 10;
    final int pageOffset = 0;
    final Filter filter1 = new Filter("/id", "test*-*");
    final Filter filter2 = new Filter("/ext/value", "test$1*e");
    final Sort sortOption = new Sort("/id");
    createDevices(tenantId, Map.of("testDevice", new Device(), "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(1);
            assertThat(searchResult.getResult()).hasSize(1);
            assertThat(searchResult.getResult().get(0).getId()).isEqualTo("testDevice-1");
        });
        ctx.completeNow();
    })));
}
Also used : Filter(org.eclipse.hono.service.management.Filter) Sort(org.eclipse.hono.service.management.Sort) Test(org.junit.jupiter.api.Test)

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