AbstractLuceneMetaAlertUpdateDao implements MetaAlertUpdateDao { @Override @SuppressWarnings("unchecked") public Document removeAlertsFromMetaAlert(String metaAlertGuid, List<GetRequest> alertRequests) throws IOException, IllegalStateException { Document metaAlert = retrieveLatestDao .getLatest(metaAlertGuid, MetaAlertConstants.METAALERT_TYPE); if (metaAlert == null) { throw new IOException(String.format("Unable to remove alerts from meta alert. Meta alert with guid %s cannot be found.", metaAlertGuid)); } if (MetaAlertStatus.ACTIVE.getStatusString() .equals(metaAlert.getDocument().get(MetaAlertConstants.STATUS_FIELD))) { Iterable<Document> alerts = retrieveLatestDao.getAllLatest(alertRequests); Set<String> missingAlerts = getMissingAlerts(alertRequests, alerts); if (!missingAlerts.isEmpty()) { throw new IOException(String.format("Unable to remove alerts from meta alert. Alert with guid %s cannot be found.", missingAlerts.iterator().next())); } Map<Document, Optional<String>> updates = buildRemoveAlertsFromMetaAlert(metaAlert, alerts); update(updates); return metaAlert; } else { throw new IllegalStateException("Removing alerts from an INACTIVE meta alert is not allowed"); } } protected  AbstractLuceneMetaAlertUpdateDao(
      UpdateDao updateDao,
      MetaAlertRetrieveLatestDao retrieveLatestDao,
      MetaAlertConfig config); UpdateDao getUpdateDao(); MetaAlertRetrieveLatestDao getRetrieveLatestDao(); MetaAlertConfig getConfig(); @Override Document patch(RetrieveLatestDao retrieveLatestDao, PatchRequest request,
      Optional<Long> timestamp); @Override Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates); @Override Document addAlertsToMetaAlert(String metaAlertGuid, List<GetRequest> alertRequests); @Override @SuppressWarnings("unchecked") Document removeAlertsFromMetaAlert(String metaAlertGuid, List<GetRequest> alertRequests); @Override Document updateMetaAlertStatus(String metaAlertGuid, MetaAlertStatus status);  }
