Remove custom parsing and toXContent logic from TextTemplate and delegate to the Script as much as possible

Original commit: elastic/x-pack-elasticsearch@6d23549dc1
This commit is contained in:
Martijn van Groningen 2016-08-30 15:36:04 +02:00
parent 33cdecd39e
commit 565f50dbe5
37 changed files with 242 additions and 656 deletions

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.RestUtils;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.xpack.common.http.auth.HttpAuth;
import org.elasticsearch.xpack.common.http.auth.HttpAuthRegistry;
import org.elasticsearch.xpack.common.text.TextTemplate;
@ -407,11 +408,7 @@ public class HttpRequestTemplate implements ToXContent {
}
public Builder path(String path) {
return path(TextTemplate.inline(path));
}
public Builder path(TextTemplate.Builder path) {
return path(path.build());
return path(new TextTemplate(path));
}
public Builder path(TextTemplate path) {
@ -424,10 +421,6 @@ public class HttpRequestTemplate implements ToXContent {
return this;
}
public Builder putParam(String key, TextTemplate.Builder value) {
return putParam(key, value.build());
}
public Builder putParam(String key, TextTemplate value) {
this.params.put(key, value);
return this;
@ -438,10 +431,6 @@ public class HttpRequestTemplate implements ToXContent {
return this;
}
public Builder putHeader(String key, TextTemplate.Builder value) {
return putHeader(key, value.build());
}
public Builder putHeader(String key, TextTemplate value) {
this.headers.put(key, value);
return this;
@ -453,11 +442,7 @@ public class HttpRequestTemplate implements ToXContent {
}
public Builder body(String body) {
return body(TextTemplate.inline(body));
}
public Builder body(TextTemplate.Builder body) {
return body(body.build());
return body(new TextTemplate(body));
}
public Builder body(TextTemplate body) {
@ -465,8 +450,8 @@ public class HttpRequestTemplate implements ToXContent {
return this;
}
public Builder body(XContentBuilder content) {
return body(TextTemplate.inline(content));
public Builder body(XContentBuilder content) throws IOException {
return body(new TextTemplate(content.string(), content.contentType(), ScriptService.ScriptType.INLINE, null));
}
public Builder connectionTimeout(TimeValue timeout) {
@ -503,7 +488,7 @@ public class HttpRequestTemplate implements ToXContent {
port = uri.getPort() > 0 ? uri.getPort() : scheme.defaultPort();
host = uri.getHost();
if (Strings.hasLength(uri.getPath())) {
path = TextTemplate.inline(uri.getPath()).build();
path = new TextTemplate(uri.getPath());
}
String rawQuery = uri.getRawQuery();
@ -511,7 +496,7 @@ public class HttpRequestTemplate implements ToXContent {
Map<String, String> stringParams = new HashMap<>();
RestUtils.decodeQueryString(rawQuery, 0, stringParams);
for (Map.Entry<String, String> entry : stringParams.entrySet()) {
params.put(entry.getKey(), TextTemplate.inline(entry.getValue()).build());
params.put(entry.getKey(), new TextTemplate(entry.getValue()));
}
}
} catch (URISyntaxException e) {

View File

@ -5,58 +5,58 @@
*/
package org.elasticsearch.xpack.common.text;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
/**
*
*/
public class TextTemplate implements ToXContent {
private final String template;
@Nullable private final XContentType contentType;
@Nullable private final ScriptType type;
@Nullable private final Map<String, Object> params;
private final Script script;
private final String value;
public TextTemplate(String template) {
this(template, null, null, null);
this.script = null;
this.value = template;
}
public TextTemplate(String template, @Nullable XContentType contentType, @Nullable ScriptType type,
public TextTemplate(String template, @Nullable XContentType contentType, ScriptType type,
@Nullable Map<String, Object> params) {
this.template = template;
this.contentType = contentType;
this.type = type;
this.params = params;
this.script = new Script(template, type, null, params, contentType);
this.value = null;
}
public TextTemplate(Script script) {
this.script = script;
this.value = null;
}
public Script getScript() {
return script;
}
public String getTemplate() {
return template;
return script != null ? script.getScript() : value;
}
public XContentType getContentType() {
return contentType;
return script != null ? script.getContentType() : null;
}
public ScriptType getType() {
return type != null ? type : ScriptType.INLINE;
return script != null ? script.getType(): ScriptType.INLINE;
}
public Map<String, Object> getParams() {
return params != null ? params : Collections.emptyMap();
return script != null ? script.getParams(): null;
}
@Override
@ -65,210 +65,31 @@ public class TextTemplate implements ToXContent {
if (o == null || getClass() != o.getClass()) return false;
TextTemplate template1 = (TextTemplate) o;
if (!template.equals(template1.template)) return false;
if (contentType != template1.contentType) return false;
if (type != template1.type) return false;
return !(params != null ? !params.equals(template1.params) : template1.params != null);
return Objects.equals(script, template1.script) &&
Objects.equals(value, template1.value);
}
@Override
public int hashCode() {
int result = template.hashCode();
result = 31 * result + (contentType != null ? contentType.hashCode() : 0);
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (params != null ? params.hashCode() : 0);
return result;
return Objects.hash(script, value);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (type == null) {
return builder.value(template);
if (script != null) {
script.toXContent(builder, params);
} else {
builder.value(value);
}
builder.startObject();
switch (type) {
case INLINE:
if (contentType != null && builder.contentType() == contentType) {
builder.rawField(Field.INLINE.getPreferredName(), new BytesArray(template));
} else {
builder.field(Field.INLINE.getPreferredName(), template);
}
break;
case FILE:
builder.field(Field.FILE.getPreferredName(), template);
break;
default: // STORED
assert type == ScriptType.STORED : "template type [" + type + "] is not supported";
builder.field(Field.ID.getPreferredName(), template);
}
if (this.params != null) {
builder.field(Field.PARAMS.getPreferredName(), this.params);
}
return builder.endObject();
return builder;
}
public static TextTemplate parse(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
if (token.isValue()) {
return new TextTemplate(String.valueOf(parser.objectText()));
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
return new TextTemplate(parser.text());
} else {
return new TextTemplate(Script.parse(parser, ParseFieldMatcher.STRICT, null));
}
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("expected a string value or an object, but found [{}] instead", token);
}
String template = null;
XContentType contentType = null;
ScriptType type = null;
Map<String, Object> params = null;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.INLINE)) {
type = ScriptType.INLINE;
if (token.isValue()) {
template = String.valueOf(parser.objectText());
} else {
contentType = parser.contentType();
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
template = builder.copyCurrentStructure(parser).bytes().utf8ToString();
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.FILE)) {
type = ScriptType.FILE;
if (token == XContentParser.Token.VALUE_STRING) {
template = parser.text();
} else {
throw new ElasticsearchParseException("expected a string value for field [{}], but found [{}]",
currentFieldName, token);
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.ID)) {
type = ScriptType.STORED;
if (token == XContentParser.Token.VALUE_STRING) {
template = parser.text();
} else {
throw new ElasticsearchParseException("expected a string value for field [{}], but found [{}]",
currentFieldName, token);
}
} else if (ParseFieldMatcher.STRICT.match(currentFieldName, Field.PARAMS)) {
if (token == XContentParser.Token.START_OBJECT) {
params = parser.map();
} else {
throw new ElasticsearchParseException("expected an object for field [{}], but found [{}]", currentFieldName, token);
}
} else {
throw new ElasticsearchParseException("unexpected field [{}]", currentFieldName);
}
}
if (template == null) {
throw new ElasticsearchParseException("expected one of [{}], [{}] or [{}] fields, but found none",
Field.INLINE.getPreferredName(), Field.FILE.getPreferredName(), Field.ID.getPreferredName());
}
assert type != null : "if template is not null, type should definitely not be null";
return new TextTemplate(template, contentType, type, params);
}
public static Builder inline(XContentBuilder template) {
return new Builder.Inline(template.bytes().utf8ToString()).contentType(template.contentType());
}
public static Builder<Builder.Inline> inline(String text) {
return new Builder.Inline(text);
}
public static Builder<Builder.File> file(String file) {
return new Builder.File(file);
}
public static Builder<Builder.Indexed> indexed(String id) {
return new Builder.Indexed(id);
}
public static Builder.DefaultType defaultType(String text) {
return new Builder.DefaultType(text);
}
public abstract static class Builder<B extends Builder> {
protected final ScriptType type;
protected final String template;
protected Map<String, Object> params;
protected Builder(String template, ScriptType type) {
this.template = template;
this.type = type;
}
public B params(Map<String, Object> params) {
this.params = params;
return (B) this;
}
public abstract TextTemplate build();
public static class Inline extends Builder<Inline> {
private XContentType contentType;
public Inline(String script) {
super(script, ScriptType.INLINE);
}
public Inline contentType(XContentType contentType) {
this.contentType = contentType;
return this;
}
@Override
public TextTemplate build() {
return new TextTemplate(template, contentType, type, params);
}
}
public static class File extends Builder<File> {
public File(String file) {
super(file, ScriptType.FILE);
}
@Override
public TextTemplate build() {
return new TextTemplate(template, null, type, params);
}
}
public static class Indexed extends Builder<Indexed> {
public Indexed(String id) {
super(id, ScriptType.STORED);
}
@Override
public TextTemplate build() {
return new TextTemplate(template, null, type, params);
}
}
public static class DefaultType extends Builder<DefaultType> {
public DefaultType(String text) {
super(text, null);
}
@Override
public TextTemplate build() {
return new TextTemplate(template, null, type, params);
}
}
}
public interface Field {
ParseField INLINE = new ParseField("inline");
ParseField FILE = new ParseField("file");
ParseField ID = new ParseField("id");
ParseField PARAMS = new ParseField("params");
}
}

View File

@ -28,18 +28,24 @@ public class TextTemplateEngine extends AbstractComponent {
this.service = service;
}
// TODO: move over to use o.e.script.Script instead
public String render(TextTemplate template, Map<String, Object> model) {
if (template == null) {
public String render(TextTemplate textTemplate, Map<String, Object> model) {
if (textTemplate == null) {
return null;
}
String template = textTemplate.getTemplate();
XContentType contentType = detectContentType(template);
Map<String, String> compileParams = compileParams(contentType);
template = trimContentType(template);
template = trimContentType(textTemplate);
CompiledScript compiledScript = service.compile(convert(template, model), Watcher.SCRIPT_CONTEXT,
compileParams);
Map<String, Object> mergedModel = new HashMap<>();
if (textTemplate.getParams() != null) {
mergedModel.putAll(textTemplate.getParams());
}
mergedModel.putAll(model);
Script script = new Script(template, textTemplate.getType(), "mustache", mergedModel, textTemplate.getContentType());
CompiledScript compiledScript = service.compile(script, Watcher.SCRIPT_CONTEXT, compileParams);
ExecutableScript executable = service.executable(compiledScript, model);
Object result = executable.run();
if (result instanceof BytesReference) {
@ -48,10 +54,10 @@ public class TextTemplateEngine extends AbstractComponent {
return result.toString();
}
private TextTemplate trimContentType(TextTemplate textTemplate) {
private String trimContentType(TextTemplate textTemplate) {
String template = textTemplate.getTemplate();
if (!template.startsWith("__")){
return textTemplate; //Doesn't even start with __ so can't have a content type
return template; //Doesn't even start with __ so can't have a content type
}
// There must be a __<content_type__:: prefix so the minimum length before detecting '__::' is 3
int index = template.indexOf("__::", 3);
@ -64,28 +70,20 @@ public class TextTemplateEngine extends AbstractComponent {
template = template.substring(index + 4);
}
}
return new TextTemplate(template, textTemplate.getContentType(), textTemplate.getType(), textTemplate.getParams());
return template;
}
private XContentType detectContentType(TextTemplate textTemplate) {
String template = textTemplate.getTemplate();
if (template.startsWith("__")) {
private XContentType detectContentType(String content) {
if (content.startsWith("__")) {
//There must be a __<content_type__:: prefix so the minimum length before detecting '__::' is 3
int endOfContentName = template.indexOf("__::", 3);
int endOfContentName = content.indexOf("__::", 3);
if (endOfContentName != -1) {
return XContentType.fromMediaTypeOrFormat(template.substring(2, endOfContentName));
return XContentType.fromMediaTypeOrFormat(content.substring(2, endOfContentName));
}
}
return null;
}
private Script convert(TextTemplate textTemplate, Map<String, Object> model) {
Map<String, Object> mergedModel = new HashMap<>();
mergedModel.putAll(textTemplate.getParams());
mergedModel.putAll(model);
return new Script(textTemplate.getTemplate(), textTemplate.getType(), "mustache", mergedModel, textTemplate.getContentType());
}
private Map<String, String> compileParams(XContentType contentType) {
if (contentType == XContentType.JSON) {
return Collections.singletonMap("content_type", "application/json");

View File

@ -21,9 +21,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
*
*/
public class EmailTemplate implements ToXContent {
final TextTemplate from;
@ -238,11 +235,7 @@ public class EmailTemplate implements ToXContent {
}
public Builder from(String from) {
return from(TextTemplate.inline(from));
}
public Builder from(TextTemplate.Builder from) {
return from(from.build());
return from(new TextTemplate(from));
}
public Builder from(TextTemplate from) {
@ -253,15 +246,7 @@ public class EmailTemplate implements ToXContent {
public Builder replyTo(String... replyTo) {
TextTemplate[] templates = new TextTemplate[replyTo.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = TextTemplate.defaultType(replyTo[i]).build();
}
return replyTo(templates);
}
public Builder replyTo(TextTemplate.Builder... replyTo) {
TextTemplate[] templates = new TextTemplate[replyTo.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = replyTo[i].build();
templates[i] = new TextTemplate(replyTo[i]);
}
return replyTo(templates);
}
@ -272,11 +257,7 @@ public class EmailTemplate implements ToXContent {
}
public Builder priority(Email.Priority priority) {
return priority(TextTemplate.inline(priority.name()));
}
public Builder priority(TextTemplate.Builder priority) {
return priority(priority.build());
return priority(new TextTemplate(priority.name()));
}
public Builder priority(TextTemplate priority) {
@ -287,15 +268,7 @@ public class EmailTemplate implements ToXContent {
public Builder to(String... to) {
TextTemplate[] templates = new TextTemplate[to.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = TextTemplate.defaultType(to[i]).build();
}
return to(templates);
}
public Builder to(TextTemplate.Builder... to) {
TextTemplate[] templates = new TextTemplate[to.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = to[i].build();
templates[i] = new TextTemplate(to[i]);
}
return to(templates);
}
@ -308,15 +281,7 @@ public class EmailTemplate implements ToXContent {
public Builder cc(String... cc) {
TextTemplate[] templates = new TextTemplate[cc.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = TextTemplate.defaultType(cc[i]).build();
}
return cc(templates);
}
public Builder cc(TextTemplate.Builder... cc) {
TextTemplate[] templates = new TextTemplate[cc.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = cc[i].build();
templates[i] = new TextTemplate(cc[i]);
}
return cc(templates);
}
@ -329,15 +294,7 @@ public class EmailTemplate implements ToXContent {
public Builder bcc(String... bcc) {
TextTemplate[] templates = new TextTemplate[bcc.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = TextTemplate.defaultType(bcc[i]).build();
}
return bcc(templates);
}
public Builder bcc(TextTemplate.Builder... bcc) {
TextTemplate[] templates = new TextTemplate[bcc.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = bcc[i].build();
templates[i] = new TextTemplate(bcc[i]);
}
return bcc(templates);
}
@ -348,11 +305,7 @@ public class EmailTemplate implements ToXContent {
}
public Builder subject(String subject) {
return subject(TextTemplate.defaultType(subject));
}
public Builder subject(TextTemplate.Builder subject) {
return subject(subject.build());
return subject(new TextTemplate(subject));
}
public Builder subject(TextTemplate subject) {
@ -361,11 +314,7 @@ public class EmailTemplate implements ToXContent {
}
public Builder textBody(String text) {
return textBody(TextTemplate.defaultType(text));
}
public Builder textBody(TextTemplate.Builder text) {
return textBody(text.build());
return textBody(new TextTemplate(text));
}
public Builder textBody(TextTemplate text) {
@ -374,11 +323,7 @@ public class EmailTemplate implements ToXContent {
}
public Builder htmlBody(String html) {
return htmlBody(TextTemplate.defaultType(html));
}
public Builder htmlBody(TextTemplate.Builder html) {
return htmlBody(html.build());
return htmlBody(new TextTemplate(html));
}
public Builder htmlBody(TextTemplate html) {

View File

@ -23,9 +23,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
*
*/
public class HipChatMessage implements ToXContent {
final String body;
@ -405,7 +402,7 @@ public class HipChatMessage implements ToXContent {
public enum Color implements ToXContent {
YELLOW, GREEN, RED, PURPLE, GRAY, RANDOM;
private final TextTemplate template = TextTemplate.inline(name()).build();
private final TextTemplate template = new TextTemplate(name());
public TextTemplate asTemplate() {
return template;
@ -453,7 +450,7 @@ public class HipChatMessage implements ToXContent {
TEXT,
HTML;
private final TextTemplate template = TextTemplate.inline(name()).build();
private final TextTemplate template = new TextTemplate(name());
public TextTemplate asTemplate() {
return template;

View File

@ -155,7 +155,7 @@ public class IncidentEvent implements ToXContent {
return builder.endObject();
}
public static Template.Builder templateBuilder(String description) {
return templateBuilder(TextTemplate.inline(description).build());
return templateBuilder(new TextTemplate(description));
}
public static Template.Builder templateBuilder(TextTemplate description) {

View File

@ -21,9 +21,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
/**
*
*/
public class IncidentEventContext implements ToXContent {
enum Type {

View File

@ -20,9 +20,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
*
*/
public class Attachment implements MessageElement {
final String fallback;
@ -460,12 +457,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setFallback(TextTemplate.Builder fallback) {
return setFallback(fallback.build());
}
public Builder setFallback(String fallback) {
return setFallback(TextTemplate.indexed(fallback));
return setFallback(new TextTemplate(fallback));
}
public Builder setColor(TextTemplate color) {
@ -473,12 +466,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setColor(TextTemplate.Builder color) {
return setColor(color.build());
}
public Builder setColor(String color) {
return setColor(TextTemplate.inline(color));
return setColor(new TextTemplate(color));
}
public Builder setPretext(TextTemplate pretext) {
@ -486,12 +475,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setPretext(TextTemplate.Builder pretext) {
return setPretext(pretext.build());
}
public Builder setPretext(String pretext) {
return setPretext(TextTemplate.inline(pretext));
return setPretext(new TextTemplate(pretext));
}
public Builder setAuthorName(TextTemplate authorName) {
@ -499,12 +484,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setAuthorName(TextTemplate.Builder authorName) {
return setAuthorName(authorName.build());
}
public Builder setAuthorName(String authorName) {
return setAuthorName(TextTemplate.inline(authorName));
return setAuthorName(new TextTemplate(authorName));
}
public Builder setAuthorLink(TextTemplate authorLink) {
@ -512,12 +493,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setAuthorLink(TextTemplate.Builder authorLink) {
return setAuthorLink(authorLink.build());
}
public Builder setAuthorLink(String authorLink) {
return setAuthorLink(TextTemplate.inline(authorLink));
return setAuthorLink(new TextTemplate(authorLink));
}
public Builder setAuthorIcon(TextTemplate authorIcon) {
@ -525,12 +502,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setAuthorIcon(TextTemplate.Builder authorIcon) {
return setAuthorIcon(authorIcon.build());
}
public Builder setAuthorIcon(String authorIcon) {
return setAuthorIcon(TextTemplate.inline(authorIcon));
return setAuthorIcon(new TextTemplate(authorIcon));
}
public Builder setTitle(TextTemplate title) {
@ -538,12 +511,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setTitle(TextTemplate.Builder title) {
return setTitle(title.build());
}
public Builder setTitle(String title) {
return setTitle(TextTemplate.inline(title));
return setTitle(new TextTemplate(title));
}
public Builder setTitleLink(TextTemplate titleLink) {
@ -551,12 +520,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setTitleLink(TextTemplate.Builder titleLink) {
return setTitleLink(titleLink.build());
}
public Builder setTitleLink(String titleLink) {
return setTitleLink(TextTemplate.inline(titleLink));
return setTitleLink(new TextTemplate(titleLink));
}
public Builder setText(TextTemplate text) {
@ -564,12 +529,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setText(TextTemplate.Builder text) {
return setText(text.build());
}
public Builder setText(String text) {
return setText(TextTemplate.inline(text));
return setText(new TextTemplate(text));
}
public Builder addField(TextTemplate title, TextTemplate value, boolean isShort) {
@ -577,12 +538,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder addField(TextTemplate.Builder title, TextTemplate.Builder value, boolean isShort) {
return addField(title.build(), value.build(), isShort);
}
public Builder addField(String title, String value, boolean isShort) {
return addField(TextTemplate.inline(title), TextTemplate.inline(value), isShort);
return addField(new TextTemplate(title), new TextTemplate(value), isShort);
}
public Builder setImageUrl(TextTemplate imageUrl) {
@ -590,12 +547,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setImageUrl(TextTemplate.Builder imageUrl) {
return setImageUrl(imageUrl.build());
}
public Builder setImageUrl(String imageUrl) {
return setImageUrl(TextTemplate.inline(imageUrl));
return setImageUrl(new TextTemplate(imageUrl));
}
public Builder setThumbUrl(TextTemplate thumbUrl) {
@ -603,12 +556,8 @@ public class Attachment implements MessageElement {
return this;
}
public Builder setThumbUrl(TextTemplate.Builder thumbUrl) {
return setThumbUrl(thumbUrl.build());
}
public Builder setThumbUrl(String thumbUrl) {
return setThumbUrl(TextTemplate.inline(thumbUrl));
return setThumbUrl(new TextTemplate(thumbUrl));
}
public Template build() {

View File

@ -17,9 +17,6 @@ import org.elasticsearch.xpack.common.text.TextTemplateEngine;
import java.io.IOException;
import java.util.Map;
/**
*
*/
class Field implements MessageElement {
final String title;

View File

@ -22,9 +22,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
*
*/
public class SlackMessage implements MessageElement {
final String from;
@ -350,12 +347,8 @@ public class SlackMessage implements MessageElement {
return this;
}
public Builder setFrom(TextTemplate.Builder from) {
return setFrom(from.build());
}
public Builder setFrom(String from) {
return setFrom(TextTemplate.inline(from));
return setFrom(new TextTemplate(from));
}
public Builder addTo(TextTemplate... to) {
@ -363,16 +356,9 @@ public class SlackMessage implements MessageElement {
return this;
}
public Builder addTo(TextTemplate.Builder... to) {
for (TextTemplate.Builder name : to) {
this.to.add(name.build());
}
return this;
}
public Builder addTo(String... to) {
for (String name : to) {
this.to.add(TextTemplate.inline(name).build());
this.to.add(new TextTemplate(name));
}
return this;
}
@ -382,12 +368,8 @@ public class SlackMessage implements MessageElement {
return this;
}
public Builder setText(TextTemplate.Builder text) {
return setText(text.build());
}
public Builder setText(String text) {
return setText(TextTemplate.inline(text));
return setText(new TextTemplate(text));
}
public Builder setIcon(TextTemplate icon) {
@ -395,12 +377,8 @@ public class SlackMessage implements MessageElement {
return this;
}
public Builder setIcon(TextTemplate.Builder icon) {
return setIcon(icon.build());
}
public Builder setIcon(String icon) {
return setIcon(TextTemplate.inline(icon));
return setIcon(new TextTemplate(icon));
}
public Builder addAttachments(Attachment.Template... attachments) {

View File

@ -61,10 +61,10 @@ public class HttpRequestTemplateTests extends ESTestCase {
public void testRender() {
HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234)
.body(TextTemplate.inline("_body"))
.path(TextTemplate.inline("_path"))
.putParam("_key1", TextTemplate.inline("_value1"))
.putHeader("_key2", TextTemplate.inline("_value2"))
.body(new TextTemplate("_body"))
.path(new TextTemplate("_path"))
.putParam("_key1", new TextTemplate("_value1"))
.putHeader("_key2", new TextTemplate("_value2"))
.build();
HttpRequest result = template.render(new MockTextTemplateEngine(), Collections.emptyMap());
@ -114,10 +114,10 @@ public class HttpRequestTemplateTests extends ESTestCase {
builder.auth(new BasicAuth("_username", "_password".toCharArray()));
}
if (randomBoolean()) {
builder.putParam("_key", TextTemplate.inline("_value"));
builder.putParam("_key", new TextTemplate("_value"));
}
if (randomBoolean()) {
builder.putHeader("_key", TextTemplate.inline("_value"));
builder.putHeader("_key", new TextTemplate("_value"));
}
long connectionTimeout = randomBoolean() ? 0 : randomIntBetween(5, 100000);
if (connectionTimeout > 0) {
@ -149,7 +149,7 @@ public class HttpRequestTemplateTests extends ESTestCase {
public void testParsingFromUrl() throws Exception {
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("www.example.org", 1234);
builder.path("/foo/bar/org");
builder.putParam("param", TextTemplate.inline("test"));
builder.putParam("param", new TextTemplate("test"));
builder.scheme(Scheme.HTTPS);
assertThatManualBuilderEqualsParsingFromUrl("https://www.example.org:1234/foo/bar/org?param=test", builder);
@ -162,7 +162,7 @@ public class HttpRequestTemplateTests extends ESTestCase {
assertThatManualBuilderEqualsParsingFromUrl("http://www.example.org", builder);
// encoded values
builder = HttpRequestTemplate.builder("www.example.org", 80).putParam("foo", TextTemplate.inline(" white space"));
builder = HttpRequestTemplate.builder("www.example.org", 80).putParam("foo", new TextTemplate(" white space"));
assertThatManualBuilderEqualsParsingFromUrl("http://www.example.org?foo=%20white%20space", builder);
}

View File

@ -27,7 +27,6 @@ import java.util.Map;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.support.Exceptions.illegalArgument;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@ -64,7 +63,7 @@ public class TextTemplateTests extends ESTestCase {
when(service.executable(compiledScript, model)).thenReturn(script);
when(script.run()).thenReturn("rendered_text");
TextTemplate template = templateBuilder(type, templateText).params(params).build();
TextTemplate template = templateBuilder(type, templateText, params);
assertThat(engine.render(template, model), is("rendered_text"));
}
@ -80,7 +79,7 @@ public class TextTemplateTests extends ESTestCase {
when(service.executable(compiledScript, model)).thenReturn(script);
when(script.run()).thenReturn("rendered_text");
TextTemplate template = templateBuilder(scriptType, templateText).params(params).build();
TextTemplate template = templateBuilder(scriptType, templateText, params);
assertThat(engine.render(template, model), is("rendered_text"));
}
@ -100,7 +99,7 @@ public class TextTemplateTests extends ESTestCase {
public void testParser() throws Exception {
ScriptType type = randomScriptType();
TextTemplate template = templateBuilder(type, "_template").params(singletonMap("param_key", "param_val")).build();
TextTemplate template = templateBuilder(type, "_template", singletonMap("param_key", "param_val"));
XContentBuilder builder = jsonBuilder().startObject();
switch (type) {
case INLINE:
@ -123,7 +122,7 @@ public class TextTemplateTests extends ESTestCase {
}
public void testParserParserSelfGenerated() throws Exception {
TextTemplate template = templateBuilder(randomScriptType(), "_template").params(singletonMap("param_key", "param_val")).build();
TextTemplate template = templateBuilder(randomScriptType(), "_template", singletonMap("param_key", "param_val"));
XContentBuilder builder = jsonBuilder().value(template);
BytesReference bytes = builder.bytes();
@ -186,14 +185,8 @@ public class TextTemplateTests extends ESTestCase {
assertThat(engine.render(null ,new HashMap<>()), is(nullValue()));
}
private TextTemplate.Builder templateBuilder(ScriptType type, String text) {
switch (type) {
case INLINE: return TextTemplate.inline(text);
case FILE: return TextTemplate.file(text);
case STORED: return TextTemplate.indexed(text);
default:
throw illegalArgument("unsupported script type [{}]", type);
}
private TextTemplate templateBuilder(ScriptType type, String text, Map<String, Object> params) {
return new TextTemplate(text, null, type, params);
}
private static ScriptType randomScriptType() {

View File

@ -24,29 +24,27 @@ import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*/
public class EmailTemplateTests extends ESTestCase {
public void testEmailTemplateParserSelfGenerated() throws Exception {
TextTemplate from = randomFrom(TextTemplate.inline("from@from.com").build(), null);
TextTemplate from = randomFrom(new TextTemplate("from@from.com"), null);
List<TextTemplate> addresses = new ArrayList<>();
for( int i = 0; i < randomIntBetween(1, 5); ++i){
addresses.add(TextTemplate.inline("address" + i + "@test.com").build());
addresses.add(new TextTemplate("address" + i + "@test.com"));
}
TextTemplate[] possibleList = addresses.toArray(new TextTemplate[addresses.size()]);
TextTemplate[] replyTo = randomFrom(possibleList, null);
TextTemplate[] to = randomFrom(possibleList, null);
TextTemplate[] cc = randomFrom(possibleList, null);
TextTemplate[] bcc = randomFrom(possibleList, null);
TextTemplate priority = TextTemplate.inline(randomFrom(Email.Priority.values()).name()).build();
TextTemplate priority = new TextTemplate(randomFrom(Email.Priority.values()).name());
TextTemplate subjectTemplate = TextTemplate.inline("Templated Subject {{foo}}").build();
TextTemplate subjectTemplate = new TextTemplate("Templated Subject {{foo}}");
String subject = "Templated Subject bar";
TextTemplate textBodyTemplate = TextTemplate.inline("Templated Body {{foo}}").build();
TextTemplate textBodyTemplate = new TextTemplate("Templated Body {{foo}}");
String textBody = "Templated Body bar";
TextTemplate htmlBodyTemplate = TextTemplate.inline("Templated Html Body <script>nefarious scripting</script>").build();
TextTemplate htmlBodyTemplate = new TextTemplate("Templated Html Body <script>nefarious scripting</script>");
String htmlBody = "Templated Html Body <script>nefarious scripting</script>";
String sanitizedHtmlBody = "Templated Html Body";

View File

@ -25,9 +25,6 @@ import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
*
*/
public class HipChatMessageTests extends ESTestCase {
public void testToXContent() throws Exception {
String message = randomAsciiOfLength(10);
@ -163,14 +160,14 @@ public class HipChatMessageTests extends ESTestCase {
XContentBuilder jsonBuilder = jsonBuilder();
jsonBuilder.startObject();
TextTemplate body = TextTemplate.inline(randomAsciiOfLength(200)).build();
TextTemplate body = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("body", body, ToXContent.EMPTY_PARAMS);
TextTemplate[] rooms = null;
if (randomBoolean()) {
jsonBuilder.startArray("room");
rooms = new TextTemplate[randomIntBetween(1, 3)];
for (int i = 0; i < rooms.length; i++) {
rooms[i] = TextTemplate.inline(randomAsciiOfLength(10)).build();
rooms[i] = new TextTemplate(randomAsciiOfLength(10));
rooms[i].toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
}
jsonBuilder.endArray();
@ -180,7 +177,7 @@ public class HipChatMessageTests extends ESTestCase {
jsonBuilder.startArray("user");
users = new TextTemplate[randomIntBetween(1, 3)];
for (int i = 0; i < users.length; i++) {
users[i] = TextTemplate.inline(randomAsciiOfLength(10)).build();
users[i] = new TextTemplate(randomAsciiOfLength(10));
users[i].toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
}
jsonBuilder.endArray();
@ -192,7 +189,7 @@ public class HipChatMessageTests extends ESTestCase {
}
TextTemplate color = null;
if (randomBoolean()) {
color = TextTemplate.inline(randomAsciiOfLength(10)).build();
color = new TextTemplate(randomAsciiOfLength(10));
jsonBuilder.field("color", color, ToXContent.EMPTY_PARAMS);
}
HipChatMessage.Format format = null;
@ -231,26 +228,26 @@ public class HipChatMessageTests extends ESTestCase {
}
public void testTemplateParseSelfGenerated() throws Exception {
TextTemplate body = TextTemplate.inline(randomAsciiOfLength(10)).build();
TextTemplate body = new TextTemplate(randomAsciiOfLength(10));
HipChatMessage.Template.Builder templateBuilder = new HipChatMessage.Template.Builder(body);
if (randomBoolean()) {
int count = randomIntBetween(1, 3);
for (int i = 0; i < count; i++) {
templateBuilder.addRooms(TextTemplate.inline(randomAsciiOfLength(10)).build());
templateBuilder.addRooms(new TextTemplate(randomAsciiOfLength(10)));
}
}
if (randomBoolean()) {
int count = randomIntBetween(1, 3);
for (int i = 0; i < count; i++) {
templateBuilder.addUsers(TextTemplate.inline(randomAsciiOfLength(10)).build());
templateBuilder.addUsers(new TextTemplate(randomAsciiOfLength(10)));
}
}
if (randomBoolean()) {
templateBuilder.setFrom(randomAsciiOfLength(10));
}
if (randomBoolean()) {
templateBuilder.setColor(TextTemplate.inline(randomAsciiOfLength(5)).build());
templateBuilder.setColor(new TextTemplate(randomAsciiOfLength(5)));
}
if (randomBoolean()) {
templateBuilder.setFormat(randomFrom(HipChatMessage.Format.values()));

View File

@ -32,9 +32,6 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
*
*/
public class UserAccountTests extends ESTestCase {
public void testSettings() throws Exception {
@ -258,8 +255,8 @@ public class UserAccountTests extends ESTestCase {
.build();
UserAccount userAccount = createUserAccount(settings);
TextTemplate body = TextTemplate.inline("body").build();
TextTemplate[] rooms = new TextTemplate[] { TextTemplate.inline("room").build() };
TextTemplate body = new TextTemplate("body");
TextTemplate[] rooms = new TextTemplate[] { new TextTemplate("room")};
HipChatMessage.Template template = new HipChatMessage.Template(body, rooms, null, "sender", HipChatMessage.Format.TEXT, null, true);
HipChatMessage message = userAccount.render("watchId", "actionId", new MockTextTemplateEngine(), template, new HashMap<>());
@ -273,10 +270,10 @@ public class UserAccountTests extends ESTestCase {
.build();
UserAccount userAccount = createUserAccount(settings);
TextTemplate body = TextTemplate.inline("body").build();
TextTemplate[] rooms = new TextTemplate[] { TextTemplate.inline("room").build() };
TextTemplate body = new TextTemplate("body");
TextTemplate[] rooms = new TextTemplate[] { new TextTemplate("room") };
HipChatMessage.Template template = new HipChatMessage.Template(body, rooms, null, "sender", null,
TextTemplate.inline("yellow").build(), true);
new TextTemplate("yellow"), true);
HipChatMessage message = userAccount.render("watchId", "actionId", new MockTextTemplateEngine(), template, new HashMap<>());
assertThat(message.format, is(nullValue()));

View File

@ -27,9 +27,6 @@ import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
/**
*
*/
public class SlackMessageTests extends ESTestCase {
public void testToXContent() throws Exception {
String from = randomBoolean() ? null : randomAsciiOfLength(10);
@ -230,7 +227,7 @@ public class SlackMessageTests extends ESTestCase {
TextTemplate from = null;
if (randomBoolean()) {
from = TextTemplate.inline(randomAsciiOfLength(200)).build();
from = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("from", from, params);
}
TextTemplate[] to = null;
@ -238,19 +235,19 @@ public class SlackMessageTests extends ESTestCase {
jsonBuilder.startArray("to");
to = new TextTemplate[randomIntBetween(1, 3)];
for (int i = 0; i < to.length; i++) {
to[i] = TextTemplate.inline(randomAsciiOfLength(10)).build();
to[i] = new TextTemplate(randomAsciiOfLength(10));
to[i].toXContent(jsonBuilder, params);
}
jsonBuilder.endArray();
}
TextTemplate text = null;
if (randomBoolean()) {
text = TextTemplate.inline(randomAsciiOfLength(200)).build();
text = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("text", text, params);
}
TextTemplate icon = null;
if (randomBoolean()) {
icon = TextTemplate.inline(randomAsciiOfLength(10)).build();
icon = new TextTemplate(randomAsciiOfLength(10));
jsonBuilder.field("icon", icon);
}
Attachment.Template[] attachments = null;
@ -261,57 +258,57 @@ public class SlackMessageTests extends ESTestCase {
jsonBuilder.startObject();
TextTemplate fallback = null;
if (randomBoolean()) {
fallback = TextTemplate.inline(randomAsciiOfLength(200)).build();
fallback = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("fallback", fallback, params);
}
TextTemplate color = null;
if (randomBoolean()) {
color = TextTemplate.inline(randomAsciiOfLength(200)).build();
color = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("color", color, params);
}
TextTemplate pretext = null;
if (randomBoolean()) {
pretext = TextTemplate.inline(randomAsciiOfLength(200)).build();
pretext = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("pretext", pretext, params);
}
TextTemplate authorName = null;
TextTemplate authorLink = null;
TextTemplate authorIcon = null;
if (randomBoolean()) {
authorName = TextTemplate.inline(randomAsciiOfLength(200)).build();
authorName = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("author_name", authorName, params);
if (randomBoolean()) {
authorLink = TextTemplate.inline(randomAsciiOfLength(200)).build();
authorLink = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("author_link", authorLink, params);
}
if (randomBoolean()) {
authorIcon = TextTemplate.inline(randomAsciiOfLength(200)).build();
authorIcon = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("author_icon", authorIcon, params);
}
}
TextTemplate title = null;
TextTemplate titleLink = null;
if (randomBoolean()) {
title = TextTemplate.inline(randomAsciiOfLength(200)).build();
title = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("title", title, params);
if (randomBoolean()) {
titleLink = TextTemplate.inline(randomAsciiOfLength(200)).build();
titleLink = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("title_link", titleLink, params);
}
}
TextTemplate attachmentText = null;
if (randomBoolean()) {
attachmentText = TextTemplate.inline(randomAsciiOfLength(200)).build();
attachmentText = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("text", attachmentText, params);
}
TextTemplate imageUrl = null;
if (randomBoolean()) {
imageUrl = TextTemplate.inline(randomAsciiOfLength(200)).build();
imageUrl = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("image_url", imageUrl, params);
}
TextTemplate thumbUrl = null;
if (randomBoolean()) {
thumbUrl = TextTemplate.inline(randomAsciiOfLength(200)).build();
thumbUrl = new TextTemplate(randomAsciiOfLength(200));
jsonBuilder.field("thumb_url", thumbUrl, params);
}
Field.Template[] fields = null;
@ -320,9 +317,9 @@ public class SlackMessageTests extends ESTestCase {
fields = new Field.Template[randomIntBetween(1,3)];
for (int j = 0; j < fields.length; j++) {
jsonBuilder.startObject();
TextTemplate fieldTitle = TextTemplate.inline(randomAsciiOfLength(50)).build();
TextTemplate fieldTitle = new TextTemplate(randomAsciiOfLength(50));
jsonBuilder.field("title", fieldTitle, params);
TextTemplate fieldValue = TextTemplate.inline(randomAsciiOfLength(50)).build();
TextTemplate fieldValue = new TextTemplate(randomAsciiOfLength(50));
jsonBuilder.field("value", fieldValue, params);
boolean isShort = randomBoolean();
jsonBuilder.field("short", isShort);

View File

@ -18,9 +18,6 @@ import org.elasticsearch.xpack.watcher.actions.webhook.WebhookAction;
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
import org.elasticsearch.xpack.common.text.TextTemplate;
/**
*
*/
public final class ActionBuilders {
private ActionBuilders() {
@ -47,11 +44,7 @@ public final class ActionBuilders {
}
public static LoggingAction.Builder loggingAction(String text) {
return loggingAction(TextTemplate.inline(text));
}
public static LoggingAction.Builder loggingAction(TextTemplate.Builder text) {
return loggingAction(text.build());
return loggingAction(new TextTemplate(text));
}
public static LoggingAction.Builder loggingAction(TextTemplate text) {
@ -59,20 +52,13 @@ public final class ActionBuilders {
}
public static HipChatAction.Builder hipchatAction(String message) {
return hipchatAction(TextTemplate.inline(message));
return hipchatAction(new TextTemplate(message));
}
public static HipChatAction.Builder hipchatAction(String account, String body) {
return hipchatAction(account, TextTemplate.inline(body));
return hipchatAction(account, new TextTemplate(body));
}
public static HipChatAction.Builder hipchatAction(TextTemplate.Builder body) {
return hipchatAction(body.build());
}
public static HipChatAction.Builder hipchatAction(String account, TextTemplate.Builder body) {
return hipchatAction(account, body.build());
}
public static HipChatAction.Builder hipchatAction(TextTemplate body) {
return hipchatAction(null, body);

View File

@ -19,9 +19,6 @@ import org.elasticsearch.xpack.common.text.TextTemplate;
import java.io.IOException;
/**
*
*/
public class HipChatAction implements Action {
public static final String TYPE = "hipchat";
@ -182,18 +179,10 @@ public class HipChatAction implements Action {
return this;
}
public Builder addRooms(TextTemplate.Builder... rooms) {
TextTemplate[] templates = new TextTemplate[rooms.length];
for (int i = 0; i < rooms.length; i++) {
templates[i] = rooms[i].build();
}
return addRooms(templates);
}
public Builder addRooms(String... rooms) {
TextTemplate[] templates = new TextTemplate[rooms.length];
for (int i = 0; i < rooms.length; i++) {
templates[i] = TextTemplate.inline(rooms[i]).build();
templates[i] = new TextTemplate(rooms[i]);
}
return addRooms(templates);
}
@ -204,18 +193,10 @@ public class HipChatAction implements Action {
return this;
}
public Builder addUsers(TextTemplate.Builder... users) {
TextTemplate[] templates = new TextTemplate[users.length];
for (int i = 0; i < users.length; i++) {
templates[i] = users[i].build();
}
return addUsers(templates);
}
public Builder addUsers(String... users) {
TextTemplate[] templates = new TextTemplate[users.length];
for (int i = 0; i < users.length; i++) {
templates[i] = TextTemplate.inline(users[i]).build();
templates[i] = new TextTemplate(users[i]);
}
return addUsers(templates);
}
@ -235,10 +216,6 @@ public class HipChatAction implements Action {
return this;
}
public Builder setColor(TextTemplate.Builder color) {
return setColor(color.build());
}
public Builder setColor(HipChatMessage.Color color) {
return setColor(color.asTemplate());
}

View File

@ -17,9 +17,6 @@ import org.elasticsearch.xpack.common.text.TextTemplate;
import java.io.IOException;
import java.util.Locale;
/**
*
*/
public class LoggingAction implements Action {
public static final String TYPE = "logging";

View File

@ -76,9 +76,6 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*
*/
public class EmailActionTests extends ESTestCase {
private HttpAuthRegistry registry = new HttpAuthRegistry(singletonMap("basic", new BasicAuthFactory(null)));
@ -113,17 +110,17 @@ public class EmailActionTests extends ESTestCase {
EmailTemplate.Builder emailBuilder = EmailTemplate.builder();
TextTemplate subject = null;
if (randomBoolean()) {
subject = TextTemplate.inline("_subject").build();
subject = new TextTemplate("_subject");
emailBuilder.subject(subject);
}
TextTemplate textBody = null;
if (randomBoolean()) {
textBody = TextTemplate.inline("_text_body").build();
textBody = new TextTemplate("_text_body");
emailBuilder.textBody(textBody);
}
TextTemplate htmlBody = null;
if (randomBoolean()) {
htmlBody = TextTemplate.inline("_html_body").build();
htmlBody = new TextTemplate("_html_body");
emailBuilder.htmlBody(htmlBody);
}
EmailTemplate email = emailBuilder.build();
@ -204,9 +201,9 @@ public class EmailActionTests extends ESTestCase {
randomBoolean() ? "bcc@domain" : "bcc1@domain,bcc2@domain").toArray();
Email.Address[] replyTo = rarely() ? null : Email.AddressList.parse(
randomBoolean() ? "reply@domain" : "reply1@domain,reply2@domain").toArray();
TextTemplate subject = randomBoolean() ? TextTemplate.inline("_subject").build() : null;
TextTemplate textBody = randomBoolean() ? TextTemplate.inline("_text_body").build() : null;
TextTemplate htmlBody = randomBoolean() ? TextTemplate.inline("_text_html").build() : null;
TextTemplate subject = randomBoolean() ? new TextTemplate("_subject") : null;
TextTemplate textBody = randomBoolean() ? new TextTemplate("_text_body") : null;
TextTemplate htmlBody = randomBoolean() ? new TextTemplate("_text_html") : null;
DataAttachment dataAttachment = randomDataAttachment();
XContentBuilder builder = jsonBuilder().startObject()
.field("account", "_account")
@ -312,7 +309,7 @@ public class EmailActionTests extends ESTestCase {
assertThat(executable.action().getAuth(), notNullValue());
assertThat(executable.action().getAuth().user(), is("_user"));
assertThat(executable.action().getAuth().password(), is(new Secret("_passwd".toCharArray())));
assertThat(executable.action().getEmail().priority(), is(TextTemplate.defaultType(priority.name()).build()));
assertThat(executable.action().getEmail().priority(), is(new TextTemplate(priority.name())));
if (to != null) {
assertThat(executable.action().getEmail().to(), arrayContainingInAnyOrder(addressesToTemplates(to)));
} else {
@ -338,7 +335,7 @@ public class EmailActionTests extends ESTestCase {
private static TextTemplate[] addressesToTemplates(Email.Address[] addresses) {
TextTemplate[] templates = new TextTemplate[addresses.length];
for (int i = 0; i < templates.length; i++) {
templates[i] = TextTemplate.defaultType(addresses[i].toString()).build();
templates[i] = new TextTemplate(addresses[i].toString());
}
return templates;
}

View File

@ -29,9 +29,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
*
*/
public class HipChatActionFactoryTests extends ESTestCase {
private HipChatActionFactory factory;
private HipChatService hipchatService;
@ -79,20 +76,20 @@ public class HipChatActionFactoryTests extends ESTestCase {
builder.field("account", accountName);
builder.startObject("message");
TextTemplate body = TextTemplate.inline("_body").build();
TextTemplate body = new TextTemplate("_body");
builder.field("body", body);
TextTemplate[] rooms = null;
if (randomBoolean()) {
TextTemplate r1 = TextTemplate.inline("_r1").build();
TextTemplate r2 = TextTemplate.inline("_r2").build();
TextTemplate r1 = new TextTemplate("_r1");
TextTemplate r2 = new TextTemplate("_r2");
rooms = new TextTemplate[] { r1, r2 };
builder.array("room", r1, r2);
}
TextTemplate[] users = null;
if (randomBoolean()) {
TextTemplate u1 = TextTemplate.inline("_u1").build();
TextTemplate u2 = TextTemplate.inline("_u2").build();
TextTemplate u1 = new TextTemplate("_u1");
TextTemplate u2 = new TextTemplate("_u2");
users = new TextTemplate[] { u1, u2 };
builder.array("user", u1, u2);
}
@ -108,7 +105,7 @@ public class HipChatActionFactoryTests extends ESTestCase {
}
TextTemplate color = null;
if (randomBoolean()) {
color = TextTemplate.inline(randomFrom(HipChatMessage.Color.values()).value()).build();
color = new TextTemplate(randomFrom(HipChatMessage.Color.values()).value());
builder.field("color", color);
}
Boolean notify = null;
@ -135,7 +132,7 @@ public class HipChatActionFactoryTests extends ESTestCase {
public void testParserSelfGenerated() throws Exception {
String accountName = randomAsciiOfLength(10);
TextTemplate body = TextTemplate.inline("_body").build();
TextTemplate body = new TextTemplate("_body");
HipChatMessage.Template.Builder templateBuilder = new HipChatMessage.Template.Builder(body);
XContentBuilder builder = jsonBuilder().startObject();
@ -144,14 +141,14 @@ public class HipChatActionFactoryTests extends ESTestCase {
builder.field("body", body);
if (randomBoolean()) {
TextTemplate r1 = TextTemplate.inline("_r1").build();
TextTemplate r2 = TextTemplate.inline("_r2").build();
TextTemplate r1 = new TextTemplate("_r1");
TextTemplate r2 = new TextTemplate("_r2");
templateBuilder.addRooms(r1, r2);
builder.array("room", r1, r2);
}
if (randomBoolean()) {
TextTemplate u1 = TextTemplate.inline("_u1").build();
TextTemplate u2 = TextTemplate.inline("_u2").build();
TextTemplate u1 = new TextTemplate("_u1");
TextTemplate u2 = new TextTemplate("_u2");
templateBuilder.addUsers(u1, u2);
builder.array("user", u1, u2);
}
@ -166,7 +163,7 @@ public class HipChatActionFactoryTests extends ESTestCase {
builder.field("format", format.value());
}
if (randomBoolean()) {
TextTemplate color = TextTemplate.inline(randomFrom(HipChatMessage.Color.values()).value()).build();
TextTemplate color = new TextTemplate(randomFrom(HipChatMessage.Color.values()).value());
templateBuilder.setColor(color);
builder.field("color", color);
}

View File

@ -45,9 +45,6 @@ import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*
*/
public class HipChatActionTests extends ESTestCase {
private HipChatService service;
@ -61,7 +58,7 @@ public class HipChatActionTests extends ESTestCase {
TextTemplateEngine templateEngine = mock(TextTemplateEngine.class);
TextTemplate body = TextTemplate.inline("_body").build();
TextTemplate body = new TextTemplate("_body");
HipChatMessage.Template.Builder messageBuilder = new HipChatMessage.Template.Builder(body);
HipChatMessage.Template messageTemplate = messageBuilder.build();
@ -129,20 +126,20 @@ public class HipChatActionTests extends ESTestCase {
builder.field("account", accountName);
builder.startObject("message");
TextTemplate body = TextTemplate.inline("_body").build();
TextTemplate body = new TextTemplate("_body");
builder.field("body", body);
TextTemplate[] rooms = null;
if (randomBoolean()) {
TextTemplate r1 = TextTemplate.inline("_r1").build();
TextTemplate r2 = TextTemplate.inline("_r2").build();
TextTemplate r1 = new TextTemplate("_r1");
TextTemplate r2 = new TextTemplate("_r2");
rooms = new TextTemplate[] { r1, r2 };
builder.array("room", r1, r2);
}
TextTemplate[] users = null;
if (randomBoolean()) {
TextTemplate u1 = TextTemplate.inline("_u1").build();
TextTemplate u2 = TextTemplate.inline("_u2").build();
TextTemplate u1 = new TextTemplate("_u1");
TextTemplate u2 = new TextTemplate("_u2");
users = new TextTemplate[] { u1, u2 };
builder.array("user", u1, u2);
}
@ -158,7 +155,7 @@ public class HipChatActionTests extends ESTestCase {
}
TextTemplate color = null;
if (randomBoolean()) {
color = TextTemplate.inline(randomFrom(HipChatMessage.Color.values()).value()).build();
color = new TextTemplate(randomFrom(HipChatMessage.Color.values()).value());
builder.field("color", color);
}
Boolean notify = null;
@ -185,7 +182,7 @@ public class HipChatActionTests extends ESTestCase {
public void testParserSelfGenerated() throws Exception {
String accountName = randomAsciiOfLength(10);
TextTemplate body = TextTemplate.inline("_body").build();
TextTemplate body = new TextTemplate("_body");
HipChatMessage.Template.Builder templateBuilder = new HipChatMessage.Template.Builder(body);
XContentBuilder builder = jsonBuilder().startObject();
@ -194,14 +191,14 @@ public class HipChatActionTests extends ESTestCase {
builder.field("body", body);
if (randomBoolean()) {
TextTemplate r1 = TextTemplate.inline("_r1").build();
TextTemplate r2 = TextTemplate.inline("_r2").build();
TextTemplate r1 = new TextTemplate("_r1");
TextTemplate r2 = new TextTemplate("_r2");
templateBuilder.addRooms(r1, r2);
builder.array("room", r1, r2);
}
if (randomBoolean()) {
TextTemplate u1 = TextTemplate.inline("_u1").build();
TextTemplate u2 = TextTemplate.inline("_u2").build();
TextTemplate u1 = new TextTemplate("_u1");
TextTemplate u2 = new TextTemplate("_u2");
templateBuilder.addUsers(u1, u2);
builder.array("user", u1, u2);
}
@ -216,7 +213,7 @@ public class HipChatActionTests extends ESTestCase {
builder.field("format", format.value());
}
if (randomBoolean()) {
TextTemplate color = TextTemplate.inline(randomFrom(HipChatMessage.Color.values()).value()).build();
TextTemplate color = new TextTemplate(randomFrom(HipChatMessage.Color.values()).value());
templateBuilder.setColor(color);
builder.field("color", color);
}

View File

@ -76,7 +76,7 @@ public class LoggingActionTests extends ESTestCase {
Map<String, Object> expectedModel = singletonMap("ctx", ctxModel);
String text = randomAsciiOfLength(10);
TextTemplate template = TextTemplate.inline(text).build();
TextTemplate template = new TextTemplate(text);
LoggingAction action = new LoggingAction(template, level, "_category");
ExecutableLoggingAction executable = new ExecutableLoggingAction(action, logger, actionLogger, engine);
when(engine.render(template, expectedModel)).thenReturn(text);
@ -97,7 +97,7 @@ public class LoggingActionTests extends ESTestCase {
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
String text = randomAsciiOfLength(10);
TextTemplate template = TextTemplate.inline(text).build();
TextTemplate template = new TextTemplate(text);
XContentBuilder builder = jsonBuilder().startObject();
builder.field("text", template);
@ -131,7 +131,7 @@ public class LoggingActionTests extends ESTestCase {
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
String text = randomAsciiOfLength(10);
TextTemplate template = TextTemplate.inline(text).build();
TextTemplate template = new TextTemplate(text);
String category = randomAsciiOfLength(10);
LoggingAction action = new LoggingAction(template, level, category);
ExecutableLoggingAction executable = new ExecutableLoggingAction(action, logger, settings, engine);
@ -151,7 +151,7 @@ public class LoggingActionTests extends ESTestCase {
LoggingActionFactory parser = new LoggingActionFactory(settings, engine);
String text = randomAsciiOfLength(10);
TextTemplate template = TextTemplate.inline(text).build();
TextTemplate template = new TextTemplate(text);
LoggingAction.Builder actionBuilder = loggingAction(template);
if (randomBoolean()) {
actionBuilder.setCategory(randomAsciiOfLength(10));

View File

@ -47,9 +47,6 @@ import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*
*/
public class PagerDutyActionTests extends ESTestCase {
private PagerDutyService service;
@ -64,7 +61,7 @@ public class PagerDutyActionTests extends ESTestCase {
TextTemplateEngine templateEngine = mock(TextTemplateEngine.class);
TextTemplate description = TextTemplate.inline("_description").build();
TextTemplate description = new TextTemplate("_description");
IncidentEvent.Template.Builder eventBuilder = new IncidentEvent.Template.Builder(description);
boolean attachPayload = randomBoolean();
eventBuilder.setAttachPayload(attachPayload);
@ -133,31 +130,31 @@ public class PagerDutyActionTests extends ESTestCase {
TextTemplate incidentKey = null;
if (randomBoolean()) {
incidentKey = TextTemplate.inline("_incident_key").build();
incidentKey = new TextTemplate("_incident_key");
builder.field("incident_key", incidentKey);
}
TextTemplate description = null;
if (randomBoolean()) {
description = TextTemplate.inline("_description").build();
description = new TextTemplate("_description");
builder.field("description", description);
}
TextTemplate client = null;
if (randomBoolean()) {
client = TextTemplate.inline("_client").build();
client = new TextTemplate("_client");
builder.field("client", client);
}
TextTemplate clientUrl = null;
if (randomBoolean()) {
clientUrl = TextTemplate.inline("_client_url").build();
clientUrl = new TextTemplate("_client_url");
builder.field("client_url", clientUrl);
}
TextTemplate eventType = null;
if (randomBoolean()) {
eventType = TextTemplate.inline(randomFrom("trigger", "resolve", "acknowledge")).build();
eventType = new TextTemplate(randomFrom("trigger", "resolve", "acknowledge"));
builder.field("event_type", eventType);
}
@ -169,9 +166,8 @@ public class PagerDutyActionTests extends ESTestCase {
IncidentEventContext.Template[] contexts = null;
if (randomBoolean()) {
contexts = new IncidentEventContext.Template[] {
IncidentEventContext.Template.link(TextTemplate.inline("_href").build(), TextTemplate.inline("_text").build()),
IncidentEventContext.Template.image(TextTemplate.inline("_src").build(), TextTemplate.inline("_href").build(),
TextTemplate.inline("_alt").build())
IncidentEventContext.Template.link(new TextTemplate("_href"), new TextTemplate("_text")),
IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"), new TextTemplate("_alt"))
};
builder.array("context", (Object) contexts);
}
@ -197,27 +193,26 @@ public class PagerDutyActionTests extends ESTestCase {
IncidentEvent.Template.Builder event = IncidentEvent.templateBuilder(randomAsciiOfLength(50));
if (randomBoolean()) {
event.setIncidentKey(TextTemplate.inline(randomAsciiOfLength(50)).build());
event.setIncidentKey(new TextTemplate(randomAsciiOfLength(50)));
}
if (randomBoolean()) {
event.setClient(TextTemplate.inline(randomAsciiOfLength(50)).build());
event.setClient(new TextTemplate(randomAsciiOfLength(50)));
}
if (randomBoolean()) {
event.setClientUrl(TextTemplate.inline(randomAsciiOfLength(50)).build());
event.setClientUrl(new TextTemplate(randomAsciiOfLength(50)));
}
if (randomBoolean()) {
event.setAttachPayload(randomBoolean());
}
if (randomBoolean()) {
event.addContext(IncidentEventContext.Template.link(TextTemplate.inline("_href").build(),
TextTemplate.inline("_text").build()));
event.addContext(IncidentEventContext.Template.link(new TextTemplate("_href"), new TextTemplate("_text")));
}
if (randomBoolean()) {
event.addContext(IncidentEventContext.Template.image(TextTemplate.inline("_src").build(),
TextTemplate.inline("_href").build(), TextTemplate.inline("_alt").build()));
event.addContext(IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"),
new TextTemplate("_alt")));
}
if (randomBoolean()) {
event.setEventType(TextTemplate.inline(randomAsciiOfLength(50)).build());
event.setEventType(new TextTemplate(randomAsciiOfLength(50)));
}
if (randomBoolean()) {
event.setAccount(randomAsciiOfLength(50)).build();

View File

@ -399,8 +399,7 @@ public class ActionThrottleTests extends AbstractWatcherIntegrationTestCase {
LOGGING {
@Override
public Action.Builder action() throws Exception {
TextTemplate.Builder templateBuilder = new TextTemplate.Builder.Inline("_logging");
return LoggingAction.builder(templateBuilder.build());
return LoggingAction.builder(new TextTemplate("_logging"));
}
@Override

View File

@ -67,9 +67,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
*/
public class WebhookActionTests extends ESTestCase {
static final String TEST_HOST = "test.com";
@ -87,8 +84,8 @@ public class WebhookActionTests extends ESTestCase {
@Before
public void init() throws Exception {
templateEngine = new MockTextTemplateEngine();
testBody = TextTemplate.inline(TEST_BODY_STRING).build();
testPath = TextTemplate.inline(TEST_PATH_STRING).build();
testBody = new TextTemplate(TEST_BODY_STRING);
testPath = new TextTemplate(TEST_PATH_STRING);
authRegistry = new HttpAuthRegistry(singletonMap("basic", new BasicAuthFactory(null)));
}
@ -128,8 +125,8 @@ public class WebhookActionTests extends ESTestCase {
}
public void testParser() throws Exception {
TextTemplate body = randomBoolean() ? TextTemplate.inline("_subject").build() : null;
TextTemplate path = TextTemplate.inline("_url").build();
TextTemplate body = randomBoolean() ? new TextTemplate("_subject") : null;
TextTemplate path = new TextTemplate("_url");
String host = "test.host";
HttpMethod method = randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, null);
HttpRequestTemplate request = getHttpRequestTemplate(method, host, TEST_PORT, path, body, null);
@ -148,8 +145,8 @@ public class WebhookActionTests extends ESTestCase {
}
public void testParserSelfGenerated() throws Exception {
TextTemplate body = randomBoolean() ? TextTemplate.inline("_body").build() : null;
TextTemplate path = TextTemplate.inline("_url").build();
TextTemplate body = randomBoolean() ? new TextTemplate("_body") : null;
TextTemplate path = new TextTemplate("_url");
String host = "test.host";
String watchId = "_watch";
String actionId = randomAsciiOfLength(5);
@ -174,8 +171,8 @@ public class WebhookActionTests extends ESTestCase {
}
public void testParserBuilder() throws Exception {
TextTemplate body = randomBoolean() ? TextTemplate.inline("_body").build() : null;
TextTemplate path = TextTemplate.inline("_url").build();
TextTemplate body = randomBoolean() ? new TextTemplate("_body") : null;
TextTemplate path = new TextTemplate("_url");
String host = "test.host";
String watchId = "_watch";
@ -257,9 +254,9 @@ public class WebhookActionTests extends ESTestCase {
String watchId = "test_url_encode" + randomAsciiOfLength(10);
HttpMethod method = HttpMethod.POST;
TextTemplate path = TextTemplate.defaultType("/test_" + watchId).build();
TextTemplate path = new TextTemplate("/test_" + watchId);
String host = "test.host";
TextTemplate testBody = TextTemplate.inline("ERROR HAPPENED").build();
TextTemplate testBody = new TextTemplate("ERROR HAPPENED");
HttpRequestTemplate requestTemplate = getHttpRequestTemplate(method, host, TEST_PORT, path, testBody, null);
WebhookAction action = new WebhookAction(requestTemplate);

View File

@ -84,8 +84,8 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
.scheme(Scheme.HTTPS)
.path(TextTemplate.inline("/test/_id").build())
.body(TextTemplate.inline("{key=value}").build());
.path(new TextTemplate("/test/_id"))
.body(new TextTemplate("{key=value}"));
watcherClient().preparePutWatch("_id")
.setSource(watchBuilder()
@ -127,8 +127,8 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
.scheme(Scheme.HTTPS)
.auth(new BasicAuth("_username", "_password".toCharArray()))
.path(TextTemplate.inline("/test/_id").build())
.body(TextTemplate.inline("{key=value}").build());
.path(new TextTemplate("/test/_id"))
.body(new TextTemplate("{key=value}"));
watcherClient().preparePutWatch("_id")
.setSource(watchBuilder()

View File

@ -39,8 +39,6 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
*/
public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase {
private int webPort;
private MockWebServer webServer;
@ -70,10 +68,10 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase
public void testWebhook() throws Exception {
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
.path(TextTemplate.inline("/test/_id"))
.putParam("param1", TextTemplate.inline("value1"))
.putParam("watch_id", TextTemplate.inline("_id"))
.body(TextTemplate.inline("_body"));
.path(new TextTemplate("/test/_id"))
.putParam("param1", new TextTemplate("value1"))
.putParam("watch_id", new TextTemplate("_id"))
.body(new TextTemplate("_body"));
watcherClient().preparePutWatch("_id")
.setSource(watchBuilder()
@ -115,10 +113,10 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTestCase
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webPort)
.auth(new BasicAuth("_username", "_password".toCharArray()))
.path(TextTemplate.inline("/test/_id").build())
.putParam("param1", TextTemplate.inline("value1").build())
.putParam("watch_id", TextTemplate.inline("_id").build())
.body(TextTemplate.inline("_body").build());
.path(new TextTemplate("/test/_id"))
.putParam("param1", new TextTemplate("value1"))
.putParam("watch_id", new TextTemplate("_id"))
.body(new TextTemplate("_body"));
watcherClient().preparePutWatch("_id")
.setSource(watchBuilder()

View File

@ -142,7 +142,7 @@ public class ChainInputTests extends ESTestCase {
HttpInput.Builder httpInputBuilder = httpInput(HttpRequestTemplate.builder("theHost", 1234)
.path("/index/_search")
.body(jsonBuilder().startObject().field("size", 1).endObject())
.body(jsonBuilder().startObject().field("size", 1).endObject().string())
.auth(new BasicAuth("test", "changeme".toCharArray())));
ChainInput.Builder chainedInputBuilder = chainInput()

View File

@ -61,7 +61,7 @@ public class ChainIntegrationTests extends AbstractWatcherIntegrationTestCase {
InetSocketAddress address = internalCluster().httpAddresses()[0];
HttpInput.Builder httpInputBuilder = httpInput(HttpRequestTemplate.builder(address.getHostString(), address.getPort())
.path("/" + index + "/_search")
.body(jsonBuilder().startObject().field("size", 1).endObject())
.body(jsonBuilder().startObject().field("size", 1).endObject().string())
.auth(securityEnabled() ? new BasicAuth("test", "changeme".toCharArray()) : null));
ChainInput.Builder chainedInputBuilder = chainInput()

View File

@ -71,7 +71,7 @@ public class HttpInputIntegrationTests extends AbstractWatcherIntegrationTestCas
.trigger(schedule(interval("5s")))
.input(httpInput(HttpRequestTemplate.builder(address.getHostString(), address.getPort())
.path("/index/_search")
.body(jsonBuilder().startObject().field("size", 1).endObject())
.body(jsonBuilder().startObject().field("size", 1).endObject().string())
.auth(securityEnabled() ? new BasicAuth("test", "changeme".toCharArray()) : null)))
.condition(compareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))
.addAction("_id", loggingAction("watch [{{ctx.watch_id}}] matched")))
@ -117,8 +117,8 @@ public class HttpInputIntegrationTests extends AbstractWatcherIntegrationTestCas
.field("query").value(termQuery("field", "value"))
.endObject();
HttpRequestTemplate.Builder requestBuilder = HttpRequestTemplate.builder(address.getHostString(), address.getPort())
.path(TextTemplate.inline("/idx/_search"))
.body(body);
.path(new TextTemplate("/idx/_search"))
.body(body.string());
if (securityEnabled()) {
requestBuilder.auth(new BasicAuth("test", "changeme".toCharArray()));
}

View File

@ -66,8 +66,6 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*/
public class HttpInputTests extends ESTestCase {
private HttpClient httpClient;
private HttpInputFactory httpParser;
@ -123,7 +121,7 @@ public class HttpInputTests extends ESTestCase {
ExecutableHttpInput input = new ExecutableHttpInput(httpInput, logger, httpClient, templateEngine);
when(httpClient.execute(any(HttpRequest.class))).thenReturn(response);
when(templateEngine.render(eq(TextTemplate.inline("_body").build()), any(Map.class))).thenReturn("_body");
when(templateEngine.render(eq(new TextTemplate("_body")), any(Map.class))).thenReturn("_body");
WatchExecutionContext ctx = createWatchExecutionContext();
HttpInput.Result result = input.execute(ctx, new Payload.Simple());
@ -142,7 +140,7 @@ public class HttpInputTests extends ESTestCase {
String notJson = "This is not json";
HttpResponse response = new HttpResponse(123, notJson.getBytes(StandardCharsets.UTF_8));
when(httpClient.execute(any(HttpRequest.class))).thenReturn(response);
when(templateEngine.render(eq(TextTemplate.inline("_body").build()), any(Map.class))).thenReturn("_body");
when(templateEngine.render(eq(new TextTemplate("_body")), any(Map.class))).thenReturn("_body");
WatchExecutionContext ctx = createWatchExecutionContext();
HttpInput.Result result = input.execute(ctx, new Payload.Simple());
@ -156,18 +154,18 @@ public class HttpInputTests extends ESTestCase {
String host = randomAsciiOfLength(3);
int port = randomIntBetween(8000, 9000);
String path = randomAsciiOfLength(3);
TextTemplate pathTemplate = TextTemplate.inline(path).build();
TextTemplate pathTemplate = new TextTemplate(path);
String body = randomBoolean() ? randomAsciiOfLength(3) : null;
Map<String, TextTemplate> params =
randomBoolean() ? new MapBuilder<String, TextTemplate>().put("a", TextTemplate.inline("b").build()).map() : null;
randomBoolean() ? new MapBuilder<String, TextTemplate>().put("a", new TextTemplate("b")).map() : null;
Map<String, TextTemplate> headers =
randomBoolean() ? new MapBuilder<String, TextTemplate>().put("c", TextTemplate.inline("d").build()).map() : null;
randomBoolean() ? new MapBuilder<String, TextTemplate>().put("c", new TextTemplate("d")).map() : null;
HttpAuth auth = randomBoolean() ? new BasicAuth("username", "password".toCharArray()) : null;
HttpRequestTemplate.Builder requestBuilder = HttpRequestTemplate.builder(host, port)
.scheme(scheme)
.method(httpMethod)
.path(pathTemplate)
.body(body != null ? TextTemplate.inline(body).build() : null)
.body(body != null ? new TextTemplate(body) : null)
.auth(auth);
if (params != null) {
@ -197,7 +195,7 @@ public class HttpInputTests extends ESTestCase {
assertThat(result.getRequest().method(), equalTo(httpMethod != null ? httpMethod : HttpMethod.GET)); // get is the default
assertThat(result.getRequest().host(), equalTo(host));
assertThat(result.getRequest().port(), equalTo(port));
assertThat(result.getRequest().path(), is(TextTemplate.inline(path).build()));
assertThat(result.getRequest().path(), is(new TextTemplate(path)));
assertThat(result.getExpectedResponseXContentType(), equalTo(expectedResponseXContentType));
if (expectedResponseXContentType != HttpContentType.TEXT && extractKeys != null) {
for (String key : extractKeys) {
@ -205,14 +203,14 @@ public class HttpInputTests extends ESTestCase {
}
}
if (params != null) {
assertThat(result.getRequest().params(), hasEntry(is("a"), is(TextTemplate.inline("b").build())));
assertThat(result.getRequest().params(), hasEntry(is("a"), is(new TextTemplate("b"))));
}
if (headers != null) {
assertThat(result.getRequest().headers(), hasEntry(is("c"), is(TextTemplate.inline("d").build())));
assertThat(result.getRequest().headers(), hasEntry(is("c"), is(new TextTemplate("d"))));
}
assertThat(result.getRequest().auth(), equalTo(auth));
if (body != null) {
assertThat(result.getRequest().body(), is(TextTemplate.inline(body).build()));
assertThat(result.getRequest().body(), is(new TextTemplate(body)));
} else {
assertThat(result.getRequest().body(), nullValue());
}
@ -256,7 +254,7 @@ public class HttpInputTests extends ESTestCase {
when(httpClient.execute(any(HttpRequest.class))).thenReturn(response);
when(templateEngine.render(eq(TextTemplate.inline("_body").build()), any(Map.class))).thenReturn("_body");
when(templateEngine.render(eq(new TextTemplate("_body")), any(Map.class))).thenReturn("_body");
WatchExecutionContext ctx = createWatchExecutionContext();
HttpInput.Result result = input.execute(ctx, new Payload.Simple());

View File

@ -17,11 +17,11 @@ public class MockTextTemplateEngine extends TextTemplateEngine {
}
@Override
public String render(TextTemplate template, Map<String, Object> model) {
if (template == null ) {
public String render(TextTemplate textTemplate, Map<String, Object> model) {
if (textTemplate == null ) {
return null;
}
return template.getTemplate();
return textTemplate.getTemplate();
}
}

View File

@ -27,7 +27,6 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod;
import org.elasticsearch.xpack.common.http.HttpRequestTemplate;
@ -191,9 +190,9 @@ public final class WatcherTestUtils {
HttpRequestTemplate.Builder httpRequest = HttpRequestTemplate.builder("localhost", 80);
httpRequest.method(HttpMethod.POST);
TextTemplate path = TextTemplate.inline("/foobarbaz/{{ctx.watch_id}}").build();
TextTemplate path = new TextTemplate("/foobarbaz/{{ctx.watch_id}}");
httpRequest.path(path);
TextTemplate body = TextTemplate.inline("{{ctx.watch_id}} executed with {{ctx.payload.response.hits.total_hits}} hits").build();
TextTemplate body = new TextTemplate("{{ctx.watch_id}} executed with {{ctx.payload.response.hits.total_hits}} hits");
httpRequest.body(body);
TextTemplateEngine engine = new MockTextTemplateEngine();

View File

@ -194,7 +194,7 @@ public class SearchTransformTests extends ESIntegTestCase {
builder.field("search_type", searchType.name());
}
if (templateName != null) {
TextTemplate template = TextTemplate.file(templateName).build();
TextTemplate template = new TextTemplate(templateName, null, ScriptService.ScriptType.FILE, null);
builder.field("template", template);
}

View File

@ -78,7 +78,7 @@ public class WatchMetadataTests extends AbstractWatcherIntegrationTestCase {
metadata.put("foo", "bar");
metadata.put("logtext", "This is a test");
LoggingAction.Builder loggingAction = loggingAction(TextTemplate.inline("_logging"))
LoggingAction.Builder loggingAction = loggingAction(new TextTemplate("_logging"))
.setLevel(LoggingLevel.DEBUG)
.setCategory("test");

View File

@ -440,7 +440,7 @@ public class WatchTests extends ESTestCase {
if (randomBoolean()) {
HttpRequestTemplate httpRequest = HttpRequestTemplate.builder("test.host", randomIntBetween(8000, 9000))
.method(randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT))
.path(TextTemplate.inline("_url").build())
.path(new TextTemplate("_url"))
.build();
WebhookAction action = new WebhookAction(httpRequest);
list.add(new ActionWrapper("_webhook_" + randomAsciiOfLength(8), randomThrottler(), randomCondition(), randomTransform(),