/** * {@inheritDoc} */ @Override public void onApplicationEvent(AbstractAlertingDefinitionEvent event) { if (event == null) { return; } switch (event.getType()) { case LOADED: loadedAlertingDefinitions(event); break; case ADDED: createdAlertingDefinition(event); break; case REMOVED: deletedAlertingDefinition(event); break; case UPDATE: updatedAlertingDefinition(event); break; default: break; } }
@Test public void nullEvent() { alertingScheduler.onApplicationEvent(null); verifyZeroInteractions(thresholdChecker, executorService); }
@Test public void noAlertingStates() throws Exception { alertingScheduler.run(); verifyZeroInteractions(thresholdChecker); verifyZeroInteractions(executorService); }
@Test public void thresholdCheckerThrowsException() throws Exception { AlertingDefinition definitionOne = mock(AlertingDefinition.class); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); doThrow(RuntimeException.class).when(thresholdChecker).checkThreshold(any(AlertingState.class)); alertingScheduler.run(); verify(thresholdChecker).checkThreshold(any(AlertingState.class)); verifyNoMoreInteractions(thresholdChecker); verifyZeroInteractions(executorService); } }
@Test public void deactivate() { alertingScheduler.active = false; alertingScheduler.updateState(); verifyZeroInteractions(thresholdChecker); verifyZeroInteractions(executorService); }
@Test public void checkExistingAlertingStates() throws Exception { AlertingDefinition definitionOne = mock(AlertingDefinition.class); AlertingDefinition definitionTwo = mock(AlertingDefinition.class); when(definitionOne.getTimeRange(any(TimeUnit.class))).thenReturn(1L); when(definitionTwo.getTimeRange(any(TimeUnit.class))).thenReturn(3600000L); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionTwo)); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ((AlertingState) invocation.getArguments()[0]).setLastCheckTime(System.currentTimeMillis()); return null; } }).when(thresholdChecker).checkThreshold(any(AlertingState.class)); alertingScheduler.run(); // both are checked Thread.sleep(10); alertingScheduler.run(); // only first is checked ArgumentCaptor<AlertingState> stateCaptor = ArgumentCaptor.forClass(AlertingState.class); verify(thresholdChecker, times(3)).checkThreshold(stateCaptor.capture()); verifyNoMoreInteractions(thresholdChecker); verifyZeroInteractions(executorService); assertThat(stateCaptor.getAllValues().get(0).getAlertingDefinition(), equalTo(definitionOne)); assertThat(stateCaptor.getAllValues().get(1).getAlertingDefinition(), equalTo(definitionTwo)); assertThat(stateCaptor.getAllValues().get(2).getAlertingDefinition(), equalTo(definitionOne)); }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void activate() { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(executorService.scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES)).thenReturn(future); alertingScheduler.active = true; alertingScheduler.updateState(); verify(executorService).scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES); verifyNoMoreInteractions(executorService); verifyZeroInteractions(thresholdChecker); verifyZeroInteractions(future); }
@Test public void deletedEvent() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); assertThat(getAlertingStates(), hasSize(1)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionDeletedEvent(this, definitionOne); when(definitionOne.getId()).thenReturn("id"); alertingScheduler.onApplicationEvent(event); verify(definitionOne, times(2)).getId(); verifyNoMoreInteractions(definitionOne); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(0)); }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void activateWhenInactive() { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(future.isDone()).thenReturn(true); when(executorService.scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES)).thenReturn(future); alertingScheduler.active = true; alertingScheduler.updateState(); alertingScheduler.updateState(); verify(executorService, times(2)).scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES); verifyNoMoreInteractions(executorService); verify(future).isDone(); verifyNoMoreInteractions(future); verifyZeroInteractions(thresholdChecker); }
@Test public void createEvent() { AbstractAlertingDefinitionEvent event = new AlertingDefinitionCreatedEvent(this, definitionOne); alertingScheduler.onApplicationEvent(event); verifyZeroInteractions(thresholdChecker, executorService, definitionOne); assertThat(getAlertingStates(), hasSize(1)); assertThat(getAlertingStates().get(0).getAlertingDefinition(), equalTo(definitionOne)); }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void activateWhenActive() { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(future.isDone()).thenReturn(false); when(executorService.scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES)).thenReturn(future); alertingScheduler.active = true; alertingScheduler.updateState(); alertingScheduler.updateState(); verify(executorService).scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES); verifyNoMoreInteractions(executorService); verify(future).isDone(); verifyNoMoreInteractions(future); verifyZeroInteractions(thresholdChecker); }
@Test public void loadingEvent() { AbstractAlertingDefinitionEvent event = new AlertingDefinitionLoadedEvent(this, Arrays.asList(definitionOne, definitionTwo)); alertingScheduler.onApplicationEvent(event); verifyZeroInteractions(thresholdChecker, executorService, definitionOne, definitionTwo); assertThat(getAlertingStates(), hasSize(2)); assertThat(getAlertingStates().get(0).getAlertingDefinition(), equalTo(definitionOne)); assertThat(getAlertingStates().get(1).getAlertingDefinition(), equalTo(definitionTwo)); }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void deactivateWhenActive() { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(future.isDone()).thenReturn(false); when(executorService.scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES)).thenReturn(future); alertingScheduler.active = true; alertingScheduler.updateState(); alertingScheduler.active = false; alertingScheduler.updateState(); verify(executorService).scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES); verifyNoMoreInteractions(executorService); verify(future).isDone(); verify(future).cancel(false); verifyNoMoreInteractions(future); verifyZeroInteractions(thresholdChecker); }
@Test public void deletedUnknownEvent() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); assertThat(getAlertingStates(), hasSize(1)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionDeletedEvent(this, definitionTwo); when(definitionOne.getId()).thenReturn("id"); alertingScheduler.onApplicationEvent(event); verify(definitionOne).getId(); verify(definitionTwo).getId(); verifyNoMoreInteractions(definitionOne, definitionTwo); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(1)); }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void deactivateWhenInactive() { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(future.isDone()).thenReturn(false, true); when(executorService.scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES)).thenReturn(future); alertingScheduler.active = true; alertingScheduler.updateState(); alertingScheduler.active = false; alertingScheduler.updateState(); alertingScheduler.updateState(); verify(executorService).scheduleAtFixedRate(alertingScheduler, 0L, AlertingScheduler.CHECK_INTERVAL, TimeUnit.MINUTES); verifyNoMoreInteractions(executorService); verify(future, times(2)).isDone(); verify(future).cancel(false); verifyNoMoreInteractions(future); verifyZeroInteractions(thresholdChecker); } }
@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)); }
@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 public void updateEvent() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); assertThat(getAlertingStates().get(0).getAlertingDefinition(), equalTo(definitionOne)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionUpdateEvent(this, definitionTwo); when(definitionOne.getId()).thenReturn("id"); when(definitionTwo.getId()).thenReturn("id"); alertingScheduler.onApplicationEvent(event); verify(definitionOne).getId(); verify(definitionTwo).getId(); verifyNoMoreInteractions(definitionOne, definitionTwo); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(1)); assertThat(getAlertingStates().get(0).getAlertingDefinition(), equalTo(definitionTwo)); }
@Test public void updateUnknownEvent() { alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); AbstractAlertingDefinitionEvent event = new AlertingDefinitionUpdateEvent(this, definitionTwo); when(definitionOne.getId()).thenReturn("id"); when(definitionTwo.getId()).thenReturn("id_2"); alertingScheduler.onApplicationEvent(event); assertThat(getAlertingStates(), hasSize(1)); assertThat(getAlertingStates().get(0).getAlertingDefinition(), equalTo(definitionOne)); verify(definitionOne).getId(); verify(definitionTwo).getId(); verifyNoMoreInteractions(definitionOne, definitionTwo); verifyZeroInteractions(thresholdChecker, executorService); assertThat(getAlertingStates(), hasSize(1)); } }