Fix null pointer in writing email action.
This commit fixes two issues. 1. The actual emails that were being sent after getting the defaults applied were not being returned as part of EmailSent 2. There was a possible NPE when writing an `Email` if from or to was null. Closes elastic/elasticsearch#147 Original commit: elastic/x-pack-elasticsearch@0468280090
This commit is contained in:
parent
27f6239fe9
commit
b85c0ca1a7
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class InternalEmailService extends AbstractLifecycleComponent<InternalEma
|
|||
EmailSent send(Email email, Authentication auth, Profile profile, Account account) {
|
||||
assert account != null;
|
||||
try {
|
||||
account.send(email, auth, profile);
|
||||
email = account.send(email, auth, profile);
|
||||
} catch (MessagingException me) {
|
||||
throw new EmailException("failed to send email [" + email + "] via account [" + account.name() + "]", me);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.watcher.actions.email.service;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
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.json.JsonXContent;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class EmailTest extends ElasticsearchTestCase {
|
||||
|
||||
@Test @Repeat(iterations = 100)
|
||||
public void testEmail_Parser_SelfGenerated() throws Exception {
|
||||
String id = "test-id";
|
||||
Email.Address from = randomFrom(new Email.Address("from@from.com"), null);
|
||||
List<Email.Address> 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("<hr/>BODY<b/><hr/>", "", null);
|
||||
ImmutableMap<String, Attachment> attachments = null;
|
||||
ImmutableMap<String, Inline> 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));
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue