AlertDefinitionsCacheRefresherThread extends Thread { void initializeAlertDefinitionsCache() { List<Alert> enabledAlerts = alertService.findAlertsByStatus(true); Map<BigInteger, Alert> enabledValidAlertsMap = enabledAlerts.stream(). filter(this::checkIsValidAlert). collect(Collectors.toMap(alert -> alert.getId(), alert -> alert)); for (Alert a : enabledAlerts) { if (enabledValidAlertsMap.containsKey(a.getId())) { addEntrytoCronMap(a); } } alertDefinitionsCache.setAlertsMapById(enabledValidAlertsMap); alertDefinitionsCache.setAlertsCacheInitialized(true); } AlertDefinitionsCacheRefresherThread(AlertDefinitionsCache cache, AlertService alertService); void run();  }
@Test public void testInitializeCache() { AlertDefinitionsCache cache = new AlertDefinitionsCache(alertService, false); AlertDefinitionsCacheRefresherThread refresherThread = new AlertDefinitionsCacheRefresherThread(cache, alertService); refresherThread.interrupt(); Alert alert = TestUtils.generateAlert("alert-name", admin, EXPRESSION); Trigger trigger = TestUtils.generateTrigger("trigger-name", alert); Notification notification = TestUtils.generateNotification("notification-name", alert, Arrays.asList(new Trigger[]{trigger})); alert.setNotifications(Arrays.asList(new Notification[]{notification})); alert.setTriggers(Arrays.asList(new Trigger[]{trigger})); alert.setEnabled(true); Alert expectedAlert = alertService.updateAlert(alert); refresherThread.initializeAlertDefinitionsCache(); Map<BigInteger, Alert> alertsMapById = cache.getAlertsMapById(); assertEquals(1, alertsMapById.size()); Alert actualAlert = (Alert) alertsMapById.values().toArray()[0]; assertTrue(actualAlert.equals(expectedAlert)); assertTrue(actualAlert.getTriggers().toArray()[0].equals(trigger)); assertTrue(actualAlert.getNotifications().toArray()[0].equals(notification)); }