TbJsFilterNode implements TbNode { @Override public void onMsg(TbContext ctx, TbMsg msg) { ListeningExecutor jsExecutor = ctx.getJsExecutor(); withCallback(jsExecutor.executeAsync(() -> jsEngine.executeFilter(msg)), filterResult -> ctx.tellNext(msg, filterResult ? "True" : "False"), t -> ctx.tellFailure(msg, t)); }  @Override void init(TbContext ctx, TbNodeConfiguration configuration); @Override void onMsg(TbContext ctx, TbMsg msg); @Override void destroy();  }
@Test public void falseEvaluationDoNotSendMsg() throws TbNodeException, ScriptException { initWithScript(); TbMsg msg = new TbMsg(UUIDs.timeBased(), "USER", null, new TbMsgMetaData(), "{}", ruleChainId, ruleNodeId, 0L); mockJsExecutor(); when(scriptEngine.executeFilter(msg)).thenReturn(false); node.onMsg(ctx, msg); verify(ctx).getJsExecutor(); verify(ctx).tellNext(msg, "False"); }
@Test public void exceptionInJsThrowsException() throws TbNodeException, ScriptException { initWithScript(); TbMsgMetaData metaData = new TbMsgMetaData(); TbMsg msg = new TbMsg(UUIDs.timeBased(), "USER", null, metaData, "{}", ruleChainId, ruleNodeId, 0L); mockJsExecutor(); when(scriptEngine.executeFilter(msg)).thenThrow(new ScriptException("error")); node.onMsg(ctx, msg); verifyError(msg, "error", ScriptException.class); }
@Test public void metadataConditionCanBeTrue() throws TbNodeException, ScriptException { initWithScript(); TbMsgMetaData metaData = new TbMsgMetaData(); TbMsg msg = new TbMsg(UUIDs.timeBased(), "USER", null, metaData, "{}", ruleChainId, ruleNodeId, 0L); mockJsExecutor(); when(scriptEngine.executeFilter(msg)).thenReturn(true); node.onMsg(ctx, msg); verify(ctx).getJsExecutor(); verify(ctx).tellNext(msg, "True"); }