use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class TextRendererTest method before.
@BeforeEach
public void before() {
portalRequest = new PortalRequest();
portalResponse = PortalResponse.create().build();
PortalUrlServiceImpl portalUrlService = new PortalUrlServiceImpl();
portalUrlService.setMacroService(new MockMacroService());
portalUrlService.setStyleDescriptorService(new MockStyleDescriptorService());
service = portalUrlService;
portalRequest.setMode(RenderMode.LIVE);
}
use of com.enonic.xp.portal.PortalRequest 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());
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class ResponseProcessorExecutorTest method testExecuteResponseProcessorNotImplementingMethod.
@Test
public void testExecuteResponseProcessorNotImplementingMethod() throws Exception {
final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
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().build();
try {
filterExecutor.execute(filter, request, response);
fail("Expected exception");
} catch (RenderException e) {
assertEquals("Missing exported function [responseProcessor] in response filter [/site/processors/filter1.js]", e.getMessage());
}
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class LayoutRendererTest method before.
@BeforeEach
public void before() {
this.portalRequest = new PortalRequest();
this.portalRequest.setBranch(Branch.from("draft"));
this.portalRequest.setApplicationKey(ApplicationKey.from("myapplication"));
this.portalRequest.setBaseUri("/site");
this.portalRequest.setContentPath(ContentPath.from("context/path"));
this.portalResponse = PortalResponse.create().build();
this.portalRequest.setMode(RenderMode.EDIT);
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class LayoutRendererTest method htmlResponseComponentEditMode.
@Test
public void htmlResponseComponentEditMode() {
final LayoutDescriptor layoutDescriptor = LayoutDescriptor.create().displayName("My layout component").config(Form.create().build()).key(DescriptorKey.from("module:myLayoutComponent")).regions(RegionDescriptors.create().add(RegionDescriptor.create().name("left").build()).add(RegionDescriptor.create().name("right").build()).build()).build();
final ControllerScript controllerScript = new ControllerScript() {
@Override
public PortalResponse execute(final PortalRequest portalRequest) {
return PortalResponse.create().body("<div class=\"row\"><div data-portal-region=\"left\" class=\"col-left\"></div><div data-portal-region=\"right\" class=\"col-right\"></div></div>").contentType(MediaType.HTML_UTF_8).status(HttpStatus.OK).build();
}
@Override
public void onSocketEvent(final WebSocketEvent event) {
}
};
final LayoutDescriptorService layoutDescriptorService = Mockito.mock(LayoutDescriptorService.class);
final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
renderer = new LayoutRenderer();
renderer.setLayoutDescriptorService(layoutDescriptorService);
renderer.setControllerScriptFactory(controllerScriptFactory);
when(layoutDescriptorService.getByKey(any())).thenReturn(layoutDescriptor);
when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
portalRequest.setMode(RenderMode.EDIT);
layoutComponent = LayoutComponent.create().descriptor("myapp:myLayoutComponent").descriptor(layoutDescriptor.getKey()).build();
// exercise
portalResponse = renderer.render(layoutComponent, portalRequest);
// verify
String expected = "<div data-portal-component-type=\"layout\" class=\"row\"><div data-portal-region=\"left\" class=\"col-left\"></div><div data-portal-region=\"right\" class=\"col-right\"></div></div>";
assertEquals(expected, portalResponse.getAsString());
}
Aggregations