Browse Source

fix json path node class cast exception

pull/7687/head
ShvaykaD 4 years ago
parent
commit
77e4ab6a5e
  1. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbJsonPathNode.java
  2. 13
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbJsonPathNodeTest.java

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

@ -70,7 +70,7 @@ public class TbJsonPathNode implements TbNode {
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);
Object 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);

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

@ -85,7 +85,7 @@ public class TbJsonPathNodeTest {
}
@Test
void givenPrimitiveMsg_whenOnMsg_thenVerifyOutput() throws Exception {
void givenJsonMsg_whenOnMsg_thenVerifyOutputJsonPrimitiveNode() throws Exception {
String data = "{\"Attribute_1\":22.5,\"Attribute_2\":100}";
VerifyOutputMsg(data, 1, 100);
@ -93,6 +93,17 @@ public class TbJsonPathNodeTest {
VerifyOutputMsg(data, 2, "StringValue");
}
@Test
void givenJsonMsg_whenOnMsg_thenVerifyJavaPrimitiveOutput() throws Exception {
config.setJsonPath("$.attributes.length()");
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
node.init(ctx, nodeConfiguration);
String data = "{\"attributes\":[{\"attribute_1\":10},{\"attribute_2\":20},{\"attribute_3\":30},{\"attribute_4\":40}]}";
VerifyOutputMsg(data, 1, 4);
}
@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}]}";

Loading…
Cancel
Save