@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 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)); }
/** * Creates a new {@link AlertingDefinition}. * * @param alertingDefinition * {@link AlertingDefinition} template * @return the created {@link AlertingDefinition} * @throws IOException * if {@link IOException} occurs during create * @throws JAXBException * if {@link JAXBException} occurs during create */ public AlertingDefinition createAlertingDefinition(AlertingDefinition alertingDefinition) throws JAXBException, IOException { alertingDefinition.setId(getRandomUUIDString()); alertingDefinition.setCreatedDate(new Date()); existingAlertingDefinitions.put(alertingDefinition.getId(), alertingDefinition); saveAlertingDefinition(alertingDefinition); eventPublisher.publishEvent(new AbstractAlertingDefinitionEvent.AlertingDefinitionCreatedEvent(this, alertingDefinition)); return alertingDefinition; }
@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)); } }
@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 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 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 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 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 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)); }