diff --git a/src/main/java/org/elasticsearch/watcher/actions/email/service/Accounts.java b/src/main/java/org/elasticsearch/watcher/actions/email/service/Accounts.java index 68a2eee01e3..eb53958378a 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/email/service/Accounts.java +++ b/src/main/java/org/elasticsearch/watcher/actions/email/service/Accounts.java @@ -51,9 +51,13 @@ public class Accounts { * * @param name The name of the requested account * @return The account associated with the given name. + * @throws EmailException if the name is null and the default account is null. */ - public Account account(String name) { + public Account account(String name) throws EmailException { if (name == null) { + if (defaultAccountName == null) { + throw new EmailSettingsException("cannot find default email account as no accounts have been configured"); + } name = defaultAccountName; } return accounts.get(name); diff --git a/src/test/java/org/elasticsearch/watcher/actions/email/service/AccountsTests.java b/src/test/java/org/elasticsearch/watcher/actions/email/service/AccountsTests.java index b0cad983e08..e87c0a24c2d 100644 --- a/src/test/java/org/elasticsearch/watcher/actions/email/service/AccountsTests.java +++ b/src/test/java/org/elasticsearch/watcher/actions/email/service/AccountsTests.java @@ -10,10 +10,7 @@ import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.watcher.support.secret.SecretService; import org.junit.Test; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.isOneOf; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.core.IsNull.nullValue; +import static org.hamcrest.Matchers.*; /** * @@ -96,12 +93,12 @@ public class AccountsTests extends ElasticsearchTestCase { new Accounts(builder.build(), new SecretService.PlainText(), logger); } - @Test + @Test(expected = EmailSettingsException.class) public void testNoAccount() throws Exception { ImmutableSettings.Builder builder = ImmutableSettings.builder(); Accounts accounts = new Accounts(builder.build(), new SecretService.PlainText(), logger); - Account account = accounts.account(null); - assertThat(account, nullValue()); + accounts.account(null); + fail("no accounts are configured so trying to get the default account should throw an EmailSettingsException"); } @Test(expected = EmailSettingsException.class)