Search in sources :

Example 36 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData 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 37 with TbMsgMetaData

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

the class AbstractAttributeNodeTest method deviceCustomerTelemetryFetched.

void deviceCustomerTelemetryFetched(Device device) throws TbNodeException {
    ObjectMapper mapper = JacksonUtil.OBJECT_MAPPER;
    TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(getTbNodeConfigForTelemetry()));
    TbEntityGetAttrNode node = getEmptyNode();
    node.init(null, nodeConfiguration);
    msg = TbMsg.newMsg("DEVICE", device.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    List<TsKvEntry> timeseries = Lists.newArrayList(new BasicTsKvEntry(1L, new StringDataEntry("temperature", "highest")));
    when(ctx.getTimeseriesService()).thenReturn(timeseriesService);
    when(timeseriesService.findLatest(any(), eq(getEntityId()), anyCollection())).thenReturn(Futures.immediateFuture(timeseries));
    node.onMsg(ctx, msg);
    verify(ctx).tellSuccess(msg);
    assertEquals(msg.getMetaData().getValue("answer"), "highest");
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 38 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData 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 39 with TbMsgMetaData

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

the class AlarmStateTest method testSetAlarmConditionMetadata_repeatingCondition.

@Test
public void testSetAlarmConditionMetadata_repeatingCondition() {
    AlarmRuleState ruleState = createMockAlarmRuleState(new RepeatingAlarmConditionSpec());
    int eventCount = 3;
    ruleState.getState().setEventCount(eventCount);
    AlarmState alarmState = createMockAlarmState();
    TbMsgMetaData metaData = new TbMsgMetaData();
    alarmState.setAlarmConditionMetadata(ruleState, metaData);
    assertEquals(AlarmConditionSpecType.REPEATING, ruleState.getSpec().getType());
    assertNotNull(metaData.getValue(DataConstants.ALARM_CONDITION_REPEATS));
    assertNull(metaData.getValue(DataConstants.ALARM_CONDITION_DURATION));
    assertEquals(String.valueOf(eventCount), metaData.getValue(DataConstants.ALARM_CONDITION_REPEATS));
}
Also used : RepeatingAlarmConditionSpec(org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) Test(org.junit.Test)

Example 40 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData 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)

Aggregations

TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)89 TbMsg (org.thingsboard.server.common.msg.TbMsg)56 Test (org.junit.Test)49 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)26 Device (org.thingsboard.server.common.data.Device)17 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)17 List (java.util.List)16 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 DeviceId (org.thingsboard.server.common.data.id.DeviceId)15 EntityId (org.thingsboard.server.common.data.id.EntityId)15 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 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)14 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13