add options fromMetadata
This commit is contained in:
parent
d4b3eebb50
commit
db489f2ae0
@ -28,6 +28,7 @@ import org.thingsboard.rule.engine.api.TbNodeException;
|
|||||||
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
||||||
import org.thingsboard.server.common.data.plugin.ComponentType;
|
import org.thingsboard.server.common.data.plugin.ComponentType;
|
||||||
import org.thingsboard.server.common.msg.TbMsg;
|
import org.thingsboard.server.common.msg.TbMsg;
|
||||||
|
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@ -36,22 +37,22 @@ import java.util.concurrent.ExecutionException;
|
|||||||
@RuleNode(
|
@RuleNode(
|
||||||
type = ComponentType.TRANSFORMATION,
|
type = ComponentType.TRANSFORMATION,
|
||||||
name = "rename keys",
|
name = "rename keys",
|
||||||
configClazz = TbRenameMsgKeysNodeConfiguration.class,
|
configClazz = TbRenameKeysNodeConfiguration.class,
|
||||||
nodeDescription = "Renames msg data keys to the new key names selected in the key mapping.",
|
nodeDescription = "Renames msg data or metadata keys to the new key names selected in the key mapping.",
|
||||||
nodeDetails = "If the key that is selected in the key mapping is missed in the msg data, it will be ignored." +
|
nodeDetails = "If the key that is selected in the key mapping is missed in the msg data or metadata, it will be ignored." +
|
||||||
"If the msg is not a JSON object returns the incoming message as outbound message with <code>Failure</code> chain," +
|
"If the msg data is not a JSON object returns the incoming message as outbound message with <code>Failure</code> chain," +
|
||||||
" otherwise returns transformed messages via <code>Success</code> chain",
|
" otherwise returns transformed messages via <code>Success</code> chain",
|
||||||
uiResources = {"static/rulenode/rulenode-core-config.js"},
|
uiResources = {"static/rulenode/rulenode-core-config.js"},
|
||||||
configDirective = "tbTransformationNodeRenameMsgKeysConfig",
|
configDirective = "tbTransformationNodeRenameKeysConfig",
|
||||||
icon = "functions"
|
icon = "find_replace"
|
||||||
)
|
)
|
||||||
public class TbRenameMsgKeysNode implements TbNode {
|
public class TbRenameKeysNode implements TbNode {
|
||||||
|
|
||||||
TbRenameMsgKeysNodeConfiguration config;
|
TbRenameKeysNodeConfiguration config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
||||||
this.config = TbNodeUtils.convert(configuration, TbRenameMsgKeysNodeConfiguration.class);
|
this.config = TbNodeUtils.convert(configuration, TbRenameKeysNodeConfiguration.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -59,21 +60,40 @@ public class TbRenameMsgKeysNode implements TbNode {
|
|||||||
Map<String, String> renameKeysMapping = config.getRenameKeysMapping();
|
Map<String, String> renameKeysMapping = config.getRenameKeysMapping();
|
||||||
if (CollectionUtils.isEmpty(renameKeysMapping)) {
|
if (CollectionUtils.isEmpty(renameKeysMapping)) {
|
||||||
ctx.tellSuccess(msg);
|
ctx.tellSuccess(msg);
|
||||||
|
} else {
|
||||||
|
TbMsgMetaData metaData = msg.getMetaData();
|
||||||
|
String data = msg.getData();
|
||||||
|
if (config.isFromMetadata()) {
|
||||||
|
Map<String, String> metaDataMap = metaData.getData();
|
||||||
|
renameKeysMapping.forEach((nameKey, newNameKey) -> {
|
||||||
|
if (!newNameKey.equals(nameKey)) {
|
||||||
|
if (metaDataMap.containsKey(nameKey)) {
|
||||||
|
metaDataMap.put(newNameKey, metaDataMap.get(nameKey));
|
||||||
|
metaDataMap.remove(nameKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
metaData = new TbMsgMetaData(metaDataMap);
|
||||||
} else {
|
} else {
|
||||||
JsonNode dataNode = JacksonUtil.toJsonNode(msg.getData());
|
JsonNode dataNode = JacksonUtil.toJsonNode(msg.getData());
|
||||||
if (dataNode.isObject()) {
|
if (dataNode.isObject()) {
|
||||||
ObjectNode msgData = (ObjectNode) dataNode;
|
ObjectNode msgData = (ObjectNode) dataNode;
|
||||||
renameKeysMapping.forEach((nameKey, newNameKey) -> {
|
renameKeysMapping.forEach((nameKey, newNameKey) -> {
|
||||||
|
if (!newNameKey.equals(nameKey)) {
|
||||||
if (msgData.has(nameKey)) {
|
if (msgData.has(nameKey)) {
|
||||||
msgData.set(newNameKey, msgData.get(nameKey));
|
msgData.set(newNameKey, msgData.get(nameKey));
|
||||||
msgData.remove(nameKey);
|
msgData.remove(nameKey);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(msgData)));
|
data = JacksonUtil.toString(msgData);
|
||||||
} else {
|
} else {
|
||||||
ctx.tellFailure(msg, new RuntimeException("Msg data is not a JSON Object!"));
|
ctx.tellFailure(msg, new RuntimeException("Msg data is not a JSON Object!"));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -22,14 +22,16 @@ import java.util.Collections;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class TbRenameMsgKeysNodeConfiguration implements NodeConfiguration<TbRenameMsgKeysNodeConfiguration> {
|
public class TbRenameKeysNodeConfiguration implements NodeConfiguration<TbRenameKeysNodeConfiguration> {
|
||||||
|
|
||||||
|
private boolean fromMetadata;
|
||||||
private Map<String, String> renameKeysMapping;
|
private Map<String, String> renameKeysMapping;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TbRenameMsgKeysNodeConfiguration defaultConfiguration() {
|
public TbRenameKeysNodeConfiguration defaultConfiguration() {
|
||||||
TbRenameMsgKeysNodeConfiguration configuration = new TbRenameMsgKeysNodeConfiguration();
|
TbRenameKeysNodeConfiguration configuration = new TbRenameKeysNodeConfiguration();
|
||||||
configuration.setRenameKeysMapping(Collections.emptyMap());
|
configuration.setRenameKeysMapping(Collections.emptyMap());
|
||||||
|
configuration.setFromMetadata(false);
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,8 +44,8 @@ import static org.mockito.Mockito.verify;
|
|||||||
|
|
||||||
public class TbRenameMsgKeysNodeTest {
|
public class TbRenameMsgKeysNodeTest {
|
||||||
DeviceId deviceId;
|
DeviceId deviceId;
|
||||||
TbRenameMsgKeysNode node;
|
TbRenameKeysNode node;
|
||||||
TbRenameMsgKeysNodeConfiguration config;
|
TbRenameKeysNodeConfiguration config;
|
||||||
TbNodeConfiguration nodeConfiguration;
|
TbNodeConfiguration nodeConfiguration;
|
||||||
TbContext ctx;
|
TbContext ctx;
|
||||||
TbMsgCallback callback;
|
TbMsgCallback callback;
|
||||||
@ -55,10 +55,10 @@ public class TbRenameMsgKeysNodeTest {
|
|||||||
deviceId = new DeviceId(UUID.randomUUID());
|
deviceId = new DeviceId(UUID.randomUUID());
|
||||||
callback = mock(TbMsgCallback.class);
|
callback = mock(TbMsgCallback.class);
|
||||||
ctx = mock(TbContext.class);
|
ctx = mock(TbContext.class);
|
||||||
config = new TbRenameMsgKeysNodeConfiguration().defaultConfiguration();
|
config = new TbRenameKeysNodeConfiguration().defaultConfiguration();
|
||||||
config.setRenameKeysMapping(Map.of("TestKey_1", "Attribute_1", "TestKey_2", "Attribute_2"));
|
config.setRenameKeysMapping(Map.of("TestKey_1", "Attribute_1", "TestKey_2", "Attribute_2"));
|
||||||
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
|
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
|
||||||
node = spy(new TbRenameMsgKeysNode());
|
node = spy(new TbRenameKeysNode());
|
||||||
node.init(ctx, nodeConfiguration);
|
node.init(ctx, nodeConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,8 +74,9 @@ public class TbRenameMsgKeysNodeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDefaultConfig_whenVerify_thenOK() {
|
void givenDefaultConfig_whenVerify_thenOK() {
|
||||||
TbRenameMsgKeysNodeConfiguration defaultConfig = new TbRenameMsgKeysNodeConfiguration().defaultConfiguration();
|
TbRenameKeysNodeConfiguration defaultConfig = new TbRenameKeysNodeConfiguration().defaultConfiguration();
|
||||||
assertThat(defaultConfig.getRenameKeysMapping()).isEqualTo(Collections.emptyMap());
|
assertThat(defaultConfig.getRenameKeysMapping()).isEqualTo(Collections.emptyMap());
|
||||||
|
assertThat(defaultConfig.isFromMetadata()).isEqualTo(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -95,9 +96,31 @@ public class TbRenameMsgKeysNodeTest {
|
|||||||
assertThat(dataNode.has("Temperature_1")).isEqualTo(true);
|
assertThat(dataNode.has("Temperature_1")).isEqualTo(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMetadata_whenOnMsg_thenVerifyOutput() throws Exception {
|
||||||
|
config = new TbRenameKeysNodeConfiguration().defaultConfiguration();
|
||||||
|
config.setRenameKeysMapping(Map.of("TestKey_1", "Attribute_1", "TestKey_2", "Attribute_2"));
|
||||||
|
config.setFromMetadata(true);
|
||||||
|
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
|
||||||
|
node.init(ctx, nodeConfiguration);
|
||||||
|
|
||||||
|
String data = "{\"Temperature_1\":22.5,\"TestKey_2\":10.3}";
|
||||||
|
node.onMsg(ctx, getTbMsg(deviceId, data));
|
||||||
|
|
||||||
|
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
|
||||||
|
verify(ctx, times(1)).tellSuccess(newMsgCaptor.capture());
|
||||||
|
verify(ctx, never()).tellFailure(any(), any());
|
||||||
|
|
||||||
|
TbMsg newMsg = newMsgCaptor.getValue();
|
||||||
|
assertThat(newMsg).isNotNull();
|
||||||
|
|
||||||
|
Map<String, String> mdDataMap = newMsg.getMetaData().getData();
|
||||||
|
assertThat(mdDataMap.containsKey("Attribute_1")).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception {
|
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception {
|
||||||
TbRenameMsgKeysNodeConfiguration defaultConfig = new TbRenameMsgKeysNodeConfiguration().defaultConfiguration();
|
TbRenameKeysNodeConfiguration defaultConfig = new TbRenameKeysNodeConfiguration().defaultConfiguration();
|
||||||
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(defaultConfig));
|
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(defaultConfig));
|
||||||
node.init(ctx, nodeConfiguration);
|
node.init(ctx, nodeConfiguration);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user