Search in sources :

Example 1 with QueueToRuleEngineMsg

use of org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg in project thingsboard by thingsboard.

the class AbstractRuleEngineFlowIntegrationTest method testRuleChainWithTwoRules.

@Test
public void testRuleChainWithTwoRules() 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 ruleNode1 = new RuleNode();
    ruleNode1.setName("Simple Rule Node 1");
    ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode1.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration();
    configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1"));
    ruleNode1.setConfiguration(mapper.valueToTree(configuration1));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("Simple Rule Node 2");
    ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode2.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration();
    configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2"));
    ruleNode2.setConfiguration(mapper.valueToTree(configuration2));
    metaData.setNodes(Arrays.asList(ruleNode1, ruleNode2));
    metaData.setFirstNodeIndex(0);
    metaData.addConnectionInfo(0, 1, "Success");
    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.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey1", "serverAttributeValue1"), System.currentTimeMillis()))).get();
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey2", "serverAttributeValue2"), System.currentTimeMillis()))).get();
    TbMsgCallback tbMsgCallback = Mockito.mock(TbMsgCallback.class);
    Mockito.when(tbMsgCallback.isMsgValid()).thenReturn(true);
    TbMsg tbMsg = TbMsg.newMsg("CUSTOM", device.getId(), new TbMsgMetaData(), "{}", tbMsgCallback);
    QueueToRuleEngineMsg qMsg = new QueueToRuleEngineMsg(savedTenant.getId(), tbMsg, null, null);
    // Pushing Message to the system
    actorSystem.tell(qMsg);
    Mockito.verify(tbMsgCallback, Mockito.timeout(10000)).onSuccess();
    PageData<Event> eventsPage = getDebugEvents(savedTenant.getId(), ruleChain.getFirstRuleNodeId(), 1000);
    List<Event> events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    Event inEvent = events.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.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("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    RuleChain finalRuleChain = ruleChain;
    RuleNode lastRuleNode = metaData.getNodes().stream().filter(node -> !node.getId().equals(finalRuleChain.getFirstRuleNodeId())).findFirst().get();
    eventsPage = getDebugEvents(savedTenant.getId(), lastRuleNode.getId(), 1000);
    events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    Assert.assertEquals("serverAttributeValue2", getMetadata(outEvent).get("ss_serverAttributeKey2").asText());
}
Also used : Event(org.thingsboard.server.common.data.Event) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Device(org.thingsboard.server.common.data.Device) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) Mockito.spy(org.mockito.Mockito.spy) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) Answer(org.mockito.stubbing.Answer) User(org.thingsboard.server.common.data.User) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) After(org.junit.After) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) Before(org.junit.Before) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) DataConstants(org.thingsboard.server.common.data.DataConstants) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Test(org.junit.Test) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) Slf4j(lombok.extern.slf4j.Slf4j) EventService(org.thingsboard.server.dao.event.EventService) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Assert(org.junit.Assert) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) Collections(java.util.Collections) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Device(org.thingsboard.server.common.data.Device) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Event(org.thingsboard.server.common.data.Event) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) TbMsg(org.thingsboard.server.common.msg.TbMsg) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) Test(org.junit.Test)

Example 2 with QueueToRuleEngineMsg

use of org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg in project thingsboard by thingsboard.

the class AbstractRuleEngineLifecycleIntegrationTest method testRuleChainWithOneRule.

@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.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey", "serverAttributeValue"), System.currentTimeMillis())));
    await("total inMemory queue lag is empty").atMost(30, TimeUnit.SECONDS).until(() -> InMemoryStorage.getInstance().getLagTotal() == 0);
    Thread.sleep(1000);
    TbMsgCallback tbMsgCallback = Mockito.mock(TbMsgCallback.class);
    Mockito.when(tbMsgCallback.isMsgValid()).thenReturn(true);
    TbMsg tbMsg = TbMsg.newMsg("CUSTOM", device.getId(), new TbMsgMetaData(), "{}", tbMsgCallback);
    QueueToRuleEngineMsg qMsg = new QueueToRuleEngineMsg(savedTenant.getId(), tbMsg, null, null);
    // Pushing Message to the system
    actorSystem.tell(qMsg);
    Mockito.verify(tbMsgCallback, Mockito.timeout(10000)).onSuccess();
    PageData<Event> eventsPage = getDebugEvents(savedTenant.getId(), ruleChain.getFirstRuleNodeId(), 1000);
    List<Event> events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    Event inEvent = events.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.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", getMetadata(outEvent).get("ss_serverAttributeKey").asText());
}
Also used : QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Device(org.thingsboard.server.common.data.Device) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Event(org.thingsboard.server.common.data.Event) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) TbMsg(org.thingsboard.server.common.msg.TbMsg) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) Test(org.junit.Test)

