Refactoring
This commit is contained in:
parent
03d94c77d7
commit
8f55fdd94b
@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright © 2016-2023 The Thingsboard Authors
|
* Copyright © 2016-2023 The Thingsboard Authors
|
||||||
*
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* 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.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -97,13 +96,13 @@ public class TbUtils {
|
|||||||
parserConfig.addImport("bytesToHex", new MethodStub(TbUtils.class.getMethod("bytesToHex",
|
parserConfig.addImport("bytesToHex", new MethodStub(TbUtils.class.getMethod("bytesToHex",
|
||||||
ExecutionArrayList.class)));
|
ExecutionArrayList.class)));
|
||||||
parserConfig.addImport("toFlatMap", new MethodStub(TbUtils.class.getMethod("toFlatMap",
|
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",
|
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",
|
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",
|
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) {
|
public static String btoa(String input) {
|
||||||
@ -240,7 +239,7 @@ public class TbUtils {
|
|||||||
}
|
}
|
||||||
ExecutionArrayList<Byte> data = new ExecutionArrayList<>(ctx);
|
ExecutionArrayList<Byte> data = new ExecutionArrayList<>(ctx);
|
||||||
for (int i = 0; i < len; i += 2) {
|
for (int i = 0; i < len; i += 2) {
|
||||||
data.add((byte)((Character.digit(hex.charAt(i), 16) << 4)
|
data.add((byte) ((Character.digit(hex.charAt(i), 16) << 4)
|
||||||
+ Character.digit(hex.charAt(i + 1), 16)));
|
+ Character.digit(hex.charAt(i + 1), 16)));
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
@ -332,23 +331,25 @@ public class TbUtils {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void toFlatMap(Object json, HashMap<String, Object> map) {
|
public static ExecutionHashMap<String, Object> toFlatMap(ExecutionContext ctx, Map<String, Object> json) {
|
||||||
toFlatMap(json, map, new ArrayList<>(), true);
|
return toFlatMap(ctx, json, new ArrayList<>(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void toFlatMap(Object json, HashMap<String, Object> map, boolean pathInKey) {
|
public static ExecutionHashMap<String, Object> toFlatMap(ExecutionContext ctx, Map<String, Object> json, boolean pathInKey) {
|
||||||
toFlatMap(json, map, new ArrayList<>(), pathInKey);
|
return toFlatMap(ctx, json, new ArrayList<>(), pathInKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void toFlatMap(Object json, HashMap<String, Object> map, List<String> excludeList) {
|
public static ExecutionHashMap<String, Object> toFlatMap(ExecutionContext ctx, Map<String, Object> json, List<String> excludeList) {
|
||||||
toFlatMap(json, map, excludeList, true);
|
return toFlatMap(ctx, json, excludeList, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void toFlatMap(Object json, HashMap<String, Object> map, List<String> excludeList, boolean pathInKey) {
|
public static ExecutionHashMap<String, Object> toFlatMap(ExecutionContext ctx, Map<String, Object> json, List<String> excludeList, boolean pathInKey) {
|
||||||
|
ExecutionHashMap<String, Object> map = new ExecutionHashMap<>(16, ctx);
|
||||||
parseRecursive(json, map, excludeList, "", pathInKey);
|
parseRecursive(json, map, excludeList, "", pathInKey);
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void parseRecursive(Object json, HashMap<String, Object> map, List<String> excludeList, String path, boolean pathInKey) {
|
private static void parseRecursive(Object json, Map<String, Object> map, List<String> excludeList, String path, boolean pathInKey) {
|
||||||
if (json instanceof Map.Entry) {
|
if (json instanceof Map.Entry) {
|
||||||
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) json;
|
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) json;
|
||||||
if (StringUtils.isNotBlank(path)) {
|
if (StringUtils.isNotBlank(path)) {
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright © 2016-2023 The Thingsboard Authors
|
* Copyright © 2016-2023 The Thingsboard Authors
|
||||||
*
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
@ -15,17 +15,50 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.script.api.tbel;
|
package org.thingsboard.script.api.tbel;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Random;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
|
||||||
public class TbUtilsTest {
|
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
|
@Test
|
||||||
public void parseHexToInt() {
|
public void parseHexToInt() {
|
||||||
Assert.assertEquals(0xAB, TbUtils.parseHexToInt("AB"));
|
Assert.assertEquals(0xAB, TbUtils.parseHexToInt("AB"));
|
||||||
@ -91,35 +124,35 @@ public class TbUtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toFlatMap() {
|
public void toFlatMap() {
|
||||||
HashMap<String, Object> inputMap = new HashMap<>();
|
ExecutionHashMap<String, Object> inputMap = new ExecutionHashMap<>(16, ctx);
|
||||||
inputMap.put("name", "Alice");
|
inputMap.put("name", "Alice");
|
||||||
inputMap.put("age", 30);
|
inputMap.put("age", 30);
|
||||||
inputMap.put("devices", List.of(
|
inputMap.put("devices", new ExecutionArrayList<>(List.of(
|
||||||
new HashMap<String, Object>() {{
|
new ExecutionHashMap<>(16, ctx) {{
|
||||||
put("id", "dev001");
|
put("id", "dev001");
|
||||||
put("type", "sensor");
|
put("type", "sensor");
|
||||||
}},
|
}},
|
||||||
new HashMap<String, Object>() {{
|
new ExecutionHashMap<>(16, ctx) {{
|
||||||
put("id", "dev002");
|
put("id", "dev002");
|
||||||
put("type", "actuator");
|
put("type", "actuator");
|
||||||
}}
|
}}
|
||||||
));
|
), ctx));
|
||||||
inputMap.put("settings", new HashMap<String, Object>() {{
|
inputMap.put("settings", new ExecutionHashMap<>(16, ctx) {{
|
||||||
put("notifications", true);
|
put("notifications", true);
|
||||||
put("timezone", "UTC-5");
|
put("timezone", "UTC-5");
|
||||||
put("params", new HashMap<String, Object>() {{
|
put("params", new ExecutionHashMap<>(16, ctx) {{
|
||||||
put("param1", "value1");
|
put("param1", "value1");
|
||||||
put("param2", "value2");
|
put("param2", "value2");
|
||||||
put("param3", new HashMap<String, Object>() {{
|
put("param3", new ExecutionHashMap<>(16, ctx) {{
|
||||||
put("subParam1", "value1");
|
put("subParam1", "value1");
|
||||||
put("subParam2", "value2");
|
put("subParam2", "value2");
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
|
ExecutionArrayList<String> excludeList = new ExecutionArrayList<>(ctx);
|
||||||
|
excludeList.addAll(List.of("age", "id", "param1", "subParam2"));
|
||||||
|
|
||||||
List<String> excludeList = List.of("age", "id", "param1", "subParam2");
|
ExecutionHashMap<String, Object> expectedMapWithPath = new ExecutionHashMap<>(16, ctx);
|
||||||
|
|
||||||
HashMap<String, Object> expectedMapWithPath = new HashMap<>();
|
|
||||||
expectedMapWithPath.put("name", "Alice");
|
expectedMapWithPath.put("name", "Alice");
|
||||||
expectedMapWithPath.put("devices.0.type", "sensor");
|
expectedMapWithPath.put("devices.0.type", "sensor");
|
||||||
expectedMapWithPath.put("devices.1.type", "actuator");
|
expectedMapWithPath.put("devices.1.type", "actuator");
|
||||||
@ -128,12 +161,11 @@ public class TbUtilsTest {
|
|||||||
expectedMapWithPath.put("settings.params.param2", "value2");
|
expectedMapWithPath.put("settings.params.param2", "value2");
|
||||||
expectedMapWithPath.put("settings.params.param3.subParam1", "value1");
|
expectedMapWithPath.put("settings.params.param3.subParam1", "value1");
|
||||||
|
|
||||||
HashMap<String, Object> actualMapWithPaths = new HashMap<>();
|
ExecutionHashMap<String, Object> actualMapWithPaths = TbUtils.toFlatMap(ctx, inputMap, excludeList, true);
|
||||||
TbUtils.toFlatMap(inputMap, actualMapWithPaths, excludeList, true);
|
|
||||||
|
|
||||||
Assert.assertEquals(expectedMapWithPath, actualMapWithPaths);
|
Assert.assertEquals(expectedMapWithPath, actualMapWithPaths);
|
||||||
|
|
||||||
HashMap<String, Object> expectedMapWithoutPaths = new HashMap<>();
|
ExecutionHashMap<String, Object> expectedMapWithoutPaths = new ExecutionHashMap<>(16, ctx);
|
||||||
expectedMapWithoutPaths.put("timezone", "UTC-5");
|
expectedMapWithoutPaths.put("timezone", "UTC-5");
|
||||||
expectedMapWithoutPaths.put("name", "Alice");
|
expectedMapWithoutPaths.put("name", "Alice");
|
||||||
expectedMapWithoutPaths.put("id", "dev002");
|
expectedMapWithoutPaths.put("id", "dev002");
|
||||||
@ -145,14 +177,12 @@ public class TbUtilsTest {
|
|||||||
expectedMapWithoutPaths.put("age", 30);
|
expectedMapWithoutPaths.put("age", 30);
|
||||||
expectedMapWithoutPaths.put("param2", "value2");
|
expectedMapWithoutPaths.put("param2", "value2");
|
||||||
|
|
||||||
HashMap<String, Object> actualMapWithoutPaths = new HashMap<>();
|
ExecutionHashMap<String, Object> actualMapWithoutPaths = TbUtils.toFlatMap(ctx, inputMap, false);
|
||||||
TbUtils.toFlatMap(inputMap, actualMapWithoutPaths, new ArrayList<>(), false);
|
|
||||||
|
|
||||||
Assert.assertEquals(expectedMapWithoutPaths, actualMapWithoutPaths);
|
Assert.assertEquals(expectedMapWithoutPaths, actualMapWithoutPaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static String keyToValue(String key, String extraSymbol) {
|
private static String keyToValue(String key, String extraSymbol) {
|
||||||
return key + "Value" + (extraSymbol == null ? "" : extraSymbol);
|
return key + "Value" + (extraSymbol == null ? "" : extraSymbol);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user