From 7fcb82cea950887e6401e81784482c1e4fa0ca1f Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 25 Jul 2024 13:22:21 +0300 Subject: [PATCH] tbel: added new version tbel and new test hexToBytes --- .../thingsboard/script/api/tbel/TbUtils.java | 5 +++++ .../script/api/tbel/TbUtilsTest.java | 20 +++++++++++++++++++ pom.xml | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) 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 9286ffec46..d8aecb2f09 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 @@ -598,6 +598,11 @@ public class TbUtils { if (len % 2 > 0) { throw new IllegalArgumentException("Hex string must be even-length."); } + int radix = isHexadecimal(value); + if (radix != HEX_RADIX) { + throw new NumberFormatException("Value: \"" + value + "\" is not numeric or hexDecimal format!"); + } + ExecutionArrayList data = new ExecutionArrayList<>(ctx); for (int i = 0; i < hex.length(); i += 2) { // Extract two characters from the hex string 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 52bafe2fd2..a7292d4280 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 @@ -668,6 +668,26 @@ public class TbUtilsTest { Assertions.assertEquals(expectedBe, actualBe); } + @Test + public void hexToBytes_Test() { + String input = "0x01752B0367FA000500010488FFFFFFFFFFFFFFFF33"; + byte[] expected = {1, 117, 43, 3, 103, -6, 0, 5, 0, 1, 4, -120, -1, -1, -1, -1, -1, -1, -1, -1, 51}; + List actual = TbUtils.hexToBytes(ctx, input); + Assertions.assertEquals(toList(expected), actual); + try { + input = "0x01752B0367FA000500010488FFFFFFFFFFFFFFFF3"; + actual = TbUtils.hexToBytes(ctx, input); + } catch (IllegalArgumentException e) { + Assertions.assertTrue(e.getMessage().contains("Hex string must be even-length.")); + } + try { + input = "0x01752B0367KA000500010488FFFFFFFFFFFFFFFF33"; + actual = TbUtils.hexToBytes(ctx, input); + } catch (NumberFormatException e) { + Assertions.assertTrue(e.getMessage().contains("Value: \"" + input + "\" is not numeric or hexDecimal format!")); + } + } + @Test public void floatToHex_Test() { Float value = 123456789.00f; diff --git a/pom.xml b/pom.xml index 3364cc1eba..f2154720e2 100755 --- a/pom.xml +++ b/pom.xml @@ -83,7 +83,7 @@ 3.9.2 3.25.3 1.63.0 - 1.2.1 + 1.2.2 1.18.32 1.2.5 1.2.5