Search in sources :

Example 96 with Edge

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);
}
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 97 with Edge

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());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 98 with Edge

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);
}
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)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13