AbstractLuceneMetaAlertUpdateDao implements MetaAlertUpdateDao { @SuppressWarnings("unchecked") protected Map<Document, Optional<String>> buildRemoveAlertsFromMetaAlert(Document metaAlert, Iterable<Document> alerts) throws IOException { Map<Document, Optional<String>> updates = new HashMap<>(); List<String> alertGuids = new ArrayList<>(); for (Document alert : alerts) { alertGuids.add(alert.getGuid()); } List<Map<String, Object>> alertsBefore = new ArrayList<>(); Map<String, Object> documentBefore = metaAlert.getDocument(); if (documentBefore.containsKey(MetaAlertConstants.ALERT_FIELD)) { alertsBefore .addAll((List<Map<String, Object>>) documentBefore.get(MetaAlertConstants.ALERT_FIELD)); } boolean metaAlertUpdated = removeAlertsFromMetaAlert(metaAlert, alertGuids); if (metaAlertUpdated) { List<Map<String, Object>> alertsAfter = (List<Map<String, Object>>) metaAlert.getDocument() .get(MetaAlertConstants.ALERT_FIELD); if (alertsAfter.size() < alertsBefore.size() && alertsAfter.size() == 0) { throw new IllegalStateException("Removing these alerts will result in an empty meta alert. Empty meta alerts are not allowed."); } MetaScores .calculateMetaScores(metaAlert, config.getThreatTriageField(), config.getThreatSort()); updates.put(metaAlert, Optional.of(config.getMetaAlertIndex())); for (Document alert : alerts) { if (removeMetaAlertFromAlert(metaAlert.getGuid(), alert)) { updates.put(alert, Optional.empty()); } } } return updates; } 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 testBuildRemoveAlertsFromMetaAlertThrowsException() { List<Document> alerts = buildChildAlerts(1, METAALERT_GUID, null); Document metaDoc = buildMetaAlert(alerts); IllegalStateException e = assertThrows(IllegalStateException.class, () -> dao.buildRemoveAlertsFromMetaAlert(metaDoc, alerts)); assertEquals("Removing these alerts will result in an empty meta alert. Empty meta alerts are not allowed.", e.getMessage()); }