use of org.thingsboard.server.common.data.edge.Edge 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);
}
use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method testFindEdgeRuleChainsByTenantIdAndName.
@Test
public void testFindEdgeRuleChainsByTenantIdAndName() {
Edge edge = constructEdge(tenantId, "My edge", "default");
Edge savedEdge = edgeService.saveEdge(edge);
String name1 = "Edge RuleChain name 1";
List<RuleChain> ruleChainsName1 = new ArrayList<>();
for (int i = 0; i < 123; i++) {
RuleChain ruleChain = new RuleChain();
ruleChain.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric((int) (Math.random() * 17));
String name = name1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
ruleChain.setName(name);
ruleChain.setType(RuleChainType.EDGE);
ruleChainsName1.add(ruleChainService.saveRuleChain(ruleChain));
}
ruleChainsName1.forEach(ruleChain -> ruleChainService.assignRuleChainToEdge(tenantId, ruleChain.getId(), savedEdge.getId()));
String name2 = "Edge RuleChain name 2";
List<RuleChain> ruleChainsName2 = new ArrayList<>();
for (int i = 0; i < 193; i++) {
RuleChain ruleChain = new RuleChain();
ruleChain.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric((int) (Math.random() * 15));
String name = name2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
ruleChain.setName(name);
ruleChain.setType(RuleChainType.EDGE);
ruleChainsName2.add(ruleChainService.saveRuleChain(ruleChain));
}
ruleChainsName2.forEach(ruleChain -> ruleChainService.assignRuleChainToEdge(tenantId, ruleChain.getId(), savedEdge.getId()));
List<RuleChain> loadedRuleChainsName1 = new ArrayList<>();
PageLink pageLink = new PageLink(19, 0, name1);
PageData<RuleChain> pageData = null;
do {
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, savedEdge.getId(), pageLink);
loadedRuleChainsName1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(ruleChainsName1, idComparator);
Collections.sort(loadedRuleChainsName1, idComparator);
Assert.assertEquals(ruleChainsName1, loadedRuleChainsName1);
List<RuleChain> loadedRuleChainsName2 = new ArrayList<>();
pageLink = new PageLink(4, 0, name2);
do {
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, savedEdge.getId(), pageLink);
loadedRuleChainsName2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(ruleChainsName2, idComparator);
Collections.sort(loadedRuleChainsName2, idComparator);
Assert.assertEquals(ruleChainsName2, loadedRuleChainsName2);
for (RuleChain ruleChain : loadedRuleChainsName1) {
ruleChainService.deleteRuleChainById(tenantId, ruleChain.getId());
}
pageLink = new PageLink(4, 0, name1);
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, savedEdge.getId(), pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (RuleChain ruleChain : loadedRuleChainsName2) {
ruleChainService.deleteRuleChainById(tenantId, ruleChain.getId());
}
pageLink = new PageLink(4, 0, name2);
pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, savedEdge.getId(), pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
}
use of org.thingsboard.server.common.data.edge.Edge 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);
}
Aggregations