Search in sources :

Example 6 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class PartRendererTest method htmlResponseComponentEditModeNoMethodToHandleReq.

@Test
public void htmlResponseComponentEditModeNoMethodToHandleReq() {
    final PartDescriptor partDescriptor = PartDescriptor.create().displayName("My part component").config(Form.create().build()).key(DescriptorKey.from("module:myPartComponent")).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return new PortalResponseSerializer(null, HttpStatus.METHOD_NOT_ALLOWED).serialize();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final PartDescriptorService partDescriptorService = Mockito.mock(PartDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new PartRenderer();
    renderer.setPartDescriptorService(partDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(partDescriptorService.getByKey(any())).thenReturn(partDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    partComponent = PartComponent.create().descriptor("myapp:myPartComponent").descriptor(partDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(partComponent, portalRequest);
    // verify
    String expected = "<div data-portal-component-type=\"part\" data-portal-placeholder=\"true\" data-portal-placeholder-error=\"true\"><span class=\"data-portal-placeholder-error\">No method provided to handle request</span></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) PartDescriptor(com.enonic.xp.region.PartDescriptor) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 7 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class ErrorHandlerScriptImpl method doExecute.

private PortalResponse doExecute(final PortalError portalError, final String handlerMethod) {
    Tracer.withCurrent(this::addTraceInfo);
    final PortalErrorMapper portalErrorMapper = new PortalErrorMapper(portalError);
    final ScriptValue result = this.scriptExports.executeMethod(handlerMethod, portalErrorMapper);
    if ((result == null) || !result.isObject()) {
        return null;
    } else {
        return new PortalResponseSerializer(result, portalError.getStatus()).defaultPostProcess(false).applyFilters(false).serialize();
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) PortalErrorMapper(com.enonic.xp.portal.impl.mapper.PortalErrorMapper)

Example 8 with PortalResponseSerializer

use of com.enonic.xp.portal.impl.controller.PortalResponseSerializer in project xp by enonic.

the class PartRendererTest method nullResponseComponentEditMode.

@Test
public void nullResponseComponentEditMode() {
    final PartDescriptor partDescriptor = PartDescriptor.create().displayName("My part component").config(Form.create().build()).key(DescriptorKey.from("module:myPartComponent")).build();
    final ControllerScript controllerScript = new ControllerScript() {

        @Override
        public PortalResponse execute(final PortalRequest portalRequest) {
            return new PortalResponseSerializer(null).serialize();
        }

        @Override
        public void onSocketEvent(final WebSocketEvent event) {
        }
    };
    final PartDescriptorService partDescriptorService = Mockito.mock(PartDescriptorService.class);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    renderer = new PartRenderer();
    renderer.setPartDescriptorService(partDescriptorService);
    renderer.setControllerScriptFactory(controllerScriptFactory);
    when(partDescriptorService.getByKey(any())).thenReturn(partDescriptor);
    when(controllerScriptFactory.fromDir(any())).thenReturn(controllerScript);
    portalRequest.setMode(RenderMode.EDIT);
    partComponent = PartComponent.create().descriptor("myapp:myPartComponent").descriptor(partDescriptor.getKey()).build();
    // exercise
    portalResponse = renderer.render(partComponent, portalRequest);
    // verify
    String expected = "<div data-portal-component-type=\"part\"></div>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) PortalResponseSerializer(com.enonic.xp.portal.impl.controller.PortalResponseSerializer) WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) PartDescriptor(com.enonic.xp.region.PartDescriptor) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Aggregations

PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)8 ScriptValue (com.enonic.xp.script.ScriptValue)5 PortalRequest (com.enonic.xp.portal.PortalRequest)3 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)3 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)3 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)3 WebSocketEvent (com.enonic.xp.web.websocket.WebSocketEvent)3 Test (org.junit.jupiter.api.Test)3 PartDescriptor (com.enonic.xp.region.PartDescriptor)2 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)2 PortalErrorMapper (com.enonic.xp.portal.impl.mapper.PortalErrorMapper)1 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)1 LayoutDescriptor (com.enonic.xp.region.LayoutDescriptor)1 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)1 WebException (com.enonic.xp.web.WebException)1