mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-13 11:14:38 +08:00
新增一个验证脚本的方法
This commit is contained in:
@@ -10,6 +10,7 @@ import com.yomahub.liteflow.exception.LiteFlowException;
|
||||
import com.yomahub.liteflow.slot.DataBus;
|
||||
import com.yomahub.liteflow.slot.Slot;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@@ -86,4 +87,12 @@ public abstract class ScriptExecutor {
|
||||
ScriptBeanManager.getScriptBeanMap().forEach(putIfAbsentConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用相应框架编译脚本
|
||||
*
|
||||
* @param script 脚本
|
||||
* @return boolean
|
||||
* @throws Exception 例外
|
||||
*/
|
||||
public abstract Object compile(String script) throws Exception;
|
||||
}
|
||||
|
||||
@@ -38,8 +38,7 @@ public abstract class JSR223ScriptExecutor extends ScriptExecutor {
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try {
|
||||
CompiledScript compiledScript = ((Compilable) scriptEngine).compile(convertScript(script));
|
||||
compiledScriptMap.put(nodeId, compiledScript);
|
||||
compiledScriptMap.put(nodeId, (CompiledScript) compile(script));
|
||||
}
|
||||
catch (Exception e) {
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
|
||||
@@ -68,4 +67,12 @@ public abstract class JSR223ScriptExecutor extends ScriptExecutor {
|
||||
compiledScriptMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script) throws ScriptException {
|
||||
if(scriptEngine == null) {
|
||||
LOG.error("script engine has not init");
|
||||
}
|
||||
return ((Compilable) scriptEngine).compile(convertScript(script));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.yomahub.liteflow.script.validator;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
|
||||
import com.yomahub.liteflow.enums.ScriptTypeEnum;
|
||||
import com.yomahub.liteflow.log.LFLog;
|
||||
import com.yomahub.liteflow.log.LFLoggerManager;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 脚本验证类
|
||||
*
|
||||
* @author Ge_Zuao
|
||||
* @since 2.12.0
|
||||
*/
|
||||
public class ScriptValidator {
|
||||
|
||||
private static final LFLog LOG = LFLoggerManager.getLogger(ScriptValidator.class);
|
||||
|
||||
private static Map<ScriptTypeEnum, ScriptExecutor> scriptExecutors;
|
||||
|
||||
static {
|
||||
List<ScriptExecutor> scriptExecutorList = new ArrayList<>();
|
||||
scriptExecutors = new HashMap<>();
|
||||
ServiceLoader.load(ScriptExecutor.class).forEach(scriptExecutorList::add);
|
||||
scriptExecutorList.stream()
|
||||
.peek(ScriptExecutor::init)
|
||||
.forEach(scriptExecutor -> scriptExecutors.put(scriptExecutor.scriptType(), scriptExecutor));
|
||||
}
|
||||
|
||||
/**
|
||||
* 只引入一种脚本语言时,使用该语言验证
|
||||
*
|
||||
* @param script 脚本
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean validate(String script){
|
||||
if(scriptExecutors.isEmpty()){
|
||||
LOG.error("The loaded script modules not found.");
|
||||
return false;
|
||||
}
|
||||
// 使用多脚本语言需要指定验证语言
|
||||
if(scriptExecutors.size() > 1){
|
||||
LOG.error("The loaded script modules more than 1. Please specify the script language.");
|
||||
return false;
|
||||
}
|
||||
|
||||
ScriptExecutor scriptExecutor = scriptExecutors.values().iterator().next();
|
||||
try {
|
||||
scriptExecutor.compile(script);
|
||||
} catch (Exception e) {
|
||||
LOG.error("script component validate failure. " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多语言脚本验证
|
||||
*
|
||||
* @param scripts 脚本
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean validate(Map<String, ScriptTypeEnum> scripts){
|
||||
for(Map.Entry<String, ScriptTypeEnum> script : scripts.entrySet()){
|
||||
ScriptExecutor scriptExecutor = scriptExecutors.getOrDefault(script.getValue(), null);
|
||||
if(scriptExecutor == null){
|
||||
LOG.error(StrUtil.format("Specified script language {} was not found.", script.getValue()));
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
scriptExecutor.compile(script.getKey());
|
||||
} catch (Exception e) {
|
||||
LOG.error(StrUtil.format("{} script component validate failure. ", script.getValue()) + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -33,8 +33,7 @@ public class GraalJavaScriptExecutor extends ScriptExecutor {
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try {
|
||||
String wrapScript = StrUtil.format("function process(){{}} process();", script);
|
||||
scriptMap.put(nodeId, Source.create("js", wrapScript));
|
||||
scriptMap.put(nodeId, Source.create("js", (CharSequence) compile(script)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
|
||||
@@ -84,4 +83,12 @@ public class GraalJavaScriptExecutor extends ScriptExecutor {
|
||||
return ScriptTypeEnum.JS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script) throws Exception {
|
||||
String wrapScript = StrUtil.format("function process(){{}} process();", script);
|
||||
Context context = Context.newBuilder().allowAllAccess(true).engine(engine).build();
|
||||
context.parse(Source.create("js", wrapScript));
|
||||
return wrapScript;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,12 +19,7 @@ public class JavaExecutor extends ScriptExecutor {
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try{
|
||||
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory(this.getClass().getClassLoader()).newScriptEvaluator();
|
||||
se.setTargetVersion(8);
|
||||
se.setReturnType(Object.class);
|
||||
se.setParameters(new String[] {"_meta"}, new Class[] {ScriptExecuteWrap.class});
|
||||
se.cook(convertScript(script));
|
||||
compiledScriptMap.put(nodeId, se);
|
||||
compiledScriptMap.put(nodeId, (IScriptEvaluator) compile(script));
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}],error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
@@ -52,6 +47,16 @@ public class JavaExecutor extends ScriptExecutor {
|
||||
return ScriptTypeEnum.JAVA;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script) throws Exception {
|
||||
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory(this.getClass().getClassLoader()).newScriptEvaluator();
|
||||
se.setTargetVersion(8);
|
||||
se.setReturnType(Object.class);
|
||||
se.setParameters(new String[] {"_meta"}, new Class[] {ScriptExecuteWrap.class});
|
||||
se.cook(convertScript(script));
|
||||
return se;
|
||||
}
|
||||
|
||||
private String convertScript(String script){
|
||||
//替换掉public,private,protected等修饰词
|
||||
String script1 = script.replaceAll("public class", "class")
|
||||
|
||||
@@ -13,6 +13,8 @@ import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -40,8 +42,7 @@ public class QLExpressScriptExecutor extends ScriptExecutor {
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try {
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(script);
|
||||
compiledScriptMap.put(nodeId, instructionSet);
|
||||
compiledScriptMap.put(nodeId, (InstructionSet) compile(script));
|
||||
}
|
||||
catch (Exception e) {
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}],error msg:{}", nodeId, e.getMessage());
|
||||
@@ -85,4 +86,9 @@ public class QLExpressScriptExecutor extends ScriptExecutor {
|
||||
return ScriptTypeEnum.QLEXPRESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script) throws Exception {
|
||||
return expressRunner.getInstructionSetFromLocalCache(script);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.yomahub.liteflow.test.script.aviator.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.aviator.AviatorScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@SpringBootTest(classes = ValidateAviatorScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateAviatorScriptComponentTest {
|
||||
|
||||
@Test
|
||||
public void testAviatorScriptComponentValidateFunction(){
|
||||
String correctScript = " use java.util.Date;\n" +
|
||||
" use cn.hutool.core.date.DateUtil;\n" +
|
||||
" let d = DateUtil.formatDateTime(new Date());\n" +
|
||||
" println(d);\n" +
|
||||
"\n" +
|
||||
" a = 2;\n" +
|
||||
" b = 3;\n" +
|
||||
"\n" +
|
||||
" setData(defaultContext, \"s1\", a*b);";
|
||||
// 语法错误
|
||||
String wrongScript = " use java.util.Date;\n" +
|
||||
" use cn.hutool.core.date.DateUtil;\n" +
|
||||
" lt d = DateUtil.formatDateTime(new Date());\n" +
|
||||
" println(d);\n" +
|
||||
"\n" +
|
||||
" a = 2;\n" +
|
||||
" b = 3;\n" +
|
||||
"\n" +
|
||||
" setData(defaultContext, \"s1\", a*b);";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.yomahub.liteflow.test.script.graaljs.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.graaljs.GraalJavaScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateGraaljsScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateGraaljsScriptComponentTest {
|
||||
@Test
|
||||
public void testGraaljsScriptComponentValidateFunction(){
|
||||
String correctScript = " var a=3;\n" +
|
||||
" var b=2;\n" +
|
||||
" var c=1;\n" +
|
||||
" var d=5;\n" +
|
||||
"\n" +
|
||||
" function addByArray(values) {\n" +
|
||||
" var sum = 0;\n" +
|
||||
" for (var i = 0; i < values.length; i++) {\n" +
|
||||
" sum += values[i];\n" +
|
||||
" }\n" +
|
||||
" return sum;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" var result = addByArray([a,b,c,d]);\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"s1\",parseInt(result));";
|
||||
// 语法错误
|
||||
String wrongScript = " var a=3;\n" +
|
||||
" var b=2;\n" +
|
||||
" var c=1;\n" +
|
||||
" var d=5;\n" +
|
||||
"\n" +
|
||||
" fn addByArray(values) {\n" +
|
||||
" var sum = 0;\n" +
|
||||
" for (var i = 0; i < values.length; i++) {\n" +
|
||||
" sum += values[i];\n" +
|
||||
" }\n" +
|
||||
" return sum;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" var result = addByArray([a,b,c,d]);\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"s1\",parseInt(result));";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.yomahub.liteflow.test.script.groovy.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.groovy.GroovyScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateGroovyScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateGroovyScriptComponentTest {
|
||||
@Test
|
||||
public void testGroovyScriptComponentValidateFunction(){
|
||||
String correctScript = " import cn.hutool.core.collection.ListUtil\n" +
|
||||
" import cn.hutool.core.date.DateUtil\n" +
|
||||
"\n" +
|
||||
" import java.util.function.Consumer\n" +
|
||||
" import java.util.function.Function\n" +
|
||||
" import java.util.stream.Collectors\n" +
|
||||
"\n" +
|
||||
" def date = DateUtil.parse(\"2022-10-17 13:31:43\")\n" +
|
||||
" println(date)\n" +
|
||||
" defaultContext.setData(\"demoDate\", date)\n" +
|
||||
"\n" +
|
||||
" List<String> list = ListUtil.toList(\"a\", \"b\", \"c\")\n" +
|
||||
"\n" +
|
||||
" List<String> resultList = list.stream().map(s -> \"hello,\" + s).collect(Collectors.toList())\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"resultList\", resultList)\n" +
|
||||
"\n" +
|
||||
" class Student {\n" +
|
||||
" int studentID\n" +
|
||||
" String studentName\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" Student student = new Student()\n" +
|
||||
" student.studentID = 100301\n" +
|
||||
" student.studentName = \"张三\"\n" +
|
||||
" defaultContext.setData(\"student\", student)\n" +
|
||||
"\n" +
|
||||
" def a = 3\n" +
|
||||
" def b = 2\n" +
|
||||
" defaultContext.setData(\"s1\", a * b)";
|
||||
// 语法错误
|
||||
String wrongScript = " import cn.hutool.core.collection.ListUtil\n" +
|
||||
" import cn.hutool.core.date.DateUtil\n" +
|
||||
"\n" +
|
||||
" import java.util.function.Consumer\n" +
|
||||
" import java.util.function.Function\n" +
|
||||
" import java.util.stream.Collectors\n" +
|
||||
"\n" +
|
||||
" d date = DateUtil.parse(\"2022-10-17 13:31:43\")\n" +
|
||||
" println(date)\n" +
|
||||
" defaultContext.setData(\"demoDate\", date)\n" +
|
||||
"\n" +
|
||||
" List<String> list = ListUtil.toList(\"a\", \"b\", \"c\")\n" +
|
||||
"\n" +
|
||||
" List<String> resultList = list.stream().map(s -> \"hello,\" + s).collect(Collectors.toList())\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"resultList\", resultList)\n" +
|
||||
"\n" +
|
||||
" class Student {\n" +
|
||||
" int studentID\n" +
|
||||
" String studentName\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" Student student = new Student()\n" +
|
||||
" student.studentID = 100301\n" +
|
||||
" student.studentName = \"张三\"\n" +
|
||||
" defaultContext.setData(\"student\", student)\n" +
|
||||
"\n" +
|
||||
" def a = 3\n" +
|
||||
" def b = 2\n" +
|
||||
" defaultContext.setData(\"s1\", a * b)";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.yomahub.liteflow.test.script.java.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.java.JavaExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateJavaScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateJavaScriptComponentTest {
|
||||
@Test
|
||||
public void testJavaScriptComponentValidateFunction(){
|
||||
String correctScript = "import com.alibaba.fastjson2.JSON;\n" +
|
||||
" import com.yomahub.liteflow.slot.DefaultContext;\n" +
|
||||
" import com.yomahub.liteflow.spi.holder.ContextAwareHolder;\n" +
|
||||
" import com.yomahub.liteflow.test.script.java.common.cmp.TestDomain;\n" +
|
||||
" import com.yomahub.liteflow.script.body.JaninoCommonScriptBody;\n" +
|
||||
" import com.yomahub.liteflow.script.ScriptExecuteWrap;\n" +
|
||||
"\n" +
|
||||
" public class Demo implements JaninoCommonScriptBody {\n" +
|
||||
" public Void body(ScriptExecuteWrap wrap) {\n" +
|
||||
" int v1 = 2;\n" +
|
||||
" int v2 = 3;\n" +
|
||||
" DefaultContext ctx = (DefaultContext) wrap.getCmp().getFirstContextBean();\n" +
|
||||
" ctx.setData(\"s1\", v1 * v2);\n" +
|
||||
"\n" +
|
||||
" TestDomain domain = (TestDomain) ContextAwareHolder.loadContextAware().getBean(TestDomain.class);\n" +
|
||||
" System.out.println(JSON.toJSONString(domain));\n" +
|
||||
" String str = domain.sayHello(\"jack\");\n" +
|
||||
" ctx.setData(\"hi\", str);\n" +
|
||||
"\n" +
|
||||
" return null;\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
// 未指定类型名错误
|
||||
String wrongScript = "import com.alibaba.fastjson2.JSON;\n" +
|
||||
" import com.yomahub.liteflow.slot.DefaultContext;\n" +
|
||||
" import com.yomahub.liteflow.spi.holder.ContextAwareHolder;\n" +
|
||||
" import com.yomahub.liteflow.test.script.java.common.cmp.TestDomain;\n" +
|
||||
" import com.yomahub.liteflow.script.body.JaninoCommonScriptBody;\n" +
|
||||
" import com.yomahub.liteflow.script.ScriptExecuteWrap;\n" +
|
||||
"\n" +
|
||||
" public class Demo implements JaninoCommonScriptBody {\n" +
|
||||
" public Void body(ScriptExecuteWrap wrap) {\n" +
|
||||
" v1 = 2;\n" +
|
||||
" int v2 = 3;\n" +
|
||||
" DefaultContext ctx = (DefaultContext) wrap.getCmp().getFirstContextBean();\n" +
|
||||
" ctx.setData(\"s1\", v1 * v2);\n" +
|
||||
"\n" +
|
||||
" TestDomain domain = (TestDomain) ContextAwareHolder.loadContextAware().getBean(TestDomain.class);\n" +
|
||||
" System.out.println(JSON.toJSONString(domain));\n" +
|
||||
" String str = domain.sayHello(\"jack\");\n" +
|
||||
" ctx.setData(\"hi\", str);\n" +
|
||||
"\n" +
|
||||
" return null;\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.javascript.JavaScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateJavaScriptScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateJavaScriptScriptComponentTest {
|
||||
@Test
|
||||
public void testJavaScriptScriptComponentValidateFunction(){
|
||||
String correctScript = "var a=3;\n" +
|
||||
" var b=2;\n" +
|
||||
" var c=1;\n" +
|
||||
" var d=5;\n" +
|
||||
"\n" +
|
||||
" function addByArray(values) {\n" +
|
||||
" var sum = 0;\n" +
|
||||
" for (var i = 0; i < values.length; i++) {\n" +
|
||||
" sum += values[i];\n" +
|
||||
" }\n" +
|
||||
" return sum;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" var result = addByArray([a,b,c,d]);\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"s1\",parseInt(result));";
|
||||
// 语法错误
|
||||
String wrongScript = "var a=3;\n" +
|
||||
" var b=2;\n" +
|
||||
" var c=1;\n" +
|
||||
" var d=5;\n" +
|
||||
"\n" +
|
||||
" fon addByArray(values) {\n" +
|
||||
" var sum = 0;\n" +
|
||||
" for (var i = 0; i < values.length; i++) {\n" +
|
||||
" sum += values[i];\n" +
|
||||
" }\n" +
|
||||
" return sum;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" var result = addByArray([a,b,c,d]);\n" +
|
||||
"\n" +
|
||||
" defaultContext.setData(\"s1\",parseInt(result));";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.yomahub.liteflow.test.script.lua.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.lua.LuaScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateLuaScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateLuaScriptComponentTest {
|
||||
@Test
|
||||
public void testLuaScriptComponentValidateFunction(){
|
||||
String correctScript = " local a=6\n" +
|
||||
" local b=10\n" +
|
||||
" if(a>5) then\n" +
|
||||
" b=5\n" +
|
||||
" else\n" +
|
||||
" b=2\n" +
|
||||
" end\n" +
|
||||
" defaultContext:setData(\"s1\",a*b)\n" +
|
||||
" defaultContext:setData(\"s2\",_meta:get(\"nodeId\"))";
|
||||
// 语法错误
|
||||
String wrongScript = " local a=6\n" +
|
||||
" local b=10\n" +
|
||||
" if(a>5) tn\n" +
|
||||
" b=5\n" +
|
||||
" else\n" +
|
||||
" b=2\n" +
|
||||
" end\n" +
|
||||
" defaultContext:setData(\"s1\",a*b)\n" +
|
||||
" defaultContext:setData(\"s2\",_meta:get(\"nodeId\"))";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.yomahub.liteflow.test.script.multi.language.validate;
|
||||
|
||||
import com.yomahub.liteflow.enums.ScriptTypeEnum;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootTest(classes = ValidateMultiLanguageScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateMultiLanguageScriptComponentTest {
|
||||
@Test
|
||||
public void testMultiLanguageScriptComponentValidateFunction(){
|
||||
String correctGroovyScript = " class Student {\n" +
|
||||
" int studentID;\n" +
|
||||
" String studentName;\n" +
|
||||
"\n" +
|
||||
" public void setStudentID(int id){\n" +
|
||||
" this.studentID = id;\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" Student student = new Student()\n" +
|
||||
" student.studentID = 100301\n" +
|
||||
" student.studentName = \"张三\"\n" +
|
||||
" defaultContext.setData(\"student\", student)\n" +
|
||||
"\n" +
|
||||
" def a = 3\n" +
|
||||
" def b = 2\n" +
|
||||
" defaultContext.setData(\"s1\", a * b)";
|
||||
String correctJavascriptScript = " var student = defaultContext.getData(\"student\");\n" +
|
||||
" student.setStudentID(10032);";
|
||||
String correctPythonScript = " a = 3\n" +
|
||||
" s1 = defaultContext.getData(\"s1\")\n" +
|
||||
" defaultContext.setData(\"s1\",s1*a)";
|
||||
// 语法错误 缩进
|
||||
String wrongPythonScript = " a = 3\n" +
|
||||
" s1 = defaultContext.getData(\"s1\")\n" +
|
||||
" defaultContext.setData(\"s1\",s1*a)";
|
||||
// 在加载多脚本时使用默认验证方法会错误
|
||||
Assertions.assertFalse(ScriptValidator.validate(correctGroovyScript));
|
||||
|
||||
// 多语言脚本验证 正确样例
|
||||
Map<String, ScriptTypeEnum> correctData = new HashMap<>();
|
||||
correctData.put(correctGroovyScript, ScriptTypeEnum.GROOVY);
|
||||
correctData.put(correctJavascriptScript, ScriptTypeEnum.JS);
|
||||
correctData.put(correctPythonScript, ScriptTypeEnum.PYTHON);
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctData));
|
||||
|
||||
// 多语言脚本验证 错误样例
|
||||
Map<String, ScriptTypeEnum> wrongData = new HashMap<>();
|
||||
wrongData.put(correctGroovyScript, ScriptTypeEnum.GROOVY);
|
||||
wrongData.put(correctJavascriptScript, ScriptTypeEnum.JS);
|
||||
wrongData.put(wrongPythonScript, ScriptTypeEnum.PYTHON);
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongData));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.yomahub.liteflow.test.script.python.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.python.PythonScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidatePythonScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidatePythonScriptComponentTest {
|
||||
@Test
|
||||
public void testPythonScriptComponentValidateFunction(){
|
||||
String correctScript = " import json\n" +
|
||||
"\n" +
|
||||
" x='{\"name\": \"杰克\", \"age\": 75, \"nationality\": \"China\"}'\n" +
|
||||
" jsonData=json.loads(x)\n" +
|
||||
" name=jsonData['name']\n" +
|
||||
" defaultContext.setData(\"name\", name.decode('utf-8'))\n" +
|
||||
"\n" +
|
||||
"\n" +
|
||||
" a=6\n" +
|
||||
" b=10\n" +
|
||||
" if a>5:\n" +
|
||||
" b=5\n" +
|
||||
" print '你好'.decode('UTF-8')\n" +
|
||||
" else:\n" +
|
||||
" print 'hi'\n" +
|
||||
" defaultContext.setData(\"s1\",a*b)\n" +
|
||||
" defaultContext.setData(\"td\", td.sayHi(\"jack\"))";
|
||||
// 语法错误 缩进
|
||||
String wrongScript = " import json\n" +
|
||||
"\n" +
|
||||
" x='{\"name\": \"杰克\", \"age\": 75, \"nationality\": \"China\"}'\n" +
|
||||
" jsonData=json.loads(x)\n" +
|
||||
" name=jsonData['name']\n" +
|
||||
" defaultContext.setData(\"name\", name.decode('utf-8'))\n" +
|
||||
"\n" +
|
||||
"\n" +
|
||||
" a=6\n" +
|
||||
" b=10\n" +
|
||||
" if a>5:\n" +
|
||||
" b=5\n" +
|
||||
" print '你好'.decode('UTF-8')\n" +
|
||||
" else:\n" +
|
||||
" print 'hi'\n" +
|
||||
" defaultContext.setData(\"s1\",a*b)\n" +
|
||||
" defaultContext.setData(\"td\", td.sayHi(\"jack\"))";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.yomahub.liteflow.test.script.qlexpress.validate;
|
||||
|
||||
import com.yomahub.liteflow.script.qlexpress.QLExpressScriptExecutor;
|
||||
import com.yomahub.liteflow.script.validator.ScriptValidator;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ValidateQLExpressScriptComponentTest.class)
|
||||
@EnableAutoConfiguration
|
||||
public class ValidateQLExpressScriptComponentTest {
|
||||
@Test
|
||||
public void testQLExpressScriptComponentValidateFunction(){
|
||||
String correctScript = " count = defaultContext.getData(\"count\");\n" +
|
||||
" if(count > 100){\n" +
|
||||
" return \"a\";\n" +
|
||||
" }else{\n" +
|
||||
" return \"b\";\n" +
|
||||
" }";
|
||||
// 语法错误
|
||||
String wrongScript = " count = defaultContext.getData(\"count\");\n" +
|
||||
" if(count > 100){\n" +
|
||||
" return \"a\";\n" +
|
||||
" }el{\n" +
|
||||
" return \"b\";\n" +
|
||||
" }";
|
||||
Assertions.assertTrue(ScriptValidator.validate(correctScript));
|
||||
Assertions.assertFalse(ScriptValidator.validate(wrongScript));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user