Search in sources :

Example 6 with KeyFilter

use of org.thingsboard.server.common.data.query.KeyFilter 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);
}
Also used : StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) Arrays(java.util.Arrays) Edge(org.thingsboard.server.common.data.edge.Edge) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) IdBased(org.thingsboard.server.common.data.id.IdBased) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) After(org.junit.After) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EdgeId(org.thingsboard.server.common.data.id.EdgeId) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) RelationRepository(org.thingsboard.server.dao.sql.relation.RelationRepository) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) Stream(java.util.stream.Stream) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RandomUtils(org.apache.commons.lang3.RandomUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) Before(org.junit.Before) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert.assertEquals(org.junit.Assert.assertEquals) Asset(org.thingsboard.server.common.data.asset.Asset) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) ArrayList(java.util.ArrayList) EntityKey(org.thingsboard.server.common.data.query.EntityKey) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) Random(java.util.Random) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) Test(org.junit.Test)

Example 7 with KeyFilter

use of org.thingsboard.server.common.data.query.KeyFilter 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);
}
Also used : Arrays(java.util.Arrays) Edge(org.thingsboard.server.common.data.edge.Edge) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) IdBased(org.thingsboard.server.common.data.id.IdBased) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) After(org.junit.After) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EdgeId(org.thingsboard.server.common.data.id.EdgeId) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) RelationRepository(org.thingsboard.server.dao.sql.relation.RelationRepository) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) Stream(java.util.stream.Stream) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RandomUtils(org.apache.commons.lang3.RandomUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) Before(org.junit.Before) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert.assertEquals(org.junit.Assert.assertEquals) Asset(org.thingsboard.server.common.data.asset.Asset) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) UUID(java.util.UUID) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) Map(java.util.Map) HashMap(java.util.HashMap) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) Test(org.junit.Test)

Example 8 with KeyFilter

use of org.thingsboard.server.common.data.query.KeyFilter in project thingsboard by thingsboard.

the class BaseEntityServiceTest method testBuildSimplePredicateQueryOperations.

@Test
public void testBuildSimplePredicateQueryOperations() throws InterruptedException {
    List<Device> devices = 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);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), EntityDataSortOrder.Direction.DESC);
    List<KeyFilter> deviceTypeFilters = createStringKeyFilters("type", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.EQUAL, "default");
    KeyFilter createdTimeFilter = createNumericKeyFilter("createdTime", EntityKeyType.ENTITY_FIELD, NumericFilterPredicate.NumericOperation.GREATER, 1L);
    List<KeyFilter> createdTimeFilters = Collections.singletonList(createdTimeFilter);
    List<KeyFilter> nameFilters = createStringKeyFilters("name", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.CONTAINS, "Device");
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "type"));
    // Device type filters
    EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, deviceTypeFilters);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    // Device create time filters
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, createdTimeFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    // Device name filters
    pageLink = new EntityDataPageLink(100, 0, null, null);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, nameFilters);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    deviceService.deleteDevicesByTenantId(tenantId);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Test(org.junit.Test)

Example 9 with KeyFilter

use of org.thingsboard.server.common.data.query.KeyFilter in project thingsboard by thingsboard.

the class BaseEntityServiceTest method testBuildStringPredicateQueryOperationsForEntityType.

@Test
public void testBuildStringPredicateQueryOperationsForEntityType() throws ExecutionException, InterruptedException {
    List<Device> devices = 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);
    }
    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<KeyFilter> keyFiltersEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.EQUAL, "device");
    List<KeyFilter> keyFiltersNotEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_EQUAL, "asset");
    List<KeyFilter> keyFiltersStartsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.STARTS_WITH, "dev");
    List<KeyFilter> keyFiltersEndsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.ENDS_WITH, "ice");
    List<KeyFilter> keyFiltersContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.CONTAINS, "vic");
    List<KeyFilter> keyFiltersNotContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_CONTAINS, "dolphin");
    // Equal Operation
    EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEqualString);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    List<String> loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    List<String> devicesNames = devices.stream().map(Device::getName).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not equal Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotEqualString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Starts with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersStartsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Ends with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEndsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    deviceService.deleteDevicesByTenantId(tenantId);
}
Also used : Arrays(java.util.Arrays) Edge(org.thingsboard.server.common.data.edge.Edge) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) IdBased(org.thingsboard.server.common.data.id.IdBased) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) After(org.junit.After) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EdgeId(org.thingsboard.server.common.data.id.EdgeId) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) RelationRepository(org.thingsboard.server.dao.sql.relation.RelationRepository) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) Stream(java.util.stream.Stream) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RandomUtils(org.apache.commons.lang3.RandomUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) Before(org.junit.Before) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert.assertEquals(org.junit.Assert.assertEquals) Asset(org.thingsboard.server.common.data.asset.Asset) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Test(org.junit.Test)

Example 10 with KeyFilter

use of org.thingsboard.server.common.data.query.KeyFilter in project thingsboard by thingsboard.

the class BaseEntityServiceTest method createDeviceSearchQuery.

private EntityDataQuery createDeviceSearchQuery(String deviceField, StringOperation operation, String searchQuery) {
    DeviceTypeFilter deviceTypeFilter = new DeviceTypeFilter();
    deviceTypeFilter.setDeviceType("default");
    deviceTypeFilter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(1000, 0, null, sortOrder);
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "label"));
    List<KeyFilter> keyFilters = createStringKeyFilters(deviceField, EntityKeyType.ENTITY_FIELD, operation, searchQuery);
    return new EntityDataQuery(deviceTypeFilter, pageLink, entityFields, null, keyFilters);
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink)

Aggregations

KeyFilter (org.thingsboard.server.common.data.query.KeyFilter)18 EntityKey (org.thingsboard.server.common.data.query.EntityKey)17 ArrayList (java.util.ArrayList)13 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)13 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)13 List (java.util.List)12 Test (org.junit.Test)12 Device (org.thingsboard.server.common.data.Device)12 EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)12 Collections (java.util.Collections)11 Collectors (java.util.stream.Collectors)11 DataConstants (org.thingsboard.server.common.data.DataConstants)11 EntityType (org.thingsboard.server.common.data.EntityType)11 Arrays (java.util.Arrays)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Stream (java.util.stream.Stream)10 StringUtils (org.apache.commons.lang3.StringUtils)10 After (org.junit.After)10 Assert (org.junit.Assert)10