BaseFilter extends Base { public BaseFilter(Map config) { super(config); this.config = config; this.nextFilter = null; this.outputs = new ArrayList<BaseOutput>(); final List<String> ifConditions = (List<String>) this.config.get("if"); if (ifConditions != null) { IF = new ArrayList<TemplateRender>(ifConditions.size()); for (String c : ifConditions) { try { IF.add(new FreeMarkerRender(c, c)); } catch (IOException e) { log.fatal(e.getMessage(),e); System.exit(1); } } } this.tagOnFailure = (String) config.get("tag_on_failure"); final List<String> remove_fields = (ArrayList<String>) config.get("remove_fields"); if (remove_fields != null) { this.removeFields = new ArrayList<>(remove_fields.size()); for (String field : remove_fields) { this.removeFields.add(FieldDeleter.getFieldDeleter(field)); } } final Map<String, String> add_fields = (Map<String, String>) config.get("add_fields"); if (add_fields != null) { this.addFields = new HashMap<>(add_fields.size()); final Iterator<Map.Entry<String, String>> it = add_fields.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); String field = entry.getKey(); Object value = entry.getValue(); try { this.addFields.put(FieldSetter.getFieldSetter(field), TemplateRender.getRender(value)); } catch (IOException e) { log.fatal(e.getMessage()); System.exit(1); } } } this.prepare(); } BaseFilter(Map config); boolean needProcess(Map event); Map process(Map event); void processExtraEvents(Stack<Map<String, Object>> to_st); void postProcess(Map event, boolean ifSuccess); public boolean processExtraEventsFunc; public BaseFilter nextFilter; public List<BaseOutput> outputs; }
@Test public void testBaseFilter() { HashMap config = new HashMap(); config.put("remove_fields", new ArrayList(Arrays.asList("test1", "[a][b]"))); config.put("add_fields", new HashMap() {{ this.put("ok", true); this.put("[extra][value]", 11.11); this.put("[extra][metric][value]", 10); }}); BaseFilter bi = new BaseFilter(config); HashMap event = new HashMap() {{ this.put("test1", "ab"); this.put("a", new HashMap() {{ this.put("b", 213); this.put("b2", "hello"); }}); }}; bi.postProcess(event, true); System.out.println(event); Assert.assertNull(event.get("test1")); Assert.assertNull(((Map) event.get("a")).get("b")); Assert.assertEquals(((Map) event.get("a")).get("b2"), "hello"); Assert.assertTrue((Boolean) event.get("ok")); Map extra = (Map) event.get("extra"); Map metric = (Map) extra.get("metric"); Assert.assertEquals(extra.size(), 2); Assert.assertEquals(extra.get("value"), 11.11); Assert.assertEquals(metric.size(), 1); Assert.assertEquals(metric.get("value"), 10); }