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