Example 3 with QueueToRuleEngineMsg

use of org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg in project thingsboard by thingsboard.

the class TenantActor method doProcess.

@Override
protected boolean doProcess(TbActorMsg msg) {
    if (cantFindTenant) {
        log.info("[{}] Processing missing Tenant msg: {}", tenantId, msg);
        if (msg.getMsgType().equals(MsgType.QUEUE_TO_RULE_ENGINE_MSG)) {
            QueueToRuleEngineMsg queueMsg = (QueueToRuleEngineMsg) msg;
            queueMsg.getMsg().getCallback().onSuccess();
        } else if (msg.getMsgType().equals(MsgType.TRANSPORT_TO_DEVICE_ACTOR_MSG)) {
            TransportToDeviceActorMsgWrapper transportMsg = (TransportToDeviceActorMsgWrapper) msg;
            transportMsg.getCallback().onSuccess();
        }
        return true;
    }
    switch(msg.getMsgType()) {
        case PARTITION_CHANGE_MSG:
            PartitionChangeMsg partitionChangeMsg = (PartitionChangeMsg) msg;
            ServiceType serviceType = partitionChangeMsg.getServiceQueueKey().getServiceType();
            if (ServiceType.TB_RULE_ENGINE.equals(serviceType)) {
                // To Rule Chain Actors
                broadcast(msg);
            } else if (ServiceType.TB_CORE.equals(serviceType)) {
                List<TbActorId> deviceActorIds = ctx.filterChildren(new TbEntityTypeActorIdPredicate(EntityType.DEVICE) {

                    @Override
                    protected boolean testEntityId(EntityId entityId) {
                        return super.testEntityId(entityId) && !isMyPartition(entityId);
                    }
                });
                deviceActorIds.forEach(id -> ctx.stop(id));
            }
            break;
        case COMPONENT_LIFE_CYCLE_MSG:
            onComponentLifecycleMsg((ComponentLifecycleMsg) msg);
            break;
        case QUEUE_TO_RULE_ENGINE_MSG:
            onQueueToRuleEngineMsg((QueueToRuleEngineMsg) msg);
            break;
        case TRANSPORT_TO_DEVICE_ACTOR_MSG:
            onToDeviceActorMsg((DeviceAwareMsg) msg, false);
            break;
        case DEVICE_ATTRIBUTES_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_CREDENTIALS_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_NAME_OR_TYPE_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_EDGE_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG:
        case DEVICE_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG:
        case SERVER_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG:
        case REMOVE_RPC_TO_DEVICE_ACTOR_MSG:
            onToDeviceActorMsg((DeviceAwareMsg) msg, true);
            break;
        case SESSION_TIMEOUT_MSG:
            ctx.broadcastToChildrenByType(msg, EntityType.DEVICE);
            break;
        case RULE_CHAIN_INPUT_MSG:
        case RULE_CHAIN_OUTPUT_MSG:
        case RULE_CHAIN_TO_RULE_CHAIN_MSG:
            onRuleChainMsg((RuleChainAwareMsg) msg);
            break;
        case EDGE_EVENT_UPDATE_TO_EDGE_SESSION_MSG:
            onToEdgeSessionMsg((EdgeEventUpdateMsg) msg);
            break;
        default:
            return false;
    }
    return true;
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Edge(org.thingsboard.server.common.data.edge.Edge) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleChainManagerActor(org.thingsboard.server.actors.ruleChain.RuleChainManagerActor) EdgeEventUpdateMsg(org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg) Tenant(org.thingsboard.server.common.data.Tenant) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceActorCreator(org.thingsboard.server.actors.device.DeviceActorCreator) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) TbActorId(org.thingsboard.server.actors.TbActorId) TbActorNotRegisteredException(org.thingsboard.server.actors.TbActorNotRegisteredException) MsgType(org.thingsboard.server.common.msg.MsgType) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) EntityId(org.thingsboard.server.common.data.id.EntityId) TbEntityTypeActorIdPredicate(org.thingsboard.server.actors.TbEntityTypeActorIdPredicate) EntityType(org.thingsboard.server.common.data.EntityType) DeviceAwareMsg(org.thingsboard.server.common.msg.aware.DeviceAwareMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) EdgeRpcService(org.thingsboard.server.service.edge.rpc.EdgeRpcService) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) TbActorException(org.thingsboard.server.actors.TbActorException) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Optional(java.util.Optional) ContextBasedCreator(org.thingsboard.server.actors.service.ContextBasedCreator) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainAwareMsg(org.thingsboard.server.common.msg.aware.RuleChainAwareMsg) TbActor(org.thingsboard.server.actors.TbActor) TbActorRef(org.thingsboard.server.actors.TbActorRef) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) List(java.util.List) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) TbEntityTypeActorIdPredicate(org.thingsboard.server.actors.TbEntityTypeActorIdPredicate)

Example 4 with QueueToRuleEngineMsg

use of org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method forwardToRuleEngineActor.

private void forwardToRuleEngineActor(String queueName, TenantId tenantId, ToRuleEngineMsg toRuleEngineMsg, TbMsgCallback callback) {
    TbMsg tbMsg = TbMsg.fromBytes(queueName, toRuleEngineMsg.getTbMsg().toByteArray(), callback);
    QueueToRuleEngineMsg msg;
    ProtocolStringList relationTypesList = toRuleEngineMsg.getRelationTypesList();
    Set<String> relationTypes = null;
    if (relationTypesList != null) {
        if (relationTypesList.size() == 1) {
            relationTypes = Collections.singleton(relationTypesList.get(0));
        } else {
            relationTypes = new HashSet<>(relationTypesList);
        }
    }
    msg = new QueueToRuleEngineMsg(tenantId, tbMsg, relationTypes, toRuleEngineMsg.getFailureMessage());
    actorContext.tell(msg);
}
Also used : QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) TbMsg(org.thingsboard.server.common.msg.TbMsg) ProtocolStringList(com.google.protobuf.ProtocolStringList)

Example 5 with QueueToRuleEngineMsg

use of org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg in project thingsboard by thingsboard.

the class AbstractRuleEngineFlowIntegrationTest method testTwoRuleChainsWithTwoRules.