@Test public void testRemoveAlertsFromMetaAlert() throws IOException { List<Document> alerts = buildChildAlerts(3, METAALERT_GUID, null); Document metaDoc = buildMetaAlert(alerts); List<Document> deletedAlerts = new ArrayList<>(); deletedAlerts.add(alerts.get(0)); deletedAlerts.add(alerts.get(2)); Map<Document, Optional<String>> actual = dao .buildRemoveAlertsFromMetaAlert(metaDoc, deletedAlerts); assertEquals(3, actual.size()); Map<String, Object> expectedDeletedAlert = new HashMap<>(); expectedDeletedAlert.put(Constants.GUID, "child_0"); expectedDeletedAlert.put(THREAT_FIELD_DEFAULT, 0.0f); expectedDeletedAlert .put(MetaAlertConstants.METAALERT_FIELD, new ArrayList<>()); Document expectedDeletedDocument = new Document(expectedDeletedAlert, "child_0", "test", 0L); Map<String, Object> expectedDeletedAlert3 = new HashMap<>(); expectedDeletedAlert3.put(Constants.GUID, "child_2"); expectedDeletedAlert3.put(THREAT_FIELD_DEFAULT, 0.0f); expectedDeletedAlert3 .put(MetaAlertConstants.METAALERT_FIELD, new ArrayList<>()); Document expectedDeletedDocument2 = new Document(expectedDeletedAlert3, "child_2", "test", 0L); List<Map<String, Object>> expectedAlerts = new ArrayList<>(); expectedAlerts.add(alerts.get(1).getDocument()); Map<String, Object> expectedMetaAlertMap = new HashMap<>(); expectedMetaAlertMap.put(Constants.GUID, METAALERT_GUID); expectedMetaAlertMap.put(ALERT_FIELD, expectedAlerts); expectedMetaAlertMap.put(THREAT_FIELD_DEFAULT, 0.0f); expectedMetaAlertMap.putAll(new MetaScores(Collections.singletonList(0.0d)).getMetaScores()); Document expectedMetaAlertDoc = new Document(expectedMetaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L); Map<Document, Optional<String>> expected = new HashMap<>(); expected.put(expectedDeletedDocument, Optional.empty()); expected.put(expectedDeletedDocument2, Optional.empty()); expected.put(expectedMetaAlertDoc, Optional.of(METAALERT_INDEX)); assertTrue(updatesMapEquals(expected, actual)); }
@Test public void testRemoveAlertsFromMetaAlertNoChildAlerts() { Document empty = new Document(new HashMap<>(), "empty", METAALERT_TYPE, 0L); boolean actual = dao.removeAlertsFromMetaAlert(empty, Collections.singletonList("child")); assertFalse(actual); }
@Test public void testRemoveAlertsFromMetaAlertEmptyRemoveList() { Document metaDoc = new Document( new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L ); metaDoc.getDocument().put( STATUS_FIELD, ACTIVE.getStatusString() ); metaDoc.getDocument().put( ALERT_FIELD, new HashMap<String, Object>() {{ put(Constants.GUID, "child_0"); }} ); boolean actual = dao.removeAlertsFromMetaAlert(metaDoc, new ArrayList<>()); assertFalse(actual); }
@Test public void testRemoveAlertsFromMetaAlertEmptyRemoveSingle() { Document metaDoc = new Document( new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L ); metaDoc.getDocument().put( STATUS_FIELD, ACTIVE.getStatusString() ); List<Map<String, Object>> alerts = new ArrayList<>(); alerts.add(new HashMap<String, Object>() {{ put(Constants.GUID, "child_0"); }}); metaDoc.getDocument().put( ALERT_FIELD, alerts ); boolean actual = dao.removeAlertsFromMetaAlert(metaDoc, Collections.singletonList("child_0")); Document expected = new Document( new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L ); expected.getDocument().put( STATUS_FIELD, ACTIVE.getStatusString() ); expected.getDocument().put(ALERT_FIELD, new ArrayList<>()); assertTrue(actual); assertEquals(expected, metaDoc); }
@Test public void testRemoveAlertsFromMetaAlertEmptyRemoveMultiple() { Document metDoc = new Document(new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L); metDoc.getDocument().put(STATUS_FIELD, ACTIVE.getStatusString()); List<Document> alerts = buildChildAlerts(3, null, null); metDoc.getDocument().put(ALERT_FIELD, getRawMaps(alerts)); List<String> removeGuids = new ArrayList<>(); removeGuids.add("child_0"); removeGuids.add("child_2"); removeGuids.add("child_doesn't_exist"); boolean actual = dao.removeAlertsFromMetaAlert(metDoc, removeGuids); Document expected = new Document(new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L); expected.getDocument().put(STATUS_FIELD, ACTIVE.getStatusString()); List<Map<String, Object>> alertsExpected = new ArrayList<>(); alertsExpected.add(new HashMap<String, Object>() {{ put(METAALERT_FIELD, new ArrayList<>()); put(Constants.GUID, "child_1"); put(THREAT_FIELD_DEFAULT, 0.0f); }} ); expected.getDocument().put(ALERT_FIELD, alertsExpected); assertEquals(expected, metDoc); assertTrue(actual); }
@Test public void testRemoveAlertsFromMetaAlertInactive() { assertThrows(IllegalStateException.class, () -> dao.removeAlertsFromMetaAlert(INACTIVE.getStatusString(), null)); }
@Test public void removeAlertsFromMetaAlertShouldThrowExceptionOnMissingMetaAlert() { IOException e = assertThrows(IOException.class, () -> dao.removeAlertsFromMetaAlert("some_guid", new ArrayList<>())); assertEquals("Unable to remove alerts from meta alert. Meta alert with guid some_guid cannot be found.", e.getMessage()); }