From 345f610bdf089098e4ffd9636fe6dc969e2b7dba Mon Sep 17 00:00:00 2001 From: uboness Date: Thu, 23 Apr 2015 15:15:46 +0200 Subject: [PATCH] Cleanup and Refactored Templates Mainly how templates read/write themselves from/to xcontent. Instead of using `text`, use `template` Original commit: elastic/x-pack-elasticsearch@0d6f3175397c00dc469847852d7649aaa719dba5 --- .../template/MustacheTemplateEngine.java | 2 +- .../watcher/support/template/Template.java | 60 +++++++++---------- .../xmustache/XMustacheTemplateEngine.java | 2 +- .../actions/email/EmailActionTests.java | 18 +++--- .../support/template/TemplateTests.java | 4 +- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/elasticsearch/watcher/support/template/MustacheTemplateEngine.java b/src/main/java/org/elasticsearch/watcher/support/template/MustacheTemplateEngine.java index 801faa019ee..5c44264d6e3 100644 --- a/src/main/java/org/elasticsearch/watcher/support/template/MustacheTemplateEngine.java +++ b/src/main/java/org/elasticsearch/watcher/support/template/MustacheTemplateEngine.java @@ -34,7 +34,7 @@ public class MustacheTemplateEngine extends AbstractComponent implements Templat Map mergedModel = new HashMap<>(); mergedModel.putAll(template.getParams()); mergedModel.putAll(model); - ExecutableScript executable = service.executable(MustacheScriptEngineService.NAME, template.getText(), template.getType(), mergedModel); + ExecutableScript executable = service.executable(MustacheScriptEngineService.NAME, template.getTemplate(), template.getType(), mergedModel); Object result = executable.run(); if (result instanceof BytesReference) { return ((BytesReference) result).toUtf8(); diff --git a/src/main/java/org/elasticsearch/watcher/support/template/Template.java b/src/main/java/org/elasticsearch/watcher/support/template/Template.java index 6fee2f0da98..06bd0ae3dbc 100644 --- a/src/main/java/org/elasticsearch/watcher/support/template/Template.java +++ b/src/main/java/org/elasticsearch/watcher/support/template/Template.java @@ -25,22 +25,22 @@ import java.util.Objects; */ public class Template implements ToXContent { - private final String text; + private final String template; private final @Nullable ScriptType type; private final @Nullable Map params; - public Template(String text) { - this(text, ScriptType.INLINE, ImmutableMap.of()); + public Template(String template) { + this(template, ScriptType.INLINE, ImmutableMap.of()); } - public Template(String text, ScriptType type, Map params) { - this.text = text; + public Template(String template, ScriptType type, Map params) { + this.template = template; this.type = type; this.params = params; } - public String getText() { - return text; + public String getTemplate() { + return template; } public ScriptType getType() { @@ -56,23 +56,23 @@ public class Template implements ToXContent { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Template template = (Template) o; - return Objects.equals(text, template.text) && + return Objects.equals(this.template, template.template) && Objects.equals(type, template.type) && Objects.equals(params, template.params); } @Override public int hashCode() { - return Objects.hash(text, type, params); + return Objects.hash(template, type, params); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (type == null && params == null) { - return builder.value(text); + return builder.value(template); } builder.startObject(); - builder.field(Field.TEXT.getPreferredName(), text); + builder.field(Field.TEMPLATE.getPreferredName(), template); if (type != null) { builder.field(Field.TYPE.getPreferredName(), type.name().toLowerCase(Locale.ROOT)); } @@ -91,7 +91,7 @@ public class Template implements ToXContent { throw new ParseException("expected a string value or an object, but found [" + token + "] instead"); } - String text = null; + String template = null; ScriptType type = ScriptType.INLINE; Map params = ImmutableMap.of(); @@ -99,11 +99,11 @@ public class Template implements ToXContent { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (Field.TEXT.match(currentFieldName)) { + } else if (Field.TEMPLATE.match(currentFieldName)) { if (token == XContentParser.Token.VALUE_STRING) { - text = parser.text(); + template = parser.text(); } else { - throw new ParseException("expected a string value for field [" + currentFieldName + "], but found [" + token + "]"); + throw new ParseException("expected a string field [{}], but found [{}]", currentFieldName, token); } } else if (Field.TYPE.match(currentFieldName)) { if (token == XContentParser.Token.VALUE_STRING) { @@ -111,23 +111,23 @@ public class Template implements ToXContent { try { type = ScriptType.valueOf(value.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException iae) { - throw new ParseException("unknown template type [" + value + "]"); + throw new ParseException("unknown template type [{}]", value); } } } else if (Field.PARAMS.match(currentFieldName)) { if (token == XContentParser.Token.START_OBJECT) { params = parser.map(); } else { - throw new ParseException("expected an object for field [" + currentFieldName + "], but found [" + token + "]"); + throw new ParseException("expected an object for field [{}], but found [{}]", currentFieldName, token); } } else { - throw new ParseException("unexpected field [" + currentFieldName + "]"); + throw new ParseException("unexpected field [{}]", currentFieldName); } } - if (text == null) { - throw new ParseException("missing required string field [" + Field.TEXT.getPreferredName() + "]"); + if (template == null) { + throw new ParseException("missing required string field [{}]", Field.TEMPLATE.getPreferredName()); } - return new Template(text, type, params); + return new Template(template, type, params); } public static Builder builder(String text) { @@ -136,12 +136,12 @@ public class Template implements ToXContent { public static class Builder { - private final String text; + private final String template; private ScriptType type; private HashMap params = new HashMap<>(); - private Builder(String text) { - this.text = text; + private Builder(String template) { + this.template = template; } public Builder setType(ScriptType type) { @@ -161,23 +161,23 @@ public class Template implements ToXContent { public Template build() { type = type != null ? type : ScriptType.INLINE; - return new Template(text, type, params); + return new Template(template, type, params); } } public static class ParseException extends WatcherException { - public ParseException(String msg) { - super(msg); + public ParseException(String msg, Object... args) { + super(msg, args); } - public ParseException(String msg, Throwable cause) { - super(msg, cause); + public ParseException(String msg, Throwable cause, Object... args) { + super(msg, cause, args); } } public interface Field { - ParseField TEXT = new ParseField("text"); + ParseField TEMPLATE = new ParseField("template"); ParseField TYPE = new ParseField("type"); ParseField PARAMS = new ParseField("params"); } diff --git a/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTemplateEngine.java b/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTemplateEngine.java index a8c99d21f9e..099ccc5b686 100644 --- a/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTemplateEngine.java +++ b/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTemplateEngine.java @@ -35,7 +35,7 @@ public class XMustacheTemplateEngine extends AbstractComponent implements Templa Map mergedModel = new HashMap<>(); mergedModel.putAll(template.getParams()); mergedModel.putAll(model); - ExecutableScript executable = service.executable(XMustacheScriptEngineService.NAME, template.getText(), template.getType(), mergedModel); + ExecutableScript executable = service.executable(XMustacheScriptEngineService.NAME, template.getTemplate(), template.getType(), mergedModel); Object result = executable.run(); if (result instanceof BytesReference) { return ((BytesReference) result).toUtf8(); diff --git a/src/test/java/org/elasticsearch/watcher/actions/email/EmailActionTests.java b/src/test/java/org/elasticsearch/watcher/actions/email/EmailActionTests.java index a7a4f6316a2..0cb183d096a 100644 --- a/src/test/java/org/elasticsearch/watcher/actions/email/EmailActionTests.java +++ b/src/test/java/org/elasticsearch/watcher/actions/email/EmailActionTests.java @@ -111,13 +111,13 @@ public class EmailActionTests extends ElasticsearchTestCase { .build(); if (subject != null) { - when(engine.render(subject, expectedModel)).thenReturn(subject.getText()); + when(engine.render(subject, expectedModel)).thenReturn(subject.getTemplate()); } if (textBody != null) { - when(engine.render(textBody, expectedModel)).thenReturn(textBody.getText()); + when(engine.render(textBody, expectedModel)).thenReturn(textBody.getTemplate()); } if (htmlBody != null) { - when(engine.render(htmlBody, expectedModel)).thenReturn(htmlBody.getText()); + when(engine.render(htmlBody, expectedModel)).thenReturn(htmlBody.getTemplate()); } EmailAction.Result result = executable.execute("_id", ctx, payload); @@ -128,9 +128,9 @@ public class EmailActionTests extends ElasticsearchTestCase { Email actualEmail = ((EmailAction.Result.Success) result).email(); assertThat(actualEmail.id(), is(wid.value())); assertThat(actualEmail, notNullValue()); - assertThat(actualEmail.subject(), is(subject == null ? null : subject.getText())); - assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getText())); - assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getText())); + assertThat(actualEmail.subject(), is(subject == null ? null : subject.getTemplate())); + assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getTemplate())); + assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getTemplate())); if (attachPayload) { assertThat(actualEmail.attachments(), hasKey("data")); } @@ -188,21 +188,21 @@ public class EmailActionTests extends ElasticsearchTestCase { } if (subject != null) { if (randomBoolean()) { - builder.field("subject", subject.getText()); + builder.field("subject", subject.getTemplate()); } else { builder.field("subject", subject); } } if (textBody != null) { if (randomBoolean()) { - builder.field("text_body", textBody.getText()); + builder.field("text_body", textBody.getTemplate()); } else { builder.field("text_body", textBody); } } if (htmlBody != null) { if (randomBoolean()) { - builder.field("html_body", htmlBody.getText()); + builder.field("html_body", htmlBody.getTemplate()); } else { builder.field("html_body", htmlBody); } diff --git a/src/test/java/org/elasticsearch/watcher/support/template/TemplateTests.java b/src/test/java/org/elasticsearch/watcher/support/template/TemplateTests.java index 9e909dd557b..a78dc2e1aef 100644 --- a/src/test/java/org/elasticsearch/watcher/support/template/TemplateTests.java +++ b/src/test/java/org/elasticsearch/watcher/support/template/TemplateTests.java @@ -90,7 +90,7 @@ public class TemplateTests extends ElasticsearchTestCase { Template template = new Template("_template", randomScriptType(), ImmutableMap.of("param_key", "param_val")); XContentBuilder builder = jsonBuilder().startObject() - .field(randomFrom("text"), template.getText()) + .field(randomFrom("template"), template.getTemplate()) .field(randomFrom("type"), template.getType().name()) .field(randomFrom("params"), template.getParams()) .endObject(); @@ -130,7 +130,7 @@ public class TemplateTests extends ElasticsearchTestCase { @Test(expected = Template.ParseException.class) public void testParser_Invalid_UnknownScriptType() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .field("text", "_template") + .field("template", "_template") .field("type", "unknown_type") .startObject("params").endObject() .endObject();