Browse Source

Merge branch 'master' of github.com:thingsboard/thingsboard

pull/7362/head
Andrii Shvaika 4 years ago
parent
commit
655a619d0d
  1. 1
      application/pom.xml
  2. 2
      application/src/main/resources/thingsboard.yml
  3. 4
      dao/src/main/java/org/thingsboard/server/dao/aspect/SqlDaoCallsAspect.java
  4. 1
      pom.xml
  5. 4
      rule-engine/rule-engine-components/pom.xml
  6. 3
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbAbstractTransformNode.java
  7. 107
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbCopyKeysNode.java
  8. 38
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbCopyKeysNodeConfiguration.java
  9. 105
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNode.java
  10. 38
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNodeConfiguration.java
  11. 82
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNode.java
  12. 34
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNodeConfiguration.java
  13. 97
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbRenameKeysNode.java
  14. 37
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbRenameKeysNodeConfiguration.java
  15. 85
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNode.java
  16. 2
      rule-engine/rule-engine-components/src/main/resources/public/static/rulenode/rulenode-core-config.js
  17. 166
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbCopyKeysNodeTest.java
  18. 149
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNodeTest.java
  19. 167
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbJsonPathNodeTest.java
  20. 159
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbRenameKeysNodeTest.java
  21. 139
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbSplitArrayMsgNodeTest.java

1
application/pom.xml

@ -286,7 +286,6 @@
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>

2
application/src/main/resources/thingsboard.yml

@ -273,7 +273,7 @@ sql:
log_queries: "${SQL_LOG_QUERIES:false}"
log_queries_threshold: "${SQL_LOG_QUERIES_THRESHOLD:5000}"
log_tenant_stats: "${SQL_LOG_TENANT_STATS:true}"
log_tenant_stats_interval: "${SQL_LOG_TENANT_STATS_INTERVAL:60000}"
log_tenant_stats_interval_ms: "${SQL_LOG_TENANT_STATS_INTERVAL_MS:60000}"
postgres:
# Specify partitioning size for timestamp key-value storage. Example: DAYS, MONTHS, YEARS, INDEFINITE.
ts_key_value_partitioning: "${SQL_POSTGRES_TS_KV_PARTITIONING:MONTHS}"

4
dao/src/main/java/org/thingsboard/server/dao/aspect/SqlDaoCallsAspect.java

@ -55,8 +55,8 @@ public class SqlDaoCallsAspect {
private final Set<String> invalidTenantDbCallMethods = ConcurrentHashMap.newKeySet();
private final ConcurrentMap<TenantId, DbCallStats> statsMap = new ConcurrentHashMap<>();
@Scheduled(initialDelayString = "${sql.log_tenant_stats.log_tenant_stats_interval:60000}",
fixedDelayString = "${sql.log_tenant_stats.log_tenant_stats_interval:60000}")
@Scheduled(initialDelayString = "${sql.log_tenant_stats_interval_ms:60000}",
fixedDelayString = "${sql.log_tenant_stats_interval_ms:60000}")
public void printStats() {
List<DbCallStatsSnapshot> snapshots = snapshot();
if (snapshots.isEmpty()) return;

1
pom.xml

@ -1213,7 +1213,6 @@
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>${json-path.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>

4
rule-engine/rule-engine-components/pom.xml

@ -160,6 +160,10 @@
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
</dependencies>
<build>

3
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);

107
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbCopyKeysNode.java

@ -0,0 +1,107 @@
/**
* 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.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
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.util.TbNodeUtils;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
@Slf4j
@RuleNode(
type = ComponentType.TRANSFORMATION,
name = "copy keys",
configClazz = TbCopyKeysNodeConfiguration.class,
nodeDescription = "Copies the msg or metadata keys with specified key names selected in the list",
nodeDetails = "Will fetch fields values specified in list. If specified field is not part of msg or metadata fields it will be ignored." +
"Returns transformed messages via <code>Success</code> chain",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbTransformationNodeCopyKeysConfig",
icon = "content_copy"
)
public class TbCopyKeysNode implements TbNode {
private TbCopyKeysNodeConfiguration config;
private List<Pattern> patternKeys;
private boolean fromMetadata;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbCopyKeysNodeConfiguration.class);
this.fromMetadata = config.isFromMetadata();
this.patternKeys = new ArrayList<>();
config.getKeys().forEach(key -> {
this.patternKeys.add(Pattern.compile(key));
});
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
TbMsgMetaData metaData = msg.getMetaData();
String msgData = msg.getData();
boolean msgChanged = false;
JsonNode dataNode = JacksonUtil.toJsonNode(msgData);
if (dataNode.isObject()) {
if (fromMetadata) {
ObjectNode msgDataNode = (ObjectNode) dataNode;
Map<String, String> metaDataMap = metaData.getData();
for (Map.Entry<String, String> entry : metaDataMap.entrySet()) {
String keyData = entry.getKey();
if (checkKey(keyData)) {
msgChanged = true;
msgDataNode.put(keyData, entry.getValue());
}
}
msgData = JacksonUtil.toString(msgDataNode);
} else {
Iterator<Map.Entry<String, JsonNode>> iteratorNode = dataNode.fields();
while (iteratorNode.hasNext()) {
Map.Entry<String, JsonNode> entry = iteratorNode.next();
String keyData = entry.getKey();
if (checkKey(keyData)) {
msgChanged = true;
metaData.putValue(keyData, JacksonUtil.toString(entry.getValue()));
}
}
}
}
if (msgChanged) {
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, msgData));
} else {
ctx.tellSuccess(msg);
}
}
boolean checkKey(String key) {
return patternKeys.stream().anyMatch(pattern -> pattern.matcher(key).matches());
}
}

38
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbCopyKeysNodeConfiguration.java

@ -0,0 +1,38 @@
/**
* 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 lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
import java.util.Collections;
import java.util.Set;
@Data
public class TbCopyKeysNodeConfiguration implements NodeConfiguration<TbCopyKeysNodeConfiguration> {
private boolean fromMetadata;
private Set<String> keys;
@Override
public TbCopyKeysNodeConfiguration defaultConfiguration() {
TbCopyKeysNodeConfiguration configuration = new TbCopyKeysNodeConfiguration();
configuration.setKeys(Collections.emptySet());
configuration.setFromMetadata(false);
return configuration;
}
}

105
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNode.java

@ -0,0 +1,105 @@
/**
* 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.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
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.util.TbNodeUtils;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
@Slf4j
@RuleNode(
type = ComponentType.TRANSFORMATION,
name = "delete keys",
configClazz = TbDeleteKeysNodeConfiguration.class,
nodeDescription = "Removes keys from the msg data or metadata with the specified key names selected in the list",
nodeDetails = "Will fetch fields (regex) values specified in list. If specified field (regex) is not part of msg " +
"or metadata fields it will be ignored. Returns transformed messages via <code>Success</code> chain",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbTransformationNodeDeleteKeysConfig",
icon = "remove_circle"
)
public class TbDeleteKeysNode implements TbNode {
private TbDeleteKeysNodeConfiguration config;
private List<Pattern> patternKeys;
private boolean fromMetadata;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbDeleteKeysNodeConfiguration.class);
this.fromMetadata = config.isFromMetadata();
this.patternKeys = new ArrayList<>();
config.getKeys().forEach(key -> {
this.patternKeys.add(Pattern.compile(key));
});
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
TbMsgMetaData metaData = msg.getMetaData();
String msgData = msg.getData();
List<String> keysToDelete = new ArrayList<>();
if (fromMetadata) {
Map<String, String> metaDataMap = metaData.getData();
metaDataMap.forEach((keyMetaData, valueMetaData) -> {
if (checkKey(keyMetaData)) {
keysToDelete.add(keyMetaData);
}
});
keysToDelete.forEach(key -> metaDataMap.remove(key));
metaData = new TbMsgMetaData(metaDataMap);
} else {
JsonNode dataNode = JacksonUtil.toJsonNode(msgData);
if (dataNode.isObject()) {
ObjectNode msgDataObject = (ObjectNode) dataNode;
dataNode.fields().forEachRemaining(entry -> {
String keyData = entry.getKey();
if (checkKey(keyData)) {
keysToDelete.add(keyData);
}
});
msgDataObject.remove(keysToDelete);
msgData = JacksonUtil.toString(msgDataObject);
}
}
if (keysToDelete.isEmpty()) {
ctx.tellSuccess(msg);
} else {
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, msgData));
}
}
boolean checkKey(String key) {
return patternKeys.stream().anyMatch(pattern -> pattern.matcher(key).matches());
}
}

38
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNodeConfiguration.java

@ -0,0 +1,38 @@
/**
* 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 lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
import java.util.Collections;
import java.util.Set;
@Data
public class TbDeleteKeysNodeConfiguration implements NodeConfiguration<TbDeleteKeysNodeConfiguration> {
private boolean fromMetadata;
private Set<String> keys;
@Override
public TbDeleteKeysNodeConfiguration defaultConfiguration() {
TbDeleteKeysNodeConfiguration configuration = new TbDeleteKeysNodeConfiguration();
configuration.setKeys(Collections.emptySet());
configuration.setFromMetadata(false);
return configuration;
}
}

82
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNode.java

@ -0,0 +1,82 @@
/**
* 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.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
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.util.TbNodeUtils;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import java.util.concurrent.ExecutionException;
@Slf4j
@RuleNode(
type = ComponentType.TRANSFORMATION,
name = "json path",
configClazz = TbJsonPathNodeConfiguration.class,
nodeDescription = "Transforms incoming message body using JSONPath expression.",
nodeDetails = "JSONPath expression specifies a path to an element or a set of elements in a JSON structure. <br/>"
+ "<b>'$'</b> represents the root object or array. <br/>"
+ "If JSONPath expression evaluation failed, incoming message routes via <code>Failure</code> chain, "
+ "otherwise <code>Success</code> chain is used.",
uiResources = {"static/rulenode/rulenode-core-config.js"},
icon = "functions",
configDirective = "tbTransformationNodeJsonPathConfig"
)
public class TbJsonPathNode implements TbNode {
private TbJsonPathNodeConfiguration config;
private Configuration configurationJsonPath;
private JsonPath jsonPath;
private String jsonPathValue;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbJsonPathNodeConfiguration.class);
this.jsonPathValue = config.getJsonPath();
if (!TbJsonPathNodeConfiguration.DEFAULT_JSON_PATH.equals(this.jsonPathValue)) {
this.configurationJsonPath = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.build();
this.jsonPath = JsonPath.compile(config.getJsonPath());
}
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
if (!TbJsonPathNodeConfiguration.DEFAULT_JSON_PATH.equals(this.jsonPathValue)) {
try {
JsonNode jsonPathData = jsonPath.read(msg.getData(), this.configurationJsonPath);
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(jsonPathData)));
} catch (PathNotFoundException e) {
ctx.tellFailure(msg, e);
}
} else {
ctx.tellSuccess(msg);
}
}
}

34
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNodeConfiguration.java

@ -0,0 +1,34 @@
/**
* 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 lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
@Data
public class TbJsonPathNodeConfiguration implements NodeConfiguration<TbJsonPathNodeConfiguration> {
static final String DEFAULT_JSON_PATH = "$";
private String jsonPath;
@Override
public TbJsonPathNodeConfiguration defaultConfiguration() {
TbJsonPathNodeConfiguration configuration = new TbJsonPathNodeConfiguration();
configuration.setJsonPath(DEFAULT_JSON_PATH);
return configuration;
}
}

97
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbRenameKeysNode.java

@ -0,0 +1,97 @@
/**
* 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.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
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.util.TbNodeUtils;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@Slf4j
@RuleNode(
type = ComponentType.TRANSFORMATION,
name = "rename keys",
configClazz = TbRenameKeysNodeConfiguration.class,
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 selected msg source(data or metadata), it will be ignored." +
" Returns transformed messages via <code>Success</code> chain",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbTransformationNodeRenameKeysConfig",
icon = "find_replace"
)
public class TbRenameKeysNode implements TbNode {
private TbRenameKeysNodeConfiguration config;
private Map<String, String> renameKeysMapping;
private boolean fromMetadata;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, TbRenameKeysNodeConfiguration.class);
this.renameKeysMapping = config.getRenameKeysMapping();
this.fromMetadata = config.isFromMetadata();
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
TbMsgMetaData metaData = msg.getMetaData();
String data = msg.getData();
boolean msgChanged = false;
if (fromMetadata) {
Map<String, String> metaDataMap = metaData.getData();
for (Map.Entry<String, String> entry : renameKeysMapping.entrySet()) {
String nameKey = entry.getKey();
if (metaDataMap.containsKey(nameKey)) {
msgChanged = true;
metaDataMap.put(entry.getValue(), metaDataMap.get(nameKey));
metaDataMap.remove(nameKey);
}
}
metaData = new TbMsgMetaData(metaDataMap);
} else {
JsonNode dataNode = JacksonUtil.toJsonNode(msg.getData());
if (dataNode.isObject()) {
ObjectNode msgData = (ObjectNode) dataNode;
for (Map.Entry<String, String> entry : renameKeysMapping.entrySet()) {
String nameKey = entry.getKey();
if (msgData.has(nameKey)) {
msgChanged = true;
msgData.set(entry.getValue(), msgData.get(nameKey));
msgData.remove(nameKey);
}
}
data = JacksonUtil.toString(msgData);
}
}
if (msgChanged) {
ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), metaData, data));
} else {
ctx.tellSuccess(msg);
}
}
}

37
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbRenameKeysNodeConfiguration.java

@ -0,0 +1,37 @@
/**
* 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 lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
import java.util.Map;
@Data
public class TbRenameKeysNodeConfiguration implements NodeConfiguration<TbRenameKeysNodeConfiguration> {
private boolean fromMetadata;
private Map<String, String> renameKeysMapping;
@Override
public TbRenameKeysNodeConfiguration defaultConfiguration() {
TbRenameKeysNodeConfiguration configuration = new TbRenameKeysNodeConfiguration();
configuration.setRenameKeysMapping(Map.of("temp", "temperature"));
configuration.setFromMetadata(false);
return configuration;
}
}

85
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 <code>Failure</code> chain, otherwise returns "
+ "inner objects of the extracted array as separate messages via <code>Success</code> 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!"));
}
}
}

2
rule-engine/rule-engine-components/src/main/resources/public/static/rulenode/rulenode-core-config.js

File diff suppressed because one or more lines are too long

166
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbCopyKeysNodeTest.java

@ -0,0 +1,166 @@
/**
* 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.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.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
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 TbCopyKeysNodeTest {
final ObjectMapper mapper = new ObjectMapper();
DeviceId deviceId;
TbCopyKeysNode node;
TbCopyKeysNodeConfiguration 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 TbCopyKeysNodeConfiguration().defaultConfiguration();
config.setKeys(Set.of("TestKey_1", "TestKey_2", "TestKey_3", "(\\w*)Data(\\w*)"));
config.setFromMetadata(true);
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node = spy(new TbCopyKeysNode());
node.init(ctx, nodeConfiguration);
}
@AfterEach
void tearDown() {
node.destroy();
}
@Test
void givenDefaultConfig_whenVerify_thenOK() {
TbCopyKeysNodeConfiguration defaultConfig = new TbCopyKeysNodeConfiguration().defaultConfiguration();
assertThat(defaultConfig.getKeys()).isEqualTo(Collections.emptySet());
assertThat(defaultConfig.isFromMetadata()).isEqualTo(false);
}
@Test
void givenMsgFromMetadata_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "{}";
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();
JsonNode dataNode = JacksonUtil.toJsonNode(newMsg.getData());
assertThat(dataNode.has("TestKey_1")).isEqualTo(true);
assertThat(dataNode.has("voltageDataValue")).isEqualTo(true);
}
@Test
void givenMsgFromMsg_whenOnMsg_thenVerifyOutput() throws Exception {
config.setFromMetadata(false);
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node.init(ctx, nodeConfiguration);
String data = "{\"DigitData\":22.5,\"TempDataValue\":10.5}";
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> metaDataMap = newMsg.getMetaData().getData();
assertThat(metaDataMap.containsKey("DigitData")).isEqualTo(true);
assertThat(metaDataMap.containsKey("TempDataValue")).isEqualTo(true);
}
@Test
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception {
TbCopyKeysNodeConfiguration defaultConfig = new TbCopyKeysNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(defaultConfig));
node.init(ctx, nodeConfiguration);
String data = "{\"DigitData\":22.5,\"TempDataValue\":10.5}";
TbMsg msg = getTbMsg(deviceId, data);
node.onMsg(ctx, msg);
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();
assertThat(newMsg.getMetaData()).isEqualTo(msg.getMetaData());
}
@Test
void givenMsgDataNotJSONObject_whenOnMsg_thenTVerifyOutput() throws Exception {
String data = "[]";
TbMsg msg = getTbMsg(deviceId, data);
node.onMsg(ctx, msg);
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();
assertThat(newMsg).isSameAs(msg);
}
private TbMsg getTbMsg(EntityId entityId, String data) {
final Map<String, String> mdMap = Map.of(
"TestKey_1", "Test",
"country", "US",
"voltageDataValue", "220",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
}
}

149
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbDeleteKeysNodeTest.java

@ -0,0 +1,149 @@
/**
* 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.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.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
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 TbDeleteKeysNodeTest {
final ObjectMapper mapper = new ObjectMapper();
DeviceId deviceId;
TbDeleteKeysNode node;
TbDeleteKeysNodeConfiguration 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 TbDeleteKeysNodeConfiguration().defaultConfiguration();
config.setKeys(Set.of("TestKey_1", "TestKey_2", "TestKey_3", "(\\w*)Data(\\w*)"));
config.setFromMetadata(true);
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node = spy(new TbDeleteKeysNode());
node.init(ctx, nodeConfiguration);
}
@AfterEach
void tearDown() {
node.destroy();
}
@Test
void givenDefaultConfig_whenVerify_thenOK() {
TbDeleteKeysNodeConfiguration defaultConfig = new TbDeleteKeysNodeConfiguration().defaultConfiguration();
assertThat(defaultConfig.getKeys()).isEqualTo(Collections.emptySet());
assertThat(defaultConfig.isFromMetadata()).isEqualTo(false);
}
@Test
void givenMsgFromMetadata_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "{}";
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> metaDataMap = newMsg.getMetaData().getData();
assertThat(metaDataMap.containsKey("TestKey_1")).isEqualTo(false);
assertThat(metaDataMap.containsKey("voltageDataValue")).isEqualTo(false);
}
@Test
void givenMsgFromMsg_whenOnMsg_thenVerifyOutput() throws Exception {
config.setFromMetadata(false);
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node.init(ctx, nodeConfiguration);
String data = "{\"Voltage\":22.5,\"TempDataValue\":10.5}";
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();
JsonNode dataNode = JacksonUtil.toJsonNode(newMsg.getData());
assertThat(dataNode.has("TempDataValue")).isEqualTo(false);
assertThat(dataNode.has("Voltage")).isEqualTo(true);
}
@Test
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception {
TbDeleteKeysNodeConfiguration defaultConfig = new TbDeleteKeysNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(defaultConfig));
node.init(ctx, nodeConfiguration);
String data = "{\"Voltage\":220,\"Humidity\":56}";
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();
assertThat(newMsg.getData()).isEqualTo(data);
}
private TbMsg getTbMsg(EntityId entityId, String data) {
final Map<String, String> mdMap = Map.of(
"TestKey_1", "Test",
"country", "US",
"voltageDataValue", "220",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
}
}

167
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbJsonPathNodeTest.java

@ -0,0 +1,167 @@
/**
* 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 com.jayway.jsonpath.PathNotFoundException;
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.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 static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
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 TbJsonPathNodeTest {
final ObjectMapper mapper = new ObjectMapper();
DeviceId deviceId;
TbJsonPathNode node;
TbJsonPathNodeConfiguration 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 TbJsonPathNodeConfiguration();
config.setJsonPath("$.Attribute_2");
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node = spy(new TbJsonPathNode());
node.init(ctx, nodeConfiguration);
}
@AfterEach
void tearDown() {
node.destroy();
}
@Test
void givenDefaultConfig_whenInit_thenFail() {
config.setJsonPath("");
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
assertThatThrownBy(() -> node.init(ctx, nodeConfiguration)).isInstanceOf(IllegalArgumentException.class);
}
@Test
void givenDefaultConfig_whenVerify_thenOK() {
TbJsonPathNodeConfiguration defaultConfig = new TbJsonPathNodeConfiguration().defaultConfiguration();
assertThat(defaultConfig.getJsonPath()).isEqualTo(TbJsonPathNodeConfiguration.DEFAULT_JSON_PATH);
}
@Test
void givenPrimitiveMsg_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_2\":100}";
VerifyOutputMsg(data, 1, 100);
data = "{\"Attribute_1\":22.5,\"Attribute_2\":\"StringValue\"}";
VerifyOutputMsg(data, 2, "StringValue");
}
@Test
void givenJsonArray_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_2\":[{\"Attribute_3\":22.5,\"Attribute_4\":10.3}, {\"Attribute_5\":22.5,\"Attribute_6\":10.3}]}";
VerifyOutputMsg(data, 1, JacksonUtil.toJsonNode(data).get("Attribute_2"));
}
@Test
void givenJsonNode_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_2\":{\"Attribute_3\":22.5,\"Attribute_4\":10.3}}";
VerifyOutputMsg(data, 1, JacksonUtil.toJsonNode(data).get("Attribute_2"));
}
@Test
void givenJsonArrayWithFilter_whenOnMsg_thenVerifyOutput() throws Exception {
config.setJsonPath("$.Attribute_2[?(@.voltage > 200)]");
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node.init(ctx, nodeConfiguration);
String data = "{\"Attribute_1\":22.5,\"Attribute_2\":[{\"voltage\":220}, {\"voltage\":250}, {\"voltage\":110}]}";
VerifyOutputMsg(data, 1, JacksonUtil.toJsonNode("[{\"voltage\":220}, {\"voltage\":250}]"));
}
@Test
void givenNoArrayMsg_whenOnMsg_thenTellFailure() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_5\":10.3}";
JsonNode dataNode = JacksonUtil.toJsonNode(data);
TbMsg msg = getTbMsg(deviceId, dataNode.toString());
node.onMsg(ctx, msg);
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(ctx, never()).tellSuccess(any());
verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
assertThat(newMsgCaptor.getValue()).isSameAs(msg);
assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class);
}
@Test
void givenNoResultsForPath_whenOnMsg_thenTellFailure() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_5\":10.3}";
JsonNode dataNode = JacksonUtil.toJsonNode(data);
TbMsg msg = getTbMsg(deviceId, dataNode.toString());
node.onMsg(ctx, msg);
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(ctx, never()).tellSuccess(any());
verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
assertThat(newMsgCaptor.getValue()).isSameAs(msg);
assertThat(exceptionCaptor.getValue()).isInstanceOf(PathNotFoundException.class);
}
private void VerifyOutputMsg(String data, int countTellSuccess, Object value) throws Exception {
JsonNode dataNode = JacksonUtil.toJsonNode(data);
node.onMsg(ctx, getTbMsg(deviceId, dataNode.toString()));
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(countTellSuccess)).tellSuccess(newMsgCaptor.capture());
verify(ctx, never()).tellFailure(any(), any());
assertThat(newMsgCaptor.getValue().getData()).isEqualTo(JacksonUtil.toString(value));
}
private TbMsg getTbMsg(EntityId entityId, String data) {
Map<String, String> mdMap = Map.of("country", "US",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
}
}

159
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbRenameKeysNodeTest.java

@ -0,0 +1,159 @@
/**
* 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 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.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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
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 TbRenameKeysNodeTest {
DeviceId deviceId;
TbRenameKeysNode node;
TbRenameKeysNodeConfiguration 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 TbRenameKeysNodeConfiguration().defaultConfiguration();
config.setRenameKeysMapping(Map.of("TestKey_1", "Attribute_1", "TestKey_2", "Attribute_2"));
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
node = spy(new TbRenameKeysNode());
node.init(ctx, nodeConfiguration);
}
@AfterEach
void tearDown() {
node.destroy();
}
@Test
void givenDefaultConfig_whenVerify_thenOK() {
TbRenameKeysNodeConfiguration defaultConfig = new TbRenameKeysNodeConfiguration().defaultConfiguration();
assertThat(defaultConfig.getRenameKeysMapping()).isEqualTo(Map.of("temp", "temperature"));
assertThat(defaultConfig.isFromMetadata()).isEqualTo(false);
}
@Test
void givenMsg_whenOnMsg_thenVerifyOutput() throws Exception {
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();
JsonNode dataNode = JacksonUtil.toJsonNode(newMsg.getData());
assertThat(dataNode.has("Attribute_2")).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
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception {
TbRenameKeysNodeConfiguration defaultConfig = new TbRenameKeysNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(defaultConfig));
node.init(ctx, nodeConfiguration);
String data = "{\"Temperature_1\":22.5,\"TestKey_2\":10.3}";
TbMsg msg = getTbMsg(deviceId, data);
node.onMsg(ctx, msg);
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();
assertThat(newMsg.getMetaData()).isEqualTo(msg.getMetaData());
}
@Test
void givenMsgDataNotJSONObject_whenOnMsg_thenVerifyOutput() throws Exception {
String data = "[]";
TbMsg msg = getTbMsg(deviceId, data);
node.onMsg(ctx, msg);
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();
assertThat(newMsg).isSameAs(msg);
}
private TbMsg getTbMsg(EntityId entityId, String data) {
final Map<String, String> mdMap = Map.of(
"TestKey_1", "Test",
"country", "US",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
}
}

139
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<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
ArgumentCaptor<Exception> 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<Runnable> successCaptor = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Consumer<Throwable>> 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<TbMsg> 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<String, String> mdMap = Map.of(
"country", "US",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
}
}
Loading…
Cancel
Save