Search in sources :

Example 6 with ControllerScriptFactory

use of com.enonic.xp.portal.controller.ControllerScriptFactory 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 ControllerScriptFactory

use of com.enonic.xp.portal.controller.ControllerScriptFactory in project xp by enonic.

the class PartRendererTest method htmlResponseComponentEditMode.

@Test
public void htmlResponseComponentEditMode() {
    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 PortalResponse.create().body("<h1 class=\"important\">My component</h1>").contentType(MediaType.HTML_UTF_8).status(HttpStatus.OK).build();
        }

        @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 = "<h1 data-portal-component-type=\"part\" class=\"important\">My component</h1>";
    assertEquals(expected, portalResponse.getAsString());
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) 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 8 with ControllerScriptFactory

use of com.enonic.xp.portal.controller.ControllerScriptFactory 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)

Example 9 with ControllerScriptFactory

use of com.enonic.xp.portal.controller.ControllerScriptFactory in project xp by enonic.

the class ServiceHandlerTest method setup.

@BeforeEach
public final void setup() throws Exception {
    this.request = new PortalRequest();
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    this.controllerScript = Mockito.mock(ControllerScript.class);
    Mockito.when(controllerScriptFactory.fromDir(Mockito.any())).thenReturn(this.controllerScript);
    final PortalResponse portalResponse = PortalResponse.create().build();
    Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
    this.resourceService = Mockito.mock(ResourceService.class);
    final Resource resourceNotFound = Mockito.mock(Resource.class);
    Mockito.when(resourceNotFound.exists()).thenReturn(false);
    final Resource resource = Mockito.mock(Resource.class);
    Mockito.when(resource.exists()).thenReturn(true);
    Mockito.when(this.resourceService.getResource(ResourceKey.from("demo:/services/test"))).thenReturn(resourceNotFound);
    this.serviceDescriptorService = Mockito.mock(ServiceDescriptorService.class);
    final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
    final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).build();
    Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
    this.contentService = Mockito.mock(ContentService.class);
    this.handler = new ServiceHandler();
    this.handler.setControllerScriptFactory(controllerScriptFactory);
    this.handler.setContentService(this.contentService);
    this.handler.setResourceService(this.resourceService);
    this.handler.setServiceDescriptorService(this.serviceDescriptorService);
    this.request.setMethod(HttpMethod.GET);
    this.request.setContentPath(ContentPath.from("/site/somepath/content"));
    this.request.setEndpointPath("/_/service/demo/myservice");
    this.request.setRawPath("/site/draft/site/somepath/content/_/service/demo/myservice");
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) ResourceService(com.enonic.xp.resource.ResourceService) Resource(com.enonic.xp.resource.Resource) DescriptorKey(com.enonic.xp.page.DescriptorKey) ContentService(com.enonic.xp.content.ContentService) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) ServiceDescriptorService(com.enonic.xp.service.ServiceDescriptorService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)9 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)9 WebSocketEvent (com.enonic.xp.web.websocket.WebSocketEvent)5 Test (org.junit.jupiter.api.Test)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ContentService (com.enonic.xp.content.ContentService)3 PortalResponse (com.enonic.xp.portal.PortalResponse)3 PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)3 PartDescriptor (com.enonic.xp.region.PartDescriptor)3 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)3 DescriptorKey (com.enonic.xp.page.DescriptorKey)2 LayoutDescriptor (com.enonic.xp.region.LayoutDescriptor)2 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)2 Resource (com.enonic.xp.resource.Resource)2 ResourceService (com.enonic.xp.resource.ResourceService)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AdminToolDescriptorService (com.enonic.xp.admin.tool.AdminToolDescriptorService)1 WidgetDescriptorService (com.enonic.xp.admin.widget.WidgetDescriptorService)1 FilterScript (com.enonic.xp.portal.filter.FilterScript)1