/** * This method is called when an active alert is ending. * * @param alertingState * the ending alert */ public void alertEnding(AlertingState alertingState) { if (alertingState == null) { throw new IllegalArgumentException("The given alerting state may not be null."); } if (log.isDebugEnabled()) { log.debug("Alert definition '{}' is ending.", alertingState.getAlertingDefinition().getName()); } alertingState.getAlert().close(System.currentTimeMillis(), AlertClosingReason.ALERT_RESOLVED); for (IAlertAction alertAction : alertActions) { alertAction.onEnding(alertingState); } alertingState.setAlert(null); } }
@Test @SuppressWarnings("unchecked") public void alertEnded() { 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); Alert alert = Mockito.mock(Alert.class); AlertingState alertingState = Mockito.mock(AlertingState.class); when(alertingState.getAlert()).thenReturn(alert); long leftBorder = System.currentTimeMillis(); alertingService.alertEnding(alertingState); long rightBorder = System.currentTimeMillis(); ArgumentCaptor<Long> timeCaptor = ArgumentCaptor.forClass(Long.class); ArgumentCaptor<AlertClosingReason> closingReasonCaptor = ArgumentCaptor.forClass(AlertClosingReason.class); verify(alert).close(timeCaptor.capture(), closingReasonCaptor.capture()); assertThat(timeCaptor.getValue(), greaterThanOrEqualTo(leftBorder)); assertThat(timeCaptor.getValue(), lessThanOrEqualTo(rightBorder)); assertThat(closingReasonCaptor.getValue(), is(AlertClosingReason.ALERT_RESOLVED)); verifyNoMoreInteractions(alert); verify(alertingState).getAlert(); verify(alertingState).setAlert(null); verifyNoMoreInteractions(alertingState); verify(alertAction).onEnding(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 public void updateEventAlertActive() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionUpdateEvent(this, definitionOne); when(definitionOne.getId()).thenReturn("id"); Alert alertMock = mock(Alert.class); // set manually because it would done by the threshold checker which is also mocked getAlertingStates().get(0).setAlert(alertMock); alertingScheduler.onApplicationEvent(event); verify(alertMock, times(1)).setAlertingDefinition(definitionOne); verify(definitionOne, times(2)).getId(); verifyNoMoreInteractions(definitionOne); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(1)); }
@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); }
@Test public void deletedEventAlertActive() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); assertThat(getAlertingStates(), hasSize(1)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionDeletedEvent(this, definitionOne); when(definitionOne.getId()).thenReturn("id"); Alert alertMock = mock(Alert.class); // set manually because it would done by the threshold checker which is also mocked getAlertingStates().get(0).setAlert(alertMock); alertingScheduler.onApplicationEvent(event); ArgumentCaptor<AlertClosingReason> reasonCapture = ArgumentCaptor.forClass(AlertClosingReason.class); verify(alertMock, times(1)).close(any(Long.class), reasonCapture.capture()); verify(definitionOne, times(2)).getId(); verifyNoMoreInteractions(definitionOne); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(0)); assertThat(reasonCapture.getValue(), equalTo(AlertClosingReason.ALERTING_DEFINITION_DELETED)); }