From d7658bd9a286b424c056d27ce0b022cc7a07e18b Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Wed, 7 Jun 2017 16:06:30 -0700 Subject: [PATCH] Generate Painless Factory for Creating Script Instances. (elastic/x-pack-elasticsearch#1667) Original commit: elastic/x-pack-elasticsearch@bda1668eecca0b65c0d74292aa005bf2b68f1118 --- .../script/MockMustacheScriptEngine.java | 8 +++++- .../xpack/common/text/TextTemplateTests.java | 27 ++++++++++++++++--- ...SecurityIndexSearcherWrapperUnitTests.java | 9 ++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java b/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java index 5852b8ab081..879b86eb001 100644 --- a/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java +++ b/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java @@ -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; + } + }); } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java b/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java index 320e9ac9736..68dd5198a65 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java @@ -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 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 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); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java index a76165696f0..315ed624cf3 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java @@ -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();