/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.hydra.job.alert;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import com.addthis.hydra.job.Job;
import com.addthis.hydra.job.JobTask;
import com.addthis.hydra.job.alert.types.OnErrorJobAlert;
import com.addthis.hydra.job.alert.types.RekickTimeoutJobAlert;
import com.addthis.hydra.job.alert.types.RuntimeExceededJobAlert;
import com.addthis.hydra.job.spawn.Spawn;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import static com.addthis.hydra.job.alert.AutoGenerated.BASIC_ALERT;
import static com.addthis.hydra.job.alert.AutoGenerated.BASIC_PAGE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class JobAlertManagerImplTest {
JobAlertManagerImpl impl;
Spawn spawn;
JobAlertRunner runner;
Job job;
@Before public void setUp() throws Exception {
spawn = mock(Spawn.class);
runner = mock(JobAlertRunner.class);
GroupManager groupManager = new GroupManager(Lists.newArrayList(
new Group("nosettings", null, null, null),
new Group("allsettings", "email", "page", "webhook"),
new Group("emailonly", "email", null, null)
));
impl = new JobAlertManagerImpl(groupManager, runner, mock(ScheduledExecutorService.class));
job = new Job("jobid");
job.addTask(new JobTask());
job.setRekickTimeout(30L);
job.setMaxRunTime(30L);
}
private void setupPreviousAlerts() {
when(runner.getAlertsForJob("jobid")).thenReturn(Sets.newHashSet(
new OnErrorJobAlert("error",
null,
0,
null,
null,
Lists.newArrayList("jobid"),
null,
BASIC_ALERT,
0,
null,
null
),
new RekickTimeoutJobAlert("rekick",
null,
0,
0,
null,
null,
Lists.newArrayList("jobid"),
null,
BASIC_ALERT,
0,
null,
null
),
new RuntimeExceededJobAlert("runtime",
null,
0,
0,
null,
null,
Lists.newArrayList("jobid"),
null,
BASIC_ALERT,
0,
null,
null
)
));
}
@Test
public void basicAlerts_groupWithOnlyEmail() throws Exception {
job.setGroup("emailonly");
impl.updateBasicAlerts(job, true, true);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, times(3)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
for (AbstractJobAlert alert : alerts) {
assertSame(BASIC_ALERT, alert.autoGenerated);
assertEquals("email", alert.email);
assertNull(alert.webhookURL);
}
assertTrue(job.getBasicAlerts());
}
@Test public void basicAlerts_groupWithNoSettings() throws Exception {
job.setGroup("nosettings");
impl.updateBasicAlerts(job, true, true);
verify(runner, never()).putAlert(anyString(), any());
assertFalse(job.getBasicAlerts());
assertFalse(job.getBasicPages());
}
@Test public void basicAlerts_groupWithAllSettings() throws Exception {
job.setGroup("allsettings");
impl.updateBasicAlerts(job, true, true);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, times(6)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
int emails = 0;
int pages = 0;
for (AbstractJobAlert alert : alerts) {
if ("email".equals(alert.email) && "webhook".equals(alert.webhookURL) &&
(alert.autoGenerated == BASIC_ALERT)) emails++;
if ("page".equals(alert.email) && (alert.autoGenerated == BASIC_PAGE)) pages++;
}
assertEquals(3, emails);
assertEquals(3, pages);
assertTrue(job.getBasicAlerts());
assertTrue(job.getBasicPages());
}
@Test public void basicAlerts_noGroup() throws Exception {
job.setGroup("nogroup");
impl.updateBasicAlerts(job, true, true);
verify(runner, never()).putAlert(anyString(), any());
assertFalse(job.getBasicAlerts());
assertFalse(job.getBasicPages());
}
@Test public void basicAlerts_noJobSettings() throws Exception {
job.setGroup("email");
impl.updateBasicAlerts(job, true, true);
impl = new JobAlertManagerImpl(new GroupManager(Lists.newArrayList()), runner, mock(ScheduledExecutorService.class));
verify(runner, never()).putAlert(anyString(), any());
assertFalse(job.getBasicAlerts());
assertFalse(job.getBasicPages());
}
@Test public void basicAlerts_create() throws Exception {
job.setGroup("allsettings");
impl.updateBasicAlerts(job, true, true);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, times(6)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
int emails = 0;
int pages = 0;
for (AbstractJobAlert alert : alerts) {
if ("email".equals(alert.email) && "webhook".equals(alert.webhookURL) &&
(alert.autoGenerated == BASIC_ALERT)) emails++;
if ("page".equals(alert.email) && (alert.autoGenerated == BASIC_PAGE)) pages++;
}
assertEquals(3, emails);
assertEquals(3, pages);
}
@Test public void basicAlerts_update() throws Exception {
setupPreviousAlerts();
job.setGroup("allsettings");
impl.updateBasicAlerts(job, true, true);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner).putAlert(Matchers.eq("error"), any(OnErrorJobAlert.class));
verify(runner).putAlert(Matchers.eq("runtime"), any(RuntimeExceededJobAlert.class));
verify(runner).putAlert(Matchers.eq("rekick"), any(RekickTimeoutJobAlert.class));
verify(runner, times(6)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
int emails = 0;
int pages = 0;
for (AbstractJobAlert alert : alerts) {
if ("email".equals(alert.email) && "webhook".equals(alert.webhookURL) &&
(alert.autoGenerated == BASIC_ALERT)) emails++;
if ("page".equals(alert.email) && (alert.autoGenerated == BASIC_PAGE)) pages++;
}
assertEquals(3, emails);
assertEquals(3, pages);
}
@Test public void basicAlerts_delete() throws Exception {
setupPreviousAlerts();
job.setGroup("allsettings");
job.setBasicAlerts(true);
job.setBasicPages(false);
impl.updateBasicAlerts(job, false, false);
verify(runner).removeAlert("error");
verify(runner).removeAlert("runtime");
verify(runner).removeAlert("rekick");
}
@Test public void basicAlerts_onlyBasic() throws Exception {
job.setGroup("allsettings");
impl.updateBasicAlerts(job, true, false);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, times(3)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
for (AbstractJobAlert alert : alerts) {
assertEquals("email", alert.email);
assertEquals("webhook", alert.webhookURL);
assertEquals(BASIC_ALERT, alert.autoGenerated);
}
}
@Test public void basicAlerts_onlyPage() throws Exception {
job.setGroup("allsettings");
impl.updateBasicAlerts(job, false, true);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, times(3)).putAlert(anyString(), captor.capture());
List<AbstractJobAlert> alerts = captor.getAllValues();
for (AbstractJobAlert alert : alerts) {
assertEquals("page", alert.email);
assertEquals(BASIC_PAGE, alert.autoGenerated);
}
}
@Test public void basicAlerts_noop() throws Exception {
job.setGroup("allsettings");
impl.updateBasicAlerts(job, false, false);
ArgumentCaptor<AbstractJobAlert> captor = ArgumentCaptor.forClass(AbstractJobAlert.class);
verify(runner, never()).putAlert(anyString(), captor.capture());
}
@Test public void basicAlerts_removeRuntimeAlert() throws Exception {
setupPreviousAlerts();
job.setGroup("allsettings");
job.setMaxRunTime(0L);
impl.updateBasicAlerts(job, true, false);
verify(runner, times(2)).putAlert(anyString(), any());
verify(runner, times(1)).removeAlert("runtime");
}
@Test public void basicAlerts_removeRekickAlert() throws Exception {
setupPreviousAlerts();
job.setGroup("allsettings");
job.setRekickTimeout(0L);
impl.updateBasicAlerts(job, true, false);
verify(runner, times(2)).putAlert(anyString(), any());
verify(runner, times(1)).removeAlert("rekick");
}
}