/** * This method is called when an active alert is still violating its threshold. * * @param alertingState * the ongoing alert * @param violationValue * the value has violated the threshold */ public void alertOngoing(AlertingState alertingState, double violationValue) { if (alertingState == null) { throw new IllegalArgumentException("The given alerting state may not be null."); } if (log.isDebugEnabled()) { log.debug("Alert definition '{}' is ongoing.", alertingState.getAlertingDefinition().getName()); } if (alertingState.getAlertingDefinition().getThresholdType() == ThresholdType.UPPER_THRESHOLD) { alertingState.setExtremeValue(Math.max(violationValue, alertingState.getExtremeValue())); } else { alertingState.setExtremeValue(Math.min(violationValue, alertingState.getExtremeValue())); } for (IAlertAction alertAction : alertActions) { alertAction.onOngoing(alertingState); } }
@Test @SuppressWarnings("unchecked") public void upperThreshold() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingDefinition.getThresholdType()).thenReturn(ThresholdType.UPPER_THRESHOLD); AlertingState alertingState = Mockito.mock(AlertingState.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); when(alertingState.getExtremeValue()).thenReturn(5D); alertingService.alertOngoing(alertingState, 3D); verify(alertingState).setExtremeValue(5D); verify(alertingState).getExtremeValue(); verify(alertingState).getAlertingDefinition(); verifyNoMoreInteractions(alertingState); verify(alertAction).onOngoing(alertingState); verifyNoMoreInteractions(alertAction); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verifyZeroInteractions(alertRegistry); }
/** * This method is called when a new alert is started. * * @param alertingState * the started alert * @param violationValue * the value has violated the threshold */ public void alertStarting(AlertingState alertingState, double violationValue) { if (alertingState == null) { throw new IllegalArgumentException("The given alerting state may not be null."); } Alert alert = new Alert(alertingState.getAlertingDefinition(), alertingState.getLastCheckTime()); alertRegistry.registerAlert(alert); alertingState.setAlert(alert); alertingState.setExtremeValue(violationValue); for (IAlertAction alertAction : alertActions) { alertAction.onStarting(alertingState); } }
@Test @SuppressWarnings("unchecked") public void upperThresholdNewExtremeValue() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingDefinition.getThresholdType()).thenReturn(ThresholdType.UPPER_THRESHOLD); AlertingState alertingState = Mockito.mock(AlertingState.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); when(alertingState.getExtremeValue()).thenReturn(5D); alertingService.alertOngoing(alertingState, 10D); verify(alertingState).setExtremeValue(10D); verify(alertingState).getExtremeValue(); verify(alertingState).getAlertingDefinition(); verifyNoMoreInteractions(alertingState); verify(alertAction).onOngoing(alertingState); verifyNoMoreInteractions(alertAction); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verifyZeroInteractions(alertRegistry); }
@Test @SuppressWarnings("unchecked") public void lowerThreshold() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingDefinition.getThresholdType()).thenReturn(ThresholdType.LOWER_THRESHOLD); AlertingState alertingState = Mockito.mock(AlertingState.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); when(alertingState.getExtremeValue()).thenReturn(5D); alertingService.alertOngoing(alertingState, 10D); verify(alertingState).setExtremeValue(5D); verify(alertingState).getExtremeValue(); verify(alertingState).getAlertingDefinition(); verifyNoMoreInteractions(alertingState); verify(alertAction).onOngoing(alertingState); verifyNoMoreInteractions(alertAction); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verifyZeroInteractions(alertRegistry); }
@Test @SuppressWarnings("unchecked") public void lowerThresholdNewExtremeValue() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingDefinition.getThresholdType()).thenReturn(ThresholdType.LOWER_THRESHOLD); AlertingState alertingState = Mockito.mock(AlertingState.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); when(alertingState.getExtremeValue()).thenReturn(5D); alertingService.alertOngoing(alertingState, 3D); verify(alertingState).setExtremeValue(3D); verify(alertingState).getExtremeValue(); verify(alertingState).getAlertingDefinition(); verifyNoMoreInteractions(alertingState); verify(alertAction).onOngoing(alertingState); verifyNoMoreInteractions(alertAction); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verifyZeroInteractions(alertRegistry); }
@Test @SuppressWarnings("unchecked") public void startAlert() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingState alertingState = Mockito.mock(AlertingState.class); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); alertingService.alertStarting(alertingState, 1.0D); ArgumentCaptor<Alert> captor = ArgumentCaptor.forClass(Alert.class); verify(alertRegistry).registerAlert(captor.capture()); verifyNoMoreInteractions(alertRegistry); assertThat(captor.getValue().getAlertingDefinition(), is(alertingDefinition)); assertThat(captor.getValue().getStartTimestamp(), is(1234L)); verify(alertAction).onStarting(alertingState); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verify(alertingState).getAlertingDefinition(); verify(alertingState).getLastCheckTime(); verify(alertingState).setAlert(any(Alert.class)); verify(alertingState).setExtremeValue(1.0D); verifyNoMoreInteractions(alertingState); }