Browse Source

new ruleNode 'split array msg'

pull/7103/head
Yuriy Lytvynchuk 4 years ago
parent
commit
84c91a17cb
  1. 6
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java
  2. 11
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java

6
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java

@ -39,8 +39,10 @@ import java.util.concurrent.ExecutionException;
name = "split array msg", name = "split array msg",
configClazz = EmptyNodeConfiguration.class, configClazz = EmptyNodeConfiguration.class,
nodeDescription = "Split array message into several msgs", nodeDescription = "Split array message into several msgs",
nodeDetails = "", nodeDetails = "Split the array fetched from the msg body. Returns inner objects of the extracted array as "
icon = "functions", + "separate messages via <code>Success</code> chain. If msg data is not a JSON array, "
+ "the incoming message is returned",
icon = "content_copy",
configDirective = "tbNodeEmptyConfig" configDirective = "tbNodeEmptyConfig"
) )
public class TbSplitArrayMsgNode implements TbNode { public class TbSplitArrayMsgNode implements TbNode {

11
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java

@ -78,19 +78,19 @@ public class TbSplitArrayMsgNodeTest {
@Test @Test
void givenFewMsg_whenOnMsg_thenVerifyOutput() throws Exception { void givenFewMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}, {\"Attribute_1\":1,\"Attribute_2\":2}]"; String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}, {\"Attribute_1\":1,\"Attribute_2\":2}]";
VerifyOutputMsg(data); VerifyOutputMsg(data, 2);
} }
@Test @Test
void givenOneMsg_whenOnMsg_thenVerifyOutput() throws Exception { void givenOneMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}]"; String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}]";
VerifyOutputMsg(data); VerifyOutputMsg(data, 1);
} }
@Test @Test
void givenZeroMsg_whenOnMsg_thenVerifyOutput() throws Exception { void givenZeroMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[]"; String data = "[]";
VerifyOutputMsg(data); VerifyOutputMsg(data, 0);
} }
@Test @Test
@ -110,12 +110,13 @@ public class TbSplitArrayMsgNodeTest {
assertThat(newMsg).isSameAs(msg); assertThat(newMsg).isSameAs(msg);
} }
private void VerifyOutputMsg(String data) throws Exception { private void VerifyOutputMsg(String data, int sizeArray) throws Exception {
ArrayNode dataNode = (ArrayNode) JacksonUtil.toJsonNode(data); JsonNode dataNode = JacksonUtil.toJsonNode(data);
node.onMsg(ctx, getTbMsg(deviceId, dataNode.toString())); node.onMsg(ctx, getTbMsg(deviceId, dataNode.toString()));
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(dataNode.size())).tellSuccess(newMsgCaptor.capture()); verify(ctx, times(dataNode.size())).tellSuccess(newMsgCaptor.capture());
verify(ctx, times(sizeArray)).tellSuccess(newMsgCaptor.capture());
verify(ctx, never()).tellFailure(any(), any()); verify(ctx, never()).tellFailure(any(), any());
} }

Loading…
Cancel
Save