Browse Source

Merge e3f85987e5 into a61aec729d

pull/15544/merge
matthezJ 1 day ago
committed by GitHub
parent
commit
aadcd9c527
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 26
      common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java
  2. 38
      common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

26
common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java

@ -5,6 +5,7 @@ package org.thingsboard.script.api.tbel;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.primitives.Bytes;
import com.upokecenter.cbor.CBORObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.mvel2.ExecutionContext;
@ -99,11 +100,18 @@ public class TbUtils {
ExecutionContext.class, List.class)));
parserConfig.addImport("decodeToJson", new MethodStub(TbUtils.class.getMethod("decodeToJson",
ExecutionContext.class, String.class)));
parserConfig.addImport("cborToJson", new MethodStub(TbUtils.class.getMethod("cborToJson",
ExecutionContext.class, List.class)));
parserConfig.addImport("jsonToCbor", new MethodStub(TbUtils.class.getMethod("jsonToCbor",
ExecutionContext.class, Object.class)));
parserConfig.addImport("jsonToCbor", new MethodStub(TbUtils.class.getMethod("jsonToCbor",
ExecutionContext.class, String.class)));
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
ExecutionContext.class, Object.class)));
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
ExecutionContext.class, Object.class, String.class)));
parserConfig.registerNonConvertableMethods(TbUtils.class, Collections.singleton("stringToBytes"));
parserConfig.registerNonConvertableMethods(TbUtils.class, Collections.singleton("jsonToCbor"));
parserConfig.addImport("parseInt", new MethodStub(TbUtils.class.getMethod("parseInt",
String.class)));
parserConfig.addImport("parseInt", new MethodStub(TbUtils.class.getMethod("parseInt",
@ -404,6 +412,23 @@ public class TbUtils {
return TbJson.parse(ctx, jsonStr);
}
public static Object cborToJson(ExecutionContext ctx, List<?> bytesList) throws IOException {
CBORObject cborObject = CBORObject.DecodeFromBytes(bytesFromList(bytesList));
return TbJson.parse(ctx, cborObject.ToJSONString());
}
public static List<Byte> jsonToCbor(ExecutionContext ctx, Object jsonValue) {
String jsonStr = TbJson.stringify(jsonValue);
byte[] cborBytes = CBORObject.FromJSONBytes(jsonStr.getBytes(StandardCharsets.UTF_8)).EncodeToBytes();
return bytesToList(ctx, cborBytes);
}
public static List<Byte> jsonToCbor(ExecutionContext ctx, String jsonStr) {
String safeJsonStr = jsonStr != null ? jsonStr : "null";
byte[] cborBytes = CBORObject.FromJSONBytes(safeJsonStr.getBytes(StandardCharsets.UTF_8)).EncodeToBytes();
return bytesToList(ctx, cborBytes);
}
public static String bytesToString(List<?> bytesList) {
byte[] bytes = bytesFromList(bytesList);
return new String(bytes);
@ -1569,4 +1594,3 @@ public class TbUtils {
}
}

38
common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

@ -5,6 +5,7 @@ package org.thingsboard.script.api.tbel;
import com.google.common.collect.Lists;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Ints;
import com.upokecenter.cbor.CBORObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
@ -637,6 +638,42 @@ public class TbUtilsTest {
Assertions.assertEquals(expectedJson, actualJson);
}
@Test
public void parseCborToJson() throws IOException {
LinkedHashMap<String, Object> cborData = new LinkedHashMap<>();
cborData.put("hello", "world");
cborData.put("value", 42);
byte[] cborPayload = CBORObject.FromObject(cborData).EncodeToBytes();
Object actualJson = TbUtils.cborToJson(ctx, Bytes.asList(cborPayload));
ExecutionHashMap<String, Object> expectedJson = new ExecutionHashMap<>(2, ctx);
expectedJson.put("hello", "world");
expectedJson.put("value", 42);
Assertions.assertEquals(expectedJson, actualJson);
}
@Test
public void parseJsonObjectToCborAndBack() throws IOException {
String jsonStr = "{\"hello\": \"world\", \"value\": 42}";
Object expectedJson = TbUtils.decodeToJson(ctx, jsonStr);
List<Byte> cborBytes = TbUtils.jsonToCbor(ctx, expectedJson);
Object actualJson = TbUtils.cborToJson(ctx, cborBytes);
Assertions.assertEquals(expectedJson, actualJson);
}
@Test
public void parseJsonStringToCborAndBack() throws IOException {
String jsonStr = "{\"hello\": \"world\", \"value\": 42}";
Object expectedJson = TbUtils.decodeToJson(ctx, jsonStr);
List<Byte> cborBytes = TbUtils.jsonToCbor(ctx, jsonStr);
Object actualJson = TbUtils.cborToJson(ctx, cborBytes);
Assertions.assertEquals(expectedJson, actualJson);
}
@Test
public void stringToBytesInputObjectTest() throws IOException, IllegalAccessException {
String expectedStr = "{\"hello\": \"world\"}";
@ -1320,4 +1357,3 @@ public class TbUtilsTest {
return result;
}
}

Loading…
Cancel
Save