mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
add more script tests
This commit is contained in:
parent
a14b73bc13
commit
18d8e9dcdd
@ -40,4 +40,10 @@ public interface ExecutableScript {
|
||||
* Executes the script.
|
||||
*/
|
||||
Object run(Map<String, Object> vars);
|
||||
|
||||
/**
|
||||
* Unwraps a possible script value. For example, when passing vars and expecting the returned value to
|
||||
* be part of the vars.
|
||||
*/
|
||||
Object unwrap(Object value);
|
||||
}
|
||||
|
@ -34,5 +34,7 @@ public interface ScriptEngineService {
|
||||
|
||||
Object execute(Object compiledScript, Map<String, Object> vars);
|
||||
|
||||
Object unwrap(Object value);
|
||||
|
||||
void close();
|
||||
}
|
||||
|
@ -75,6 +75,10 @@ public class MvelScriptEngineService extends AbstractComponent implements Script
|
||||
return new MvelExecutableScript(compiledScript, vars);
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static class MvelExecutableScript implements ExecutableScript {
|
||||
|
||||
private final Object compiledScript;
|
||||
@ -94,5 +98,9 @@ public class MvelScriptEngineService extends AbstractComponent implements Script
|
||||
vars.putAll(this.vars);
|
||||
return MVEL.executeExpression(compiledScript, vars);
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,6 +85,10 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri
|
||||
}
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
private String generateScriptName() {
|
||||
return "Script" + counter.incrementAndGet() + ".groovy";
|
||||
}
|
||||
@ -105,5 +109,9 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri
|
||||
script.getBinding().getVariables().putAll(vars);
|
||||
return script.run();
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -116,6 +116,10 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
|
||||
}
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return ScriptValueConverter.unwrapValue(value);
|
||||
}
|
||||
|
||||
private String generateScriptName() {
|
||||
return "Script" + counter.incrementAndGet() + ".js";
|
||||
}
|
||||
@ -153,6 +157,10 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return ScriptValueConverter.unwrapValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,10 +130,8 @@ public final class ScriptValueConverter {
|
||||
* Convert an object from any repository serialized value to a valid script object.
|
||||
* This includes converting Collection multi-value properties into JavaScript Array objects.
|
||||
*
|
||||
* @param services Repository Services Registry
|
||||
* @param scope Scripting scope
|
||||
* @param qname QName of the property value for conversion
|
||||
* @param value Property value
|
||||
* @param scope Scripting scope
|
||||
* @param value Property value
|
||||
* @return Value safe for scripting usage
|
||||
*/
|
||||
public static Object wrapValue(Scriptable scope, Object value) {
|
||||
|
@ -73,6 +73,22 @@ public class JavaScriptScriptEngineTests {
|
||||
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
|
||||
}
|
||||
|
||||
@Test public void testJavaScriptObjectMapInter() {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
Map<String, Object> ctx = new HashMap<String, Object>();
|
||||
Map<String, Object> obj1 = new HashMap<String, Object>();
|
||||
obj1.put("prop1", "value1");
|
||||
ctx.put("obj1", obj1);
|
||||
vars.put("ctx", ctx);
|
||||
|
||||
se.execute(se.compile("ctx.obj2 = {}; ctx.obj2.prop2 = 'value2'; ctx.obj1.prop1 = 'uvalue1'"), vars);
|
||||
ctx = (Map<String, Object>) se.unwrap(vars.get("ctx"));
|
||||
assertThat(ctx.containsKey("obj1"), equalTo(true));
|
||||
assertThat((String) ((Map<String, Object>) ctx.get("obj1")).get("prop1"), equalTo("uvalue1"));
|
||||
assertThat(ctx.containsKey("obj2"), equalTo(true));
|
||||
assertThat((String) ((Map<String, Object>) ctx.get("obj2")).get("prop2"), equalTo("value2"));
|
||||
}
|
||||
|
||||
@Test public void testAccessListInScript() {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
|
||||
|
@ -68,6 +68,10 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
|
||||
return ret.__tojava__(Object.class);
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return unwrapValue(value);
|
||||
}
|
||||
|
||||
@Override public void close() {
|
||||
interp.cleanup();
|
||||
}
|
||||
@ -106,5 +110,20 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
|
||||
}
|
||||
return ret.__tojava__(Object.class);
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return unwrapValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Object unwrapValue(Object value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
} else if (value instanceof PyObject) {
|
||||
// seems like this is enough, inner PyDictionary will do the conversion for us for example, so expose it directly
|
||||
return ((PyObject) value).__tojava__(Object.class);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +65,22 @@ public class PythonScriptEngineTests {
|
||||
assertThat(((String) o), equalTo("2"));
|
||||
}
|
||||
|
||||
@Test public void testObjectMapInter() {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
Map<String, Object> ctx = new HashMap<String, Object>();
|
||||
Map<String, Object> obj1 = new HashMap<String, Object>();
|
||||
obj1.put("prop1", "value1");
|
||||
ctx.put("obj1", obj1);
|
||||
vars.put("ctx", ctx);
|
||||
|
||||
se.execute(se.compile("ctx['obj2'] = { 'prop2' : 'value2' }; ctx['obj1']['prop1'] = 'uvalue1'"), vars);
|
||||
ctx = (Map<String, Object>) se.unwrap(vars.get("ctx"));
|
||||
assertThat(ctx.containsKey("obj1"), equalTo(true));
|
||||
assertThat((String) ((Map<String, Object>) ctx.get("obj1")).get("prop1"), equalTo("uvalue1"));
|
||||
assertThat(ctx.containsKey("obj2"), equalTo(true));
|
||||
assertThat((String) ((Map<String, Object>) ctx.get("obj2")).get("prop2"), equalTo("value2"));
|
||||
}
|
||||
|
||||
@Test public void testAccessListInScript() {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
|
||||
|
@ -65,7 +65,7 @@ public class PythonScriptSearchTests {
|
||||
node.close();
|
||||
}
|
||||
|
||||
@Test public void testJavaScriptFilter() throws Exception {
|
||||
@Test public void testPythonFilter() throws Exception {
|
||||
client.admin().indices().prepareCreate("test").execute().actionGet();
|
||||
client.prepareIndex("test", "type1", "1")
|
||||
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).endObject())
|
||||
|
@ -61,6 +61,10 @@ public class RubyScriptEngineService extends AbstractComponent implements Script
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override public Object unwrap(Object value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public void close() {
|
||||
container.clear();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user