diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java index bd14871fe2..31bd634221 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java @@ -61,9 +61,9 @@ public class TbUtils { parserConfig.addImport("decodeToJson", new MethodStub(TbUtils.class.getMethod("decodeToJson", ExecutionContext.class, String.class))); parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes", - ExecutionContext.class, String.class))); + ExecutionContext.class, Object.class))); parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes", - ExecutionContext.class, String.class, String.class))); + ExecutionContext.class, Object.class, String.class))); parserConfig.registerNonConvertableMethods(TbUtils.class, Collections.singleton("stringToBytes")); parserConfig.addImport("parseInt", new MethodStub(TbUtils.class.getMethod("parseInt", String.class))); @@ -192,14 +192,22 @@ public class TbUtils { return new String(bytes, charsetName); } - public static List stringToBytes(ExecutionContext ctx, String str) { - byte[] bytes = str.getBytes(); - return bytesToList(ctx, bytes); + public static List stringToBytes(ExecutionContext ctx, Object str) throws IllegalAccessException { + if (str instanceof String) { + byte[] bytes = str.toString().getBytes(); + return bytesToList(ctx, bytes); + } else { + throw new IllegalAccessException("Invalid type parameter [" + str.getClass().getSimpleName() + "]. Expected 'String'"); + } } - public static List stringToBytes(ExecutionContext ctx, String str, String charsetName) throws UnsupportedEncodingException { - byte[] bytes = str.getBytes(charsetName); - return bytesToList(ctx, bytes); + public static List stringToBytes(ExecutionContext ctx, Object str, String charsetName) throws UnsupportedEncodingException, IllegalAccessException { + if (str instanceof String) { + byte[] bytes = str.toString().getBytes(charsetName); + return bytesToList(ctx, bytes); + } else { + throw new IllegalAccessException("Invalid type parameter [" + str.getClass().getSimpleName() + "]. Expected 'String'"); + } } private static byte[] bytesFromList(List bytesList) { diff --git a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java index a16a70b962..d2900d6ec4 100644 --- a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java +++ b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java @@ -350,7 +350,7 @@ public class TbUtilsTest { } @Test - public void parseBytesDecodeToJson() throws IOException { + public void parseBytesDecodeToJson() throws IOException, IllegalAccessException { String expectedStr = "{\"hello\": \"world\"}"; ExecutionHashMap expectedJson = new ExecutionHashMap<>(1, ctx); expectedJson.put("hello", "world"); @@ -367,6 +367,24 @@ public class TbUtilsTest { Assert.assertEquals(expectedJson,actualJson); } + @Test + public void stringToBytesInputObjectTest() throws IOException, IllegalAccessException { + String expectedStr = "{\"hello\": \"world\"}"; + Object inputJson = TbUtils.decodeToJson(ctx, expectedStr); + byte[] arrayBytes = {119, 111, 114, 108, 100}; + List expectedBytes = Bytes.asList(arrayBytes); + List actualBytes = TbUtils.stringToBytes(ctx, ((ExecutionHashMap) inputJson).get("hello")); + Assert.assertEquals(expectedBytes, actualBytes); + actualBytes = TbUtils.stringToBytes(ctx, ((ExecutionHashMap) inputJson).get("hello"), "UTF-8"); + Assert.assertEquals(expectedBytes, actualBytes); + + expectedStr = "{\"hello\": 1234}"; + inputJson = TbUtils.decodeToJson(ctx, expectedStr); + Object finalInputJson = inputJson; + Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello"))); + Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello"), "UTF-8")); + } + private static List toList(byte[] data) { List result = new ArrayList<>(data.length); for (Byte b : data) {