18 changed files with 363 additions and 255 deletions
@ -0,0 +1,158 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.rules.lifecycle; |
|||
|
|||
import com.datastax.driver.core.utils.UUIDs; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.junit.After; |
|||
import org.junit.Assert; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration; |
|||
import org.thingsboard.server.actors.service.ActorService; |
|||
import org.thingsboard.server.common.data.DataConstants; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.Event; |
|||
import org.thingsboard.server.common.data.Tenant; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
|||
import org.thingsboard.server.common.data.kv.StringDataEntry; |
|||
import org.thingsboard.server.common.data.page.TimePageData; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
import org.thingsboard.server.common.data.rule.RuleNode; |
|||
import org.thingsboard.server.common.data.security.Authority; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.common.msg.system.ServiceToRuleEngineMsg; |
|||
import org.thingsboard.server.controller.AbstractRuleEngineControllerTest; |
|||
import org.thingsboard.server.dao.attributes.AttributesService; |
|||
|
|||
import java.util.Collections; |
|||
|
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
/** |
|||
* @author Valerii Sosliuk |
|||
*/ |
|||
@Slf4j |
|||
public abstract class AbstractRuleEngineLifecycleIntegrationTest extends AbstractRuleEngineControllerTest { |
|||
|
|||
protected Tenant savedTenant; |
|||
protected User tenantAdmin; |
|||
|
|||
@Autowired |
|||
protected ActorService actorService; |
|||
|
|||
@Autowired |
|||
protected AttributesService attributesService; |
|||
|
|||
@Before |
|||
public void beforeTest() throws Exception { |
|||
loginSysAdmin(); |
|||
|
|||
Tenant tenant = new Tenant(); |
|||
tenant.setTitle("My tenant"); |
|||
savedTenant = doPost("/api/tenant", tenant, Tenant.class); |
|||
Assert.assertNotNull(savedTenant); |
|||
|
|||
tenantAdmin = new User(); |
|||
tenantAdmin.setAuthority(Authority.TENANT_ADMIN); |
|||
tenantAdmin.setTenantId(savedTenant.getId()); |
|||
tenantAdmin.setEmail("tenant2@thingsboard.org"); |
|||
tenantAdmin.setFirstName("Joe"); |
|||
tenantAdmin.setLastName("Downs"); |
|||
|
|||
createUserAndLogin(tenantAdmin, "testPassword1"); |
|||
} |
|||
|
|||
@After |
|||
public void afterTest() throws Exception { |
|||
loginSysAdmin(); |
|||
if (savedTenant != null) { |
|||
doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk()); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testRuleChainWithOneRule() throws Exception { |
|||
// Creating Rule Chain
|
|||
RuleChain ruleChain = new RuleChain(); |
|||
ruleChain.setName("Simple Rule Chain"); |
|||
ruleChain.setTenantId(savedTenant.getId()); |
|||
ruleChain.setRoot(true); |
|||
ruleChain.setDebugMode(true); |
|||
ruleChain = saveRuleChain(ruleChain); |
|||
Assert.assertNull(ruleChain.getFirstRuleNodeId()); |
|||
|
|||
RuleChainMetaData metaData = new RuleChainMetaData(); |
|||
metaData.setRuleChainId(ruleChain.getId()); |
|||
|
|||
RuleNode ruleNode = new RuleNode(); |
|||
ruleNode.setName("Simple Rule Node"); |
|||
ruleNode.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); |
|||
ruleNode.setDebugMode(true); |
|||
TbGetAttributesNodeConfiguration configuration = new TbGetAttributesNodeConfiguration(); |
|||
configuration.setServerAttributeNames(Collections.singletonList("serverAttributeKey")); |
|||
ruleNode.setConfiguration(mapper.valueToTree(configuration)); |
|||
|
|||
metaData.setNodes(Collections.singletonList(ruleNode)); |
|||
metaData.setFirstNodeIndex(0); |
|||
|
|||
metaData = saveRuleChainMetaData(metaData); |
|||
Assert.assertNotNull(metaData); |
|||
|
|||
ruleChain = getRuleChain(ruleChain.getId()); |
|||
Assert.assertNotNull(ruleChain.getFirstRuleNodeId()); |
|||
|
|||
// Saving the device
|
|||
Device device = new Device(); |
|||
device.setName("My device"); |
|||
device.setType("default"); |
|||
device = doPost("/api/device", device, Device.class); |
|||
|
|||
attributesService.save(device.getId(), DataConstants.SERVER_SCOPE, |
|||
Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey", "serverAttributeValue"), System.currentTimeMillis()))); |
|||
|
|||
Thread.sleep(1000); |
|||
|
|||
// Pushing Message to the system
|
|||
TbMsg tbMsg = new TbMsg(UUIDs.timeBased(), |
|||
"CUSTOM", |
|||
device.getId(), |
|||
new TbMsgMetaData(), |
|||
new byte[]{}); |
|||
actorService.onMsg(new ServiceToRuleEngineMsg(savedTenant.getId(), tbMsg)); |
|||
|
|||
Thread.sleep(3000); |
|||
|
|||
TimePageData<Event> events = getDebugEvents(savedTenant.getId(), ruleChain.getFirstRuleNodeId(), 1000); |
|||
|
|||
Assert.assertEquals(2, events.getData().size()); |
|||
|
|||
Event inEvent = events.getData().stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get(); |
|||
Assert.assertEquals(ruleChain.getFirstRuleNodeId(), inEvent.getEntityId()); |
|||
Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText()); |
|||
|
|||
Event outEvent = events.getData().stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get(); |
|||
Assert.assertEquals(ruleChain.getFirstRuleNodeId(), outEvent.getEntityId()); |
|||
Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText()); |
|||
|
|||
Assert.assertEquals("serverAttributeValue", outEvent.getBody().get("metadata").get("ss.serverAttributeKey").asText()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.rules.lifecycle; |
|||
|
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.rules.flow.AbstractRuleEngineFlowIntegrationTest; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 8/22/2017. |
|||
*/ |
|||
@DaoSqlTest |
|||
public class RuleEngineLifecycleSqlIntegrationTest extends AbstractRuleEngineLifecycleIntegrationTest { |
|||
} |
|||
@ -1,163 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.service.rule; |
|||
|
|||
import com.datastax.driver.core.utils.UUIDs; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.TextPageData; |
|||
import org.thingsboard.server.common.data.page.TextPageLink; |
|||
import org.thingsboard.server.common.data.plugin.PluginMetaData; |
|||
import org.thingsboard.server.common.data.rule.RuleMetaData; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.service.AbstractServiceTest; |
|||
|
|||
import java.util.List; |
|||
import java.util.concurrent.ThreadLocalRandom; |
|||
|
|||
public abstract class BaseRuleServiceTest extends AbstractServiceTest { |
|||
|
|||
@Test |
|||
public void saveRule() throws Exception { |
|||
PluginMetaData plugin = generatePlugin(null, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(plugin); |
|||
RuleMetaData ruleMetaData = ruleService.saveRule(generateRule(plugin.getTenantId(), null, plugin.getApiToken())); |
|||
Assert.assertNotNull(ruleMetaData.getId()); |
|||
Assert.assertNotNull(ruleMetaData.getAdditionalInfo()); |
|||
ruleMetaData.setAdditionalInfo(mapper.readTree("{\"description\":\"test\"}")); |
|||
RuleMetaData newRuleMetaData = ruleService.saveRule(ruleMetaData); |
|||
Assert.assertEquals(ruleMetaData.getAdditionalInfo(), newRuleMetaData.getAdditionalInfo()); |
|||
} |
|||
|
|||
@Test |
|||
public void findRuleById() throws Exception { |
|||
PluginMetaData plugin = generatePlugin(null, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(plugin); |
|||
|
|||
RuleMetaData expected = ruleService.saveRule(generateRule(plugin.getTenantId(), null, plugin.getApiToken())); |
|||
Assert.assertNotNull(expected.getId()); |
|||
RuleMetaData found = ruleService.findRuleById(expected.getId()); |
|||
Assert.assertEquals(expected, found); |
|||
} |
|||
|
|||
@Test |
|||
public void findPluginRules() throws Exception { |
|||
TenantId tenantIdA = new TenantId(UUIDs.timeBased()); |
|||
TenantId tenantIdB = new TenantId(UUIDs.timeBased()); |
|||
|
|||
PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(pluginA); |
|||
pluginService.savePlugin(pluginB); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
|
|||
List<RuleMetaData> foundA = ruleService.findPluginRules(pluginA.getApiToken()); |
|||
Assert.assertEquals(3, foundA.size()); |
|||
|
|||
List<RuleMetaData> foundB = ruleService.findPluginRules(pluginB.getApiToken()); |
|||
Assert.assertEquals(2, foundB.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void findSystemRules() throws Exception { |
|||
TenantId systemTenant = new TenantId(ModelConstants.NULL_UUID); // system tenant id
|
|||
|
|||
PluginMetaData plugin = generatePlugin(systemTenant, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(plugin); |
|||
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken())); |
|||
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken())); |
|||
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken())); |
|||
TextPageData<RuleMetaData> found = ruleService.findSystemRules(new TextPageLink(100)); |
|||
Assert.assertEquals(3, found.getData().size()); |
|||
} |
|||
|
|||
@Test |
|||
public void findTenantRules() throws Exception { |
|||
TenantId tenantIdA = new TenantId(UUIDs.timeBased()); |
|||
TenantId tenantIdB = new TenantId(UUIDs.timeBased()); |
|||
|
|||
PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(pluginA); |
|||
pluginService.savePlugin(pluginB); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
|
|||
TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100)); |
|||
Assert.assertEquals(3, foundA.getData().size()); |
|||
|
|||
TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100)); |
|||
Assert.assertEquals(2, foundB.getData().size()); |
|||
} |
|||
|
|||
@Test |
|||
public void deleteRuleById() throws Exception { |
|||
PluginMetaData plugin = generatePlugin(null, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(plugin); |
|||
|
|||
RuleMetaData expected = ruleService.saveRule(generateRule(plugin.getTenantId(), null, plugin.getApiToken())); |
|||
Assert.assertNotNull(expected.getId()); |
|||
RuleMetaData found = ruleService.findRuleById(expected.getId()); |
|||
Assert.assertEquals(expected, found); |
|||
ruleService.deleteRuleById(expected.getId()); |
|||
found = ruleService.findRuleById(expected.getId()); |
|||
Assert.assertNull(found); |
|||
} |
|||
|
|||
@Test |
|||
public void deleteRulesByTenantId() throws Exception { |
|||
TenantId tenantIdA = new TenantId(UUIDs.timeBased()); |
|||
TenantId tenantIdB = new TenantId(UUIDs.timeBased()); |
|||
|
|||
PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt()); |
|||
pluginService.savePlugin(pluginA); |
|||
pluginService.savePlugin(pluginB); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken())); |
|||
|
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken())); |
|||
|
|||
TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100)); |
|||
Assert.assertEquals(3, foundA.getData().size()); |
|||
|
|||
TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100)); |
|||
Assert.assertEquals(2, foundB.getData().size()); |
|||
|
|||
ruleService.deleteRulesByTenantId(tenantIdA); |
|||
|
|||
foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100)); |
|||
Assert.assertEquals(0, foundA.getData().size()); |
|||
|
|||
foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100)); |
|||
Assert.assertEquals(2, foundB.getData().size()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue