Search in sources :

Example 41 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbJsFilterNodeTest method exceptionInJsThrowsException.

@Test
public void exceptionInJsThrowsException() throws TbNodeException, ScriptException {
    initWithScript();
    TbMsgMetaData metaData = new TbMsgMetaData();
    TbMsg msg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(scriptEngine.executeFilterAsync(msg)).thenReturn(Futures.immediateFailedFuture(new ScriptException("error")));
    node.onMsg(ctx, msg);
    verifyError(msg, "error", ScriptException.class);
}
Also used : ScriptException(javax.script.ScriptException) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Example 42 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbMsgToEmailNodeTest method msgCanBeConverted.

@Test
public void msgCanBeConverted() throws IOException {
    initWithScript();
    metaData.putValue("username", "oreo");
    metaData.putValue("userEmail", "user@email.io");
    metaData.putValue("name", "temp");
    metaData.putValue("passed", "5");
    metaData.putValue("count", "100");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    emailNode.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("SEND_EMAIL", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals("oreo", metadataCaptor.getValue().getValue("username"));
    assertNotSame(metaData, metadataCaptor.getValue());
    TbEmail actual = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), TbEmail.class);
    TbEmail expected = TbEmail.builder().from("test@mail.org").to("user@email.io").subject("Hi oreo there").body("temp is to high. Current 5 and 100").build();
    assertEquals(expected, actual);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbEmail(org.thingsboard.rule.engine.api.TbEmail) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 43 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method exceptionThrownIfCannotFindNewOriginator.

@Test
public void exceptionThrownIfCannotFindNewOriginator() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(null));
    node.onMsg(ctx, msg);
    verify(ctx).tellNext(same(msg), same(FAILURE));
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 44 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method originatorCanBeChangedToCustomerId.

@Test
public void originatorCanBeChangedToCustomerId() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset));
    node.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals(customerId, originatorCaptor.getValue());
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 45 with TbMsg

use of org.thingsboard.server.common.msg.TbMsg in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method newChainCanBeStarted.

@Test
public void newChainCanBeStarted() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset));
    node.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals(customerId, originatorCaptor.getValue());
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Aggregations

TbMsg (org.thingsboard.server.common.msg.TbMsg)88 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)57 Test (org.junit.Test)46 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)29 List (java.util.List)19 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)18 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)17 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 EntityId (org.thingsboard.server.common.data.id.EntityId)16 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)16 Device (org.thingsboard.server.common.data.Device)15 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)14 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)14 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)14 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13