Remove interfaces for notification services

We have 4 types of notification services, and all of them have an
interface with a single implementation class. They also all
unnecessarily are lifecycle componenets, but the only thing start does
is read the settings.

This change converts all 4 notification services to classes, and makes
them regular components instead of lifecycle services.

Original commit: elastic/x-pack-elasticsearch@897115ae65
This commit is contained in:
Ryan Ernst 2016-08-03 00:07:26 -07:00
parent b498fd32a2
commit 549a5d3e73
16 changed files with 211 additions and 414 deletions

View File

@ -180,13 +180,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I
return modules; return modules;
} }
@Override
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
ArrayList<Class<? extends LifecycleComponent>> services = new ArrayList<>();
services.addAll(notification.nodeServices());
return services;
}
@Override @Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService) { ResourceWatcherService resourceWatcherService) {

View File

@ -5,29 +5,23 @@
*/ */
package org.elasticsearch.xpack.notification; package org.elasticsearch.xpack.notification;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsModule;
import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.notification.email.EmailService;
import org.elasticsearch.xpack.notification.email.InternalEmailService;
import org.elasticsearch.xpack.notification.hipchat.HipChatService;
import org.elasticsearch.xpack.notification.hipchat.InternalHipChatService;
import org.elasticsearch.xpack.notification.pagerduty.InternalPagerDutyService;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyAccount;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService;
import org.elasticsearch.xpack.notification.slack.InternalSlackService;
import org.elasticsearch.xpack.notification.slack.SlackService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.notification.email.EmailService;
import org.elasticsearch.xpack.notification.hipchat.HipChatService;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyAccount;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService;
import org.elasticsearch.xpack.notification.slack.SlackService;
public class Notification { public class Notification {
private final boolean transportClient; private final boolean transportClient;
@ -37,10 +31,10 @@ public class Notification {
} }
public List<Setting<?>> getSettings() { public List<Setting<?>> getSettings() {
return Arrays.asList(InternalSlackService.SLACK_ACCOUNT_SETTING, return Arrays.asList(SlackService.SLACK_ACCOUNT_SETTING,
InternalEmailService.EMAIL_ACCOUNT_SETTING, EmailService.EMAIL_ACCOUNT_SETTING,
InternalHipChatService.HIPCHAT_ACCOUNT_SETTING, HipChatService.HIPCHAT_ACCOUNT_SETTING,
InternalPagerDutyService.PAGERDUTY_ACCOUNT_SETTING); PagerDutyService.PAGERDUTY_ACCOUNT_SETTING);
} }
public List<String> getSettingsFilter() { public List<String> getSettingsFilter() {
@ -54,18 +48,6 @@ public class Notification {
return settingsFilter; return settingsFilter;
} }
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
if (transportClient) {
return Collections.emptyList();
}
return Arrays.<Class<? extends LifecycleComponent>>asList(
EmailService.class,
HipChatService.class,
SlackService.class,
PagerDutyService.class
);
}
public Collection<? extends Module> nodeModules() { public Collection<? extends Module> nodeModules() {
if (transportClient) { if (transportClient) {
return Collections.emptyList(); return Collections.emptyList();

View File

@ -8,17 +8,12 @@ package org.elasticsearch.xpack.notification;
import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.MapBinder; import org.elasticsearch.common.inject.multibindings.MapBinder;
import org.elasticsearch.xpack.notification.email.EmailService; import org.elasticsearch.xpack.notification.email.EmailService;
import org.elasticsearch.xpack.notification.email.HtmlSanitizer;
import org.elasticsearch.xpack.notification.email.InternalEmailService;
import org.elasticsearch.xpack.notification.email.attachment.DataAttachmentParser; import org.elasticsearch.xpack.notification.email.attachment.DataAttachmentParser;
import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentParser; import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentParser;
import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentsParser; import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentsParser;
import org.elasticsearch.xpack.notification.email.attachment.HttpEmailAttachementParser; import org.elasticsearch.xpack.notification.email.attachment.HttpEmailAttachementParser;
import org.elasticsearch.xpack.notification.hipchat.HipChatService; import org.elasticsearch.xpack.notification.hipchat.HipChatService;
import org.elasticsearch.xpack.notification.hipchat.InternalHipChatService;
import org.elasticsearch.xpack.notification.pagerduty.InternalPagerDutyService;
import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService; import org.elasticsearch.xpack.notification.pagerduty.PagerDutyService;
import org.elasticsearch.xpack.notification.slack.InternalSlackService;
import org.elasticsearch.xpack.notification.slack.SlackService; import org.elasticsearch.xpack.notification.slack.SlackService;
import java.util.HashMap; import java.util.HashMap;
@ -40,9 +35,7 @@ public class NotificationModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
// email bind(EmailService.class).asEagerSingleton();
bind(InternalEmailService.class).asEagerSingleton();
bind(EmailService.class).to(InternalEmailService.class).asEagerSingleton();
MapBinder<String, EmailAttachmentParser> emailParsersBinder = MapBinder.newMapBinder(binder(), String.class, MapBinder<String, EmailAttachmentParser> emailParsersBinder = MapBinder.newMapBinder(binder(), String.class,
EmailAttachmentParser.class); EmailAttachmentParser.class);
@ -51,16 +44,8 @@ public class NotificationModule extends AbstractModule {
} }
bind(EmailAttachmentsParser.class).asEagerSingleton(); bind(EmailAttachmentsParser.class).asEagerSingleton();
// hipchat bind(HipChatService.class).asEagerSingleton();
bind(InternalHipChatService.class).asEagerSingleton(); bind(SlackService.class).asEagerSingleton();
bind(HipChatService.class).to(InternalHipChatService.class); bind(PagerDutyService.class).asEagerSingleton();
// slack
bind(InternalSlackService.class).asEagerSingleton();
bind(SlackService.class).to(InternalSlackService.class);
// pager duty
bind(InternalPagerDutyService.class).asEagerSingleton();
bind(PagerDutyService.class).to(InternalPagerDutyService.class);
} }
} }

View File

@ -5,20 +5,69 @@
*/ */
package org.elasticsearch.xpack.notification.email; package org.elasticsearch.xpack.notification.email;
import org.elasticsearch.common.component.LifecycleComponent;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.security.crypto.CryptoService;
/** /**
* * A component to store email credentials and handle sending email notifications.
*/ */
public interface EmailService extends LifecycleComponent{ public class EmailService extends AbstractComponent {
EmailSent send(Email email, Authentication auth, Profile profile) throws MessagingException; private final CryptoService cryptoService;
public static final Setting<Settings> EMAIL_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.email.", Setting.Property.Dynamic, Setting.Property.NodeScope);
EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException; private volatile Accounts accounts;
class EmailSent { @Inject
public EmailService(Settings settings, @Nullable CryptoService cryptoService, ClusterSettings clusterSettings) {
super(settings);
this.cryptoService = cryptoService;
clusterSettings.addSettingsUpdateConsumer(EMAIL_ACCOUNT_SETTING, this::setEmailAccountSettings);
setEmailAccountSettings(EMAIL_ACCOUNT_SETTING.get(settings));
}
private void setEmailAccountSettings(Settings settings) {
this.accounts = createAccounts(settings, logger);
}
public EmailSent send(Email email, Authentication auth, Profile profile) throws MessagingException {
return send(email, auth, profile, (String) null);
}
public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException {
Account account = accounts.account(accountName);
if (account == null) {
throw new IllegalArgumentException("failed to send email with subject [" + email.subject() + "] via account [" + accountName
+ "]. account does not exist");
}
return send(email, auth, profile, account);
}
EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException {
assert account != null;
try {
email = account.send(email, auth, profile);
} catch (MessagingException me) {
throw new MessagingException("failed to send email with subject [" + email.subject() + "] via account [" + account.name() +
"]", me);
}
return new EmailSent(account.name(), email);
}
protected Accounts createAccounts(Settings settings, ESLogger logger) {
return new Accounts(settings, cryptoService, logger);
}
public static class EmailSent {
private final String account; private final String account;
private final Email email; private final Email email;

View File

@ -1,85 +0,0 @@
/*
* 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.xpack.notification.email;
import javax.mail.MessagingException;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.security.crypto.CryptoService;
/**
*
*/
public class InternalEmailService extends AbstractLifecycleComponent implements EmailService {
private final CryptoService cryptoService;
public static final Setting<Settings> EMAIL_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.email.", Setting.Property.Dynamic, Setting.Property.NodeScope);
private volatile Accounts accounts;
@Inject
public InternalEmailService(Settings settings, @Nullable CryptoService cryptoService, ClusterSettings clusterSettings) {
super(settings);
this.cryptoService = cryptoService;
clusterSettings.addSettingsUpdateConsumer(EMAIL_ACCOUNT_SETTING, this::setEmailAccountSettings);
setEmailAccountSettings(EMAIL_ACCOUNT_SETTING.get(settings));
}
private void setEmailAccountSettings(Settings settings) {
this.accounts = createAccounts(settings, logger);
}
@Override
protected void doStart() throws ElasticsearchException {
}
@Override
protected void doStop() throws ElasticsearchException {
}
@Override
protected void doClose() throws ElasticsearchException {
}
@Override
public EmailSent send(Email email, Authentication auth, Profile profile) throws MessagingException {
return send(email, auth, profile, (String) null);
}
@Override
public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException {
Account account = accounts.account(accountName);
if (account == null) {
throw new IllegalArgumentException("failed to send email with subject [" + email.subject() + "] via account [" + accountName
+ "]. account does not exist");
}
return send(email, auth, profile, account);
}
EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException {
assert account != null;
try {
email = account.send(email, auth, profile);
} catch (MessagingException me) {
throw new MessagingException("failed to send email with subject [" + email.subject() + "] via account [" + account.name() +
"]", me);
}
return new EmailSent(account.name(), email);
}
protected Accounts createAccounts(Settings settings, ESLogger logger) {
return new Accounts(settings, cryptoService, logger);
}
}

View File

@ -5,22 +5,48 @@
*/ */
package org.elasticsearch.xpack.notification.hipchat; package org.elasticsearch.xpack.notification.hipchat;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/** /**
* * A component to store hipchat credentials.
*/ */
public interface HipChatService extends LifecycleComponent { public class HipChatService extends AbstractComponent {
private final HttpClient httpClient;
private volatile HipChatAccounts accounts;
public static final Setting<Settings> HIPCHAT_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.hipchat.", Setting.Property.Dynamic, Setting.Property.NodeScope);
@Inject
public HipChatService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(HIPCHAT_ACCOUNT_SETTING, this::setHipchatAccountSetting);
setHipchatAccountSetting(HIPCHAT_ACCOUNT_SETTING.get(settings));
}
private void setHipchatAccountSetting(Settings setting) {
accounts = new HipChatAccounts(setting, httpClient, logger);
}
/** /**
* @return The default hipchat account. * @return The default hipchat account.
*/ */
HipChatAccount getDefaultAccount(); public HipChatAccount getDefaultAccount() {
return accounts.account(null);
}
/** /**
* @return The account identified by the given name. If the given name is {@code null} the default * @return The account identified by the given name. If the given name is {@code null} the default
* account will be returned. * account will be returned.
*/ */
HipChatAccount getAccount(String accountName); public HipChatAccount getAccount(String name) {
return accounts.account(name);
}
} }

View File

@ -1,58 +0,0 @@
/*
* 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.xpack.notification.hipchat;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/**
*
*/
public class InternalHipChatService extends AbstractLifecycleComponent implements HipChatService {
private final HttpClient httpClient;
private volatile HipChatAccounts accounts;
public static final Setting<Settings> HIPCHAT_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.hipchat.", Setting.Property.Dynamic, Setting.Property.NodeScope);
@Inject
public InternalHipChatService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(HIPCHAT_ACCOUNT_SETTING, this::setHipchatAccountSetting);
}
@Override
protected void doStart() {
setHipchatAccountSetting(HIPCHAT_ACCOUNT_SETTING.get(settings));
}
@Override
protected void doStop() {
}
@Override
protected void doClose() {
}
private void setHipchatAccountSetting(Settings setting) {
accounts = new HipChatAccounts(setting, httpClient, logger);
}
@Override
public HipChatAccount getDefaultAccount() {
return accounts.account(null);
}
@Override
public HipChatAccount getAccount(String name) {
return accounts.account(name);
}
}

View File

@ -1,59 +0,0 @@
/*
* 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.xpack.notification.pagerduty;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/**
*
*/
public class InternalPagerDutyService extends AbstractLifecycleComponent implements PagerDutyService {
public static final Setting<Settings> PAGERDUTY_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.pagerduty.", Setting.Property.Dynamic, Setting.Property.NodeScope);
private final HttpClient httpClient;
private volatile PagerDutyAccounts accounts;
@Inject
public InternalPagerDutyService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(PAGERDUTY_ACCOUNT_SETTING, this::setPagerDutyAccountSetting);
}
@Override
protected void doStart() {
setPagerDutyAccountSetting(PAGERDUTY_ACCOUNT_SETTING.get(settings));
}
@Override
protected void doStop() {
}
@Override
protected void doClose() {
}
private void setPagerDutyAccountSetting(Settings settings) {
accounts = new PagerDutyAccounts(settings, httpClient, logger);
}
@Override
public PagerDutyAccount getDefaultAccount() {
return accounts.account(null);
}
@Override
public PagerDutyAccount getAccount(String name) {
return accounts.account(name);
}
}

View File

@ -5,14 +5,41 @@
*/ */
package org.elasticsearch.xpack.notification.pagerduty; package org.elasticsearch.xpack.notification.pagerduty;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/** /**
* * A component to store pagerduty credentials.
*/ */
public interface PagerDutyService extends LifecycleComponent { public class PagerDutyService extends AbstractComponent {
PagerDutyAccount getDefaultAccount(); public static final Setting<Settings> PAGERDUTY_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.pagerduty.", Setting.Property.Dynamic, Setting.Property.NodeScope);
PagerDutyAccount getAccount(String accountName); private final HttpClient httpClient;
private volatile PagerDutyAccounts accounts;
@Inject
public PagerDutyService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(PAGERDUTY_ACCOUNT_SETTING, this::setPagerDutyAccountSetting);
setPagerDutyAccountSetting(PAGERDUTY_ACCOUNT_SETTING.get(settings));
}
private void setPagerDutyAccountSetting(Settings settings) {
accounts = new PagerDutyAccounts(settings, httpClient, logger);
}
public PagerDutyAccount getDefaultAccount() {
return accounts.account(null);
}
public PagerDutyAccount getAccount(String name) {
return accounts.account(name);
}
} }

View File

@ -1,58 +0,0 @@
/*
* 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.xpack.notification.slack;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/**
*
*/
public class InternalSlackService extends AbstractLifecycleComponent implements SlackService {
private final HttpClient httpClient;
public static final Setting<Settings> SLACK_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.slack.", Setting.Property.Dynamic, Setting.Property.NodeScope);
private volatile SlackAccounts accounts;
@Inject
public InternalSlackService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(SLACK_ACCOUNT_SETTING, this::setSlackAccountSetting);
}
@Override
protected void doStart() {
setSlackAccountSetting(SLACK_ACCOUNT_SETTING.get(settings));
}
@Override
protected void doStop() {
}
@Override
protected void doClose() {
}
@Override
public SlackAccount getDefaultAccount() {
return accounts.account(null);
}
private void setSlackAccountSetting(Settings setting) {
accounts = new SlackAccounts(setting, httpClient, logger);
}
@Override
public SlackAccount getAccount(String name) {
return accounts.account(name);
}
}

