use of com.enonic.xp.script.ScriptExports in project xp by enonic.
the class ScriptRuntimeTest method testRuntimeError.
@Test
public void testRuntimeError() {
final ResourceKey script = ResourceKey.from("myapplication:/error/error-in-export-test.js");
final ScriptExports exports = runTestScript(script);
assertNotNull(exports);
try {
exports.executeMethod("hello");
fail("Should throw ResourceProblemException");
} catch (final ResourceProblemException e) {
assertEquals(1, e.getLineNumber());
assertEquals(ResourceKey.from("myapplication:/error/error-test.js"), e.getResource());
}
}
use of com.enonic.xp.script.ScriptExports in project xp by enonic.
the class ScriptRuntimeTest method testExecuteExported.
@Test
public void testExecuteExported() {
final ResourceKey script = ResourceKey.from("myapplication:/export-test.js");
final ScriptExports exports = runTestScript(script);
assertNotNull(exports);
assertSame(script, exports.getScript());
assertTrue(exports.hasMethod("hello"));
assertEquals("Hello World!", exports.executeMethod("hello", "World").getValue());
}
use of com.enonic.xp.script.ScriptExports in project xp by enonic.
the class ResponseProcessorExecutor method execute.
public PortalResponse execute(final ResponseProcessorDescriptor filter, final PortalRequest request, final PortalResponse response) {
final String filterName = filter.getName();
final String filterJsPath = "/site/processors/" + filterName + ".js";
final ResourceKey script = ResourceKey.from(filter.getApplication(), filterJsPath);
final ScriptExports filterExports;
try {
filterExports = this.scriptService.execute(script);
} catch (ResourceNotFoundException e) {
LOG.warn("Filter execution failed: {}", e.getMessage());
throw e;
}
final boolean exists = filterExports.hasMethod(RESPONSE_PROCESSOR_METHOD);
if (!exists) {
throw new RenderException("Missing exported function [{0}] in response filter [{1}]", RESPONSE_PROCESSOR_METHOD, filterJsPath);
}
final ApplicationKey previousApp = request.getApplicationKey();
// set application of the filter in the current context PortalRequest
request.setApplicationKey(filter.getApplication());
PortalRequestAccessor.set(request);
try {
return Tracer.trace("controllerScript", () -> executeFilter(filterExports, request, response));
} finally {
PortalRequestAccessor.remove();
request.setApplicationKey(previousApp);
}
}
use of com.enonic.xp.script.ScriptExports in project xp by enonic.
the class ScriptRuntimeTest method testResolve.
@Test
public void testResolve() {
final ResourceKey script = ResourceKey.from("myapplication:/resolve/resolve-test.js");
final ScriptExports exports = runTestScript(script);
assertNotNull(exports);
assertSame(script, exports.getScript());
}
use of com.enonic.xp.script.ScriptExports in project xp by enonic.
the class ScriptMapGeneratorTest method testMapValue.
@Test
public void testMapValue() {
final Map<String, Object> submap = new HashMap<>();
submap.put("child1", 1);
submap.put("child2", 2);
final Map<String, Object> map = new HashMap<>();
map.put("value1", 1);
map.put("value2", true);
map.put("value3", "string");
map.put("value4", submap);
final Object obj = (MapSerializable) gen -> {
gen.value("map", map);
gen.value("b", 2);
};
final ScriptExports exports = runTestScript("serializer/serializer-test.js");
exports.executeMethod("testMapValue", obj);
}
Aggregations