diff --git a/src/main/java/org/elasticsearch/alerts/actions/email/EmailAction.java b/src/main/java/org/elasticsearch/alerts/actions/email/EmailAction.java index 42d6b90987a..9cd58e8a597 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/email/EmailAction.java +++ b/src/main/java/org/elasticsearch/alerts/actions/email/EmailAction.java @@ -38,15 +38,16 @@ public class EmailAction extends Action { final Authentication auth; final Profile profile; final String account; - final Template subject; - final Template textBody; - final Template htmlBody; + final @Nullable Template subject; + final @Nullable Template textBody; + final @Nullable Template htmlBody; final boolean attachPayload; final EmailService emailService; - public EmailAction(ESLogger logger, @Nullable Transform transform, EmailService emailService, Email emailPrototype, Authentication auth, Profile profile, - String account, Template subject, Template textBody, Template htmlBody, boolean attachPayload) { + public EmailAction(ESLogger logger, @Nullable Transform transform, EmailService emailService, Email emailPrototype, + Authentication auth, Profile profile, String account, @Nullable Template subject, + @Nullable Template textBody, @Nullable Template htmlBody, boolean attachPayload) { super(logger, transform); this.emailService = emailService; @@ -74,8 +75,8 @@ public class EmailAction extends Action { .copyFrom(emailPrototype); email.id(ctx.id()); - email.subject(subject.render(model)); - email.textBody(textBody.render(model)); + email.subject(subject != null ? subject.render(model) : ""); + email.textBody(textBody != null ? textBody.render(model) : ""); if (htmlBody != null) { email.htmlBody(htmlBody.render(model)); diff --git a/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java b/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java index e84ee138c39..fcdcf17f655 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java +++ b/src/test/java/org/elasticsearch/alerts/actions/email/EmailActionTests.java @@ -59,8 +59,8 @@ public class EmailActionTests extends ElasticsearchTestCase { Authentication auth = new Authentication("user", "passwd"); Profile profile = randomFrom(Profile.values()); - Template subject = mock(Template.class); - Template textBody = mock(Template.class); + Template subject = randomBoolean() ? null : mock(Template.class); + Template textBody = randomBoolean() ? null : mock(Template.class); Template htmlBody = randomBoolean() ? null : mock(Template.class); boolean attachPayload = randomBoolean(); Transform transform = randomBoolean() ? null : mock(Transform.class); @@ -98,8 +98,12 @@ public class EmailActionTests extends ElasticsearchTestCase { .put("scheduled_fire_time", now).build()) .build(); - when(subject.render(expectedModel)).thenReturn("_subject"); - when(textBody.render(expectedModel)).thenReturn("_text_body"); + if (subject != null) { + when(subject.render(expectedModel)).thenReturn("_subject"); + } + if (textBody != null) { + when(textBody.render(expectedModel)).thenReturn("_text_body"); + } if (htmlBody != null) { when (htmlBody.render(expectedModel)).thenReturn("_html_body"); } @@ -112,8 +116,8 @@ public class EmailActionTests extends ElasticsearchTestCase { Email actualEmail = ((EmailAction.Result.Success) result).email(); assertThat(actualEmail.id(), is(ctxId)); assertThat(actualEmail, notNullValue()); - assertThat(actualEmail.subject(), is("_subject")); - assertThat(actualEmail.textBody(), is("_text_body")); + assertThat(actualEmail.subject(), is(subject == null ? "" : "_subject")); + assertThat(actualEmail.textBody(), is(textBody == null ? "" : "_text_body")); if (htmlBody != null) { assertThat(actualEmail.htmlBody(), is("_html_body")); }