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 c4f86f80a3..f13ff8cad6 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 @@ -1,12 +1,12 @@ /** * Copyright © 2016-2023 The Thingsboard Authors - * + *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -32,7 +32,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -97,13 +96,13 @@ public class TbUtils {
parserConfig.addImport("bytesToHex", new MethodStub(TbUtils.class.getMethod("bytesToHex",
ExecutionArrayList.class)));
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap",
- Object.class, HashMap.class)));
+ ExecutionContext.class, Map.class)));
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap",
- Object.class, HashMap.class, boolean.class)));
+ ExecutionContext.class, Map.class, boolean.class)));
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap",
- Object.class, HashMap.class, List.class)));
+ ExecutionContext.class, Map.class, List.class)));
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap",
- Object.class, HashMap.class, List.class, boolean.class)));
+ ExecutionContext.class, Map.class, List.class, boolean.class)));
}
public static String btoa(String input) {
@@ -240,7 +239,7 @@ public class TbUtils {
}
ExecutionArrayList
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,17 +15,50 @@
*/
package org.thingsboard.script.api.tbel;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
+import org.mvel2.ExecutionContext;
+import org.mvel2.ParserContext;
+import org.mvel2.SandboxedParserConfiguration;
+import org.mvel2.execution.ExecutionArrayList;
+import org.mvel2.execution.ExecutionHashMap;
import java.nio.ByteBuffer;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.Calendar;
import java.util.List;
-import java.util.Map;
+import java.util.Random;
+
+@Slf4j
public class TbUtilsTest {
+ private ExecutionContext ctx;
+
+ @Before
+ public void before() {
+ SandboxedParserConfiguration parserConfig = ParserContext.enableSandboxedMode();
+ parserConfig.addImport("JSON", TbJson.class);
+ parserConfig.registerDataType("Date", TbDate.class, date -> 8L);
+ parserConfig.registerDataType("Random", Random.class, date -> 8L);
+ parserConfig.registerDataType("Calendar", Calendar.class, date -> 8L);
+ try {
+ TbUtils.register(parserConfig);
+ } catch (Exception e) {
+ log.error("Cannot register functions", e);
+ }
+ ctx = new ExecutionContext(parserConfig);
+ Assert.assertNotNull(ctx);
+ }
+
+ @After
+ public void after() {
+ ctx.stop();
+ }
+
@Test
public void parseHexToInt() {
Assert.assertEquals(0xAB, TbUtils.parseHexToInt("AB"));
@@ -91,35 +124,35 @@ public class TbUtilsTest {
@Test
public void toFlatMap() {
- HashMap