use of org.thingsboard.rule.engine.api.TbEmail in project thingsboard by thingsboard.
the class TbMsgToEmailNode method onMsg.
@Override
public void onMsg(TbContext ctx, TbMsg msg) {
try {
TbEmail email = convert(msg);
TbMsg emailMsg = buildEmailMsg(ctx, msg, email);
ctx.tellNext(emailMsg, SUCCESS);
} catch (Exception ex) {
log.warn("Can not convert message to email " + ex.getMessage());
ctx.tellFailure(msg, ex);
}
}
use of org.thingsboard.rule.engine.api.TbEmail 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);
}
use of org.thingsboard.rule.engine.api.TbEmail in project thingsboard by thingsboard.
the class TbSendEmailNode method onMsg.
@Override
public void onMsg(TbContext ctx, TbMsg msg) {
try {
validateType(msg.getType());
TbEmail email = getEmail(msg);
withCallback(ctx.getMailExecutor().executeAsync(() -> {
sendEmail(ctx, msg, email);
return null;
}), ok -> ctx.tellSuccess(msg), fail -> ctx.tellFailure(msg, fail));
} catch (Exception ex) {
ctx.tellFailure(msg, ex);
}
}
Aggregations