protected boolean updatesMapEquals(Map<Document, Optional<String>> expected, Map<Document, Optional<String>> actual) { Entry<Document, Optional<String>> expectedMetaEntry; Entry<Document, Optional<String>> actualMetaEntry; expectedMetaEntry = findMetaEntry(expected); actualMetaEntry = findMetaEntry(actual); // Compare the metaalerts directly: they can mess with comparison because of float scores. if (!metaAlertDocumentEquals(expectedMetaEntry.getKey(), actualMetaEntry.getKey())) { return false; } else { // Remove the potentially problematic metaalert comparison. return removeMetaEntry(expected).equals(removeMetaEntry(actual)); } }
/** * Performs a patch operation on a document based on the result of @{link #isPatchAllowed(PatchRequest)} * * @param retrieveLatestDao DAO to retrieve the item to be patched * @param request The patch request. * @param timestamp Optionally a timestamp to set. If not specified then current time is used. * @return The patched document * @throws OriginalNotFoundException If no original document is found to patch. * @throws IOException If an error occurs performing the patch. */ @Override public Document patch(RetrieveLatestDao retrieveLatestDao, PatchRequest request, Optional<Long> timestamp) throws OriginalNotFoundException, IOException { if (isPatchAllowed(request)) { return updateDao.patch(retrieveLatestDao, request, timestamp); } else { throw new IllegalArgumentException( "Meta alert patches are not allowed for /alert or /status paths. " + "Please use the add/remove alert or update status functions instead."); } }
@Test public void testBuildRemoveAlertsFromMetaAlertThrowsException() throws Exception { thrown.expect(IllegalStateException.class); thrown.expectMessage("Removing these alerts will result in an empty meta alert. Empty meta alerts are not allowed."); List<Document> alerts = buildChildAlerts(1, METAALERT_GUID, null); Document metaDoc = buildMetaAlert(alerts); dao.buildRemoveAlertsFromMetaAlert(metaDoc, alerts); }
@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); // Build the expected metaalert 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 testBuildAddAlertToMetaAlertUpdatesEmpty() { Document metaDoc = new Document( new HashMap<>(), METAALERT_GUID, METAALERT_TYPE, 0L ); metaDoc.getDocument().put( ALERT_FIELD, getRawMaps(buildChildAlerts(1, METAALERT_GUID, null)) ); Map<Document, Optional<String>> actual = dao .buildAddAlertToMetaAlertUpdates(metaDoc, new ArrayList<>()); assertEquals(0, actual.size()); }
@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(expected = IllegalStateException.class) public void testRemoveAlertsFromMetaAlertInactive() throws IOException { dao.removeAlertsFromMetaAlert(INACTIVE.getStatusString(), null); }
@Test public void testUpdateMultiple() throws IOException { Map<Document, Optional<String>> updates = new HashMap<>(); Document documentOne = new Document(new HashMap<>(), "guid", "sensor", 0L); updates.put(documentOne, Optional.empty()); Document documentTwo = new Document(new HashMap<>(), "guid2", "sensor", 0L); updates.put(documentTwo, Optional.empty()); dao.update(updates); verify(indexDao, times(1)).batchUpdate(updates); }
@Test @SuppressWarnings("unchecked") public void testPatchNotAllowedAlert() throws ParseException { PatchRequest pr = new PatchRequest(); Map<String, Object> patch = (JSONObject) new JSONParser().parse(alertPatchRequest); pr.setPatch(Collections.singletonList((JSONObject) ((JSONArray) patch.get("patch")).get(0))); assertFalse(dao.isPatchAllowed(pr)); }
@Test(expected = UnsupportedOperationException.class) public void testBatchUpdateThrowsException() { dao.batchUpdate(null); }
@Before public void setup() { dao = new TestLuceneMetaAlertUpdateDao(); }
protected Document buildMetaAlert(List<Document> alerts) { Map<String, Object> metaAlertMap = new HashMap<>(); metaAlertMap.put(ALERT_FIELD, getRawMaps(alerts)); metaAlertMap.put(Constants.GUID, METAALERT_GUID); return new Document( metaAlertMap, METAALERT_GUID, METAALERT_TYPE, 0L ); } }
@Test public void addAlertsToMetaAlertShouldThrowExceptionOnMissingMetaAlert() throws Exception { thrown.expect(IOException.class); thrown.expectMessage("Unable to add alerts to meta alert. Meta alert with guid some_guid cannot be found."); dao.addAlertsToMetaAlert("some_guid", new ArrayList<>()); }
@Test public void updateMetaAlertStatusShouldThrowExceptionOnMissingMetaAlert() throws Exception { thrown.expect(IOException.class); thrown.expectMessage("Unable to update meta alert status. Meta alert with guid some_guid cannot be found."); dao.updateMetaAlertStatus("some_guid", MetaAlertStatus.INACTIVE); }
@Test public void testRemoveMetaAlertFromAlertMissing() { List<String> metaAlertGuids = new ArrayList<>(); metaAlertGuids.add("metaalert1"); metaAlertGuids.add("metaalert2"); Map<String, Object> alertFields = new HashMap<>(); alertFields.put(METAALERT_FIELD, metaAlertGuids); Document alert = new Document(alertFields, "alert", "test", 0L); boolean actual = dao.removeMetaAlertFromAlert("metaalert3", alert); assertFalse(actual); }
@Test public void testAddMetaAlertToAlertDuplicate() { List<String> metaAlertGuids = new ArrayList<>(); metaAlertGuids.add("metaalert1"); Map<String, Object> alertFields = new HashMap<>(); alertFields.put(METAALERT_FIELD, metaAlertGuids); Document alert = new Document(alertFields, "alert", "test", 0L); boolean actual = dao.addMetaAlertToAlert("metaalert1", alert); assertFalse(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 testUpdateSingle() throws IOException { Map<Document, Optional<String>> updates = new HashMap<>(); Document document = new Document(new HashMap<>(), "guid", "sensor", 0L); updates.put(document, Optional.empty()); dao.update(updates); verify(indexDao, times(1)).update(document, Optional.empty()); }
@Test @SuppressWarnings("unchecked") public void testPatchNotAllowedStatus() throws ParseException { PatchRequest pr = new PatchRequest(); Map<String, Object> patch = (JSONObject) new JSONParser().parse(statusPatchRequest); pr.setPatch(Collections.singletonList((JSONObject) ((JSONArray) patch.get("patch")).get(0))); assertFalse(dao.isPatchAllowed(pr)); }
@Test public void removeAlertsFromMetaAlertShouldThrowExceptionOnMissingMetaAlert() throws Exception { thrown.expect(IOException.class); thrown.expectMessage("Unable to remove alerts from meta alert. Meta alert with guid some_guid cannot be found."); dao.removeAlertsFromMetaAlert("some_guid", new ArrayList<>()); }