Search in sources :

Example 1 with RelationsQueryFilter

use of org.thingsboard.server.common.data.query.RelationsQueryFilter 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);
}
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) ArrayList(java.util.ArrayList) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) 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)

Example 2 with RelationsQueryFilter

use of org.thingsboard.server.common.data.query.RelationsQueryFilter 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 3 with RelationsQueryFilter

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

the class BaseEntityServiceTest method testCountHierarchicalEntitiesByMultiRootQuery.

@Test
public void testCountHierarchicalEntitiesByMultiRootQuery() throws 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);
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(63, count);
    filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("AptToHeat", Collections.singletonList(EntityType.DEVICE))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(27, count);
    filter.setMultiRootEntitiesType(EntityType.ASSET);
    filter.setMultiRootEntityIds(apartments.stream().map(IdBased::getId).map(d -> d.getId().toString()).collect(Collectors.toSet()));
    filter.setDirection(EntitySearchDirection.TO);
    filter.setFilters(Lists.newArrayList(new RelationEntityTypeFilter("buildingToApt", Collections.singletonList(EntityType.ASSET)), new RelationEntityTypeFilter("AptToEnergy", Collections.singletonList(EntityType.DEVICE))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(9, count);
    deviceService.deleteDevicesByTenantId(tenantId);
    assetService.deleteAssetsByTenantId(tenantId);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Asset(org.thingsboard.server.common.data.asset.Asset) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with RelationsQueryFilter

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

the class BaseEntityServiceTest method testCountHierarchicalEntitiesByQuery.

@Test
public void testCountHierarchicalEntitiesByQuery() throws InterruptedException {
    List<Asset> assets = new ArrayList<>();
    List<Device> devices = new ArrayList<>();
    createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    RelationsQueryFilter filter = new RelationsQueryFilter();
    filter.setRootEntity(tenantId);
    filter.setDirection(EntitySearchDirection.FROM);
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    // due to the loop relations in hierarchy, the TenantId included in total count (1*Tenant + 5*Asset + 5*5*Devices = 31)
    Assert.assertEquals(31, count);
    filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("Contains", Collections.singletonList(EntityType.DEVICE))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(25, count);
    filter.setRootEntity(devices.get(0).getId());
    filter.setDirection(EntitySearchDirection.TO);
    filter.setFilters(Collections.singletonList(new RelationEntityTypeFilter("Manages", Collections.singletonList(EntityType.TENANT))));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(1, count);
    DeviceSearchQueryFilter filter2 = new DeviceSearchQueryFilter();
    filter2.setRootEntity(tenantId);
    filter2.setDirection(EntitySearchDirection.FROM);
    filter2.setRelationType("Contains");
    countQuery = new EntityCountQuery(filter2);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(25, count);
    filter2.setDeviceTypes(Arrays.asList("default0", "default1"));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(10, count);
    filter2.setRootEntity(devices.get(0).getId());
    filter2.setDirection(EntitySearchDirection.TO);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
    AssetSearchQueryFilter filter3 = new AssetSearchQueryFilter();
    filter3.setRootEntity(tenantId);
    filter3.setDirection(EntitySearchDirection.FROM);
    filter3.setRelationType("Manages");
    countQuery = new EntityCountQuery(filter3);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(5, count);
    filter3.setAssetTypes(Arrays.asList("type0", "type1"));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(2, count);
    filter3.setRootEntity(devices.get(0).getId());
    filter3.setDirection(EntitySearchDirection.TO);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) Asset(org.thingsboard.server.common.data.asset.Asset) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Asset (org.thingsboard.server.common.data.asset.Asset)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)4 RelationsQueryFilter (org.thingsboard.server.common.data.query.RelationsQueryFilter)4 RelationEntityTypeFilter (org.thingsboard.server.common.data.relation.RelationEntityTypeFilter)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 UUID (java.util.UUID)3 Device (org.thingsboard.server.common.data.Device)3 Lists (com.google.common.collect.Lists)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Random (java.util.Random)2 ExecutionException (java.util.concurrent.ExecutionException)2