-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better late than never for some CheckRunner tests
- Loading branch information
1 parent
f94dd3f
commit b4500d2
Showing
1 changed file
with
306 additions
and
0 deletions.
There are no files selected for viewing
306 changes: 306 additions & 0 deletions
306
seyren-core/src/test/java/com/seyren/core/service/schedule/CheckRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
package com.seyren.core.service.schedule; | ||
|
||
import static org.mockito.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.joda.time.DateTime; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import com.google.common.base.Optional; | ||
import com.seyren.core.domain.Alert; | ||
import com.seyren.core.domain.AlertType; | ||
import com.seyren.core.domain.Check; | ||
import com.seyren.core.domain.Subscription; | ||
import com.seyren.core.domain.SubscriptionType; | ||
import com.seyren.core.exception.NotificationFailedException; | ||
import com.seyren.core.service.checker.TargetChecker; | ||
import com.seyren.core.service.checker.ValueChecker; | ||
import com.seyren.core.service.notification.NotificationService; | ||
import com.seyren.core.store.AlertsStore; | ||
import com.seyren.core.store.ChecksStore; | ||
|
||
public class CheckRunnerTest { | ||
|
||
private Check mockCheck; | ||
private AlertsStore mockAlertsStore; | ||
private ChecksStore mockChecksStore; | ||
private TargetChecker mockTargetChecker; | ||
private ValueChecker mockValueChecker; | ||
private NotificationService mockNotificationService; | ||
private Iterable<NotificationService> mockNotificationServices; | ||
private CheckRunner checkRunner; | ||
|
||
@Before | ||
public void before() { | ||
mockCheck = mock(Check.class); | ||
mockAlertsStore = mock(AlertsStore.class); | ||
mockChecksStore = mock(ChecksStore.class); | ||
mockTargetChecker = mock(TargetChecker.class); | ||
mockValueChecker = mock(ValueChecker.class); | ||
mockNotificationService = mock(NotificationService.class); | ||
mockNotificationServices = Arrays.asList(mockNotificationService); | ||
checkRunner = new CheckRunner( | ||
mockCheck, | ||
mockAlertsStore, | ||
mockChecksStore, | ||
mockTargetChecker, | ||
mockValueChecker, | ||
mockNotificationServices); | ||
} | ||
|
||
@Test | ||
public void checkWhichIsNotEnabledDoesNothing() { | ||
when(mockCheck.isEnabled()).thenReturn(false); | ||
checkRunner.run(); | ||
} | ||
|
||
@Test | ||
public void noTargetValuesDoesNothing() throws Exception { | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(new HashMap<String, Optional<BigDecimal>>()); | ||
checkRunner.run(); | ||
} | ||
|
||
@Test | ||
public void anExceptionWhileRunningIsHandled() throws Exception { | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockTargetChecker.check(mockCheck)).thenThrow(new Exception("Boom!")); | ||
checkRunner.run(); | ||
} | ||
|
||
@Test | ||
public void emptyTargetValueDoesNothing() throws Exception { | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.<BigDecimal>absent()); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
checkRunner.run(); | ||
} | ||
|
||
@Test | ||
public void noPreviousAlertAndHappyCurrentValueDoesNothing() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(null); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.OK); | ||
checkRunner.run(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void createAlertButDoNotNotifyIfStateIsTheSame() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.WARN); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.WARN), any(DateTime.class))).thenReturn(mockCheck); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
verify(mockNotificationService, times(0)).sendNotification(any(Check.class), any(Subscription.class), any(List.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void changeOfStateButNoSubscriptionsDoesNothing() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
when(mockCheck.getSubscriptions()).thenReturn(new ArrayList<Subscription>()); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.ERROR); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.ERROR), any(DateTime.class))).thenReturn(mockCheck); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
verify(mockNotificationService, times(0)).sendNotification(any(Check.class), any(Subscription.class), any(List.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void changeOfStateButSubscriptionWhichShouldNotNotifyDoesNothing() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
Subscription mockSubscription = mock(Subscription.class); | ||
when(mockSubscription.getType()).thenReturn(SubscriptionType.EMAIL); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
when(mockCheck.getSubscriptions()).thenReturn(Arrays.asList(mockSubscription)); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.ERROR); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.ERROR), any(DateTime.class))).thenReturn(mockCheck); | ||
when(mockSubscription.shouldNotify(any(DateTime.class), eq(AlertType.ERROR))).thenReturn(false); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
verify(mockNotificationService, times(0)).sendNotification(any(Check.class), any(Subscription.class), any(List.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void changeOfStateAndSubscriptionWhichShouldNotifyAndCannotHandleDoesNothing() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
Subscription mockSubscription = mock(Subscription.class); | ||
when(mockSubscription.getType()).thenReturn(SubscriptionType.EMAIL); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
when(mockCheck.getSubscriptions()).thenReturn(Arrays.asList(mockSubscription)); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.ERROR); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.ERROR), any(DateTime.class))).thenReturn(mockCheck); | ||
when(mockSubscription.shouldNotify(any(DateTime.class), eq(AlertType.ERROR))).thenReturn(true); | ||
when(mockNotificationService.canHandle(SubscriptionType.EMAIL)).thenReturn(false); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
verify(mockNotificationService, times(0)).sendNotification(eq(mockCheck), eq(mockSubscription), any(List.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void changeOfStateAndSubscriptionWhichShouldNotifyAndCanHandleSendsNotification() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
Subscription mockSubscription = mock(Subscription.class); | ||
when(mockSubscription.getType()).thenReturn(SubscriptionType.EMAIL); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
when(mockCheck.getSubscriptions()).thenReturn(Arrays.asList(mockSubscription)); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.ERROR); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.ERROR), any(DateTime.class))).thenReturn(mockCheck); | ||
when(mockSubscription.shouldNotify(any(DateTime.class), eq(AlertType.ERROR))).thenReturn(true); | ||
when(mockNotificationService.canHandle(SubscriptionType.EMAIL)).thenReturn(true); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
verify(mockNotificationService).sendNotification(eq(mockCheck), eq(mockSubscription), any(List.class)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void exceptionWhileSendingNotificationIsHandled() throws Exception { | ||
BigDecimal value = BigDecimal.ONE; | ||
BigDecimal warn = BigDecimal.valueOf(2); | ||
BigDecimal error = BigDecimal.valueOf(3); | ||
|
||
Subscription mockSubscription = mock(Subscription.class); | ||
when(mockSubscription.getType()).thenReturn(SubscriptionType.EMAIL); | ||
|
||
when(mockCheck.getId()).thenReturn("id"); | ||
when(mockCheck.isEnabled()).thenReturn(true); | ||
when(mockCheck.getWarn()).thenReturn(warn); | ||
when(mockCheck.getError()).thenReturn(error); | ||
when(mockCheck.getSubscriptions()).thenReturn(Arrays.asList(mockSubscription)); | ||
|
||
Map<String, Optional<BigDecimal>> targetValues = new HashMap<String, Optional<BigDecimal>>(); | ||
targetValues.put("target", Optional.of(value)); | ||
when(mockTargetChecker.check(mockCheck)).thenReturn(targetValues); | ||
when(mockAlertsStore.getLastAlertForTargetOfCheck("target", "id")).thenReturn(new Alert().withToType(AlertType.WARN)); | ||
when(mockValueChecker.checkValue(value, warn, error)).thenReturn(AlertType.ERROR); | ||
|
||
Alert alert = new Alert(); | ||
|
||
when(mockAlertsStore.createAlert(eq("id"), any(Alert.class))).thenReturn(alert); | ||
when(mockChecksStore.updateStateAndLastCheck(eq("id"), eq(AlertType.ERROR), any(DateTime.class))).thenReturn(mockCheck); | ||
when(mockSubscription.shouldNotify(any(DateTime.class), eq(AlertType.ERROR))).thenReturn(true); | ||
when(mockNotificationService.canHandle(SubscriptionType.EMAIL)).thenReturn(true); | ||
Mockito.doThrow(new NotificationFailedException("Boom!")).when(mockNotificationService).sendNotification(eq(mockCheck), eq(mockSubscription), any(List.class)); | ||
|
||
checkRunner.run(); | ||
|
||
verify(mockAlertsStore).createAlert(eq("id"), any(Alert.class)); | ||
} | ||
|
||
} |