Browse Source

tbel_test_doc: refactoring String to Text Blocks

pull/11939/head
nick 2 years ago
parent
commit
777104e620
  1. 525
      application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java

525
application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java

@ -35,13 +35,17 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
private String decoderStr; private String decoderStr;
private String msgStr; private String msgStr;
private final String msgMapStr = "{\"temperature\": 42, \"nested\" : \"508\"};\n"; private final String msgMapStr = """
{"temperature": 42, "nested" : "508"};
""";
private final LinkedHashMap<String, Object> expectedMap = new LinkedHashMap<>(Map.of("temperature", 42, "nested", "508")); private final LinkedHashMap<String, Object> expectedMap = new LinkedHashMap<>(Map.of("temperature", 42, "nested", "508"));
// Simple Property Expression // Simple Property Expression
@Test @Test
void simplePropertyBooleanExpression_Test() throws ExecutionException, InterruptedException { void simplePropertyBooleanExpression_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"temperature\": 15, \"humidity\" : 30}"; msgStr = """
{"temperature": 15, "humidity" : 30}
""";
decoderStr = "return (msg.temperature > 10 && msg.temperature < 20) || (msg.humidity > 10 && msg.humidity < 60);"; decoderStr = "return (msg.temperature > 10 && msg.temperature < 20) || (msg.humidity > 10 && msg.humidity < 60);";
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
boolean expected = true; boolean expected = true;
@ -52,9 +56,11 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void multipleStatements_Test() throws ExecutionException, InterruptedException { public void multipleStatements_Test() throws ExecutionException, InterruptedException {
msgStr = "{}"; msgStr = "{}";
decoderStr = "var a = 2;\n" + decoderStr = """
"var b = 2;\n" + var a = 2;
"return a + b;"; var b = 2;
return a + b;
""";
Integer expected = 4; Integer expected = 4;
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
@ -64,8 +70,10 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsChangeValueKey_Test() throws ExecutionException, InterruptedException { public void mapsChangeValueKey_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.temperature = 0;\n" + decoderStr = """
"return msg;"; msg.temperature = 0;
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("temperature", 0); expected.put("temperature", 0);
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
@ -76,10 +84,12 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsCheckExistenceKey_Test() throws ExecutionException, InterruptedException { public void mapsCheckExistenceKey_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "if(msg.temperature != null){\n" + decoderStr = """
" msg.temperature = 2;\n" + if(msg.temperature != null){
"}\n" + msg.temperature = 2;
"return msg;"; }
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("temperature", 2); expected.put("temperature", 2);
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
@ -89,15 +99,17 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsNullSafeExpressionsUsing_Test() throws ExecutionException, InterruptedException { public void mapsNullSafeExpressionsUsing_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.existingKey = {};\n" + decoderStr = """
"msg.existingKey.smth = 20;\n" + msg.existingKey = {};
"if(msg.?nonExistingKey.smth > 10){\n" + msg.existingKey.smth = 20;
" msg.temperature = 2;\n" + if(msg.?nonExistingKey.smth > 10){
"}\n" + msg.temperature = 2;
"if(msg.?existingKey.smth > 10){\n" + }
" msg.nested = \"100\";\n" + if(msg.?existingKey.smth > 10){
"}\n" + msg.nested = "100";
"return msg;"; }
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("nested", "100"); expected.put("nested", "100");
LinkedHashMap<String, Object> existingKey = new LinkedHashMap<>(Map.of("smth", 20)); LinkedHashMap<String, Object> existingKey = new LinkedHashMap<>(Map.of("smth", 20));
@ -109,21 +121,23 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsIterateThrough_Test() throws ExecutionException, InterruptedException { public void mapsIterateThrough_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "foreach(element : msg.entrySet()){\n" + decoderStr = """
" if(element.getKey() == null){\n" + foreach(element : msg.entrySet()){
" return raiseError(\"Bad getKey\");\n" + if(element.getKey() == null){
" }\n" + return raiseError("Bad getKey");
" if(element.key == null){\n" + }
" return raiseError(\"Bad key\");\n" + if(element.key == null){
" }\n" + return raiseError("Bad key");
" if(element.getValue() == null){\n" + }
" return raiseError(\"Bad getValue\");\n" + if(element.getValue() == null){
" }\n" + return raiseError("Bad getValue");
" if(element.value == null){\n" + }
" return raiseError(\"Bad value\");\n" + if(element.value == null){
" }\n" + return raiseError("Bad value");
"}\n" + }
"return msg;"; }
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
@ -132,8 +146,10 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsGetInfoSize_Test() throws ExecutionException, InterruptedException { public void mapsGetInfoSize_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "var size = msg.size();\n" + decoderStr = """
"return size;"; var size = msg.size();
return size;
""";
Integer expected = 2; Integer expected = 2;
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
@ -142,9 +158,10 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsGetInfoMemorySize_Test() throws ExecutionException, InterruptedException { public void mapsGetInfoMemorySize_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = decoderStr = """
"var memorySize = msg.memorySize();\n" + var memorySize = msg.memorySize();
"return memorySize;"; return memorySize;
""";
Long expected = 24L; Long expected = 24L;
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
@ -153,10 +170,12 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsGetInfoPut_Test() throws ExecutionException, InterruptedException { public void mapsGetInfoPut_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.humidity = 73;\n" + decoderStr = """
"msg.put(\"humidity\", 74); \n" + msg.humidity = 73;
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 74);
"return msg;"; msg.putIfAbsent("temperature1", 73);
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("humidity", 74); expected.put("humidity", 74);
expected.putIfAbsent("temperature1", 73); expected.putIfAbsent("temperature1", 73);
@ -167,16 +186,17 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsChangeValuePut_Test() throws ExecutionException, InterruptedException { public void mapsChangeValuePut_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = decoderStr = """
"msg.temperature = 73;\n" + msg.temperature = 73;
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.putIfAbsent("temperature1", 73);
"var put1 = msg.put(\"temperature\", 74);\n" + var put1 = msg.put("temperature", 74);
"msg.putIfAbsent(\"humidity\", 73); \n" + msg.putIfAbsent("humidity", 73);
"var putIfAbsent1 = msg.putIfAbsent(\"humidity\", 74); \n" + var putIfAbsent1 = msg.putIfAbsent("humidity", 74);
"return {map: msg,\n" + return {map: msg,
" put1: put1,\n" + put1: put1,
" putIfAbsent1: putIfAbsent1\n" + putIfAbsent1: putIfAbsent1
" }"; };
""";
LinkedHashMap<String, Object> expectedMapChangeValue = expectedMap; LinkedHashMap<String, Object> expectedMapChangeValue = expectedMap;
expectedMapChangeValue.replace("temperature", 42, 74); expectedMapChangeValue.replace("temperature", 42, 74);
expectedMapChangeValue.put("humidity", 73); expectedMapChangeValue.put("humidity", 73);
@ -192,16 +212,18 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsChangeValueReplace_Test() throws ExecutionException, InterruptedException { public void mapsChangeValueReplace_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.put(\"humidity\", 73); \n" + decoderStr = """
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 73);
"var replace = msg.replace(\"temperature\", 56);\n" + msg.putIfAbsent("temperature1", 73);
"var replace1 = msg.replace(\"temperature\", 56, 45);\n" + var replace = msg.replace("temperature", 56);
"var replace2 = msg.replace(\"temperature\", 48, 56);\n" + var replace1 = msg.replace("temperature", 56, 45);
"return {map: msg,\n" + var replace2 = msg.replace("temperature", 48, 56);
" replace: replace,\n" + return {map: msg,
" replace1: replace1,\n" + replace: replace,
" replace2: replace2\n" + replace1: replace1,
" }"; replace2: replace2
}
""";
LinkedHashMap<String, Object> expectedMapReplaceValue = expectedMap; LinkedHashMap<String, Object> expectedMapReplaceValue = expectedMap;
expectedMapReplaceValue.replace("temperature", 42, 45); expectedMapReplaceValue.replace("temperature", 42, 45);
expectedMapReplaceValue.put("humidity", 73); expectedMapReplaceValue.put("humidity", 73);
@ -218,10 +240,12 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsRemoveEntryFromMapByKey_Test() throws ExecutionException, InterruptedException { public void mapsRemoveEntryFromMapByKey_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.put(\"humidity\", 73); \n" + decoderStr = """
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 73);
"msg.remove(\"temperature\");\n" + msg.putIfAbsent("temperature1", 73);
"return msg;"; msg.remove("temperature");
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("humidity", 73); expected.put("humidity", 73);
expected.putIfAbsent("temperature1", 73); expected.putIfAbsent("temperature1", 73);
@ -233,14 +257,16 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsGetKeysValues_Test() throws ExecutionException, InterruptedException { public void mapsGetKeysValues_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.put(\"humidity\", 73); \n" + decoderStr = """
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 73);
"msg.remove(\"temperature\");\n" + msg.putIfAbsent("temperature1", 73);
"var keys = msg.keys();\n" + msg.remove("temperature");
"var values = msg.values();\n" + var keys = msg.keys();
"return {keys: keys,\n" + var values = msg.values();
" values: values\n" + return {keys: keys,
" }"; values: values
}
""";
LinkedHashMap<String, Object> expectedMapKeysValues = expectedMap; LinkedHashMap<String, Object> expectedMapKeysValues = expectedMap;
expectedMapKeysValues.put("humidity", 73); expectedMapKeysValues.put("humidity", 73);
expectedMapKeysValues.putIfAbsent("temperature1", 73); expectedMapKeysValues.putIfAbsent("temperature1", 73);
@ -255,16 +281,18 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsSort_Test() throws ExecutionException, InterruptedException { public void mapsSort_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.put(\"humidity\", 73); \n" + decoderStr = """
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 73);
"msg.remove(\"temperature\");\n" + msg.putIfAbsent("temperature1", 73);
"msg.sortByKey();\n" + msg.remove("temperature");
"var mapSortByKey = msg.clone();\n" + msg.sortByKey();
"var sortByValue = msg.sortByValue(); \n" + var mapSortByKey = msg.clone();
"return {mapSortByKey: mapSortByKey,\n" + var sortByValue = msg.sortByValue();
" sortByValue: sortByValue,\n" + return {mapSortByKey: mapSortByKey,
" mapSortByValue: msg\n" + sortByValue: sortByValue,
" }"; mapSortByValue: msg
}
""";
LinkedHashMap<String, Object> expectedMapSortByKey = new LinkedHashMap<>(); LinkedHashMap<String, Object> expectedMapSortByKey = new LinkedHashMap<>();
expectedMapSortByKey.put("humidity", 73); expectedMapSortByKey.put("humidity", 73);
expectedMapSortByKey.put("nested", "508"); expectedMapSortByKey.put("nested", "508");
@ -291,12 +319,14 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void mapsAddNewEntry_Test() throws ExecutionException, InterruptedException { public void mapsAddNewEntry_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = "msg.put(\"humidity\", 73); \n" + decoderStr = """
"msg.putIfAbsent(\"temperature1\", 73);\n" + msg.put("humidity", 73);
"msg.remove(\"temperature\");\n" + msg.putIfAbsent("temperature1", 73);
"var mapAdd = {\"test\": 12, \"input\" : {\"http\": 130}};\n" + msg.remove("temperature");
"msg.putAll(mapAdd);\n" + var mapAdd = {"test": 12, "input" : {"http": 130}};
"return msg;"; msg.putAll(mapAdd);
return msg;
""";
LinkedHashMap<String, Object> expected = expectedMap; LinkedHashMap<String, Object> expected = expectedMap;
expected.put("humidity", 73); expected.put("humidity", 73);
expected.putIfAbsent("temperature1", 73); expected.putIfAbsent("temperature1", 73);
@ -314,24 +344,28 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
// Lists // Lists
@Test @Test
public void listsCreateNewList_Test() throws ExecutionException, InterruptedException { public void listsCreateNewList_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [\"A\", \"B\", \"C\"]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": ["A", "B", "C"]}
"var list0 = list[0];\n" + """;
"var listSize = list.size();\n" + decoderStr = """
"var smthForeach = \"\";\n" + var list = msg.list;
"foreach (item : list) {\n" + var list0 = list[0];
" smthForeach += item;\n" + var listSize = list.size();
"}\n" + var smthForeach = "";
"var smthForLoop= \"\";\n" + foreach (item : list) {
"for (var i =0; i < list.size; i++) {\n" + smthForeach += item;
" smthForLoop += list[i];\n" + }
"}\n" + var smthForLoop= "";
"return {list: list,\n" + for (var i =0; i < list.size; i++) {
" list0: list0,\n" + smthForLoop += list[i];
" listSize: listSize,\n" + }
" smthForeach: smthForeach,\n" + return {list: list,
" smthForLoop: smthForLoop\n" + list0: list0,
" }\n"; listSize: listSize,
smthForeach: smthForeach,
smthForLoop: smthForLoop
}
""";
List expectedList = new ArrayList<>(List.of("A", "B", "C")); List expectedList = new ArrayList<>(List.of("A", "B", "C"));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
expected.put("list", expectedList); expected.put("list", expectedList);
@ -351,51 +385,55 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
*/ */
@Test @Test
public void listAddPushDelete_Test() throws ExecutionException, InterruptedException { public void listAddPushDelete_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [\"A\", \"B\", \"C\", \"B\", \"C\", \"hello\", 34567]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": ["A", "B", "C", "B", "C", "hello", 34567]}
"var msgList = {};\n" + """;
"// add/push\n" + decoderStr = """
"msgList.put(\"list\", list.clone());\n" + var list = msg.list;
"var listAdd = [\"thigsboard\", 4, 67];\n" + var msgList = {};
"var addAll = list.addAll(listAdd);\n" + // add/push
"msgList.put(\"addAll\", addAll);\n" + msgList.put("list", list.clone());
"msgList.put(\"addAllList\", list.clone());\n" + var listAdd = ["thigsboard", 4, 67];
"var addAllIndex = list.addAll(2, listAdd);\n" + var addAll = list.addAll(listAdd);
"msgList.put(\"addAllIndex\", addAllIndex);\n" + msgList.put("addAll", addAll);
"msgList.put(\"addAllIndexList\", list.clone());\n" + msgList.put("addAllList", list.clone());
"var addIndex = list.add(3, \"thigsboard\");\n" + var addAllIndex = list.addAll(2, listAdd);
"if(addIndex != null) {\n" + msgList.put("addAllIndex", addAllIndex);
" msgList.put(\"addIndex\", addIndex);\n" + msgList.put("addAllIndexList", list.clone());
"};\n" + var addIndex = list.add(3, "thigsboard");
"msgList.put(\"addIndexList\", list.clone());\n" + if(addIndex != null) {
"var push = list.push(\"thigsboard\");\n" + msgList.put("addIndex", addIndex);
"msgList.put(\"push\", push);\n" + };
"if(push != null) {\n" + msgList.put("addIndexList", list.clone());
" msgList.put(\"push\", push);\n" + var push = list.push("thigsboard");
"}\n" + msgList.put("push", push);
"msgList.put(\"pushList\", list.clone());\n" + if(push != null) {
"var unshift = list.unshift(\"r\");\n" + msgList.put("push", push);
"msgList.put(\"unshift\", unshift);\n" + }
"msgList.put(\"unshiftList\", list.clone());\n" + msgList.put("pushList", list.clone());
"var unshiftQ = [\"Q\", 4];\n" + var unshift = list.unshift("r");
"msgList.put(\"unshift\", list.unshift(unshiftQ));\n" + msgList.put("unshift", unshift);
"msgList.put(\"unshiftQunList\", list.clone());\n" + msgList.put("unshiftList", list.clone());
"// delete\n" + var unshiftQ = ["Q", 4];
"msgList.put(\"removeIndex2\", list.remove(2));\n" + msgList.put("unshift", list.unshift(unshiftQ));
"msgList.put(\"removeIndex2List\", list.clone());\n" + msgList.put("unshiftQunList", list.clone());
"msgList.put(\"removeValueC\", list.remove(\"C\"));\n" + // delete
"msgList.put(\"removeValueCList\", list.clone());\n" + msgList.put("removeIndex2", list.remove(2));
"msgList.put(\"shift\", list.shift());\n" + msgList.put("removeIndex2List", list.clone());
"msgList.put(\"shiftList\", list.clone());\n" + msgList.put("removeValueC", list.remove("C"));
"msgList.put(\"pop\", list.pop());\n" + msgList.put("removeValueCList", list.clone());
"msgList.put(\"popList\", list.clone());\n" + msgList.put("shift", list.shift());
"msgList.put(\"splice3\", list.splice(3));\n" + msgList.put("shiftList", list.clone());
"msgList.put(\"splice3List\", list.clone());\n" + msgList.put("pop", list.pop());
"msgList.put(\"splice2\", list.splice(2, 2));\n" + msgList.put("popList", list.clone());
"msgList.put(\"splice2List\", list.clone());\n" + msgList.put("splice3", list.splice(3));
"msgList.put(\"splice1_4\", list.splice(1, 4, \"start\", 5, \"end\"));\n" + msgList.put("splice3List", list.clone());
"msgList.put(\"splice1_4List\", list.clone());\n" + msgList.put("splice2", list.splice(2, 2));
"return msgList"; msgList.put("splice2List", list.clone());
msgList.put("splice1_4", list.splice(1, 4, "start", 5, "end"));
msgList.put("splice1_4List", list.clone());
return msgList
""";
ArrayList list = new ArrayList<>(List.of("A", "B", "C", "B", "C", "hello", 34567)); ArrayList list = new ArrayList<>(List.of("A", "B", "C", "B", "C", "hello", 34567));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
// add/push // add/push
@ -435,17 +473,21 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listChange_Test() throws ExecutionException, InterruptedException { public void listChange_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [\"r\", \"start\", 5, \"end\"]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": ["r", "start", 5, "end"]}
"var msgList = {};\n" + """;
"msgList.put(\"list\", list.clone());\n" + decoderStr = """
"msgList.put(\"set\", list.set(3, \"65\"));\n" + var list = msg.list;
"msgList.put(\"setList\", list.clone());\n" + var msgList = {};
"list[1] = \"98\";\n" + msgList.put("list", list.clone());
"msgList.put(\"listChangeIndex1List\", list.clone());\n" + msgList.put("set", list.set(3, "65"));
"list[0] = 2096;\n" + msgList.put("setList", list.clone());
"msgList.put(\"listChangeIndex0List\", list.clone());\n" + list[1] = "98";
"return msgList"; msgList.put("listChangeIndex1List", list.clone());
list[0] = 2096;
msgList.put("listChangeIndex0List", list.clone());
return msgList
""";
ArrayList list = new ArrayList<>(List.of("r", "start", 5, "end")); ArrayList list = new ArrayList<>(List.of("r", "start", 5, "end"));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
expected.put("list", list.clone()); expected.put("list", list.clone());
@ -461,25 +503,29 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listSort_Test() throws ExecutionException, InterruptedException { public void listSort_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [2096, \"98\", 5, \"65\"]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": [2096, "98", 5, "65"]}
"var msgList = {};\n" + """;
"msgList.put(\"list\", list.clone());\n" + decoderStr = """
"msgList.put(\"sort\", list.sort());\n" + var list = msg.list;
"msgList.put(\"sortList\", list.clone());\n" + var msgList = {};
"msgList.put(\"sortTrue\", list.sort(true));\n" + msgList.put("list", list.clone());
"msgList.put(\"sortTrueList\", list.clone());\n" + msgList.put("sort", list.sort());
"msgList.put(\"sortFalse\", list.sort(false));\n" + msgList.put("sortList", list.clone());
"msgList.put(\"sortFalseList\", list.clone());\n" + msgList.put("sortTrue", list.sort(true));
"msgList.put(\"reverse\", list.reverse());\n" + msgList.put("sortTrueList", list.clone());
"msgList.put(\"reverseList\", list.clone());\n" + msgList.put("sortFalse", list.sort(false));
"msgList.put(\"fill\", list.fill(67));\n" + msgList.put("sortFalseList", list.clone());
"msgList.put(\"fillList\", list.clone());\n" + msgList.put("reverse", list.reverse());
"msgList.put(\"fill4\", list.fill(4, 1));\n" + msgList.put("reverseList", list.clone());
"msgList.put(\"fill4List\", list.clone());\n" + msgList.put("fill", list.fill(67));
"msgList.put(\"fill4_6\", list.fill(2, 1, 4));\n" + msgList.put("fillList", list.clone());
"msgList.put(\"fill4_6List\", list.clone());\n" + msgList.put("fill4", list.fill(4, 1));
"return msgList"; msgList.put("fill4List", list.clone());
msgList.put("fill4_6", list.fill(2, 1, 4));
msgList.put("fill4_6List", list.clone());
return msgList
""";
ArrayList list = new ArrayList<>(List.of(2096, "98", 5, "65")); ArrayList list = new ArrayList<>(List.of(2096, "98", 5, "65"));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
expected.put("list", list.clone()); expected.put("list", list.clone());
@ -490,11 +536,11 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
expected.put("sortFalseList", list.clone()); expected.put("sortFalseList", list.clone());
Collections.reverse(list); Collections.reverse(list);
expected.put("reverseList", list.clone()); expected.put("reverseList", list.clone());
expected.put("fill", fill(list,67)); expected.put("fill", fill(list, 67));
expected.put("fillList", list.clone()); expected.put("fillList", list.clone());
expected.put("fill4", fill(list,4, 1)); expected.put("fill4", fill(list, 4, 1));
expected.put("fill4List", list.clone()); expected.put("fill4List", list.clone());
expected.put("fill4_6", fill(list,2, 1, 4)); expected.put("fill4_6", fill(list, 2, 1, 4));
expected.put("fill4_6List", list.clone()); expected.put("fill4_6List", list.clone());
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
@ -502,40 +548,44 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listNewListOrStringSort_Test() throws ExecutionException, InterruptedException { public void listNewListOrStringSort_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [67, 2, 2, 2]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": [67, 2, 2, 2]}
"var listAdd = [\"thigsboard\", 4, 67];\n" + """;
"var msgList = {};\n" + decoderStr = """
"msgList.put(\"list\", list.clone());\n" + var list = msg.list;
"msgList.put(\"toSorted\", list.toSorted());\n" + var listAdd = ["thigsboard", 4, 67];
"msgList.put(\"toSortedList\", list.clone());\n" + var msgList = {};
"msgList.put(\"toSortedTrue\", list.toSorted(true));\n" + msgList.put("list", list.clone());
"msgList.put(\"toSortedTrueList\", list.clone());\n" + msgList.put("toSorted", list.toSorted());
"msgList.put(\"toSortedFalse\", list.toSorted(false));\n" + msgList.put("toSortedList", list.clone());
"msgList.put(\"toSortedFalseList\", list.clone());\n" + msgList.put("toSortedTrue", list.toSorted(true));
"msgList.put(\"toReversed\", list.toReversed());\n" + msgList.put("toSortedTrueList", list.clone());
"msgList.put(\"toReversedList\", list.clone());\n" + msgList.put("toSortedFalse", list.toSorted(false));
"msgList.put(\"slice\", list.slice());\n" + msgList.put("toSortedFalseList", list.clone());
"msgList.put(\"sliceList\", list.clone());\n" + msgList.put("toReversed", list.toReversed());
"msgList.put(\"slice4\", list.slice(3));\n" + msgList.put("toReversedList", list.clone());
"msgList.put(\"slice4List\", list.clone());\n" + msgList.put("slice", list.slice());
"msgList.put(\"slice1_5\", list.slice(0,2));\n" + msgList.put("sliceList", list.clone());
"msgList.put(\"slice1_5List\", list.clone());\n" + msgList.put("slice4", list.slice(3));
"msgList.put(\"with1\", list.with(1, 69));\n" + msgList.put("slice4List", list.clone());
"msgList.put(\"with1List\", list.clone());\n" + msgList.put("slice1_5", list.slice(0,2));
"msgList.put(\"concat\", list.concat(listAdd));\n" + msgList.put("slice1_5List", list.clone());
"msgList.put(\"concatList\", list.clone());\n" + msgList.put("with1", list.with(1, 69));
"msgList.put(\"join\", list.join());\n" + msgList.put("with1List", list.clone());
"msgList.put(\"joinList\", list.clone());\n" + msgList.put("concat", list.concat(listAdd));
"msgList.put(\"toSpliced2\", list.toSpliced(1, 0, \"Feb\"));\n" + msgList.put("concatList", list.clone());
"msgList.put(\"toSpliced2List\", list.clone());\n" + msgList.put("join", list.join());
"msgList.put(\"toSpliced0_2\", list.toSpliced(0, 2));\n" + msgList.put("joinList", list.clone());
"msgList.put(\"toSpliced0_2List\", list.clone());\n" + msgList.put("toSpliced2", list.toSpliced(1, 0, "Feb"));
"msgList.put(\"toSpliced2_2\", list.toSpliced(2, 2));\n" + msgList.put("toSpliced2List", list.clone());
"msgList.put(\"toSpliced2_2List\", list.clone());\n" + msgList.put("toSpliced0_2", list.toSpliced(0, 2));
"msgList.put(\"toSpliced4_5\", list.toSpliced(2, 4, \"start\", 5, \"end\"));\n" + msgList.put("toSpliced0_2List", list.clone());
"msgList.put(\"toSpliced4_5List\", list.clone());\n" + msgList.put("toSpliced2_2", list.toSpliced(2, 2));
"return msgList"; msgList.put("toSpliced2_2List", list.clone());
msgList.put("toSpliced4_5", list.toSpliced(2, 4, "start", 5, "end"));
msgList.put("toSpliced4_5List", list.clone());
return msgList
""";
ArrayList list = new ArrayList<>(List.of(67, 2, 2, 2)); ArrayList list = new ArrayList<>(List.of(67, 2, 2, 2));
ArrayList oldList = (ArrayList) list.clone(); ArrayList oldList = (ArrayList) list.clone();
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
@ -571,7 +621,7 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
expected.put("concat", list); expected.put("concat", list);
expected.put("concatList", oldList); expected.put("concatList", oldList);
list = (ArrayList) oldList.clone(); list = (ArrayList) oldList.clone();
String join = list.toString().substring(1, list.toString().length()-1).replaceAll(" ", ""); String join = list.toString().substring(1, list.toString().length() - 1).replaceAll(" ", "");
expected.put("join", join); expected.put("join", join);
expected.put("joinList", oldList); expected.put("joinList", oldList);
list.set(1, "Feb"); list.set(1, "Feb");
@ -602,17 +652,21 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listGetInfo_Test() throws ExecutionException, InterruptedException { public void listGetInfo_Test() throws ExecutionException, InterruptedException {
msgStr = "{\"list\": [67, 2, 2, 2]}"; msgStr = """
decoderStr = "var list = msg.list;\n" + {"list": [67, 2, 2, 2]}
"var listAdd = [\"thigsboard\", 4, 67];\n" + """;
"var msgList = {};\n" + decoderStr = """
"msgList.put(\"list\", list.clone());\n" + var list = msg.list;
"msgList.put(\"length\", list.length());\n" + var listAdd = ["thigsboard", 4, 67];
"msgList.put(\"memorySize\", list.memorySize());\n" + var msgList = {};
"msgList.put(\"indOf1\", list.indexOf(\"B\", 1));\n" + msgList.put("list", list.clone());
"msgList.put(\"indOf2\", list.indexOf(2, 2));\n" + msgList.put("length", list.length());
"msgList.put(\"sStr\", list.validateClazzInArrayIsOnlyString());\n" + msgList.put("memorySize", list.memorySize());
"return msgList"; msgList.put("indOf1", list.indexOf("B", 1));
msgList.put("indOf2", list.indexOf(2, 2));
msgList.put("sStr", list.validateClazzInArrayIsOnlyString());
return msgList
""";
ArrayList list = new ArrayList<>(List.of(67, 2, 2, 2)); ArrayList list = new ArrayList<>(List.of(67, 2, 2, 2));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
expected.put("list", list); expected.put("list", list);
@ -624,6 +678,7 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
private List splice(List oldList, int start, int deleteCount, Object... values) { private List splice(List oldList, int start, int deleteCount, Object... values) {
start = initStartIndex(oldList, start); start = initStartIndex(oldList, start);
deleteCount = deleteCount < 0 ? 0 : Math.min(deleteCount, (oldList.size() - start)); deleteCount = deleteCount < 0 ? 0 : Math.min(deleteCount, (oldList.size() - start));
@ -650,6 +705,7 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
private List fill(List oldList, Object value, int start) { private List fill(List oldList, Object value, int start) {
return fill(oldList, value, start, oldList.size()); return fill(oldList, value, start, oldList.size());
} }
private List fill(List oldList, Object value, int start, int end) { private List fill(List oldList, Object value, int start, int end) {
start = initStartIndex(oldList, start); start = initStartIndex(oldList, start);
end = initEndIndex(oldList, end); end = initEndIndex(oldList, end);
@ -673,6 +729,7 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
end < 0 ? end + oldList.size() : end < 0 ? end + oldList.size() :
Math.min(end, oldList.size()); Math.min(end, oldList.size());
} }
private static final Comparator numericCompAsc = new Comparator() { private static final Comparator numericCompAsc = new Comparator() {
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
Double first = Double.parseDouble(String.valueOf(o1)); Double first = Double.parseDouble(String.valueOf(o1));

Loading…
Cancel
Save