Search in sources :

Example 61 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method testSaveRuleChain.

@Test
public void testSaveRuleChain() throws IOException {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setTenantId(tenantId);
    ruleChain.setName("My RuleChain");
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    Assert.assertNotNull(savedRuleChain);
    Assert.assertNotNull(savedRuleChain.getId());
    Assert.assertTrue(savedRuleChain.getCreatedTime() > 0);
    Assert.assertEquals(ruleChain.getTenantId(), savedRuleChain.getTenantId());
    Assert.assertEquals(ruleChain.getName(), savedRuleChain.getName());
    savedRuleChain.setName("My new RuleChain");
    ruleChainService.saveRuleChain(savedRuleChain);
    RuleChain foundRuleChain = ruleChainService.findRuleChainById(tenantId, savedRuleChain.getId());
    Assert.assertEquals(foundRuleChain.getName(), savedRuleChain.getName());
    ruleChainService.deleteRuleChainById(tenantId, savedRuleChain.getId());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Test(org.junit.Test)

Example 62 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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 63 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method testFindRuleChainsByTenantIdAndName.

@Test
public void testFindRuleChainsByTenantIdAndName() {
    String name1 = "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);
        ruleChainsName1.add(ruleChainService.saveRuleChain(ruleChain));
    }
    String name2 = "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);
        ruleChainsName2.add(ruleChainService.saveRuleChain(ruleChain));
    }
    List<RuleChain> loadedRuleChainsName1 = new ArrayList<>();
    PageLink pageLink = new PageLink(19, 0, name1);
    PageData<RuleChain> pageData = null;
    do {
        pageData = ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, 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.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, 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.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, 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.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, 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) Test(org.junit.Test)

Example 64 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method createRuleChainMetadataWithCirclingRelation2.

private RuleChainMetaData createRuleChainMetadataWithCirclingRelation2() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    ruleChainMetaData.addConnectionInfo(2, 0, "success");
    return ruleChainMetaData;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 65 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method saveRuleChainAndSetAutoAssignToEdge.

private RuleChainId saveRuleChainAndSetAutoAssignToEdge(String name) {
    RuleChain edgeRuleChain = new RuleChain();
    edgeRuleChain.setTenantId(tenantId);
    edgeRuleChain.setType(RuleChainType.EDGE);
    edgeRuleChain.setName(name);
    RuleChain savedEdgeRuleChain = ruleChainService.saveRuleChain(edgeRuleChain);
    ruleChainService.setAutoAssignToEdgeRuleChain(tenantId, savedEdgeRuleChain.getId());
    return savedEdgeRuleChain.getId();
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain)

Aggregations

RuleChain (org.thingsboard.server.common.data.rule.RuleChain)67 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)26 Test (org.junit.Test)20 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)19 ArrayList (java.util.ArrayList)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)15 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)14 ApiOperation (io.swagger.annotations.ApiOperation)13 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)13 Edge (org.thingsboard.server.common.data.edge.Edge)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 PageLink (org.thingsboard.server.common.data.page.PageLink)11 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)9 EdgeId (org.thingsboard.server.common.data.id.EdgeId)8 List (java.util.List)7 Device (org.thingsboard.server.common.data.Device)7