Search in sources :

Example 11 with ParseException

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);
    }
}
Also used : Map(java.util.Map) StellarProcessorUtils.runPredicate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate) Test(org.junit.Test) HashMap(java.util.HashMap) Assert(org.junit.Assert) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ParseException(org.apache.metron.stellar.dsl.ParseException) HashMap(java.util.HashMap) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ParseException(org.apache.metron.stellar.dsl.ParseException) Test(org.junit.Test)

Example 12 with ParseException

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);
}
Also used : ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Aggregations

ParseException (org.apache.metron.stellar.dsl.ParseException)12 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 HashedMap (org.apache.commons.collections4.map.HashedMap)4 Joiner (com.google.common.base.Joiner)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)3 StellarProcessorUtils.runPredicate (org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate)3 Context (org.apache.metron.stellar.dsl.Context)3 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)3 StellarFunction (org.apache.metron.stellar.dsl.StellarFunction)3 Assert (org.junit.Assert)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 StringUtils (org.apache.commons.lang3.StringUtils)2 StellarProcessorUtils.run (org.apache.metron.stellar.common.utils.StellarProcessorUtils.run)2