View File

@ -5,22 +5,47 @@
*/ */
package org.elasticsearch.xpack.notification.slack; package org.elasticsearch.xpack.notification.slack;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.http.HttpClient;
/** /**
* * A component to store slack credentials.
*/ */
public interface SlackService extends LifecycleComponent { public class SlackService extends AbstractComponent {
private final HttpClient httpClient;
public static final Setting<Settings> SLACK_ACCOUNT_SETTING =
Setting.groupSetting("xpack.notification.slack.", Setting.Property.Dynamic, Setting.Property.NodeScope);
private volatile SlackAccounts accounts;
@Inject
public SlackService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super(settings);
this.httpClient = httpClient;
clusterSettings.addSettingsUpdateConsumer(SLACK_ACCOUNT_SETTING, this::setSlackAccountSetting);
setSlackAccountSetting(SLACK_ACCOUNT_SETTING.get(settings));
}
/** /**
* @return The default slack account. * @return The default slack account.
*/ */
SlackAccount getDefaultAccount(); public SlackAccount getDefaultAccount() {
return accounts.account(null);
}
private void setSlackAccountSetting(Settings setting) {
accounts = new SlackAccounts(setting, httpClient, logger);
}
/** /**
* @return The account identified by the given name. If the given name is {@code null} the default * @return The account identified by the given name. If the given name is {@code null} the default
* account will be returned. * account will be returned.
*/ */
SlackAccount getAccount(String accountName); public SlackAccount getAccount(String name) {
return accounts.account(name);
}
} }

