Browse Source

tbel: refactoring raiseError

pull/11551/head
nick 2 years ago
parent
commit
5699c49c53
  1. 9
      common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java
  2. 7
      common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

9
common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java

@ -315,8 +315,6 @@ public class TbUtils {
String.class)));
parserConfig.addImport("decodeURI", new MethodStub(TbUtils.class.getMethod("decodeURI",
String.class)));
parserConfig.addImport("raiseError", new MethodStub(TbUtils.class.getMethod("raiseError",
String.class, Object.class)));
parserConfig.addImport("raiseError", new MethodStub(TbUtils.class.getMethod("raiseError",
String.class)));
parserConfig.addImport("isBinary", new MethodStub(TbUtils.class.getMethod("isBinary",
@ -1150,12 +1148,7 @@ public class TbUtils {
}
public static void raiseError(String message) {
raiseError(message, null);
}
public static void raiseError(String message, Object value) {
String msg = value == null ? message : message + " A value of " + value + " is invalid.";
throw new RuntimeException(msg);
throw new RuntimeException(message);
}
private static void parseRecursive(Object json, Map<String, Object> map, List<String> excludeList, String path, boolean pathInKey) {

7
common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

@ -19,6 +19,7 @@ import com.google.common.collect.Lists;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Ints;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -885,14 +886,16 @@ public class TbUtilsTest {
@Test
public void raiseError_Test() {
String message = "frequency_weighting_type must be 0, 1 or 2.";
Object value = 4;
String message = "frequency_weighting_type must be 0, 1 or 2. A value of " + value.toString() + " is invalid.";
try {
TbUtils.raiseError(message, value);
TbUtils.raiseError(message);
Assertions.fail("Should throw NumberFormatException");
} catch (RuntimeException e) {
Assertions.assertTrue(e.getMessage().contains("frequency_weighting_type must be 0, 1 or 2. A value of 4 is invalid."));
}
message = "frequency_weighting_type must be 0, 1 or 2.";
try {
TbUtils.raiseError(message);
Assertions.fail("Should throw NumberFormatException");

Loading…
Cancel
Save