### Question:
QueryScriptEngine { public void execute(String script) throws QueryScriptException { try { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); scope.put(ARAS_SCRIPT_ENGINE_CONTEXT, scope, arasCtx); loadRhinoBinding(scope, cx); cx.evaluateString(scope, script, "queryscript", 0, null); } catch (Exception e) { throw new QueryScriptException("Execution of query script failed.", e); } finally { Context.exit(); } } QueryScriptEngine(ScriptEngineContext arasCtx); void execute(String script); static final String ARAS_SCRIPT_ENGINE_CONTEXT; }### Answer:
@Test public void shouldCallArasContext() throws QueryScriptException { final String query = "'x'='y'"; final String walkPredicate = "rdf:type"; ScriptEngineContext ctx = mock(ScriptEngineContext.class); NodeSet nodeSet = mock(NodeSet.class); when(ctx.query(anyString())).thenReturn(nodeSet); when(nodeSet.walk(anyString())).thenReturn(nodeSet); QueryScriptEngine engine = new QueryScriptEngine(ctx); engine.execute( "query(\"" + query + "\")" + " .walk('" + walkPredicate + "')" + ".result();"); verify(ctx).query(Mockito.eq(query)); verify(nodeSet).walk(Mockito.eq(walkPredicate)); verify(nodeSet).result(); }
@Test public void testFiltering() throws QueryScriptException { ScriptEngineContext ctx = mock(ScriptEngineContext.class); SNResource a = new SNResource(qualify("local:a")); SNResource b = new SNResource(qualify("local:b")); SNResource c = new SNResource(qualify("local:c")); Collection<SemanticNode> initial = Arrays.<SemanticNode>asList(a, b, c); NodeSet nodeSet = new NodeSet(initial, ctx); when(ctx.query(anyString())).thenReturn(nodeSet); QueryScriptEngine engine = new QueryScriptEngine(ctx); engine.execute("query(\"'x'='y'\").filter(" + "function (e) { " + " return e.matches('local:b'); " + "}).result();"); ArgumentCaptor<NodeSet> resultNS = ArgumentCaptor.forClass(NodeSet.class); verify(ctx).result(resultNS.capture()); SemanticNode[] filtered = resultNS.getValue().nodes(); Assert.assertTrue(filtered.length == 1); Assert.assertEquals("local:b", filtered[0].asResource().toURI()); }