RegexFilterOperation implements FilterOperation { @Override public boolean test(InternalEvent ievent) { DeserializedEvent devent = ievent.getEventObj(); if (devent == null) { return false; } boolean found; try { String field = devent.getFieldAsString(this.path); found = this.pattern.matcher(field).matches(); } catch (FieldNotFoundException e) { found = false; } return this.exclude != found; } RegexFilterOperation(Pattern pattern, String path, Boolean exclude); @Override boolean test(InternalEvent ievent);  }
@Test public void testFilterMatches() throws IOException, FieldNotFoundException { JsonParser parser = new JsonParser(); JsonElement input = parser.parse(getResourceString("filter_input.json")); GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject()); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); String path = "$.simple_object.key"; Pattern patternMatch = Pattern.compile("(val)"); Pattern patternFail = Pattern.compile("(fail)"); Boolean match = true; assertNotNull(devent.getFieldAsString(path)); RegexFilterOperation opMatch = new RegexFilterOperation(patternMatch, path, match); RegexFilterOperation opFail = new RegexFilterOperation(patternFail, path, match); assertFalse(opMatch.test(ievent)); assertTrue(opFail.test(ievent)); }
@Test public void testFilterNonMatches() throws IOException, FieldNotFoundException { JsonParser parser = new JsonParser(); JsonElement input = parser.parse(getResourceString("filter_input.json")); GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject()); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); String path = "$.simple_object.key"; Pattern patternMatch = Pattern.compile("(val)"); Pattern patternFail = Pattern.compile("(fail)"); Boolean match = false; assertNotNull(devent.getFieldAsString(path)); RegexFilterOperation opMatch = new RegexFilterOperation(patternMatch, path, match); RegexFilterOperation opFail = new RegexFilterOperation(patternFail, path, match); assertTrue(opMatch.test(ievent)); assertFalse(opFail.test(ievent)); }
@Test public void testFieldDoesNotExist() throws IOException { JsonParser parser = new JsonParser(); JsonElement input = parser.parse(getResourceString("filter_input.json")); GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject()); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); String path = "$.cookie"; Pattern pattern = Pattern.compile("(val)"); Boolean match = false; RegexFilterOperation op = new RegexFilterOperation(pattern, path, match); boolean filter = op.test(ievent); assertFalse(filter); }
@Test public void testNullDeserializedEvent() { InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(null); String path = "$.simple_object.key"; Pattern pattern = Pattern.compile("(val)"); Boolean match = false; RegexFilterOperation op = new RegexFilterOperation(pattern, path, match); boolean filter = op.test(ievent); assertFalse(filter); }
@Test public void testInvalidPath() throws IOException { JsonParser parser = new JsonParser(); JsonElement input = parser.parse(getResourceString("filter_input.json")); GenericJsonEvent devent = new GenericJsonEvent(input.getAsJsonObject()); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); Pattern pattern = Pattern.compile("(val)"); String path = "[]"; Boolean match = false; RegexFilterOperation op = new RegexFilterOperation(pattern, path, match); boolean filter = op.test(ievent); assertFalse(filter); }