Watcher: Use Mustache as default search template lang
With the latest clean ups and changes in Watcher, the default search template lang has been switched to WatcherScript.DEFAULT_LANG which points to "groovy" but it should be "mustache" instead. Original commit: elastic/x-pack-elasticsearch@1d9ef1963e
This commit is contained in:
parent
53d022a20a
commit
c2dbd5ed4a
|
@ -114,6 +114,10 @@ public class WatcherScript implements ToXContent {
|
|||
}
|
||||
|
||||
public static WatcherScript parse(XContentParser parser) throws IOException {
|
||||
return parse(parser, null);
|
||||
}
|
||||
|
||||
public static WatcherScript parse(XContentParser parser, @Nullable String lang) throws IOException {
|
||||
XContentParser.Token token = parser.currentToken();
|
||||
if (token == XContentParser.Token.VALUE_STRING) {
|
||||
return new WatcherScript(parser.text());
|
||||
|
@ -124,7 +128,6 @@ public class WatcherScript implements ToXContent {
|
|||
|
||||
String script = null;
|
||||
ScriptType type = null;
|
||||
String lang = null;
|
||||
Map<String, Object> params = null;
|
||||
|
||||
String currentFieldName = null;
|
||||
|
|
|
@ -247,7 +247,7 @@ public class WatcherSearchTemplateRequest implements ToXContent {
|
|||
indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandOpen, expandClosed,
|
||||
DEFAULT_INDICES_OPTIONS);
|
||||
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, TEMPLATE_FIELD)) {
|
||||
template = WatcherScript.parse(parser);
|
||||
template = WatcherScript.parse(parser, DEFAULT_LANG);
|
||||
} else {
|
||||
throw new ElasticsearchParseException("could not read search request. unexpected object field [" +
|
||||
currentFieldName + "]");
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.watcher.support;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class WatcherScriptTests extends ESTestCase {
|
||||
|
||||
public void testParseScript() throws IOException {
|
||||
WatcherScript script = new WatcherScript(randomAsciiOfLengthBetween(1, 5),
|
||||
randomFrom(ScriptType.values()),
|
||||
randomBoolean() ? null : randomFrom("custom", "mustache"),
|
||||
randomBoolean() ? null : randomFrom(emptyMap(), singletonMap("foo", "bar")));
|
||||
|
||||
try (XContentParser parser = createParser(script)) {
|
||||
assertThat(WatcherScript.parse(parser), equalTo(script));
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseScriptWithCustomLang() throws IOException {
|
||||
final String lang = randomFrom("custom", "painful");
|
||||
final WatcherScript script = new WatcherScript("my-script", randomFrom(ScriptType.values()), lang, null);
|
||||
|
||||
try (XContentParser parser = createParser(script)) {
|
||||
WatcherScript result = WatcherScript.parse(parser, WatcherScript.DEFAULT_LANG);
|
||||
assertThat(result.script(), equalTo(script.script()));
|
||||
assertThat(result.type(), equalTo(script.type()));
|
||||
assertThat(result.lang(), equalTo(lang));
|
||||
assertThat(result.params(), equalTo(script.params()));
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseScriptWithDefaultLang() throws IOException {
|
||||
final WatcherScript script = new WatcherScript("my-script", randomFrom(ScriptType.values()), null, null);
|
||||
|
||||
try (XContentParser parser = createParser(script)) {
|
||||
WatcherScript result = WatcherScript.parse(parser, WatcherScript.DEFAULT_LANG);
|
||||
assertThat(result.script(), equalTo(script.script()));
|
||||
assertThat(result.type(), equalTo(script.type()));
|
||||
assertThat(result.lang(), equalTo(WatcherScript.DEFAULT_LANG));
|
||||
assertThat(result.params(), equalTo(script.params()));
|
||||
}
|
||||
}
|
||||
|
||||
private static XContentParser createParser(WatcherScript watcherScript) throws IOException {
|
||||
final XContent xContent = randomFrom(XContentType.values()).xContent();
|
||||
|
||||
XContentBuilder builder = XContentBuilder.builder(xContent);
|
||||
watcherScript.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||
|
||||
XContentParser parser = XContentHelper.createParser(builder.bytes());
|
||||
assertNull(parser.currentToken());
|
||||
parser.nextToken();
|
||||
return parser;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.watcher.support.search;
|
||||
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class WatcherSearchTemplateRequestTests extends ESTestCase {
|
||||
|
||||
public void testFromXContentWithTemplateDefaultLang() throws IOException {
|
||||
String source = "{\"template\":{\"id\":\"default-script\", \"params\":{\"foo\":\"bar\"}}}";
|
||||
assertTemplate(source, "default-script", "mustache", singletonMap("foo", "bar"));
|
||||
}
|
||||
|
||||
public void testFromXContentWithTemplateCustomLang() throws IOException {
|
||||
String source = "{\"template\":{\"file\":\"custom-script\", \"lang\":\"painful\",\"params\":{\"bar\":\"baz\"}}}";
|
||||
assertTemplate(source, "custom-script", "painful", singletonMap("bar", "baz"));
|
||||
}
|
||||
|
||||
private void assertTemplate(String source, String expectedScript, String expectedLang, Map<String, Object> expectedParams) {
|
||||
try (XContentParser parser = XContentHelper.createParser(new BytesArray(source))) {
|
||||
parser.nextToken();
|
||||
|
||||
WatcherSearchTemplateRequest result = WatcherSearchTemplateRequest.fromXContent(parser, randomFrom(SearchType.values()));
|
||||
assertNotNull(result.getTemplate());
|
||||
assertThat(result.getTemplate().script(), equalTo(expectedScript));
|
||||
assertThat(result.getTemplate().lang(), equalTo(expectedLang));
|
||||
assertThat(result.getTemplate().params(), equalTo(expectedParams));
|
||||
} catch (IOException e) {
|
||||
fail("Failed to parse watch search request: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue