use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.
the class GroovyScriptTest method testBindingsAreBoundOnlyWhenEvaluation.
@Test
public void testBindingsAreBoundOnlyWhenEvaluation() {
GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine();
String groovyExpression = "temperature > 10 && humidity < 30";
GroovyScript<Boolean> groovyScript = new GroovyScript<Boolean>(groovyExpression, groovyScriptEngine);
HashMap<String, Object> fieldsAndValue = new HashMap<>();
fieldsAndValue.put("temperature", 20);
fieldsAndValue.put("humidity", 10);
try {
assertTrue(groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build()));
} catch (ScriptException e) {
e.printStackTrace();
Assert.fail("It shouldn't throw ScriptException");
}
fieldsAndValue.clear();
fieldsAndValue.put("no_related_field", 3);
try {
groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build());
// it means that previous bound variables are used now
Assert.fail("It should not evaluate correctly");
} catch (ScriptException e) {
// no-op, that's what we want
}
}
use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.
the class GroovyScriptNestedExprTest method testEvaluateNestedMapList.
@Test
public void testEvaluateNestedMapList() throws Exception {
groovyScript = new GroovyScript<>("x < y['a'][0]", new GroovyScriptEngine());
List<Integer> nestedList = new ArrayList<>();
nestedList.add(5);
nestedList.add(1);
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("a", nestedList);
Map<String, Object> kv = new HashMap<>();
kv.put("x", 2);
kv.put("y", nestedMap);
StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
Boolean result = groovyScript.evaluate(event);
System.out.println(result);
}
use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.
the class GroovyScriptNestedExprTest method testEvaluateNestedMap.
@Test
public void testEvaluateNestedMap() throws Exception {
groovyScript = new GroovyScript<>("x < y['b']", new GroovyScriptEngine());
Map<String, Object> nested = new HashMap<>();
nested.put("a", 5);
nested.put("b", 10);
Map<String, Object> kv = new HashMap<>();
kv.put("x", 2);
kv.put("y", nested);
StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
Boolean result = groovyScript.evaluate(event);
System.out.println(result);
}
use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.
the class GroovyScriptNestedExprTest method testEvaluateNestedList.
@Test
public void testEvaluateNestedList() throws Exception {
groovyScript = new GroovyScript<>("x < y[0]", new GroovyScriptEngine());
List<Integer> nested = new ArrayList<>();
nested.add(5);
nested.add(1);
Map<String, Object> kv = new HashMap<>();
kv.put("x", 2);
kv.put("y", nested);
StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
Boolean result = groovyScript.evaluate(event);
System.out.println(result);
}
use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.
the class GroovyScriptTest method testGroovyScriptEnsuresThreadSafe.
@Test
public void testGroovyScriptEnsuresThreadSafe() throws InterruptedException {
GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine();
String groovyExpression = "a % 2 == 0";
final GroovyScript<Boolean> groovyScript = new GroovyScript<>(groovyExpression, groovyScriptEngine);
final AtomicInteger index = new AtomicInteger(0);
List<Thread> threads = new ArrayList<>();
final AtomicReference<Throwable> anyException = new AtomicReference<>();
for (int i = 0; i < 500; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10; j++) {
try {
Thread.sleep(new Random().nextInt(10));
} catch (InterruptedException e) {
// no-op
}
int aVal = index.getAndIncrement();
HashMap<String, Object> fieldsAndValue = new HashMap<>();
fieldsAndValue.put("a", aVal);
try {
assertEquals(aVal % 2 == 0, groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build()));
} catch (Throwable e) {
e.printStackTrace();
anyException.set(e);
}
}
}
});
threads.add(t);
t.start();
}
for (Thread t : threads) {
t.join();
}
if (anyException.get() != null) {
Assert.fail("Exception occurred within thread, first one is " + anyException.get().getMessage());
}
}
Aggregations