use of org.eclipse.hono.service.management.SearchResult in project hono by eclipse.
the class AbstractTenantManagementSearchTenantsTest method testSearchTenantsWithCardToMatchSingleCharacter.
/**
* Verifies that a request to search tenants with filters containing the wildcard character '?'
* and matching tenants are found.
*
* @param ctx The vert.x test context.
*/
@Test
default void testSearchTenantsWithCardToMatchSingleCharacter(final VertxTestContext ctx) {
final String tenantId1 = DeviceRegistryUtils.getUniqueIdentifier();
final String tenantId2 = DeviceRegistryUtils.getUniqueIdentifier();
final String tenantId3 = DeviceRegistryUtils.getUniqueIdentifier();
final int pageSize = 1;
final int pageOffset = 0;
final Filter filter1 = new Filter("/ext/id", "testTenant-?");
final Filter filter2 = new Filter("/ext/value", "test$?Value");
final Sort sortOption = new Sort("/ext/id");
sortOption.setDirection(Sort.Direction.DESC);
createTenants(Map.of(tenantId1, new Tenant().setEnabled(false).setExtensions(Map.of("id", "testTenant-x", "value", "test$Value")), tenantId2, new Tenant().setEnabled(true).setExtensions(Map.of("id", "testTenant-2", "value", "test$2Value")), tenantId3, new Tenant().setEnabled(true).setExtensions(Map.of("id", "testTenant-3", "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(2);
assertThat(searchResult.getResult()).hasSize(1);
assertThat(searchResult.getResult().get(0).getId()).isEqualTo(tenantId3);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.SearchResult in project hono by eclipse.
the class MongoDbBasedDao method processSearchResource.
/**
* Finds resources such as tenant or device from the given MongoDB collection with the provided
* paging, filtering and sorting options.
* <p>
* A MongoDB aggregation pipeline is used to find the resources from the given MongoDB collection.
*
* @param pageSize The maximum number of results to include in a response.
* @param pageOffset The offset into the result set from which to include objects in the response.
* This allows to retrieve the whole result set page by page.
* @param filterDocument The document used for filtering the resources in a MongoDB aggregation pipeline.
* @param sortDocument The document used for sorting the resources in a MongoDB aggregation pipeline.
* @param resultMapper The mapper used for mapping the result for the search operation.
* @param <T> The type of the result namely {@link org.eclipse.hono.service.management.device.DeviceWithId} or
* {@link org.eclipse.hono.service.management.tenant.TenantWithId}
* @return A future indicating the outcome of the operation. The future will succeed if the search operation
* is successful and some resources are found. If no resources are found then the future will fail
* with a {@link ClientErrorException} with status {@link HttpURLConnection#HTTP_NOT_FOUND}.
* The future will be failed with a {@link ServiceInvocationException} if the query could not be executed.
* @throws NullPointerException if any of the parameters is {@code null}.
* @throws IllegalArgumentException if page size is <= 0 or page offset is < 0.
* @see <a href="https://docs.mongodb.com/manual/core/aggregation-pipeline">MongoDB Aggregation Pipeline</a>
*/
protected <T> Future<SearchResult<T>> processSearchResource(final int pageSize, final int pageOffset, final JsonObject filterDocument, final JsonObject sortDocument, final Function<JsonObject, List<T>> resultMapper) {
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");
}
Objects.requireNonNull(filterDocument);
Objects.requireNonNull(sortDocument);
Objects.requireNonNull(resultMapper);
final JsonArray aggregationPipelineQuery = getSearchResourceQuery(pageSize, pageOffset, filterDocument, sortDocument);
final Promise<JsonObject> searchPromise = Promise.promise();
if (LOG.isTraceEnabled()) {
LOG.trace("searching resources using aggregation pipeline:{}{}", System.lineSeparator(), aggregationPipelineQuery.encodePrettily());
}
mongoClient.aggregate(collectionName, aggregationPipelineQuery).exceptionHandler(searchPromise::fail).handler(searchPromise::complete);
return searchPromise.future().onSuccess(searchResult -> {
if (LOG.isTraceEnabled()) {
LOG.trace("search result:{}{}", System.lineSeparator(), searchResult.encodePrettily());
}
}).map(result -> Optional.ofNullable(result.getInteger(RegistryManagementConstants.FIELD_RESULT_SET_SIZE)).filter(total -> total > 0).map(total -> new SearchResult<>(total, resultMapper.apply(result))).orElseThrow(() -> new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND))).recover(this::mapError);
}
use of org.eclipse.hono.service.management.SearchResult 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();
}));
}
use of org.eclipse.hono.service.management.SearchResult 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());
}
use of org.eclipse.hono.service.management.SearchResult 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());
}
Aggregations