Search in sources :

Example 16 with ScriptExports

use of com.enonic.xp.script.ScriptExports in project xp by enonic.

the class ScriptRuntimeTest method testEmpty.

@Test
public void testEmpty() {
    final ResourceKey script = ResourceKey.from("myapplication:/empty-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    assertSame(script, exports.getScript());
    assertFalse(exports.hasMethod("hello"));
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 17 with ScriptExports

use of com.enonic.xp.script.ScriptExports in project xp by enonic.

the class NamedTaskScriptFactoryImpl method doCreate.

private NamedTaskScript doCreate(final TaskDescriptor descriptor, final PropertyTree data) {
    final ResourceKey scriptResourceKey = ResourceKey.from(descriptor.getApplicationKey(), TASKS_PATH_PREFIX + descriptor.getName() + "/" + descriptor.getName() + ".js");
    final ScriptExports exports;
    try {
        exports = this.scriptService.execute(scriptResourceKey);
    } catch (ResourceNotFoundException e) {
        throw new TaskNotFoundException(descriptor.getKey(), "Missing task script");
    }
    final boolean exists = exports.hasMethod(NamedTaskScript.SCRIPT_METHOD_NAME);
    if (!exists) {
        throw new TaskNotFoundException(descriptor.getKey(), "Missing exported function '" + NamedTaskScript.SCRIPT_METHOD_NAME + "' in task script");
    }
    return new NamedTaskScript(exports, descriptor, data);
}
Also used : TaskNotFoundException(com.enonic.xp.task.TaskNotFoundException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceNotFoundException(com.enonic.xp.resource.ResourceNotFoundException) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 18 with ScriptExports

use of com.enonic.xp.script.ScriptExports in project xp by enonic.

the class UrlServiceScriptTest method execute.

private boolean execute(final String method) {
    final ScriptExports exports = runScript("/test/url-test.js");
    final ScriptValue value = exports.executeMethod(method);
    return value != null ? value.getValue(Boolean.class) : false;
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) ScriptExports(com.enonic.xp.script.ScriptExports)

Example 19 with ScriptExports

use of com.enonic.xp.script.ScriptExports in project xp by enonic.

the class ScriptMapGeneratorTest method testArray.

@Test
public void testArray() {
    final Object obj = (MapSerializable) gen -> {
        gen.value("a", 1);
        gen.value("b", 2);
    };
    final List<Object> list = List.of(obj, 2);
    final ScriptExports exports = runTestScript("serializer/serializer-test.js");
    exports.executeMethod("testArray", list);
}
Also used : MapSerializable(com.enonic.xp.script.serializer.MapSerializable) ScriptExports(com.enonic.xp.script.ScriptExports) Test(org.junit.jupiter.api.Test) AbstractScriptTest(com.enonic.xp.script.impl.AbstractScriptTest)

Example 20 with ScriptExports

use of com.enonic.xp.script.ScriptExports in project xp by enonic.

the class ScriptMapGeneratorTest method testListValue.

@Test
public void testListValue() {
    final List<Object> level2List = new ArrayList<>();
    level2List.add("list2A");
    level2List.add("list2B");
    final List<Object> level1List = new ArrayList<>();
    level1List.add("list1A");
    level1List.add("list1B");
    level1List.add(level2List);
    final Map<String, Object> map = new HashMap<>();
    map.put("mapValue", 1);
    final HashMultimap<Object, Object> multimap = HashMultimap.create();
    multimap.put("multimapValue", 1);
    multimap.put("multimapValue", 2);
    final List<Object> list = new ArrayList<>();
    list.add(1);
    list.add(true);
    list.add("string");
    list.add(level1List);
    list.add(map);
    list.add(multimap);
    final MapSerializable value = gen -> gen.value("root", list);
    final ScriptExports exports = runTestScript("serializer/serializer-test.js");
    exports.executeMethod("testListValue", value);
}
Also used : Test(org.junit.jupiter.api.Test) AbstractScriptTest(com.enonic.xp.script.impl.AbstractScriptTest) List(java.util.List) HashMultimap(com.google.common.collect.HashMultimap) Map(java.util.Map) HashMap(java.util.HashMap) ScriptExports(com.enonic.xp.script.ScriptExports) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) ArrayList(java.util.ArrayList) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) 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