DefaultAlertService extends DefaultJPAService implements AlertService { @VisibleForTesting protected void updateRequestContext(Alert alert) { RequestContextHolder.setRequestContext(new RequestContext(alert.getOwner().getUserName() + "-alert")); } @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 updateRequestContext_test() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "alert-name_test", EXPRESSION, "* * * * *"); alertService.updateRequestContext(alert); assertNotNull(RequestContextHolder.getRequestContext()); assertEquals(userService.findAdminUser().getUserName() + "-alert", RequestContextHolder.getRequestContext().getUserName()); }