Browse Source

tbel_test_doc: add Using Java Classes

pull/11939/head
nick 2 years ago
parent
commit
4f7f8428b1
  1. 279
      application/src/test/java/org/thingsboard/server/service/script/TbelInvokeDocsIoTest.java

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

@ -188,16 +188,16 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
public void mapsChangeValuePut_Test() throws ExecutionException, InterruptedException { public void mapsChangeValuePut_Test() throws ExecutionException, InterruptedException {
msgStr = msgMapStr; msgStr = msgMapStr;
decoderStr = """ decoderStr = """
msg.temperature = 73; msg.temperature = 73;
msg.putIfAbsent("temperature1", 73); msg.putIfAbsent("temperature1", 73);
var put1 = msg.put("temperature", 74); var put1 = msg.put("temperature", 74);
msg.putIfAbsent("humidity", 73); msg.putIfAbsent("humidity", 73);
var putIfAbsent1 = msg.putIfAbsent("humidity", 74); var putIfAbsent1 = msg.putIfAbsent("humidity", 74);
return {map: msg, return {map: msg,
put1: put1, put1: put1,
putIfAbsent1: putIfAbsent1 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);
@ -346,8 +346,8 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listsCreateNewList_Test() throws ExecutionException, InterruptedException { public void listsCreateNewList_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": ["A", "B", "C"]} {"list": ["A", "B", "C"]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var list0 = list[0]; var list0 = list[0];
@ -387,53 +387,53 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listAddPushDelete_Test() throws ExecutionException, InterruptedException { public void listAddPushDelete_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": ["A", "B", "C", "B", "C", "hello", 34567]} {"list": ["A", "B", "C", "B", "C", "hello", 34567]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var msgList = {}; var msgRez = {};
// add/push // add/push
msgList.put("list", list.clone()); msgRez.put("list", list.clone());
var listAdd = ["thigsboard", 4, 67]; var listAdd = ["thigsboard", 4, 67];
var addAll = list.addAll(listAdd); var addAll = list.addAll(listAdd);
msgList.put("addAll", addAll); msgRez.put("addAll", addAll);
msgList.put("addAllList", list.clone()); msgRez.put("addAllList", list.clone());
var addAllIndex = list.addAll(2, listAdd); var addAllIndex = list.addAll(2, listAdd);
msgList.put("addAllIndex", addAllIndex); msgRez.put("addAllIndex", addAllIndex);
msgList.put("addAllIndexList", list.clone()); msgRez.put("addAllIndexList", list.clone());
var addIndex = list.add(3, "thigsboard"); var addIndex = list.add(3, "thigsboard");
if(addIndex != null) { if(addIndex != null) {
msgList.put("addIndex", addIndex); msgRez.put("addIndex", addIndex);
}; };
msgList.put("addIndexList", list.clone()); msgRez.put("addIndexList", list.clone());
var push = list.push("thigsboard"); var push = list.push("thigsboard");
msgList.put("push", push); msgRez.put("push", push);
if(push != null) { if(push != null) {
msgList.put("push", push); msgRez.put("push", push);
} }
msgList.put("pushList", list.clone()); msgRez.put("pushList", list.clone());
var unshift = list.unshift("r"); var unshift = list.unshift("r");
msgList.put("unshift", unshift); msgRez.put("unshift", unshift);
msgList.put("unshiftList", list.clone()); msgRez.put("unshiftList", list.clone());
var unshiftQ = ["Q", 4]; var unshiftQ = ["Q", 4];
msgList.put("unshift", list.unshift(unshiftQ)); msgRez.put("unshift", list.unshift(unshiftQ));
msgList.put("unshiftQunList", list.clone()); msgRez.put("unshiftQunList", list.clone());
// delete // delete
msgList.put("removeIndex2", list.remove(2)); msgRez.put("removeIndex2", list.remove(2));
msgList.put("removeIndex2List", list.clone()); msgRez.put("removeIndex2List", list.clone());
msgList.put("removeValueC", list.remove("C")); msgRez.put("removeValueC", list.remove("C"));
msgList.put("removeValueCList", list.clone()); msgRez.put("removeValueCList", list.clone());
msgList.put("shift", list.shift()); msgRez.put("shift", list.shift());
msgList.put("shiftList", list.clone()); msgRez.put("shiftList", list.clone());
msgList.put("pop", list.pop()); msgRez.put("pop", list.pop());
msgList.put("popList", list.clone()); msgRez.put("popList", list.clone());
msgList.put("splice3", list.splice(3)); msgRez.put("splice3", list.splice(3));
msgList.put("splice3List", list.clone()); msgRez.put("splice3List", list.clone());
msgList.put("splice2", list.splice(2, 2)); msgRez.put("splice2", list.splice(2, 2));
msgList.put("splice2List", list.clone()); msgRez.put("splice2List", list.clone());
msgList.put("splice1_4", list.splice(1, 4, "start", 5, "end")); msgRez.put("splice1_4", list.splice(1, 4, "start", 5, "end"));
msgList.put("splice1_4List", list.clone()); msgRez.put("splice1_4List", list.clone());
return msgList; return msgRez;
"""; """;
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<>();
@ -475,19 +475,19 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listChange_Test() throws ExecutionException, InterruptedException { public void listChange_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": ["r", "start", 5, "end"]} {"list": ["r", "start", 5, "end"]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var msgList = {}; var msgRez = {};
msgList.put("list", list.clone()); msgRez.put("list", list.clone());
msgList.put("set", list.set(3, "65")); msgRez.put("set", list.set(3, "65"));
msgList.put("setList", list.clone()); msgRez.put("setList", list.clone());
list[1] = "98"; list[1] = "98";
msgList.put("listChangeIndex1List", list.clone()); msgRez.put("listChangeIndex1List", list.clone());
list[0] = 2096; list[0] = 2096;
msgList.put("listChangeIndex0List", list.clone()); msgRez.put("listChangeIndex0List", list.clone());
return msgList; return msgRez;
"""; """;
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<>();
@ -505,27 +505,27 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listSort_Test() throws ExecutionException, InterruptedException { public void listSort_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": [2096, "98", 5, "65"]} {"list": [2096, "98", 5, "65"]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var msgList = {}; var msgRez = {};
msgList.put("list", list.clone()); msgRez.put("list", list.clone());
msgList.put("sort", list.sort()); msgRez.put("sort", list.sort());
msgList.put("sortList", list.clone()); msgRez.put("sortList", list.clone());
msgList.put("sortTrue", list.sort(true)); msgRez.put("sortTrue", list.sort(true));
msgList.put("sortTrueList", list.clone()); msgRez.put("sortTrueList", list.clone());
msgList.put("sortFalse", list.sort(false)); msgRez.put("sortFalse", list.sort(false));
msgList.put("sortFalseList", list.clone()); msgRez.put("sortFalseList", list.clone());
msgList.put("reverse", list.reverse()); msgRez.put("reverse", list.reverse());
msgList.put("reverseList", list.clone()); msgRez.put("reverseList", list.clone());
msgList.put("fill", list.fill(67)); msgRez.put("fill", list.fill(67));
msgList.put("fillList", list.clone()); msgRez.put("fillList", list.clone());
msgList.put("fill4", list.fill(4, 1)); msgRez.put("fill4", list.fill(4, 1));
msgList.put("fill4List", list.clone()); msgRez.put("fill4List", list.clone());
msgList.put("fill4_6", list.fill(2, 1, 4)); msgRez.put("fill4_6", list.fill(2, 1, 4));
msgList.put("fill4_6List", list.clone()); msgRez.put("fill4_6List", list.clone());
return msgList; return msgRez;
"""; """;
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<>();
@ -550,42 +550,42 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listNewListOrStringSort_Test() throws ExecutionException, InterruptedException { public void listNewListOrStringSort_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": [67, 2, 2, 2]} {"list": [67, 2, 2, 2]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var listAdd = ["thigsboard", 4, 67]; var listAdd = ["thigsboard", 4, 67];
var msgList = {}; var msgRez = {};
msgList.put("list", list.clone()); msgRez.put("list", list.clone());
msgList.put("toSorted", list.toSorted()); msgRez.put("toSorted", list.toSorted());
msgList.put("toSortedList", list.clone()); msgRez.put("toSortedList", list.clone());
msgList.put("toSortedTrue", list.toSorted(true)); msgRez.put("toSortedTrue", list.toSorted(true));
msgList.put("toSortedTrueList", list.clone()); msgRez.put("toSortedTrueList", list.clone());
msgList.put("toSortedFalse", list.toSorted(false)); msgRez.put("toSortedFalse", list.toSorted(false));
msgList.put("toSortedFalseList", list.clone()); msgRez.put("toSortedFalseList", list.clone());
msgList.put("toReversed", list.toReversed()); msgRez.put("toReversed", list.toReversed());
msgList.put("toReversedList", list.clone()); msgRez.put("toReversedList", list.clone());
msgList.put("slice", list.slice()); msgRez.put("slice", list.slice());
msgList.put("sliceList", list.clone()); msgRez.put("sliceList", list.clone());
msgList.put("slice4", list.slice(3)); msgRez.put("slice4", list.slice(3));
msgList.put("slice4List", list.clone()); msgRez.put("slice4List", list.clone());
msgList.put("slice1_5", list.slice(0,2)); msgRez.put("slice1_5", list.slice(0,2));
msgList.put("slice1_5List", list.clone()); msgRez.put("slice1_5List", list.clone());
msgList.put("with1", list.with(1, 69)); msgRez.put("with1", list.with(1, 69));
msgList.put("with1List", list.clone()); msgRez.put("with1List", list.clone());
msgList.put("concat", list.concat(listAdd)); msgRez.put("concat", list.concat(listAdd));
msgList.put("concatList", list.clone()); msgRez.put("concatList", list.clone());
msgList.put("join", list.join()); msgRez.put("join", list.join());
msgList.put("joinList", list.clone()); msgRez.put("joinList", list.clone());
msgList.put("toSpliced2", list.toSpliced(1, 0, "Feb")); msgRez.put("toSpliced2", list.toSpliced(1, 0, "Feb"));
msgList.put("toSpliced2List", list.clone()); msgRez.put("toSpliced2List", list.clone());
msgList.put("toSpliced0_2", list.toSpliced(0, 2)); msgRez.put("toSpliced0_2", list.toSpliced(0, 2));
msgList.put("toSpliced0_2List", list.clone()); msgRez.put("toSpliced0_2List", list.clone());
msgList.put("toSpliced2_2", list.toSpliced(2, 2)); msgRez.put("toSpliced2_2", list.toSpliced(2, 2));
msgList.put("toSpliced2_2List", list.clone()); msgRez.put("toSpliced2_2List", list.clone());
msgList.put("toSpliced4_5", list.toSpliced(2, 4, "start", 5, "end")); msgRez.put("toSpliced4_5", list.toSpliced(2, 4, "start", 5, "end"));
msgList.put("toSpliced4_5List", list.clone()); msgRez.put("toSpliced4_5List", list.clone());
return msgList; return msgRez;
"""; """;
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();
@ -654,19 +654,19 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
@Test @Test
public void listGetInfo_Test() throws ExecutionException, InterruptedException { public void listGetInfo_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{"list": [67, 2, 2, 2]} {"list": [67, 2, 2, 2]}
"""; """;
decoderStr = """ decoderStr = """
var list = msg.list; var list = msg.list;
var listAdd = ["thigsboard", 4, 67]; var listAdd = ["thigsboard", 4, 67];
var msgList = {}; return {
msgList.put("list", list.clone()); list: list.clone(),
msgList.put("length", list.length()); length: list.length(),
msgList.put("memorySize", list.memorySize()); memorySize: list.memorySize(),
msgList.put("indOf1", list.indexOf("B", 1)); indOf1: list.indexOf("B", 1),
msgList.put("indOf2", list.indexOf(2, 2)); indOf2: list.indexOf(2, 2),
msgList.put("sStr", list.validateClazzInArrayIsOnlyString()); 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<>();
@ -685,29 +685,30 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
public void arrays_Test() throws ExecutionException, InterruptedException { public void arrays_Test() throws ExecutionException, InterruptedException {
String str = "My String"; String str = "My String";
msgStr = String.format(""" msgStr = String.format("""
{"str": "%s"} {"str": "%s"}
""", str); """, str);
decoderStr = """ decoderStr = """
var msgArray = {};
// Create new array // Create new array
var array = new int[3]; var array = new int[3];
array[0] = 1; array[0] = 1;
array[1] = 2; array[1] = 2;
array[2] = 3; array[2] = 3;
msgArray.put("array", array);
var str = msg.str; var str = msg.str;
msgArray.put("str", msg.str); var str0 = str[0];
msgArray.put("str0", str[0]);
function sum(list){ function sum(list){
var result = 0; var result = 0;
for(var i = 0; i < list.length; i++){ for(var i = 0; i < list.length; i++){
result += list[i]; result += list[i];
} }
return result; return result;
}; };
msgArray.put("sum", sum(array)); var sum = sum(array);
// array[3] = 4; // Will cause ArrayIndexOutOfBoundsException return {
return msgArray; array: array,
str: str,
str0: str0,
sum: sum
}
"""; """;
ArrayList list = new ArrayList<>(List.of(1, 2, 3)); ArrayList list = new ArrayList<>(List.of(1, 2, 3));
LinkedHashMap<String, Object> expected = new LinkedHashMap<>(); LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
@ -718,11 +719,12 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
Object actual = invokeScript(evalScript(decoderStr), msgStr); Object actual = invokeScript(evalScript(decoderStr), msgStr);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test @Test
public void arraysWillCauseArrayIndexOutOfBoundsException_Test() throws ExecutionException, InterruptedException { public void arraysWillCauseArrayIndexOutOfBoundsException_Test() throws ExecutionException, InterruptedException {
msgStr = """ msgStr = """
{} {}
"""; """;
decoderStr = """ decoderStr = """
var array = new int[3]; var array = new int[3];
array[0] = 1; array[0] = 1;
@ -734,10 +736,29 @@ class TbelInvokeDocsIoTest extends AbstractTbelInvokeTest {
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
invokeScript(evalScript(decoderStr), msgStr); invokeScript(evalScript(decoderStr), msgStr);
}).hasMessageContaining("Invalid statement: 4") }).hasMessageContaining("Invalid statement: 4")
.hasMessageContaining("[Line: 5, Column: 12]"); .hasMessageContaining("[Line: 5, Column: 12]");
} }
// Using Java Classes
@Test
public void sqrt_Test() throws ExecutionException, InterruptedException {
int i = 4;
msgStr = String.format("""
{"i": %d}
""", i);
decoderStr = """
var foo = Math.sqrt(msg.i);
return{
i: msg.i,
foo: foo
}
""";
LinkedHashMap<String, Object> expected = new LinkedHashMap<>();
expected.put("i", i);
expected.put("foo", Math.sqrt(i));
Object actual = invokeScript(evalScript(decoderStr), msgStr);
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);

Loading…
Cancel
Save