DefaultAlertService extends DefaultJPAService implements AlertService { @Override public void testEvaluateAlert(String serializedAlert, Long when, String testUuid) { requireNotDisposed(); assert(serializedAlert != null && serializedAlert.length() > 0); assert(testUuid != null && testUuid.length() > 0); assert(when != 0L); AlertTestResults results = new AlertTestResults(testUuid); Alert alert; _logger.debug(MessageFormat.format("historicalTest {2} evaluationTime {1} serializedAlert {0}", serializedAlert, when, testUuid)); try { alert = _mapper.readValue(serializedAlert, Alert.class); } catch (Exception e) { String logMessage = MessageFormat.format("Failed to deserialize alert {0}. Full stack trace of exception {1}", serializedAlert, ExceptionUtils.getFullStackTrace(e)); _logger.warn(logMessage); return; } try { if (alert != null) { testEvaluateAlert(alert, when, results); } } finally { } } @Inject  DefaultAlertService(SystemConfiguration configuration, MQService mqService, MetricService metricService,
							   AuditService auditService, TSDBService tsdbService, MailService mailService, HistoryService historyService,
							   MonitorService monitorService, ImageDataRetriever imageDataRetriever, NotifierFactory notifierFactory, Provider<EntityManager> emProvider); @Override @Transactional Alert updateAlert(Alert alert); @Override @Transactional void deleteAlert(String name, PrincipalUser owner); @Override @Transactional void deleteAlert(Alert alert); @Override @Transactional void markAlertForDeletion(String name, PrincipalUser owner); @Override @Transactional void markAlertForDeletion(Alert alert); @Override List<Alert> findAlertsMarkedForDeletion(); @Override List<Alert> findAlertsMarkedForDeletion(final int limit); @Override List<Alert> findAlertsByOwner(PrincipalUser owner, boolean metadataOnly); @Override List<Alert> findAlertsByOwnerPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override Alert findAlertByPrimaryKey(BigInteger id); @Override List<Alert> findAlertsByPrimaryKeys(List<BigInteger> ids); @Override void updateNotificationsActiveStatusAndCooldown(List<Notification> notifications); @Override @Transactional Integer executeScheduledAlerts(int alertCount, int timeout); void _processNotification(INotificationHandler notificationHandler,
									 Alert alert,
									 History history,
									 List<Metric> metrics,
									 Map<BigInteger, Map<Metric, Long>> triggerFiredTimesAndMetricsByTrigger,
									 Notification notification,
									 Long alertEnqueueTimestamp); void _processTriggerlessNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												List<Metric> metrics,
												Notification notification,
												Long alertEnqueueTimestamp); void _processMissingDataNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												Set<Trigger> triggers,
												Notification notification,
												boolean isDataMissing,
												Long alertEnqueueTimestamp); void sendNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert,
			Long triggerFiredTime, Long alertEnqueueTime, String action); void sendClearNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert, Long alertEnqueueTime, String action); @Override @Transactional Alert findAlertByNameAndOwner(String name, PrincipalUser owner); @Override void enqueueAlerts(List<Alert> alerts); @Override // IMPORTANT - @Transactional ?? - should not be as it shouldn't talk to the DB void testEvaluateAlert(String serializedAlert, Long when, String testUuid); @VisibleForTesting boolean testEvaluateAlert(Alert alert, Long alertEvaluationTime, AlertTestResults testResults); @Override List<Alert> findAllAlerts(boolean metadataOnly); @Override List<Alert> findAlertsByStatus(boolean enabled); @Override List<BigInteger> findAlertIdsByStatus(boolean enabled); @Override List<Alert> findAlertsByRangeAndStatus(BigInteger fromId, BigInteger toId, boolean enabled); @Override List<Alert> findAlertsModifiedAfterDate(Date modifiedDate); @Override int alertCountByStatus(boolean enabled); @Override List<Alert> findAlertsByLimitOffsetStatus(int limit, int offset,
			boolean enabled); @Override List<Alert> findAlertsByNameWithPrefix(String prefix); @Override List<String> getSupportedNotifiers(); @Override List<Alert> findSharedAlerts(boolean metadataOnly, PrincipalUser owner, Integer limit); @Override List<Alert> findSharedAlertsPaged(Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override List<Alert> findPrivateAlertsForPrivilegedUserPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText,
		String sortField, String sortOrder); @Override int countAlerts(AlertsCountContext context); @Override Notifier getNotifier(SupportedNotifier notifier); @Override void dispose(); Long getTriggerFiredDatapointTime(Trigger trigger, Metric metric, String queryExpression, Long alertEnqueueTimestamp); Long getLatestDatapointTime(Metric metric, String queryExpression, Long alertEnqueueTimestamp); @Override @Transactional void deleteTrigger(Trigger trigger); @Override @Transactional void deleteNotification(Notification notification); @Override void exportMetric(Metric metric, Double value); @Override void updateCounter(Counter counter, Double value);  }
@Test public void testAlertsHistorical_WithWhiteListedUserWithMetricDataWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findDefaultUser(), userService.findDefaultUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.NO_DATA, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); AlertTestResults testResults = new AlertTestResults("myUuid"); spyAlertService.testEvaluateAlert( alert, 3010L, testResults); enableDatalagMonitoring(false); }