Search in sources :

Example 1 with Alert

use of org.codice.ddf.system.alerts.Alert in project ddf by codice.

the class AdminAlertImpl method getAlerts.

@Override
public List<Map<String, Object>> getAlerts() {
    List<Map<String, Object>> alerts = new ArrayList<>();
    try {
        List<Map<String, Object>> results = persistentStore.get("alerts", activeAlertQuery);
        if (!results.isEmpty()) {
            for (Map<String, Object> item : results) {
                item = PersistentItem.stripSuffixes(item);
                // strings/sets of strings. Single value sets will come back as strings.
                if (item.get(SystemNotice.SYSTEM_NOTICE_DETAILS_KEY) instanceof String) {
                    item.put(SystemNotice.SYSTEM_NOTICE_DETAILS_KEY, Collections.singleton(item.get(SystemNotice.SYSTEM_NOTICE_DETAILS_KEY)));
                }
                escapeStrings(item);
                alerts.add(item);
            }
        }
    } catch (PersistenceException pe) {
        LOGGER.debug("Error retrieving system alert.", pe);
        return Collections.singletonList(new Alert("unable_to_retrieve_alerts", NoticePriority.CRITICAL, "Persistent Storage Not Responding. Could Not Retrieve Alerts.", Collections.singleton("Critical alerts may be present, but not displayed because the persistent storage is not responding.")).getProperties());
    }
    alerts.sort((map1, map2) -> new Alert(map1).getPriority().value() >= new Alert(map2).getPriority().value() ? -1 : 1);
    return alerts;
}
Also used : ArrayList(java.util.ArrayList) PersistenceException(org.codice.ddf.persistence.PersistenceException) Alert(org.codice.ddf.system.alerts.Alert) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Alert

use of org.codice.ddf.system.alerts.Alert in project ddf by codice.

the class AlertListenerTest method testHandleEventExistingAlert.

@Test
public void testHandleEventExistingAlert() throws Exception {
    SystemNotice notice = new SystemNotice("test-source", NoticePriority.IMPORTANT, "title", new HashSet());
    Map<String, Object> props = notice.getProperties();
    props.put("event.topics", "decanter/alert/test");
    Alert alert = new Alert(props);
    ArgumentCaptor<PersistentItem> captor = ArgumentCaptor.forClass(PersistentItem.class);
    Event event = new Event("decanter/alert/test", alert.getProperties());
    when(persistentStore.get(any(), any())).thenReturn(Collections.singletonList(toSolr(alert)));
    alertListener.handleEvent(event);
    verify(persistentStore).add(eq("alerts"), captor.capture());
    PersistentItem item = captor.getValue();
    assertThat(item.getLongProperty(Alert.ALERT_COUNT), equalTo(2L));
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) Event(org.osgi.service.event.Event) Alert(org.codice.ddf.system.alerts.Alert) SystemNotice(org.codice.ddf.system.alerts.SystemNotice) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Alert

use of org.codice.ddf.system.alerts.Alert in project ddf by codice.

the class AlertListenerTest method testHandleEventDismiss.

@Test
public void testHandleEventDismiss() throws Exception {
    Alert alert = new Alert("test-source", NoticePriority.IMPORTANT, "title", new HashSet());
    ArgumentCaptor<PersistentItem> captor = ArgumentCaptor.forClass(PersistentItem.class);
    Map<String, String> dismissEvent = new HashMap<>();
    dismissEvent.put(Alert.SYSTEM_NOTICE_ID_KEY, alert.getId());
    dismissEvent.put(Alert.ALERT_DISMISSED_BY, "test-user");
    Event event = new Event(Alert.ALERT_DISMISS_TOPIC, dismissEvent);
    when(persistentStore.get(any(), any())).thenReturn(Collections.singletonList(toSolr(alert)));
    alertListener.handleEvent(event);
    verify(persistentStore).add(eq("alerts"), captor.capture());
    PersistentItem item = captor.getValue();
    assertThat(item.getTextProperty(Alert.ALERT_DISMISSED_BY), equalTo("test-user"));
    assertThat(item.getTextProperty(Alert.ALERT_STATUS), equalTo(Alert.ALERT_DISMISSED_STATUS));
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) Alert(org.codice.ddf.system.alerts.Alert) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Alert

use of org.codice.ddf.system.alerts.Alert in project ddf by codice.

the class AlertListenerTest method testHandleEventFirstAlert.

@Test
public void testHandleEventFirstAlert() throws Exception {
    SystemNotice notice = new SystemNotice("test-source", NoticePriority.IMPORTANT, "title", new HashSet());
    Map<String, Object> props = notice.getProperties();
    props.put("event.topics", "decanter/alert/test");
    Alert alert = new Alert(props);
    ArgumentCaptor<Event> captor = ArgumentCaptor.forClass(Event.class);
    Event event = new Event("decanter/alert/test", notice.getProperties());
    when(persistentStore.get(any(), any())).thenReturn(new ArrayList<>());
    alertListener.handleEvent(event);
    verify(persistentStore).add(eq("alerts"), any(PersistentItem.class));
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) Event(org.osgi.service.event.Event) Alert(org.codice.ddf.system.alerts.Alert) SystemNotice(org.codice.ddf.system.alerts.SystemNotice) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Alert

use of org.codice.ddf.system.alerts.Alert in project ddf by codice.

the class AlertListener method dismissAlert.

private void dismissAlert(Event dismissEvent) {
    String id = (String) dismissEvent.getProperty(Alert.SYSTEM_NOTICE_ID_KEY);
    if (id == null) {
        return;
    }
    Alert alert = getAlertById(id);
    if (alert == null) {
        LOGGER.debug("Could not find alert {} for dismissal.", id);
        return;
    }
    String dismissedBy = (String) dismissEvent.getProperty(Alert.ALERT_DISMISSED_BY);
    if (dismissedBy == null) {
        LOGGER.debug("Could not dismiss alert {} because the {} property was not provided.", id, Alert.ALERT_DISMISSED_BY);
        return;
    }
    addAlertToStore(alert.getDismissedAlert(dismissedBy));
}
Also used : Alert(org.codice.ddf.system.alerts.Alert)

Aggregations

Alert (org.codice.ddf.system.alerts.Alert)6 HashSet (java.util.HashSet)3 PersistentItem (org.codice.ddf.persistence.PersistentItem)3 Test (org.junit.Test)3 Event (org.osgi.service.event.Event)3 HashMap (java.util.HashMap)2 SystemNotice (org.codice.ddf.system.alerts.SystemNotice)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 PersistenceException (org.codice.ddf.persistence.PersistenceException)1