/** * {@inheritDoc} */ @Override public Alert getAlert(String alertId) { return alertRegistry.getAlert(alertId); }
/** * {@inheritDoc} */ @Override public List<Alert> getAlerts() { return alertRegistry.getAlerts(); }
/** * {@inheritDoc} */ @Override public List<Alert> getBusinessTransactionAlerts() { return alertRegistry.getBusinessTransactionAlerts(); }
@Test public void registerSuccessful() { Alert testAlert = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); verify(testAlert, times(2)).getId(); verify(testAlert).getAlertingDefinition(); verifyNoMoreInteractions(testAlert); assertThat(alertRegistry.getAlert("id"), equalTo(testAlert)); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void nullAlert() { Alert alert = null; alertRegistry.registerAlert(alert); }
@Test public void getAlerts() { Alert alertOne = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); Alert alertTwo = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(alertOne.getId()).thenReturn("id_1"); when(alertTwo.getId()).thenReturn("id_2"); alertRegistry.registerAlert(alertOne); alertRegistry.registerAlert(alertTwo); List<Alert> alerts = alertRegistry.getAlerts(); verify(alertOne, times(2)).getId(); verify(alertOne).getAlertingDefinition(); verify(alertTwo, times(2)).getId(); verify(alertTwo).getAlertingDefinition(); verifyNoMoreInteractions(alertOne, alertTwo); assertThat(alerts, hasSize(2)); assertThat(alerts, hasItems(alertOne, alertTwo)); } }
@Test public void getOnlyBTAlerts() { AlertingDefinition definitionOne = Mockito.mock(AlertingDefinition.class); AlertingDefinition definitionTwo = Mockito.mock(AlertingDefinition.class); when(definitionOne.getMeasurement()).thenReturn(Series.BusinessTransaction.NAME); when(definitionOne.getField()).thenReturn(Series.BusinessTransaction.FIELD_DURATION); Alert alertOne = Mockito.mock(Alert.class); Alert alertTwo = Mockito.mock(Alert.class); when(alertOne.getId()).thenReturn("id_1"); when(alertTwo.getId()).thenReturn("id_2"); when(alertOne.getAlertingDefinition()).thenReturn(definitionOne); when(alertTwo.getAlertingDefinition()).thenReturn(definitionTwo); alertRegistry.registerAlert(alertOne); alertRegistry.registerAlert(alertTwo); verify(alertOne, times(2)).getId(); verify(alertOne).getAlertingDefinition(); verify(alertTwo, times(2)).getId(); verify(alertTwo).getAlertingDefinition(); verifyNoMoreInteractions(alertOne, alertTwo); List<Alert> alerts = alertRegistry.getBusinessTransactionAlerts(); assertThat(alerts, hasSize(1)); assertThat(alerts, hasItems(alertOne)); } }
@Test public void getAlert() { Alert testAlert = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); Alert alert = alertRegistry.getAlert("id"); verify(testAlert, times(2)).getId(); verify(testAlert).getAlertingDefinition(); verifyNoMoreInteractions(testAlert); assertThat(alert, equalTo(testAlert)); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void alertIdIsNull() throws Exception { Alert testAlert = Mockito.mock(Alert.class); alertRegistry.registerAlert(testAlert); }
@Test public void replaceAlert() throws Exception { Alert alertOne = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(alertOne.getId()).thenReturn("id"); Alert alertTwo = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(alertTwo.getId()).thenReturn("id"); alertRegistry.registerAlert(alertOne); alertRegistry.registerAlert(alertTwo); verify(alertOne, times(2)).getId(); verify(alertOne).getAlertingDefinition(); verify(alertTwo, times(2)).getId(); verify(alertTwo).getAlertingDefinition(); verifyNoMoreInteractions(alertOne, alertTwo); assertThat(alertRegistry.getAlert("id"), equalTo(alertTwo)); } }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void alertAlertingDefinitionIsNull() throws Exception { Alert testAlert = Mockito.mock(Alert.class); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); }
@Test public void getByNull() { Alert alert = alertRegistry.getAlert(null); assertThat(alert, is(nullValue())); } }
/** * Returns all business transaction alerts in the registry. * * @return Returns all business transaction alerts in the registry. */ public List<Alert> getBusinessTransactionAlerts() { List<Alert> resultList = new ArrayList<>(); for (Alert alert : getAlerts()) { if (AlertingUtils.isBusinessTransactionAlert(alert.getAlertingDefinition())) { resultList.add(alert); } } return resultList; } }
/** * 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 getUnknownAlert() { Alert alert = alertRegistry.getAlert("unknown-id"); assertThat(alert, is(nullValue())); }
@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); }
throw new BusinessException("Retrieving invocation sequences for alert with id '" + alertId + "'", AlertErrorCodeEnum.DATABASE_OFFLINE); Alert alert = alertRegistry.getAlert(alertId); if (null == alert) { throw new BusinessException("Retrieving invocation sequences for alert with id '" + alertId + "'", AlertErrorCodeEnum.UNKNOWN_ALERT_ID);