fix json path node class cast exception

This commit is contained in:
ShvaykaD 2022-11-25 20:05:49 +02:00
parent 046d313581
commit 77e4ab6a5e
2 changed files with 13 additions and 2 deletions

View File

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

View File

@ -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}]}";