Search in sources :

Example 31 with ScriptValue

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

the class ScriptValueFactoryImplTest method newValue_array.

@Test
public void newValue_array() throws Exception {
    final Object obj = execute("var result = ['1', '2']; result;");
    final ScriptValue value = this.factory.newValue(obj);
    assertNotNull(value);
    assertEquals(true, value.isArray());
    assertEquals(false, value.isFunction());
    assertEquals(false, value.isObject());
    assertEquals(false, value.isValue());
    assertNonValue(value);
    assertNonObject(value);
    assertNonFunction(value);
    assertNotNull(value.getArray());
    assertEquals(2, value.getArray().size());
    assertNotNull(value.getArray(Integer.class));
    assertEquals(2, value.getArray(Integer.class).size());
    assertEquals("1", value.getArray().get(0).getValue());
    assertEquals("2", value.getArray().get(1).getValue());
    assertEquals(1, value.getArray(Integer.class).get(0));
    assertEquals(2, value.getArray(Integer.class).get(1));
    assertEquals(2, value.getList().size());
    assertEquals("1", value.getList().get(0));
    assertEquals("2", value.getList().get(1));
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) JSObject(jdk.nashorn.api.scripting.JSObject) Test(org.junit.jupiter.api.Test)

Example 32 with ScriptValue

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

the class ScriptValueFactoryImplTest method array_toList.

@Test
public void array_toList() throws Exception {
    final Object obj = execute("var result = ['1', null, 2, {'key': 'value'}, [42]]; result;");
    final ScriptValue value = this.factory.newValue(obj);
    assertNotNull(value);
    assertTrue(value.isArray());
    assertNonValue(value);
    assertNonObject(value);
    assertNonFunction(value);
    final List<Object> list = value.getList();
    assertNotNull(list);
    assertEquals(5, list.size());
    assertEquals("1", list.get(0));
    assertNull(list.get(1));
    assertEquals(2, list.get(2));
    assertTrue(list.get(3) instanceof Map);
    assertTrue(list.get(4) instanceof List);
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) JSObject(jdk.nashorn.api.scripting.JSObject) List(java.util.List) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 33 with ScriptValue

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

the class ScriptValueTest method testObject.

@Test
public void testObject() throws Exception {
    final ScriptValue obj = evalValue("testObject");
    assertEquals(false, obj.isArray());
    assertEquals(false, obj.isFunction());
    assertEquals(true, obj.isObject());
    assertEquals(false, obj.isValue());
    assertEquals(null, obj.getValue());
    assertEquals(2, obj.getKeys().size());
    assertEquals(true, obj.hasMember("a"));
    assertEquals(1, obj.getMember("a").getValue());
    assertEquals(0, obj.getArray().size());
    assertEquals(null, obj.call("a"));
    assertEquals(null, obj.getValue(String.class));
    assertEquals(0, obj.getArray(String.class).size());
    assertEquals("a=1,b=2", obj.getMap().entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(",")));
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) Test(org.junit.jupiter.api.Test)

Example 34 with ScriptValue

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

the class MacroProcessorScript method doProcess.

private PortalResponse doProcess(final MacroContext macroContext) {
    final boolean exists = this.scriptExports.hasMethod(SCRIPT_METHOD_NAME);
    if (!exists) {
        return null;
    }
    final MacroContextMapper macroContextMapper = new MacroContextMapper(macroContext);
    final ScriptValue result = this.scriptExports.executeMethod(SCRIPT_METHOD_NAME, macroContextMapper);
    return new PortalResponseSerializer(result).serialize();
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer)

Example 35 with ScriptValue

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

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorWithByteSourceBody.

@Test
public void testExecuteResponseProcessorWithByteSourceBody() throws Exception {
    final ByteSource data = ByteSource.wrap("DATA".getBytes(StandardCharsets.UTF_8));
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptExports.hasMethod("responseProcessor")).thenReturn(true);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ScriptValue result = Mockito.mock(ScriptValue.class);
    final ScriptValue body = Mockito.mock(ScriptValue.class);
    when(body.getValue()).thenReturn(data.toString());
    when(result.isObject()).thenReturn(true);
    when(result.getMember("body")).thenReturn(body);
    when(scriptExports.executeMethod(anyString(), any(PortalRequestMapper.class), any(PortalResponseMapper.class))).thenReturn(result);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().body(data).build();
    final PortalResponse filteredResponse = filterExecutor.execute(filter, request, response);
    assertNotNull(filteredResponse);
    assertTrue(filteredResponse.getBody() instanceof ByteSource);
    assertArrayEquals(data.read(), ((ByteSource) filteredResponse.getBody()).read());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponse(com.enonic.xp.portal.PortalResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ByteSource(com.google.common.io.ByteSource) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ScriptValue (com.enonic.xp.script.ScriptValue)40 Test (org.junit.jupiter.api.Test)20 PortalRequest (com.enonic.xp.portal.PortalRequest)7 PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)5 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)5 JSObject (jdk.nashorn.api.scripting.JSObject)4 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)3 ScriptExports (com.enonic.xp.script.ScriptExports)3 PropertyTree (com.enonic.xp.data.PropertyTree)2 ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)2 WebException (com.enonic.xp.web.WebException)2 ByteSource (com.google.common.io.ByteSource)2 Map (java.util.Map)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Content (com.enonic.xp.content.Content)1 ContentId (com.enonic.xp.content.ContentId)1 ContentQuery (com.enonic.xp.content.ContentQuery)1 Contents (com.enonic.xp.content.Contents)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)1