Search in sources :

Example 16 with DefaultVariableResolver

use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.

the class StellarProcessorUtils method run.

/**
 * This utility class is intended for use while unit testing Stellar operators.
 * It is included in the "main" code so third-party operators will not need
 * a test dependency on Stellar's test-jar.
 *
 * This class ensures the basic contract of a stellar expression is adhered to:
 * 1. Validate works on the expression
 * 2. The output can be serialized and deserialized properly
 *
 * @param rule
 * @param variables
 * @param context
 * @return ret
 */
public static Object run(String rule, Map<String, Object> variables, Context context) {
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    Object ret = processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
    byte[] raw = SerDeUtils.toBytes(ret);
    Object actual = SerDeUtils.fromBytes(raw, Object.class);
    Assert.assertEquals(ret, actual);
    return ret;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) Spliterators(java.util.Spliterators) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) IntConsumer(java.util.function.IntConsumer) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) StreamSupport(java.util.stream.StreamSupport) Assert(org.junit.Assert) StellarPredicateProcessor(org.apache.metron.stellar.common.StellarPredicateProcessor) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 17 with DefaultVariableResolver

use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.

the class StellarComparisonExpressionWithOperatorTest method differentTypesShouldThrowErrorWhenUsingLT.

@Test(expected = ParseException.class)
public void differentTypesShouldThrowErrorWhenUsingLT() throws Exception {
    final Map<String, Object> variableMap = new HashMap<>();
    runPredicate("1 < '1'", new DefaultVariableResolver(variableMap::get, variableMap::containsKey));
}
Also used : HashMap(java.util.HashMap) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Test(org.junit.Test)

Example 18 with DefaultVariableResolver

use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.

the class StellarComparisonExpressionWithOperatorTest method testSimpleOps.

@Test
public void testSimpleOps() throws Exception {
    final Map<String, String> variableMap = new HashMap<String, String>() {

        {
            put("foo", "casey");
            put("empty", "");
            put("spaced", "metron is great");
            put("foo.bar", "casey");
        }
    };
    assertTrue(runPredicate("'casey' == foo.bar", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("'casey' == foo", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertFalse(runPredicate("'casey' != foo", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("'stella' == 'stella'", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertFalse(runPredicate("'stella' == foo", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("foo== foo", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("empty== ''", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("spaced == 'metron is great'", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate(null, new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate("", new DefaultVariableResolver(variableMap::get, variableMap::containsKey)));
    assertTrue(runPredicate(" ", (new DefaultVariableResolver(variableMap::get, variableMap::containsKey))));
}
Also used : HashMap(java.util.HashMap) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Test(org.junit.Test)

Example 19 with DefaultVariableResolver

use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.

the class BasicStellarTest method non_boolean_predicate_throws_exception.

@Test
public void non_boolean_predicate_throws_exception() {
    final Map<String, String> variableMap = new HashMap<String, String>() {

        {
            put("protocol", "http");
        }
    };
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("The rule 'TO_UPPER(protocol)' does not return a boolean value.");
    runPredicate("TO_UPPER(protocol)", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v)));
}
Also used : StellarProcessorUtils.run(org.apache.metron.stellar.common.utils.StellarProcessorUtils.run) java.util(java.util) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StellarProcessorUtils.runPredicate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) StellarProcessorUtils.validate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.validate) Test(org.junit.Test) StringUtils(org.apache.commons.lang3.StringUtils) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Stellar(org.apache.metron.stellar.dsl.Stellar) ClasspathFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.ClasspathFunctionResolver) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Assert(org.junit.Assert) ParseException(org.apache.metron.stellar.dsl.ParseException) ExpectedException(org.junit.rules.ExpectedException) Joiner(com.google.common.base.Joiner) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Test(org.junit.Test)

Example 20 with DefaultVariableResolver

use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.

the class BasicStellarTest method testShortCircuit_boolean.

@Test
public void testShortCircuit_boolean() throws Exception {
    Assert.assertTrue(runPredicate("'metron' in ['metron', 'metronicus', 'mortron'] or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or (false or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("TO_UPPER('foo') == 'FOO' or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("false and (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or false or false or true", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("false or (false and THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("'casey' == 'casey' or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("TO_UPPER('casey') == 'CASEY' or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("NOT(TO_UPPER('casey') != 'CASEY') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("(TO_UPPER('casey') == 'CASEY') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("NOT(NOT(TO_UPPER('casey') != 'CASEY') or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("NOT(NOT(TO_UPPER('casey') != 'CASEY')) and THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("RET_TRUE('foo') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    boolean thrown = false;
    try {
        runPredicate("NOT(foo == null or THROW('exception')) and THROW('and exception')", new DefaultVariableResolver(x -> null, x -> false));
    } catch (ParseException pe) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
    thrown = false;
    try {
        runPredicate("(foo == null or THROW('exception') ) or THROW('and exception')", new DefaultVariableResolver(x -> null, x -> false));
    } catch (ParseException pe) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
    Assert.assertTrue(runPredicate("( RET_TRUE('foo', true, false) or ( foo == null or THROW('exception') ) or THROW('and exception')) or THROW('or exception')", new DefaultVariableResolver(x -> null, x -> false)));
}
Also used : StellarProcessorUtils.run(org.apache.metron.stellar.common.utils.StellarProcessorUtils.run) java.util(java.util) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StellarProcessorUtils.runPredicate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) StellarProcessorUtils.validate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.validate) Test(org.junit.Test) StringUtils(org.apache.commons.lang3.StringUtils) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Stellar(org.apache.metron.stellar.dsl.Stellar) ClasspathFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.ClasspathFunctionResolver) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Assert(org.junit.Assert) ParseException(org.apache.metron.stellar.dsl.ParseException) ExpectedException(org.junit.rules.ExpectedException) Joiner(com.google.common.base.Joiner) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ParseException(org.apache.metron.stellar.dsl.ParseException) Test(org.junit.Test)

Aggregations

DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)21 Test (org.junit.Test)18 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)14 HashMap (java.util.HashMap)12 Context (org.apache.metron.stellar.dsl.Context)12 Assert (org.junit.Assert)11 Map (java.util.Map)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 StellarFunctions (org.apache.metron.stellar.dsl.StellarFunctions)8 ParseException (org.apache.metron.stellar.dsl.ParseException)7 StellarProcessorUtils.runPredicate (org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate)5 Joiner (com.google.common.base.Joiner)4 ImmutableList (com.google.common.collect.ImmutableList)4 java.util (java.util)4 List (java.util.List)4 Before (org.junit.Before)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 ArrayList (java.util.ArrayList)3 StringUtils (org.apache.commons.lang3.StringUtils)3 GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)2