View File

@ -22,29 +22,20 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** public class EmailServiceTests extends ESTestCase {
* private EmailService service;
*/
public class InternalEmailServiceTests extends ESTestCase {
private InternalEmailService service;
private Accounts accounts; private Accounts accounts;
@Before @Before
public void init() throws Exception { public void init() throws Exception {
accounts = mock(Accounts.class); accounts = mock(Accounts.class);
service = new InternalEmailService(Settings.EMPTY, null, service = new EmailService(Settings.EMPTY, null,
new ClusterSettings(Settings.EMPTY, Collections.singleton(InternalEmailService.EMAIL_ACCOUNT_SETTING))) { new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING))) {
@Override @Override
protected Accounts createAccounts(Settings settings, ESLogger logger) { protected Accounts createAccounts(Settings settings, ESLogger logger) {
return accounts; return accounts;
} }
}; };
service.start();
}
@After
public void cleanup() throws Exception {
service.stop();
} }
public void testSend() throws Exception { public void testSend() throws Exception {

View File

@ -89,13 +89,11 @@ public class ManualPublicSmtpServersTester {
static void test(Profile profile, Settings.Builder settingsBuilder) throws Exception { static void test(Profile profile, Settings.Builder settingsBuilder) throws Exception {
String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png"; String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png";
if (InternalEmailServiceTests.class.getResourceAsStream(path) == null) { if (EmailServiceTests.class.getResourceAsStream(path) == null) {
throw new ElasticsearchException("Could not find logo at path {}", path); throw new ElasticsearchException("Could not find logo at path {}", path);
} }
InternalEmailService service = startEmailService(settingsBuilder); EmailService service = startEmailService(settingsBuilder);
try {
ToXContent content = (xContentBuilder, params) -> xContentBuilder.startObject() ToXContent content = (xContentBuilder, params) -> xContentBuilder.startObject()
.field("key1", "value1") .field("key1", "value1")
.field("key2", "value2") .field("key2", "value2")
@ -109,22 +107,17 @@ public class ManualPublicSmtpServersTester {
.htmlBody("<b>html body</b><p/><p/><img src=\"cid:logo.png\"/>") .htmlBody("<b>html body</b><p/><p/><img src=\"cid:logo.png\"/>")
.attach(new Attachment.XContent.Yaml("test.yml", content)) .attach(new Attachment.XContent.Yaml("test.yml", content))
.attach(new Attachment.Stream("logo.png", "logo.png", true, .attach(new Attachment.Stream("logo.png", "logo.png", true,
() -> InternalEmailServiceTests.class.getResourceAsStream(path))) () -> EmailServiceTests.class.getResourceAsStream(path)))
.build(); .build();
EmailService.EmailSent sent = service.send(email, null, profile); EmailService.EmailSent sent = service.send(email, null, profile);
terminal.println(String.format(Locale.ROOT, "email sent via account [%s]", sent.account())); terminal.println(String.format(Locale.ROOT, "email sent via account [%s]", sent.account()));
} finally {
service.stop();
}
} }
static InternalEmailService startEmailService(Settings.Builder builder) { static EmailService startEmailService(Settings.Builder builder) {
Settings settings = builder.build(); Settings settings = builder.build();
InternalEmailService service = new InternalEmailService(settings, null, return new EmailService(settings, null,
new ClusterSettings(settings, Collections.singleton(InternalEmailService.EMAIL_ACCOUNT_SETTING))); new ClusterSettings(settings, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
service.start();
return service;
} }
} }

View File

@ -22,7 +22,7 @@ public class ProfileTests extends ESTestCase {
public void testThatInlineAttachmentsAreCreated() throws Exception { public void testThatInlineAttachmentsAreCreated() throws Exception {
String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png"; String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png";
Attachment attachment = new Attachment.Stream("inline.png", "inline.png", true, Attachment attachment = new Attachment.Stream("inline.png", "inline.png", true,
() -> InternalEmailServiceTests.class.getResourceAsStream(path)); () -> EmailServiceTests.class.getResourceAsStream(path));
Email email = Email.builder() Email email = Email.builder()
.id("foo") .id("foo")

View File

@ -26,7 +26,7 @@ import static org.mockito.Mockito.mock;
/** /**
* *
*/ */
public class InternalHipChatServiceTests extends ESTestCase { public class HipChatServiceTests extends ESTestCase {
private HttpClient httpClient; private HttpClient httpClient;
@Before @Before
@ -53,9 +53,8 @@ public class InternalHipChatServiceTests extends ESTestCase {
settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port); settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port);
} }
buildMessageDefaults(accountName, settingsBuilder, defaultRoom, null, defaultFrom, defaultColor, defaultFormat, defaultNotify); buildMessageDefaults(accountName, settingsBuilder, defaultRoom, null, defaultFrom, defaultColor, defaultFormat, defaultNotify);
InternalHipChatService service = new InternalHipChatService(settingsBuilder.build(), httpClient, HipChatService service = new HipChatService(settingsBuilder.build(), httpClient,
new ClusterSettings(settingsBuilder.build(), Collections.singleton(InternalHipChatService.HIPCHAT_ACCOUNT_SETTING))); new ClusterSettings(settingsBuilder.build(), Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING)));
service.start();
HipChatAccount account = service.getAccount(accountName); HipChatAccount account = service.getAccount(accountName);
assertThat(account, notNullValue()); assertThat(account, notNullValue());
@ -102,9 +101,8 @@ public class InternalHipChatServiceTests extends ESTestCase {
settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port); settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port);
} }
buildMessageDefaults(accountName, settingsBuilder, null, null, defaultFrom, defaultColor, defaultFormat, defaultNotify); buildMessageDefaults(accountName, settingsBuilder, null, null, defaultFrom, defaultColor, defaultFormat, defaultNotify);
InternalHipChatService service = new InternalHipChatService(settingsBuilder.build(), httpClient, HipChatService service = new HipChatService(settingsBuilder.build(), httpClient,
new ClusterSettings(settingsBuilder.build(), Collections.singleton(InternalHipChatService.HIPCHAT_ACCOUNT_SETTING))); new ClusterSettings(settingsBuilder.build(), Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING)));
service.start();
HipChatAccount account = service.getAccount(accountName); HipChatAccount account = service.getAccount(accountName);
assertThat(account, notNullValue()); assertThat(account, notNullValue());
@ -131,14 +129,11 @@ public class InternalHipChatServiceTests extends ESTestCase {
.put("xpack.notification.hipchat.account." + accountName + ".profile", .put("xpack.notification.hipchat.account." + accountName + ".profile",
HipChatAccount.Profile.INTEGRATION.value()) HipChatAccount.Profile.INTEGRATION.value())
.put("xpack.notification.hipchat.account." + accountName + ".auth_token", "_token"); .put("xpack.notification.hipchat.account." + accountName + ".auth_token", "_token");
try (InternalHipChatService service = new InternalHipChatService(settingsBuilder.build(), httpClient, SettingsException e = expectThrows(SettingsException.class, () ->
new ClusterSettings(settingsBuilder.build(), Collections.singleton(InternalHipChatService.HIPCHAT_ACCOUNT_SETTING)))) { new HipChatService(settingsBuilder.build(), httpClient,
service.start(); new ClusterSettings(settingsBuilder.build(), Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING))));
fail("Expected SettingsException");
} catch (SettingsException e) {
assertThat(e.getMessage(), containsString("missing required [room] setting for [integration] account profile")); assertThat(e.getMessage(), containsString("missing required [room] setting for [integration] account profile"));
} }
}
public void testSingleAccountUser() throws Exception { public void testSingleAccountUser() throws Exception {
String accountName = randomAsciiOfLength(10); String accountName = randomAsciiOfLength(10);
@ -159,9 +154,8 @@ public class InternalHipChatServiceTests extends ESTestCase {
settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port); settingsBuilder.put("xpack.notification.hipchat.account." + accountName + ".port", port);
} }
buildMessageDefaults(accountName, settingsBuilder, defaultRoom, defaultUser, null, defaultColor, defaultFormat, defaultNotify); buildMessageDefaults(accountName, settingsBuilder, defaultRoom, defaultUser, null, defaultColor, defaultFormat, defaultNotify);
InternalHipChatService service = new InternalHipChatService(settingsBuilder.build(), httpClient, HipChatService service = new HipChatService(settingsBuilder.build(), httpClient,
new ClusterSettings(settingsBuilder.build(), Collections.singleton(InternalHipChatService.HIPCHAT_ACCOUNT_SETTING))); new ClusterSettings(settingsBuilder.build(), Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING)));
service.start();
HipChatAccount account = service.getAccount(accountName); HipChatAccount account = service.getAccount(accountName);
assertThat(account, notNullValue()); assertThat(account, notNullValue());
@ -221,9 +215,8 @@ public class InternalHipChatServiceTests extends ESTestCase {
buildMessageDefaults(name, settingsBuilder, null, null, null, defaultColor, defaultFormat, defaultNotify); buildMessageDefaults(name, settingsBuilder, null, null, null, defaultColor, defaultFormat, defaultNotify);
} }
InternalHipChatService service = new InternalHipChatService(settingsBuilder.build(), httpClient, HipChatService service = new HipChatService(settingsBuilder.build(), httpClient,
new ClusterSettings(settingsBuilder.build(), Collections.singleton(InternalHipChatService.HIPCHAT_ACCOUNT_SETTING))); new ClusterSettings(settingsBuilder.build(), Collections.singleton(HipChatService.HIPCHAT_ACCOUNT_SETTING)));
service.start();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
String name = "_a" + i; String name = "_a" + i;

View File

@ -17,6 +17,7 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.Callback; import org.elasticsearch.common.util.Callback;
import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentHelper;
@ -588,10 +589,11 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
assertThat("watcher should only run on the elected master node, but it is running on [" + running + "] nodes", running, equalTo(1)); assertThat("watcher should only run on the elected master node, but it is running on [" + running + "] nodes", running, equalTo(1));
} }
public static class NoopEmailService extends AbstractLifecycleComponent implements EmailService { public static class NoopEmailService extends EmailService {
public NoopEmailService() { public NoopEmailService() {
super(Settings.EMPTY); super(Settings.EMPTY, null,
new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
} }
@Override @Override
@ -603,15 +605,6 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) { public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) {
return new EmailSent(accountName, email); return new EmailSent(accountName, email);
} }
@Override
protected void doStart() {}
@Override
protected void doStop() {}
@Override
protected void doClose() {}
} }
protected static class TimeWarp { protected static class TimeWarp {