@Test
public void testTwoRuleChainsWithTwoRules() throws Exception {
    // Creating Rule Chain
    RuleChain rootRuleChain = new RuleChain();
    rootRuleChain.setName("Root Rule Chain");
    rootRuleChain.setTenantId(savedTenant.getId());
    rootRuleChain.setRoot(true);
    rootRuleChain.setDebugMode(true);
    rootRuleChain = saveRuleChain(rootRuleChain);
    Assert.assertNull(rootRuleChain.getFirstRuleNodeId());
    // Creating Rule Chain
    RuleChain secondaryRuleChain = new RuleChain();
    secondaryRuleChain.setName("Secondary Rule Chain");
    secondaryRuleChain.setTenantId(savedTenant.getId());
    secondaryRuleChain.setRoot(false);
    secondaryRuleChain.setDebugMode(true);
    secondaryRuleChain = saveRuleChain(secondaryRuleChain);
    Assert.assertNull(secondaryRuleChain.getFirstRuleNodeId());
    RuleChainMetaData rootMetaData = new RuleChainMetaData();
    rootMetaData.setRuleChainId(rootRuleChain.getId());
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("Simple Rule Node 1");
    ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode1.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration();
    configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1"));
    ruleNode1.setConfiguration(mapper.valueToTree(configuration1));
    RuleNode ruleNode12 = new RuleNode();
    ruleNode12.setName("Simple Rule Node 1");
    ruleNode12.setType(org.thingsboard.rule.engine.flow.TbRuleChainInputNode.class.getName());
    ruleNode12.setDebugMode(true);
    TbRuleChainInputNodeConfiguration configuration12 = new TbRuleChainInputNodeConfiguration();
    configuration12.setRuleChainId(secondaryRuleChain.getId().getId().toString());
    ruleNode12.setConfiguration(mapper.valueToTree(configuration12));
    rootMetaData.setNodes(Arrays.asList(ruleNode1, ruleNode12));
    rootMetaData.setFirstNodeIndex(0);
    NodeConnectionInfo connection = new NodeConnectionInfo();
    connection.setFromIndex(0);
    connection.setToIndex(1);
    connection.setType("Success");
    rootMetaData.setConnections(Collections.singletonList(connection));
    rootMetaData = saveRuleChainMetaData(rootMetaData);
    Assert.assertNotNull(rootMetaData);
    rootRuleChain = getRuleChain(rootRuleChain.getId());
    Assert.assertNotNull(rootRuleChain.getFirstRuleNodeId());
    RuleChainMetaData secondaryMetaData = new RuleChainMetaData();
    secondaryMetaData.setRuleChainId(secondaryRuleChain.getId());
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("Simple Rule Node 2");
    ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode2.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration();
    configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2"));
    ruleNode2.setConfiguration(mapper.valueToTree(configuration2));
    secondaryMetaData.setNodes(Collections.singletonList(ruleNode2));
    secondaryMetaData.setFirstNodeIndex(0);
    secondaryMetaData = saveRuleChainMetaData(secondaryMetaData);
    Assert.assertNotNull(secondaryMetaData);
    // Saving the device
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device = doPost("/api/device", device, Device.class);
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey1", "serverAttributeValue1"), System.currentTimeMillis()))).get();
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey2", "serverAttributeValue2"), System.currentTimeMillis()))).get();
    TbMsgCallback tbMsgCallback = Mockito.mock(TbMsgCallback.class);
    Mockito.when(tbMsgCallback.isMsgValid()).thenReturn(true);
    TbMsg tbMsg = TbMsg.newMsg("CUSTOM", device.getId(), new TbMsgMetaData(), "{}", tbMsgCallback);
    QueueToRuleEngineMsg qMsg = new QueueToRuleEngineMsg(savedTenant.getId(), tbMsg, null, null);
    // Pushing Message to the system
    actorSystem.tell(qMsg);
    Mockito.verify(tbMsgCallback, Mockito.timeout(10000)).onSuccess();
    PageData<Event> eventsPage = getDebugEvents(savedTenant.getId(), rootRuleChain.getFirstRuleNodeId(), 1000);
    List<Event> events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    Event inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(rootRuleChain.getFirstRuleNodeId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    Event outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(rootRuleChain.getFirstRuleNodeId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    RuleChain finalRuleChain = rootRuleChain;
    RuleNode lastRuleNode = secondaryMetaData.getNodes().stream().filter(node -> !node.getId().equals(finalRuleChain.getFirstRuleNodeId())).findFirst().get();
    eventsPage = getDebugEvents(savedTenant.getId(), lastRuleNode.getId(), 1000);
    events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    Assert.assertEquals("serverAttributeValue2", getMetadata(outEvent).get("ss_serverAttributeKey2").asText());
}
Also used : Event(org.thingsboard.server.common.data.Event) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Device(org.thingsboard.server.common.data.Device) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) Mockito.spy(org.mockito.Mockito.spy) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) Answer(org.mockito.stubbing.Answer) User(org.thingsboard.server.common.data.User) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) After(org.junit.After) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) Before(org.junit.Before) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) DataConstants(org.thingsboard.server.common.data.DataConstants) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Test(org.junit.Test) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) Slf4j(lombok.extern.slf4j.Slf4j) EventService(org.thingsboard.server.dao.event.EventService) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Assert(org.junit.Assert) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) Collections(java.util.Collections) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Device(org.thingsboard.server.common.data.Device) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Event(org.thingsboard.server.common.data.Event) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) TbMsg(org.thingsboard.server.common.msg.TbMsg) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) Test(org.junit.Test)

Aggregations

TbMsg (org.thingsboard.server.common.msg.TbMsg)5 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)5 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)4 List (java.util.List)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Test (org.junit.Test)3 TbGetAttributesNodeConfiguration (org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration)3 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)3 Device (org.thingsboard.server.common.data.Device)3 Event (org.thingsboard.server.common.data.Event)3 Tenant (org.thingsboard.server.common.data.Tenant)3 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)3 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)3 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)3 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)3 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)3 TbMsgCallback (org.thingsboard.server.common.msg.queue.TbMsgCallback)3 AbstractRuleEngineControllerTest (org.thingsboard.server.controller.AbstractRuleEngineControllerTest)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Arrays (java.util.Arrays)2