Search in sources :

Example 1 with ScriptExports

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());
    }
}
Also used : ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 2 with ScriptExports

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());
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 3 with ScriptExports

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);
    }
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceNotFoundException(com.enonic.xp.resource.ResourceNotFoundException) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 4 with ScriptExports

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());
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 5 with ScriptExports

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);
}
Also used : MapSerializable(com.enonic.xp.script.serializer.MapSerializable) HashMap(java.util.HashMap) ScriptExports(com.enonic.xp.script.ScriptExports) Test(org.junit.jupiter.api.Test) AbstractScriptTest(com.enonic.xp.script.impl.AbstractScriptTest)

Aggregations

ScriptExports (com.enonic.xp.script.ScriptExports)22 Test (org.junit.jupiter.api.Test)17 ResourceKey (com.enonic.xp.resource.ResourceKey)14 AbstractScriptTest (com.enonic.xp.script.impl.AbstractScriptTest)6 MapSerializable (com.enonic.xp.script.serializer.MapSerializable)6 PortalRequest (com.enonic.xp.portal.PortalRequest)3 PortalResponse (com.enonic.xp.portal.PortalResponse)3 PortalScriptService (com.enonic.xp.portal.script.PortalScriptService)3 ScriptValue (com.enonic.xp.script.ScriptValue)3 ResponseProcessorDescriptor (com.enonic.xp.site.processor.ResponseProcessorDescriptor)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)2 ResourceNotFoundException (com.enonic.xp.resource.ResourceNotFoundException)2 ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)2 HashMap (java.util.HashMap)2 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)1 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)1 TaskNotFoundException (com.enonic.xp.task.TaskNotFoundException)1 HashMultimap (com.google.common.collect.HashMultimap)1 ByteSource (com.google.common.io.ByteSource)1