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));
}
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);
}
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(",")));
}
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();
}
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());
}
Aggregations