use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class RegExFunctionsTest method testRegExGroupVal.
@Test
public void testRegExGroupVal() throws Exception {
final Map<String, String> variableMap = new HashMap<String, String>() {
{
put("numbers", "12345");
put("numberPattern", "\\d(\\d)(\\d).*");
put("numberPatternNoCaptures", "\\d\\d\\d.*");
put("letters", "abcde");
put("empty", "");
}
};
Assert.assertTrue(runPredicate("REGEXP_GROUP_VAL(numbers,numberPattern,2) == '3'", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("REGEXP_GROUP_VAL(letters,numberPattern,2) == null", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("REGEXP_GROUP_VAL(empty,numberPattern,2) == null", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("REGEXP_GROUP_VAL(numbers,numberPatternNoCaptures,2) == null", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
boolean thrown = false;
try {
runPredicate("REGEXP_GROUP_VAL(2) == null", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v)));
} catch (ParseException | IllegalStateException ise) {
thrown = true;
}
if (!thrown) {
Assert.assertTrue("Did not fail on wrong number of parameters", false);
}
}
use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.
the class StringFunctionsTest method testChop.
/**
* CHOP StringFunction
*
* @throws Exception
*/
@Test
public void testChop() throws Exception {
Assert.assertEquals("ab", run("CHOP('abc')", new HashedMap()));
Assert.assertEquals(null, run("CHOP(null)", new HashedMap()));
Assert.assertEquals("abc", run("CHOP(msg)", ImmutableMap.of("msg", "abc\r\n")));
Assert.assertEquals("", run("CHOP(msg)", ImmutableMap.of("msg", "")));
Assert.assertEquals("", run("CHOP(msg)", ImmutableMap.of("msg", "\n")));
Assert.assertEquals("", run("CHOP('')", new HashedMap()));
// No input
boolean thrown = false;
try {
run("CHOP()", Collections.emptyMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("missing argument"));
}
Assert.assertTrue(thrown);
thrown = false;
// Variable missing
try {
run("CHOMP(msg)", new HashedMap());
} catch (ParseException pe) {
thrown = true;
}
thrown = false;
// Integer input
try {
run("CHOP(123)", Collections.emptyMap());
} catch (ParseException pe) {
thrown = true;
Assert.assertTrue(pe.getMessage().contains("cannot be cast"));
}
Assert.assertTrue(thrown);
}
Aggregations