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",
configClazz = EmptyNodeConfiguration.class,
nodeDescription = "Split array message into several msgs",
nodeDetails = "",
icon = "functions",
nodeDetails = "Split the array fetched from the msg body. Returns inner objects of the extracted array as "
+ "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"
)
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
void givenFewMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}, {\"Attribute_1\":1,\"Attribute_2\":2}]";
VerifyOutputMsg(data);
VerifyOutputMsg(data, 2);
}
@Test
void givenOneMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}]";
VerifyOutputMsg(data);
VerifyOutputMsg(data, 1);
}
@Test
void givenZeroMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[]";
VerifyOutputMsg(data);
VerifyOutputMsg(data, 0);
}
@Test
@ -110,12 +110,13 @@ public class TbSplitArrayMsgNodeTest {
assertThat(newMsg).isSameAs(msg);
}
private void VerifyOutputMsg(String data) throws Exception {
ArrayNode dataNode = (ArrayNode) JacksonUtil.toJsonNode(data);
private void VerifyOutputMsg(String data, int sizeArray) throws Exception {
JsonNode dataNode = JacksonUtil.toJsonNode(data);
node.onMsg(ctx, getTbMsg(deviceId, dataNode.toString()));
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(dataNode.size())).tellSuccess(newMsgCaptor.capture());
verify(ctx, times(sizeArray)).tellSuccess(newMsgCaptor.capture());
verify(ctx, never()).tellFailure(any(), any());
}

Loading…
Cancel
Save