Generate Painless Factory for Creating Script Instances. (elastic/x-pack-elasticsearch#1667)

Original commit: elastic/x-pack-elasticsearch@bda1668eec
This commit is contained in:
Jack Conradson 2017-06-07 16:06:30 -07:00 committed by GitHub
parent adc82e7323
commit d7658bd9a2
3 changed files with 39 additions and 5 deletions

View File

@ -47,6 +47,12 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
if (context.instanceClazz.equals(TemplateScript.class) == false) {
throw new IllegalArgumentException("mock mustache only understands template scripts, not [" + context.name + "]");
}
return context.factoryClazz.cast((TemplateScript.Factory) vars -> () -> script);
return context.factoryClazz.cast((TemplateScript.Factory) vars ->
new TemplateScript(vars) {
@Override
public String execute() {
return script;
}
});
}
}

View File

@ -54,7 +54,14 @@ public class TextTemplateTests extends ESTestCase {
merged = unmodifiableMap(merged);
ScriptType type = randomFrom(ScriptType.values());
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
TemplateScript.Factory compiledTemplate = templateParams ->
new TemplateScript(templateParams) {
@Override
public String execute() {
return "rendered_text";
}
};
when(service.compile(new Script(type, lang, templateText,
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
merged), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate);
@ -69,7 +76,14 @@ public class TextTemplateTests extends ESTestCase {
Map<String, Object> model = singletonMap("key", "model_val");
ScriptType type = randomFrom(ScriptType.values());
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
TemplateScript.Factory compiledTemplate = templateParams ->
new TemplateScript(templateParams) {
@Override
public String execute() {
return "rendered_text";
}
};
when(service.compile(new Script(type, lang, templateText,
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
model), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate);
@ -82,7 +96,14 @@ public class TextTemplateTests extends ESTestCase {
String templateText = "_template";
Map<String, Object> model = singletonMap("key", "model_val");
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
TemplateScript.Factory compiledTemplate = templateParams ->
new TemplateScript(templateParams) {
@Override
public String execute() {
return "rendered_text";
}
};
when(service.compile(new Script(ScriptType.INLINE, lang, templateText,
Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_TEMPLATE_CONTEXT))
.thenReturn(compiledTemplate);

View File

@ -455,7 +455,14 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
}
};
TemplateScript.Factory compiledTemplate = params -> () -> "rendered_text";
TemplateScript.Factory compiledTemplate = templateParams ->
new TemplateScript(templateParams) {
@Override
public String execute() {
return "rendered_text";
}
};
when(scriptService.compile(any(Script.class), eq(TemplateScript.CONTEXT))).thenReturn(compiledTemplate);
XContentBuilder builder = jsonBuilder();