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;
}
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));
}
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))));
}
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)));
}
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)));
}
Aggregations