use of org.thingsboard.server.common.data.query.EntityDataQuery in project thingsboard by thingsboard.
the class BaseEntityServiceTest method doTestHierarchicalFindEntityDataWithAttributesByQuery.
private void doTestHierarchicalFindEntityDataWithAttributesByQuery(final int maxLevel, final boolean fetchLastLevelOnly) throws ExecutionException, InterruptedException {
List<Asset> assets = new ArrayList<>();
List<Device> devices = new ArrayList<>();
List<Long> temperatures = new ArrayList<>();
List<Long> highTemperatures = new ArrayList<>();
createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures);
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < devices.size(); i++) {
Device device = devices.get(i);
attributeFutures.add(saveLongAttribute(device.getId(), "temperature", temperatures.get(i), DataConstants.CLIENT_SCOPE));
}
Futures.successfulAsList(attributeFutures).get();
RelationsQueryFilter filter = new RelationsQueryFilter();
filter.setRootEntity(tenantId);
filter.setDirection(EntitySearchDirection.FROM);
filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("Contains", Collections.singletonList(EntityType.DEVICE))));
filter.setMaxLevel(maxLevel);
filter.setFetchLastLevelOnly(fetchLastLevelOnly);
EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
List<EntityData> loadedEntities = new ArrayList<>(data.getData());
while (data.hasNext()) {
query = query.next();
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities.addAll(data.getData());
}
Assert.assertEquals(25, loadedEntities.size());
List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
List<String> deviceTemperatures = temperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
Assert.assertEquals(deviceTemperatures, loadedTemperatures);
pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
KeyFilter highTemperatureFilter = new KeyFilter();
highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
NumericFilterPredicate predicate = new NumericFilterPredicate();
predicate.setValue(FilterPredicateValue.fromDouble(45));
predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
highTemperatureFilter.setPredicate(predicate);
List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = new ArrayList<>(data.getData());
while (data.hasNext()) {
query = query.next();
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities.addAll(data.getData());
}
Assert.assertEquals(highTemperatures.size(), loadedEntities.size());
List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
List<String> deviceHighTemperatures = highTemperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
deviceService.deleteDevicesByTenantId(tenantId);
}
use of org.thingsboard.server.common.data.query.EntityDataQuery in project thingsboard by thingsboard.
the class BaseEntityServiceTest method testFindEntityDataByQuery_operationEqual_emptySearchQuery.
@Test
public void testFindEntityDataByQuery_operationEqual_emptySearchQuery() {
List<Device> devices = createMockDevices(10);
devices.get(0).setLabel("");
devices.get(1).setLabel(null);
devices.forEach(deviceService::saveDevice);
String searchQuery = "";
EntityDataQuery query = createDeviceSearchQuery("label", StringOperation.EQUAL, searchQuery);
PageData<EntityData> result = searchEntities(query);
assertEquals(devices.size(), result.getTotalElements());
}
use of org.thingsboard.server.common.data.query.EntityDataQuery in project thingsboard by thingsboard.
the class BaseEntityServiceTest method testHierarchicalFindAssetsWithAttributesByQuery.
@Test
public void testHierarchicalFindAssetsWithAttributesByQuery() throws ExecutionException, InterruptedException {
List<Asset> assets = new ArrayList<>();
List<Device> devices = new ArrayList<>();
List<Long> consumptions = new ArrayList<>();
List<Long> highConsumptions = new ArrayList<>();
createTestHierarchy(tenantId, assets, devices, consumptions, highConsumptions, new ArrayList<>(), new ArrayList<>());
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < assets.size(); i++) {
Asset asset = assets.get(i);
attributeFutures.add(saveLongAttribute(asset.getId(), "consumption", consumptions.get(i), DataConstants.SERVER_SCOPE));
}
Futures.successfulAsList(attributeFutures).get();
AssetSearchQueryFilter filter = new AssetSearchQueryFilter();
filter.setRootEntity(tenantId);
filter.setDirection(EntitySearchDirection.FROM);
filter.setRelationType("Manages");
EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "consumption"));
EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
List<EntityData> loadedEntities = new ArrayList<>(data.getData());
while (data.hasNext()) {
query = query.next();
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities.addAll(data.getData());
}
Assert.assertEquals(5, loadedEntities.size());
List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("consumption").getValue()).collect(Collectors.toList());
List<String> deviceTemperatures = consumptions.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
Assert.assertEquals(deviceTemperatures, loadedTemperatures);
pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
KeyFilter highTemperatureFilter = new KeyFilter();
highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "consumption"));
NumericFilterPredicate predicate = new NumericFilterPredicate();
predicate.setValue(FilterPredicateValue.fromDouble(50));
predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
highTemperatureFilter.setPredicate(predicate);
List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = new ArrayList<>(data.getData());
while (data.hasNext()) {
query = query.next();
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities.addAll(data.getData());
}
Assert.assertEquals(highConsumptions.size(), loadedEntities.size());
List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("consumption").getValue()).collect(Collectors.toList());
List<String> deviceHighTemperatures = highConsumptions.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
deviceService.deleteDevicesByTenantId(tenantId);
}
use of org.thingsboard.server.common.data.query.EntityDataQuery in project thingsboard by thingsboard.
the class BaseEntityServiceTest method testBuildStringPredicateQueryOperations.
@Test
public void testBuildStringPredicateQueryOperations() throws ExecutionException, InterruptedException {
List<Device> devices = new ArrayList<>();
List<String> attributeStrings = new ArrayList<>();
List<String> equalStrings = new ArrayList<>();
List<String> notEqualStrings = new ArrayList<>();
List<String> startsWithStrings = new ArrayList<>();
List<String> endsWithStrings = new ArrayList<>();
List<String> containsStrings = new ArrayList<>();
List<String> notContainsStrings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Device device = new Device();
device.setTenantId(tenantId);
device.setName("Device" + i);
device.setType("default");
device.setLabel("testLabel" + (int) (Math.random() * 1000));
devices.add(deviceService.saveDevice(device));
// TO make sure devices have different created time
Thread.sleep(1);
List<StringFilterPredicate.StringOperation> operationValues = Arrays.asList(StringFilterPredicate.StringOperation.values());
StringFilterPredicate.StringOperation operation = operationValues.get(new Random().nextInt(operationValues.size()));
String operationName = operation.name();
attributeStrings.add(operationName);
switch(operation) {
case EQUAL:
equalStrings.add(operationName);
notContainsStrings.add(operationName);
notEqualStrings.add(operationName);
break;
case NOT_EQUAL:
notContainsStrings.add(operationName);
break;
case STARTS_WITH:
notEqualStrings.add(operationName);
startsWithStrings.add(operationName);
endsWithStrings.add(operationName);
notContainsStrings.add(operationName);
break;
case ENDS_WITH:
notEqualStrings.add(operationName);
endsWithStrings.add(operationName);
notContainsStrings.add(operationName);
break;
case CONTAINS:
notEqualStrings.add(operationName);
notContainsStrings.add(operationName);
containsStrings.add(operationName);
break;
case NOT_CONTAINS:
notEqualStrings.add(operationName);
containsStrings.add(operationName);
break;
case IN:
notEqualStrings.add(operationName);
notContainsStrings.add(operationName);
break;
case NOT_IN:
notEqualStrings.add(operationName);
notContainsStrings.add(operationName);
break;
}
}
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < devices.size(); i++) {
Device device = devices.get(i);
attributeFutures.add(saveStringAttribute(device.getId(), "attributeString", attributeStrings.get(i), DataConstants.CLIENT_SCOPE));
}
Futures.successfulAsList(attributeFutures).get();
DeviceTypeFilter filter = new DeviceTypeFilter();
filter.setDeviceType("default");
filter.setDeviceNameFilter("");
EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.DESC);
List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "entityType"));
List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.CLIENT_ATTRIBUTE, "attributeString"));
List<KeyFilter> keyFiltersEqualString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.EQUAL, "equal");
List<KeyFilter> keyFiltersNotEqualString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.NOT_EQUAL, "NOT_EQUAL");
List<KeyFilter> keyFiltersStartsWithString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.STARTS_WITH, "starts_");
List<KeyFilter> keyFiltersEndsWithString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.ENDS_WITH, "_WITH");
List<KeyFilter> keyFiltersContainsString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.CONTAINS, "contains");
List<KeyFilter> keyFiltersNotContainsString = createStringKeyFilters("attributeString", EntityKeyType.CLIENT_ATTRIBUTE, StringFilterPredicate.StringOperation.NOT_CONTAINS, "NOT_CONTAINS");
List<KeyFilter> deviceTypeFilters = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_EQUAL, "NOT_EQUAL");
// Equal Operation
EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersEqualString);
PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
List<EntityData> loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(equalStrings.size(), loadedEntities.size());
List<String> loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(equalStrings, loadedStrings));
// Not equal Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersNotEqualString);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(notEqualStrings.size(), loadedEntities.size());
loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(notEqualStrings, loadedStrings));
// Starts with Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersStartsWithString);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(startsWithStrings.size(), loadedEntities.size());
loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(startsWithStrings, loadedStrings));
// Ends with Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersEndsWithString);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(endsWithStrings.size(), loadedEntities.size());
loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(endsWithStrings, loadedStrings));
// Contains Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersContainsString);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(containsStrings.size(), loadedEntities.size());
loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(containsStrings, loadedStrings));
// Not contains Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFiltersNotContainsString);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(notContainsStrings.size(), loadedEntities.size());
loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.CLIENT_ATTRIBUTE).get("attributeString").getValue()).collect(Collectors.toList());
Assert.assertTrue(listEqualWithoutOrder(notContainsStrings, loadedStrings));
// Device type filters Operation
pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, deviceTypeFilters);
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities = getLoadedEntities(data, query);
Assert.assertEquals(devices.size(), loadedEntities.size());
deviceService.deleteDevicesByTenantId(tenantId);
}
use of org.thingsboard.server.common.data.query.EntityDataQuery in project thingsboard by thingsboard.
the class BaseEntityServiceTest method testMultiRootHierarchicalFindEntityDataWithAttributesByQuery.
@Test
public void testMultiRootHierarchicalFindEntityDataWithAttributesByQuery() throws ExecutionException, InterruptedException {
List<Asset> buildings = new ArrayList<>();
List<Asset> apartments = new ArrayList<>();
Map<String, Map<UUID, String>> entityNameByTypeMap = new HashMap<>();
Map<UUID, UUID> childParentRelationMap = new HashMap<>();
createMultiRootHierarchy(buildings, apartments, entityNameByTypeMap, childParentRelationMap);
RelationsQueryFilter filter = new RelationsQueryFilter();
filter.setMultiRoot(true);
filter.setMultiRootEntitiesType(EntityType.ASSET);
filter.setMultiRootEntityIds(buildings.stream().map(IdBased::getId).map(d -> d.getId().toString()).collect(Collectors.toSet()));
filter.setDirection(EntitySearchDirection.FROM);
EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
List<EntityKey> entityFields = Lists.newArrayList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "parentId"), new EntityKey(EntityKeyType.ENTITY_FIELD, "type"));
List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "status"));
KeyFilter onlineStatusFilter = new KeyFilter();
onlineStatusFilter.setKey(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
StringFilterPredicate predicate = new StringFilterPredicate();
predicate.setOperation(StringOperation.ENDS_WITH);
predicate.setValue(FilterPredicateValue.fromString("_1"));
onlineStatusFilter.setPredicate(predicate);
List<KeyFilter> keyFilters = Collections.singletonList(onlineStatusFilter);
EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
List<EntityData> loadedEntities = new ArrayList<>(data.getData());
while (data.hasNext()) {
query = query.next();
data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
loadedEntities.addAll(data.getData());
}
long expectedEntitiesCnt = entityNameByTypeMap.entrySet().stream().filter(e -> !e.getKey().equals("building")).flatMap(e -> e.getValue().entrySet().stream()).map(Map.Entry::getValue).filter(e -> StringUtils.endsWith(e, "_1")).count();
Assert.assertEquals(expectedEntitiesCnt, loadedEntities.size());
Map<UUID, UUID> actualRelations = new HashMap<>();
loadedEntities.forEach(ed -> {
UUID parentId = UUID.fromString(ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("parentId").getValue());
UUID entityId = ed.getEntityId().getId();
Assert.assertEquals(childParentRelationMap.get(entityId), parentId);
actualRelations.put(entityId, parentId);
String entityType = ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("type").getValue();
String actualEntityName = ed.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue();
String expectedEntityName = entityNameByTypeMap.get(entityType).get(entityId);
Assert.assertEquals(expectedEntityName, actualEntityName);
});
deviceService.deleteDevicesByTenantId(tenantId);
assetService.deleteAssetsByTenantId(tenantId);
}
Aggregations