Search in sources :

Example 6 with ScriptExports

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

the class ScriptMapGeneratorTest method testMultimapValue.

@Test
public void testMultimapValue() {
    final HashMultimap<Object, Object> submap = HashMultimap.create();
    submap.put("children", 5);
    submap.put("children", 6);
    submap.put("children", 7);
    final HashMultimap<Object, Object> map = HashMultimap.create();
    map.put("value1", 1);
    map.put("value1", 2);
    map.put("value1", 3);
    map.put("value2", true);
    map.put("value2", false);
    map.put("value3", "string1");
    map.put("value4", submap);
    final Object obj = (MapSerializable) gen -> gen.value("multimap", map);
    final ScriptExports exports = runTestScript("serializer/serializer-test.js");
    exports.executeMethod("testMultimapValue", obj);
}
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 7 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 8 with ScriptExports

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

the class ScriptRuntimeTest method testRequire.

@Test
public void testRequire() {
    final ResourceKey script = ResourceKey.from("myapplication:/site/require/require-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 9 with ScriptExports

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

the class ScriptRuntimeTest method testExecuteExported_objectArg.

@Test
public void testExecuteExported_objectArg() {
    final ResourceKey script = ResourceKey.from("myapplication:/export-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    assertSame(script, exports.getScript());
    assertTrue(exports.hasMethod("helloObject"));
    assertEquals("Hello World!", exports.executeMethod("helloObject", (MapSerializable) gen -> gen.value("name", "World")).getValue());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ResourceKey(com.enonic.xp.resource.ResourceKey) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) ApplicationKey(com.enonic.xp.app.ApplicationKey) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ScriptExports(com.enonic.xp.script.ScriptExports) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 10 with ScriptExports

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

the class NewBeanScriptTest method testEmpty.

@Test
public void testEmpty() {
    final ResourceKey script = ResourceKey.from("myapplication:/bean/simple-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) 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