Search in sources :

Example 1 with MapSerializable

use of com.enonic.xp.script.serializer.MapSerializable in project xp by enonic.

the class ScriptEventListenerImplTest method testEvent.

@Test
public void testEvent() {
    final Event event = Event.create("application").localOrigin(true).value("a", 1).build();
    this.listener.onEvent(event);
    assertNotNull(this.event);
    assertTrue(this.event instanceof MapSerializable);
    final MapSerializable serializable = (MapSerializable) this.event;
    final JsonMapGenerator generator = new JsonMapGenerator();
    serializable.serialize(generator);
}
Also used : MapSerializable(com.enonic.xp.script.serializer.MapSerializable) Event(com.enonic.xp.event.Event) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) Test(org.junit.jupiter.api.Test)

Example 2 with MapSerializable

use of com.enonic.xp.script.serializer.MapSerializable 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)

Example 3 with MapSerializable

use of com.enonic.xp.script.serializer.MapSerializable 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 4 with MapSerializable

use of com.enonic.xp.script.serializer.MapSerializable 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 5 with MapSerializable

use of com.enonic.xp.script.serializer.MapSerializable 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)

Aggregations

MapSerializable (com.enonic.xp.script.serializer.MapSerializable)7 Test (org.junit.jupiter.api.Test)7 ScriptExports (com.enonic.xp.script.ScriptExports)6 AbstractScriptTest (com.enonic.xp.script.impl.AbstractScriptTest)5 HashMap (java.util.HashMap)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Event (com.enonic.xp.event.Event)1 ResourceKey (com.enonic.xp.resource.ResourceKey)1 ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)1 JsonMapGenerator (com.enonic.xp.script.serializer.JsonMapGenerator)1 HashMultimap (com.google.common.collect.HashMultimap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertNotSame (org.junit.jupiter.api.Assertions.assertNotSame)1 Assertions.assertSame (org.junit.jupiter.api.Assertions.assertSame)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1