Merge pull request #9589 from thingsboard/fixBugStringAsObjectToBytes

[fix][bug]String asObjectToBytes
This commit is contained in:
Andrew Shvayka 2023-11-09 14:27:26 +02:00 committed by GitHub
commit b70e2adeb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 9 deletions

View File

@ -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<Byte> stringToBytes(ExecutionContext ctx, String str) {
byte[] bytes = str.getBytes();
public static List<Byte> 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<Byte> stringToBytes(ExecutionContext ctx, String str, String charsetName) throws UnsupportedEncodingException {
byte[] bytes = str.getBytes(charsetName);
public static List<Byte> 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<Byte> bytesList) {

View File

@ -350,7 +350,7 @@ public class TbUtilsTest {
}
@Test
public void parseBytesDecodeToJson() throws IOException {
public void parseBytesDecodeToJson() throws IOException, IllegalAccessException {
String expectedStr = "{\"hello\": \"world\"}";
ExecutionHashMap<String, Object> 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<Byte> expectedBytes = Bytes.asList(arrayBytes);
List<Byte> 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<Byte> toList(byte[] data) {
List<Byte> result = new ArrayList<>(data.length);
for (Byte b : data) {