Throw exception when trying to use the default account but no accounts are configured.

This change throws an exception to let the user know that they are trying to send an email using the default account
but no accounts have been configured.

Fixes elastic/elasticsearch#303

Original commit: elastic/x-pack-elasticsearch@3e68bddf24
This commit is contained in:
Brian Murphy 2015-04-30 15:12:22 -04:00
parent ba3037f5fe
commit 1819dc97f4
2 changed files with 9 additions and 8 deletions

View File

@ -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);

View File

@ -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)