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();
})));
}
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();
}));
}
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();
}));
}
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();
}));
}
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());
}
Aggregations