AlertDefinitionsCache { public static List<Alert> getEnabledAlertsForMinute(long minuteStartTimeMillis) { List<Alert> enabledAlerts = new ArrayList<Alert>(); List<BigInteger> enabledAlertIds = new ArrayList<BigInteger>(); for (String cronEntry : alertsMapByCronEntry.keySet()) { try { Date minuteStartTime = new Date(minuteStartTimeMillis); String quartzCronEntry = Cron.convertToQuartzCronEntry(cronEntry); Date previousMinuteLastSecondTime = new Date(minuteStartTimeMillis - 1000); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(quartzCronEntry)).startAt(previousMinuteLastSecondTime).build(); Date nextFireTime = cronTrigger.getFireTimeAfter(previousMinuteLastSecondTime); if (nextFireTime.equals(minuteStartTime)) { enabledAlertIds.addAll(alertsMapByCronEntry.get(cronEntry)); } } catch (Exception e) { _logger.error("Exception occured when trying to parse cron entry - " + cronEntry + " Exception - " + ExceptionUtils.getFullStackTrace(e)); } } Collections.sort(enabledAlertIds); for (BigInteger alertId : enabledAlertIds) { Alert a = alertsMapById.get(alertId); if (a != null) { enabledAlerts.add(alertsMapById.get(alertId)); } } return enabledAlerts; } AlertDefinitionsCache(AlertService alertService);  AlertDefinitionsCache(AlertService alertService, boolean refreshThreadDaemonized);  AlertDefinitionsCache(AlertDefinitionsCacheRefresherThread refresherThread); Map<BigInteger, Alert> getAlertsMapById(); void setAlertsMapById(Map<BigInteger, Alert> alertsMapById); Map<String, List<BigInteger>> getAlertsMapByCronEntry(); void setAlertsMapByCronEntry(Map<String, List<BigInteger>> alertsMapByCronEntry); boolean isAlertsCacheInitialized(); void setAlertsCacheInitialized(boolean alertsCacheInitialized); static List<Alert> getEnabledAlertsForMinute(long minuteStartTimeMillis);  }
@Test public void testGetEnabledAlertsForMinute(){ List<BigInteger> alertsIDList1 = new ArrayList<>(); alertsIDList1.add(new BigInteger("1")); alertsIDList1.add(new BigInteger("2")); Map<String, List<BigInteger>> alertMapByCronEntry = new HashMap<>(); alertMapByCronEntry.put("* * * * *", alertsIDList1); alertDefinitionsCache.setAlertsMapByCronEntry(alertMapByCronEntry); Map<BigInteger, Alert> alertMapById = new HashMap<>(); Alert alert1 = new Alert(userService.findAdminUser(), admin, "testAlert1", "COUNT(-1h:scope:metric:avg, -1h:scope:metric:avg, -1h:scope:metric:avg)", "* * * * *"); alertMapById.put(new BigInteger("1"), alert1); Alert alert2 = new Alert(userService.findAdminUser(), admin, "testAlert2", "COUNT(-1h:scope:metric:avg, -1h:scope:metric:avg, -1h:scope:metric:avg)", "* * * * *"); alertMapById.put(new BigInteger("2"), alert2); alertDefinitionsCache.setAlertsMapById(alertMapById); Long timeInMillis = new Date().getTime(); Long flooredMinuteInMillis = timeInMillis - (timeInMillis % MILLISECONDS_PER_MINUTE); List<Alert> alerts = alertDefinitionsCache.getEnabledAlertsForMinute(flooredMinuteInMillis); assertTrue(alerts.size() == 2); }