Skip to content

Commit

Permalink
Use a list instead of a method and clean test
Browse files Browse the repository at this point in the history
  • Loading branch information
hub-cap committed Jul 3, 2018
1 parent 153a97b commit 7be7b49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin, Reloa
private SetOnce<JiraService> jiraService = new SetOnce<>();
private SetOnce<SlackService> slackService = new SetOnce<>();
private SetOnce<PagerDutyService> pagerDutyService = new SetOnce<>();
List<NotificationService> reloadableServices = new ArrayList<>();

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

// notification
emailService.set(new EmailService(settings, cryptoService, clusterService.getClusterSettings()));
reloadableServices.add(emailService.get());
hipChatService.set(new HipChatService(settings, httpClient, clusterService.getClusterSettings()));
reloadableServices.add(hipChatService.get());
jiraService.set(new JiraService(settings, httpClient, clusterService.getClusterSettings()));
reloadableServices.add(jiraService.get());
slackService.set(new SlackService(settings, httpClient, clusterService.getClusterSettings()));
reloadableServices.add(slackService.get());
pagerDutyService.set(new PagerDutyService(settings, httpClient, clusterService.getClusterSettings()));
reloadableServices.add(pagerDutyService.get());


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

/**
* Used to detect which {@link NotificationService} get reloaded. Also useful for testing the reload in tests.
* @return
*/
protected List<NotificationService> getReloadableServices() {
return Arrays.asList(
emailService.get(),
hipChatService.get(),
jiraService.get(),
slackService.get(),
pagerDutyService.get());
}

/**
* Reloads all reloadable services' settings.
Expand All @@ -648,7 +643,7 @@ protected List<NotificationService> getReloadableServices() {
*/
@Override
public void reload(Settings settings) throws Exception {
for (NotificationService service : getReloadableServices()) {
for (NotificationService service : reloadableServices) {
service.reload(settings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class WatcherPluginTests extends ESTestCase {

Expand Down Expand Up @@ -126,17 +129,13 @@ public void testReload() throws Exception {
.put("xpack.watcher.enabled", false)
.put("path.home", createTempDir())
.build();
TestNotificationService service = new TestNotificationService(settings, "test");
Watcher watcher = new Watcher(settings) {
@Override
protected List<NotificationService> getReloadableServices() {
return Collections.singletonList(service);
}
};

assertFalse(service.calledCreateAccount);
watcher.reload(settings);
assertTrue(service.calledCreateAccount);
NotificationService mockService = mock(NotificationService.class);
Watcher watcher = new Watcher(settings);
watcher.reloadableServices.clear();
watcher.reloadableServices.add(mockService);

verify(mockService, times(0)).reload(settings);
watcher.reload(settings);
verify(mockService, times(1)).reload(settings);
}
}

0 comments on commit 7be7b49

Please sign in to comment.