|
|
|
@ -21,14 +21,17 @@ import org.junit.After; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.mockito.Mockito; |
|
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
import org.thingsboard.server.common.data.edge.Edge; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleChain; |
|
|
|
import org.thingsboard.server.common.data.rule.RuleChainType; |
|
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
@ -75,22 +78,45 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
public void testSaveRuleChain() throws Exception { |
|
|
|
RuleChain ruleChain = new RuleChain(); |
|
|
|
ruleChain.setName("RuleChain"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
RuleChain savedRuleChain = doPost("/api/ruleChain", ruleChain, RuleChain.class); |
|
|
|
Assert.assertNotNull(savedRuleChain); |
|
|
|
Assert.assertNotNull(savedRuleChain.getId()); |
|
|
|
Assert.assertTrue(savedRuleChain.getCreatedTime() > 0); |
|
|
|
Assert.assertEquals(ruleChain.getName(), savedRuleChain.getName()); |
|
|
|
|
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedRuleChain, savedRuleChain.getId(), savedRuleChain.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED); |
|
|
|
|
|
|
|
savedRuleChain.setName("New RuleChain"); |
|
|
|
doPost("/api/ruleChain", savedRuleChain, RuleChain.class); |
|
|
|
RuleChain foundRuleChain = doGet("/api/ruleChain/" + savedRuleChain.getId().getId().toString(), RuleChain.class); |
|
|
|
Assert.assertEquals(savedRuleChain.getName(), foundRuleChain.getName()); |
|
|
|
|
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedRuleChain, savedRuleChain.getId(), savedRuleChain.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.UPDATED); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testSaveRuleChainWithViolationOfLengthValidation() throws Exception { |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
RuleChain ruleChain = new RuleChain(); |
|
|
|
ruleChain.setName(RandomStringUtils.randomAlphabetic(300)); |
|
|
|
doPost("/api/ruleChain", ruleChain).andExpect(statusReason(containsString("length of name must be equal or less than 255"))); |
|
|
|
String msgError = msgErrorFieldLength("name"); |
|
|
|
doPost("/api/ruleChain", ruleChain) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
ruleChain.setTenantId(savedTenant.getId()); |
|
|
|
testNotifyEntityEqualsOneTimeServiceNeverError(ruleChain, |
|
|
|
savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED, new DataValidationException(msgError)); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -109,11 +135,19 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
ruleChain.setName("RuleChain"); |
|
|
|
RuleChain savedRuleChain = doPost("/api/ruleChain", ruleChain, RuleChain.class); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
String entityIdStr = savedRuleChain.getId().getId().toString(); |
|
|
|
doDelete("/api/ruleChain/" + savedRuleChain.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
doGet("/api/ruleChain/" + savedRuleChain.getId().getId().toString()) |
|
|
|
.andExpect(status().isNotFound()); |
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedRuleChain, savedRuleChain.getId(), savedRuleChain.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.DELETED, savedRuleChain.getId().getId().toString()); |
|
|
|
|
|
|
|
doGet("/api/ruleChain/" + entityIdStr) |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Rule chain", entityIdStr)))); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -124,12 +158,16 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
List<RuleChain> edgeRuleChains = new ArrayList<>(); |
|
|
|
PageLink pageLink = new PageLink(17); |
|
|
|
PageData<RuleChain> pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", |
|
|
|
new TypeReference<>() {}, pageLink); |
|
|
|
new TypeReference<>() { |
|
|
|
}, pageLink); |
|
|
|
Assert.assertFalse(pageData.hasNext()); |
|
|
|
Assert.assertEquals(1, pageData.getTotalElements()); |
|
|
|
edgeRuleChains.addAll(pageData.getData()); |
|
|
|
|
|
|
|
for (int i = 0; i < 28; i++) { |
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
int cntEntity = 28; |
|
|
|
for (int i = 0; i < cntEntity; i++) { |
|
|
|
RuleChain ruleChain = new RuleChain(); |
|
|
|
ruleChain.setName("RuleChain " + i); |
|
|
|
ruleChain.setType(RuleChainType.EDGE); |
|
|
|
@ -139,11 +177,16 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
edgeRuleChains.add(savedRuleChain); |
|
|
|
} |
|
|
|
|
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAny(new RuleChain(), new RuleChain(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED, ActionType.ADDED, cntEntity, 0, cntEntity * 2); |
|
|
|
|
|
|
|
List<RuleChain> loadedEdgeRuleChains = new ArrayList<>(); |
|
|
|
pageLink = new PageLink(17); |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", |
|
|
|
new TypeReference<>() {}, pageLink); |
|
|
|
new TypeReference<>() { |
|
|
|
}, pageLink); |
|
|
|
loadedEdgeRuleChains.addAll(pageData.getData()); |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
@ -155,6 +198,8 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
|
|
|
|
Assert.assertEquals(edgeRuleChains, loadedEdgeRuleChains); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
for (RuleChain ruleChain : loadedEdgeRuleChains) { |
|
|
|
if (!ruleChain.isRoot()) { |
|
|
|
doDelete("/api/edge/" + savedEdge.getId().getId().toString() |
|
|
|
@ -162,9 +207,14 @@ public abstract class BaseRuleChainControllerTest extends AbstractControllerTest |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAnyAdditionalInfoAny(new RuleChain(), new RuleChain(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.UNASSIGNED_FROM_EDGE, ActionType.UNASSIGNED_FROM_EDGE, cntEntity, cntEntity, 3); |
|
|
|
|
|
|
|
pageLink = new PageLink(17); |
|
|
|
pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", |
|
|
|
new TypeReference<>() {}, pageLink); |
|
|
|
new TypeReference<>() { |
|
|
|
}, pageLink); |
|
|
|
Assert.assertFalse(pageData.hasNext()); |
|
|
|
Assert.assertEquals(1, pageData.getTotalElements()); |
|
|
|
} |
|
|
|
|