AbstractLuceneMetaAlertUpdateDao implements MetaAlertUpdateDao { protected Map<Document, Optional<String>> buildStatusChangeUpdates(Document metaAlert, Iterable<Document> alerts, MetaAlertStatus status) { metaAlert.getDocument().put(MetaAlertConstants.STATUS_FIELD, status.getStatusString()); Map<Document, Optional<String>> updates = new HashMap<>(); updates.put(metaAlert, Optional.of(config.getMetaAlertIndex())); for (Document alert : alerts) { boolean metaAlertAdded = false; boolean metaAlertRemoved = false; if (MetaAlertStatus.ACTIVE.equals(status)) { metaAlertAdded = addMetaAlertToAlert(metaAlert.getGuid(), alert); } if (MetaAlertStatus.INACTIVE.equals(status)) { metaAlertRemoved = removeMetaAlertFromAlert(metaAlert.getGuid(), alert); } if (metaAlertAdded || metaAlertRemoved) { 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 testBuildStatusChangeUpdatesToInactive() { List<Document> alerts = buildChildAlerts(2, METAALERT_GUID, null); Map<String, Object> metaAlertMap = new HashMap<>(); metaAlertMap.put(ALERT_FIELD, getRawMaps(alerts)); metaAlertMap.put(Constants.GUID, METAALERT_GUID); metaAlertMap.put(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()); Document metaDoc = new Document( metaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L ); Map<Document, Optional<String>> actual = dao .buildStatusChangeUpdates(metaDoc, alerts, MetaAlertStatus.INACTIVE); assertEquals(3, actual.size()); List<Document> expectedDeletedAlerts = buildChildAlerts(2, null, null); List<Map<String, Object>> expectedAlerts = new ArrayList<>(); expectedAlerts.add(alerts.get(0).getDocument()); 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(STATUS_FIELD, MetaAlertStatus.INACTIVE.getStatusString()); Document expectedMetaAlertDoc = new Document(expectedMetaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L); Map<Document, Optional<String>> expected = new HashMap<>(); expected.put(expectedMetaAlertDoc, Optional.of(METAALERT_INDEX)); expected.put(expectedDeletedAlerts.get(0), Optional.empty()); expected.put(expectedDeletedAlerts.get(1), Optional.empty()); assertTrue(updatesMapEquals(expected, actual)); }
@Test public void testBuildStatusChangeUpdatesToActive() { List<Document> alerts = buildChildAlerts(2, METAALERT_GUID, null); Map<String, Object> metaAlertMap = new HashMap<>(); metaAlertMap.put(ALERT_FIELD, getRawMaps(alerts)); metaAlertMap.put(Constants.GUID, METAALERT_GUID); metaAlertMap.put(STATUS_FIELD, MetaAlertStatus.INACTIVE.getStatusString()); Document metaDoc = new Document( metaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L ); Map<Document, Optional<String>> actual = dao.buildStatusChangeUpdates( metaDoc, alerts, MetaAlertStatus.ACTIVE ); List<Map<String, Object>> expectedAlerts = new ArrayList<>(); expectedAlerts.add(alerts.get(0).getDocument()); expectedAlerts.add(alerts.get(1).getDocument()); Map<String, Object> expectedMetaAlertMap = new HashMap<>(); expectedMetaAlertMap.put(ALERT_FIELD, expectedAlerts); expectedMetaAlertMap.put(Constants.GUID, METAALERT_GUID); expectedMetaAlertMap.put(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()); Document expectedMetaAlertDoc = new Document( expectedMetaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L ); Map<Document, Optional<String>> expected = new HashMap<>(); expected.put(expectedMetaAlertDoc, Optional.of(METAALERT_INDEX)); assertTrue(updatesMapEquals(expected, actual)); }