Search in sources :

Example 6 with EntityCountQuery

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

the class BaseEntityServiceTest method testCountHierarchicalEntitiesByEdgeSearchQuery.

@Test
public void testCountHierarchicalEntitiesByEdgeSearchQuery() throws InterruptedException {
    for (int i = 0; i < 5; i++) {
        Edge edge = createEdge(i, "type" + i);
        edge = edgeService.saveEdge(edge);
        // TO make sure devices have different created time
        Thread.sleep(1);
        EntityRelation er = new EntityRelation();
        er.setFrom(tenantId);
        er.setTo(edge.getId());
        er.setType("Manages");
        er.setTypeGroup(RelationTypeGroup.COMMON);
        relationService.saveRelation(tenantId, er);
    }
    EdgeSearchQueryFilter filter = new EdgeSearchQueryFilter();
    filter.setRootEntity(tenantId);
    filter.setDirection(EntitySearchDirection.FROM);
    filter.setRelationType("Manages");
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(5, count);
    filter.setEdgeTypes(Arrays.asList("type0", "type1"));
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(2, count);
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 7 with EntityCountQuery

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

the class BaseEntityServiceTest method testCountEdgeEntitiesByQuery.

@Test
public void testCountEdgeEntitiesByQuery() throws InterruptedException {
    List<Edge> edges = new ArrayList<>();
    for (int i = 0; i < 97; i++) {
        Edge edge = createEdge(i, "default");
        edges.add(edgeService.saveEdge(edge));
    }
    EdgeTypeFilter filter = new EdgeTypeFilter();
    filter.setEdgeType("default");
    filter.setEdgeNameFilter("");
    EntityCountQuery countQuery = new EntityCountQuery(filter);
    long count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(97, count);
    filter.setEdgeType("unknown");
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
    filter.setEdgeType("default");
    filter.setEdgeNameFilter("Edge1");
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(11, count);
    EntityListFilter entityListFilter = new EntityListFilter();
    entityListFilter.setEntityType(EntityType.EDGE);
    entityListFilter.setEntityList(edges.stream().map(Edge::getId).map(EdgeId::toString).collect(Collectors.toList()));
    countQuery = new EntityCountQuery(entityListFilter);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(97, count);
    edgeService.deleteEdgesByTenantId(tenantId);
    count = entityService.countEntitiesByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), countQuery);
    Assert.assertEquals(0, count);
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) ArrayList(java.util.ArrayList) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) Test(org.junit.Test)

Example 8 with EntityCountQuery

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

EntityCountQuery (org.thingsboard.server.common.data.query.EntityCountQuery)8 Test (org.junit.Test)7 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 ArrayList (java.util.ArrayList)5 Device (org.thingsboard.server.common.data.Device)4 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)4 EntityListFilter (org.thingsboard.server.common.data.query.EntityListFilter)4 RelationsQueryFilter (org.thingsboard.server.common.data.query.RelationsQueryFilter)3 RelationEntityTypeFilter (org.thingsboard.server.common.data.relation.RelationEntityTypeFilter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Asset (org.thingsboard.server.common.data.asset.Asset)2 Edge (org.thingsboard.server.common.data.edge.Edge)2 DeviceId (org.thingsboard.server.common.data.id.DeviceId)2 AssetSearchQueryFilter (org.thingsboard.server.common.data.query.AssetSearchQueryFilter)2 DeviceSearchQueryFilter (org.thingsboard.server.common.data.query.DeviceSearchQueryFilter)2 EdgeSearchQueryFilter (org.thingsboard.server.common.data.query.EdgeSearchQueryFilter)2 EdgeTypeFilter (org.thingsboard.server.common.data.query.EdgeTypeFilter)2 EntityTypeFilter (org.thingsboard.server.common.data.query.EntityTypeFilter)2