ElasticsearchQuery implements Runnable { protected String getField(Map<String, Object> source, String name) { if (source == null || name == null) { return null; } if (name.charAt(0) == '\'' && name.charAt(name.length() - 1) == '\'') { return name.substring(1, name.length() - 1); } String[] names = name.split("\\|"); String defaultValue = ""; if (names.length > 1) { if (names[1].charAt(0) == '\'' && names[1].charAt(names[1].length() - 1) == '\'') { defaultValue = names[1].substring(1, names[1].length() - 1); } name = names[0]; } String[] fields = name.split("\\."); for (int i=0; i < fields.length; i++) { Object value = source.get(fields[i]); if (value instanceof String) { return (String) value; } if (value instanceof Number) { return String.valueOf(value); } if (value instanceof Map) { source = (Map<String, Object>) value; } } return defaultValue; } ElasticsearchQuery(Trigger trigger, Map<String, String> properties, AlertsService alerts); void parseProperties(); void parseMap(); void connect(String url); List<Map<String, Object>> query(String filter, String indices); List<Event> parseEvents(List<Map<String, Object>> hits); long parseTimestamp(String timestamp); String formatTimestamp(Date date); void disconnect(); @Override void run();  }
@Test public void checkConstantMapping() throws Exception { ElasticsearchQuery esQuery = new ElasticsearchQuery(null, null, null); Map<String, Object> source = new HashMap<>(); String value = esQuery.getField(source, "test"); assertTrue(value.isEmpty()); value = esQuery.getField(source, "'value'"); assertEquals("value", value); value = esQuery.getField(source, "test|'value'"); assertEquals("value", value); source.put("test", "newvalue"); value = esQuery.getField(source, "test|'value'"); assertEquals("newvalue", value); Map<String, Object> test = new HashMap<>(); test.put("propA", "valueA"); test.put("propB", "valueB"); source.put("test", test); value = esQuery.getField(source, "test|'value'"); assertEquals("value", value); value = esQuery.getField(source, "|'value'"); assertEquals("value", value); value = esQuery.getField(source, "test.propA|'value'"); assertEquals("valueA", value); value = esQuery.getField(source, "test.propB|'value'"); assertEquals("valueB", value); }