Search in sources :

Example 76 with PortalRequest

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);
}
Also used : PortalUrlServiceImpl(com.enonic.xp.portal.impl.url.PortalUrlServiceImpl) PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 77 with PortalRequest

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

Example 78 with PortalRequest

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());
    }
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 79 with PortalRequest

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);
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 80 with PortalRequest

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());
}
Also used : WebSocketEvent(com.enonic.xp.web.websocket.WebSocketEvent) LayoutDescriptor(com.enonic.xp.region.LayoutDescriptor) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7