Skip to content

Commit 7be7b49

Browse files
committed
Use a list instead of a method and clean test
1 parent 153a97b commit 7be7b49

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin, Reloa
229229
private SetOnce<JiraService> jiraService = new SetOnce<>();
230230
private SetOnce<SlackService> slackService = new SetOnce<>();
231231
private SetOnce<PagerDutyService> pagerDutyService = new SetOnce<>();
232+
List<NotificationService> reloadableServices = new ArrayList<>();
232233

233234
public Watcher(final Settings settings) {
234235
this.settings = settings;
@@ -278,10 +279,16 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
278279

279280
// notification
280281
emailService.set(new EmailService(settings, cryptoService, clusterService.getClusterSettings()));
282+
reloadableServices.add(emailService.get());
281283
hipChatService.set(new HipChatService(settings, httpClient, clusterService.getClusterSettings()));
284+
reloadableServices.add(hipChatService.get());
282285
jiraService.set(new JiraService(settings, httpClient, clusterService.getClusterSettings()));
286+
reloadableServices.add(jiraService.get());
283287
slackService.set(new SlackService(settings, httpClient, clusterService.getClusterSettings()));
288+
reloadableServices.add(slackService.get());
284289
pagerDutyService.set(new PagerDutyService(settings, httpClient, clusterService.getClusterSettings()));
290+
reloadableServices.add(pagerDutyService.get());
291+
285292

286293
TextTemplateEngine templateEngine = new TextTemplateEngine(settings, scriptService);
287294
Map<String, EmailAttachmentParser> emailAttachmentParsers = new HashMap<>();
@@ -624,18 +631,6 @@ public void close() throws IOException {
624631
IOUtils.closeWhileHandlingException(httpClient);
625632
}
626633

627-
/**
628-
* Used to detect which {@link NotificationService} get reloaded. Also useful for testing the reload in tests.
629-
* @return
630-
*/
631-
protected List<NotificationService> getReloadableServices() {
632-
return Arrays.asList(
633-
emailService.get(),
634-
hipChatService.get(),
635-
jiraService.get(),
636-
slackService.get(),
637-
pagerDutyService.get());
638-
}
639634

640635
/**
641636
* Reloads all reloadable services' settings.
@@ -648,7 +643,7 @@ protected List<NotificationService> getReloadableServices() {
648643
*/
649644
@Override
650645
public void reload(Settings settings) throws Exception {
651-
for (NotificationService service : getReloadableServices()) {
646+
for (NotificationService service : reloadableServices) {
652647
service.reload(settings);
653648
}
654649
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import static org.hamcrest.Matchers.containsString;
2626
import static org.hamcrest.Matchers.hasSize;
2727
import static org.hamcrest.Matchers.is;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.times;
30+
import static org.mockito.Mockito.verify;
2831

2932
public class WatcherPluginTests extends ESTestCase {
3033

@@ -126,17 +129,13 @@ public void testReload() throws Exception {
126129
.put("xpack.watcher.enabled", false)
127130
.put("path.home", createTempDir())
128131
.build();
129-
TestNotificationService service = new TestNotificationService(settings, "test");
130-
Watcher watcher = new Watcher(settings) {
131-
@Override
132-
protected List<NotificationService> getReloadableServices() {
133-
return Collections.singletonList(service);
134-
}
135-
};
136-
137-
assertFalse(service.calledCreateAccount);
138-
watcher.reload(settings);
139-
assertTrue(service.calledCreateAccount);
132+
NotificationService mockService = mock(NotificationService.class);
133+
Watcher watcher = new Watcher(settings);
134+
watcher.reloadableServices.clear();
135+
watcher.reloadableServices.add(mockService);
140136

137+
verify(mockService, times(0)).reload(settings);
138+
watcher.reload(settings);
139+
verify(mockService, times(1)).reload(settings);
141140
}
142141
}

0 commit comments

Comments
 (0)