diff --git a/src/main/java/org/elasticsearch/watcher/actions/email/service/Account.java b/src/main/java/org/elasticsearch/watcher/actions/email/service/Account.java index 25a92c67a9d..90d099209d8 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/email/service/Account.java +++ b/src/main/java/org/elasticsearch/watcher/actions/email/service/Account.java @@ -51,7 +51,7 @@ public class Account { return config.name; } - public void send(Email email, Authentication auth, Profile profile) throws MessagingException { + public Email send(Email email, Authentication auth, Profile profile) throws MessagingException { // applying the defaults on missing emails fields email = config.defaults.apply(email); @@ -96,6 +96,7 @@ public class Account { } } } + return email; } static class Config { diff --git a/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java b/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java index dcf42acdbcb..b587a2f7ec4 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java +++ b/src/main/java/org/elasticsearch/watcher/actions/email/service/Email.java @@ -128,7 +128,9 @@ public class Email implements ToXContent { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(ID_FIELD.getPreferredName(), id); - builder.field(FROM_FIELD.getPreferredName(), from); + if (from != null) { + builder.field(FROM_FIELD.getPreferredName(), from); + } if (replyTo != null) { builder.field(REPLY_TO_FIELD.getPreferredName(), (ToXContent) replyTo); } @@ -136,7 +138,9 @@ public class Email implements ToXContent { builder.field(PRIORITY_FIELD.getPreferredName(), priority); } builder.field(SENT_DATE_FIELD.getPreferredName(), sentDate); - builder.field(TO_FIELD.getPreferredName(), (ToXContent) to); + if (to != null) { + builder.field(TO_FIELD.getPreferredName(), (ToXContent) to); + } if (cc != null) { builder.field(CC_FIELD.getPreferredName(), (ToXContent) cc); } diff --git a/src/main/java/org/elasticsearch/watcher/actions/email/service/InternalEmailService.java b/src/main/java/org/elasticsearch/watcher/actions/email/service/InternalEmailService.java index 73646b0ed11..91e06bf539a 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/email/service/InternalEmailService.java +++ b/src/main/java/org/elasticsearch/watcher/actions/email/service/InternalEmailService.java @@ -63,7 +63,7 @@ public class InternalEmailService extends AbstractLifecycleComponent addresses = new ArrayList<>(); + for( int i = 0; i < randomIntBetween(1, 5); ++i){ + addresses.add(new Email.Address("address" + i + "@test.com")); + } + Email.AddressList possibleList = new Email.AddressList(addresses); + Email.AddressList replyTo = randomFrom(possibleList, null); + Email.Priority priority = randomFrom(Email.Priority.values()); + DateTime sentDate = new DateTime(randomInt()); + Email.AddressList to = randomFrom(possibleList, null); + Email.AddressList cc = randomFrom(possibleList, null); + Email.AddressList bcc = randomFrom(possibleList, null); + String subject = randomFrom("Random Subject", "", null); + String textBody = randomFrom("Random Body", "", null); + String htmlBody = randomFrom("
BODY
", "", null); + ImmutableMap attachments = null; + ImmutableMap inlines = null; + + Email email = new Email(id, from, replyTo, priority, sentDate, to, cc, bcc, subject, textBody, htmlBody, attachments, inlines); + + XContentBuilder builder = XContentFactory.jsonBuilder(); + email.toXContent(builder, ToXContent.EMPTY_PARAMS); + + XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes()); + parser.nextToken(); + + Email parsedEmail = Email.parse(parser); + + + assertThat(email.id, equalTo(parsedEmail.id)); + assertThat(email.from, equalTo(parsedEmail.from)); + assertThat(email.replyTo, equalTo(parsedEmail.replyTo)); + assertThat(email.priority, equalTo(parsedEmail.priority)); + assertThat(email.sentDate, equalTo(parsedEmail.sentDate)); + assertThat(email.to, equalTo(parsedEmail.to)); + assertThat(email.cc, equalTo(parsedEmail.cc)); + assertThat(email.bcc, equalTo(parsedEmail.bcc)); + assertThat(email.subject, equalTo(parsedEmail.subject)); + assertThat(email.textBody, equalTo(parsedEmail.textBody)); + assertThat(email.htmlBody, equalTo(parsedEmail.htmlBody)); + } + +} diff --git a/src/test/java/org/elasticsearch/watcher/actions/email/service/InternalEmailServiceTests.java b/src/test/java/org/elasticsearch/watcher/actions/email/service/InternalEmailServiceTests.java index 16e5d320baa..ea874d2483d 100644 --- a/src/test/java/org/elasticsearch/watcher/actions/email/service/InternalEmailServiceTests.java +++ b/src/test/java/org/elasticsearch/watcher/actions/email/service/InternalEmailServiceTests.java @@ -48,8 +48,10 @@ public class InternalEmailServiceTests extends ElasticsearchTestCase { when(account.name()).thenReturn("account1"); when(accounts.account("account1")).thenReturn(account); Email email = mock(Email.class); + Authentication auth = new Authentication("user", "passwd"); Profile profile = randomFrom(Profile.values()); + when(account.send(email, auth, profile)).thenReturn(email); EmailService.EmailSent sent = service.send(email, auth, profile, "account1"); verify(account).send(email, auth, profile); assertThat(sent, notNullValue());