diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbAbstractTransformNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbAbstractTransformNode.java
index 29c61b81c7..e51a2d9205 100644
--- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbAbstractTransformNode.java
+++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbAbstractTransformNode.java
@@ -22,6 +22,7 @@ import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
+import org.thingsboard.rule.engine.api.TbRelationTypes;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.queue.RuleEngineException;
@@ -81,7 +82,7 @@ public abstract class TbAbstractTransformNode implements TbNode {
ctx.tellFailure(msg, e);
}
});
- msgs.forEach(newMsg -> ctx.enqueueForTellNext(newMsg, "Success", wrapper::onSuccess, wrapper::onFailure));
+ msgs.forEach(newMsg -> ctx.enqueueForTellNext(newMsg, TbRelationTypes.SUCCESS, wrapper::onSuccess, wrapper::onFailure));
}
} else {
ctx.tellNext(msg, FAILURE);
diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java
new file mode 100644
index 0000000000..2dc27b6f92
--- /dev/null
+++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java
@@ -0,0 +1,85 @@
+/**
+ * Copyright © 2016-2022 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.rule.engine.transform;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import lombok.extern.slf4j.Slf4j;
+import org.thingsboard.common.util.JacksonUtil;
+import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
+import org.thingsboard.rule.engine.api.RuleNode;
+import org.thingsboard.rule.engine.api.TbContext;
+import org.thingsboard.rule.engine.api.TbNode;
+import org.thingsboard.rule.engine.api.TbNodeConfiguration;
+import org.thingsboard.rule.engine.api.TbNodeException;
+import org.thingsboard.rule.engine.api.TbRelationTypes;
+import org.thingsboard.rule.engine.api.util.TbNodeUtils;
+import org.thingsboard.server.common.data.plugin.ComponentType;
+import org.thingsboard.server.common.msg.TbMsg;
+import org.thingsboard.server.common.msg.queue.RuleEngineException;
+import org.thingsboard.server.common.msg.queue.TbMsgCallback;
+
+import java.util.concurrent.ExecutionException;
+
+@Slf4j
+@RuleNode(
+ type = ComponentType.EXTERNAL,
+ name = "split array msg",
+ configClazz = EmptyNodeConfiguration.class,
+ nodeDescription = "Split array message into several msgs",
+ nodeDetails = "Split the array fetched from the msg body. If the msg data is not a JSON object returns the "
+ + "incoming message as outbound message with Failure chain, otherwise returns "
+ + "inner objects of the extracted array as separate messages via Success chain.",
+ uiResources = {"static/rulenode/rulenode-core-config.js"},
+ icon = "content_copy",
+ configDirective = "tbNodeEmptyConfig"
+)
+public class TbSplitArrayMsgNode implements TbNode {
+
+ private EmptyNodeConfiguration config;
+
+ @Override
+ public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
+ this.config = TbNodeUtils.convert(configuration, EmptyNodeConfiguration.class);
+ }
+
+ @Override
+ public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
+ JsonNode jsonNode = JacksonUtil.toJsonNode(msg.getData());
+ if (jsonNode.isArray()) {
+ ArrayNode data = (ArrayNode) jsonNode;
+ if (data.size() == 1) {
+ ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(data.get(0))));
+ } else {
+ TbMsgCallbackWrapper wrapper = new MultipleTbMsgsCallbackWrapper(data.size(), new TbMsgCallback() {
+ @Override
+ public void onSuccess() {
+ ctx.ack(msg);
+ }
+
+ @Override
+ public void onFailure(RuleEngineException e) {
+ ctx.tellFailure(msg, e);
+ }
+ });
+ data.forEach(msgNode -> ctx.enqueueForTellNext(TbMsg.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(msgNode)),
+ TbRelationTypes.SUCCESS, wrapper::onSuccess, wrapper::onFailure));
+ }
+ } else {
+ ctx.tellFailure(msg, new RuntimeException("Msg data is not a JSON Array!"));
+ }
+ }
+}
diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java
new file mode 100644
index 0000000000..2925ab96ae
--- /dev/null
+++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java
@@ -0,0 +1,139 @@
+/**
+ * Copyright © 2016-2022 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.rule.engine.transform;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.thingsboard.common.util.JacksonUtil;
+import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
+import org.thingsboard.rule.engine.api.TbContext;
+import org.thingsboard.rule.engine.api.TbNodeConfiguration;
+import org.thingsboard.rule.engine.api.TbNodeException;
+import org.thingsboard.server.common.data.id.DeviceId;
+import org.thingsboard.server.common.data.id.EntityId;
+import org.thingsboard.server.common.msg.TbMsg;
+import org.thingsboard.server.common.msg.TbMsgMetaData;
+import org.thingsboard.server.common.msg.queue.TbMsgCallback;
+
+import java.util.Map;
+import java.util.UUID;
+import java.util.function.Consumer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class TbSplitArrayMsgNodeTest {
+ final ObjectMapper mapper = new ObjectMapper();
+
+ DeviceId deviceId;
+ TbSplitArrayMsgNode node;
+ EmptyNodeConfiguration config;
+ TbNodeConfiguration nodeConfiguration;
+ TbContext ctx;
+ TbMsgCallback callback;
+
+ @BeforeEach
+ void setUp() throws TbNodeException {
+ deviceId = new DeviceId(UUID.randomUUID());
+ callback = mock(TbMsgCallback.class);
+ ctx = mock(TbContext.class);
+ config = new EmptyNodeConfiguration();
+ nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
+ node = spy(new TbSplitArrayMsgNode());
+ node.init(ctx, nodeConfiguration);
+ }
+
+ @AfterEach
+ void tearDown() {
+ node.destroy();
+ }
+
+ @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);
+ }
+
+ @Test
+ void givenOneMsg_whenOnMsg_thenVerifyOutput() throws Exception {
+ String data = "[{\"Attribute_1\":22.5,\"Attribute_2\":10.3}]";
+ VerifyOutputMsg(data);
+ }
+
+ @Test
+ void givenZeroMsg_whenOnMsg_thenVerifyOutput() throws Exception {
+ String data = "[]";
+ VerifyOutputMsg(data);
+ }
+
+ @Test
+ void givenNoArrayMsg_whenOnMsg_thenFailure() throws Exception {
+ String data = "{\"Attribute_1\":22.5,\"Attribute_2\":10.3}";
+ JsonNode dataNode = JacksonUtil.toJsonNode(data);
+ TbMsg msg = getTbMsg(deviceId, dataNode.toString());
+ node.onMsg(ctx, msg);
+
+ ArgumentCaptor newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
+ ArgumentCaptor exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
+ verify(ctx, never()).tellSuccess(any());
+ verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
+
+ assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class);
+
+ TbMsg newMsg = newMsgCaptor.getValue();
+ assertThat(newMsg).isNotNull();
+
+ assertThat(newMsg).isSameAs(msg);
+ }
+
+ private void VerifyOutputMsg(String data) throws Exception {
+ JsonNode dataNode = JacksonUtil.toJsonNode(data);
+ TbMsg tbMsg = getTbMsg(deviceId, dataNode.toString());
+ node.onMsg(ctx, tbMsg);
+
+ if (dataNode.size() > 1) {
+ ArgumentCaptor successCaptor = ArgumentCaptor.forClass(Runnable.class);
+ ArgumentCaptor> failureCaptor = ArgumentCaptor.forClass(Consumer.class);
+ verify(ctx, times(dataNode.size())).enqueueForTellNext(any(), anyString(), successCaptor.capture(), failureCaptor.capture());
+ for (Runnable valueCaptor : successCaptor.getAllValues()) {
+ valueCaptor.run();
+ }
+ verify(ctx, times(1)).ack(tbMsg);
+ } else {
+ ArgumentCaptor newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
+ verify(ctx, times(dataNode.size())).tellSuccess(newMsgCaptor.capture());
+ }
+ verify(ctx, never()).tellFailure(any(), any());
+ }
+
+ private TbMsg getTbMsg(EntityId entityId, String data) {
+ Map mdMap = Map.of(
+ "country", "US",
+ "city", "NY"
+ );
+ return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
+ }
+}