Search in sources :

Example 96 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MacroInstructionTest method setUp.

@BeforeEach
public void setUp() {
    macroDescriptorService = Mockito.mock(MacroDescriptorService.class);
    macroProcessorFactory = Mockito.mock(MacroProcessorFactory.class);
    macroInstruction = new MacroInstruction();
    macroInstruction.setMacroDescriptorService(macroDescriptorService);
    macroInstruction.setMacroProcessorFactory(macroProcessorFactory);
    portalRequest = new PortalRequest();
    Site site = createSite("site-id", "site-name", "myapplication:content-type");
    portalRequest.setSite(site);
    portalRequest.setContent(site);
}
Also used : Site(com.enonic.xp.site.Site) MacroDescriptorService(com.enonic.xp.macro.MacroDescriptorService) MacroProcessorFactory(com.enonic.xp.portal.macro.MacroProcessorFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 97 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MacroInstructionTest method testInstructionMacro.

@Test
public void testInstructionMacro() throws Exception {
    MacroKey key = MacroKey.from("myapp:mymacro");
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParameter("param1").get(0) + ", body=" + ctx.getBody()).build();
    when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MacroProcessorFactory(com.enonic.xp.portal.macro.MacroProcessorFactory) SiteConfigsDataSerializer(com.enonic.xp.site.SiteConfigsDataSerializer) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ContentId(com.enonic.xp.content.ContentId) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Mockito.when(org.mockito.Mockito.when) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Test(org.junit.jupiter.api.Test) Form(com.enonic.xp.form.Form) MacroDescriptors(com.enonic.xp.macro.MacroDescriptors) Mockito(org.mockito.Mockito) MacroDescriptorService(com.enonic.xp.macro.MacroDescriptorService) InputTypeName(com.enonic.xp.inputtype.InputTypeName) Site(com.enonic.xp.site.Site) PrincipalKey(com.enonic.xp.security.PrincipalKey) SiteConfigs(com.enonic.xp.site.SiteConfigs) Input(com.enonic.xp.form.Input) MacroKey(com.enonic.xp.macro.MacroKey) MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) Test(org.junit.jupiter.api.Test)

Example 98 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MacroInstructionTest method testInstructionSystemMacro.

@Test
public void testInstructionSystemMacro() throws Exception {
    MacroKey key = MacroKey.from(ApplicationKey.SYSTEM, "mymacro");
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    when(macroDescriptorService.getByApplication(any())).thenReturn(MacroDescriptors.empty());
    MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParam("param1") + ", body=" + ctx.getBody()).build();
    when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MacroProcessorFactory(com.enonic.xp.portal.macro.MacroProcessorFactory) SiteConfigsDataSerializer(com.enonic.xp.site.SiteConfigsDataSerializer) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ContentId(com.enonic.xp.content.ContentId) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Mockito.when(org.mockito.Mockito.when) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Test(org.junit.jupiter.api.Test) Form(com.enonic.xp.form.Form) MacroDescriptors(com.enonic.xp.macro.MacroDescriptors) Mockito(org.mockito.Mockito) MacroDescriptorService(com.enonic.xp.macro.MacroDescriptorService) InputTypeName(com.enonic.xp.inputtype.InputTypeName) Site(com.enonic.xp.site.Site) PrincipalKey(com.enonic.xp.security.PrincipalKey) SiteConfigs(com.enonic.xp.site.SiteConfigs) Input(com.enonic.xp.form.Input) MacroKey(com.enonic.xp.macro.MacroKey) MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) Test(org.junit.jupiter.api.Test)

Example 99 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MacroInstructionTest method testInstructionMacroParamsCaseInsensitive.

@Test
public void testInstructionMacroParamsCaseInsensitive() throws Exception {
    MacroKey key = MacroKey.from("myapp:mymacro");
    Form form = Form.create().addFormItem(createTextLineInput("param1", "Param 1").occurrences(1, 1).build()).addFormItem(createTextLineInput("param2", "Param 2").occurrences(1, 1).build()).build();
    MacroDescriptor macroDescriptor = MacroDescriptor.create().key(key).form(form).build();
    when(macroDescriptorService.getByKey(key)).thenReturn(macroDescriptor);
    when(macroDescriptorService.getByApplication(key.getApplicationKey())).thenReturn(MacroDescriptors.from(macroDescriptor));
    MacroProcessor macro = (ctx) -> PortalResponse.create().body(ctx.getName() + ": param1=" + ctx.getParam("param1") + ", body=" + ctx.getBody()).build();
    when(macroProcessorFactory.fromScript(any())).thenReturn(macro);
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"MYMACRO\" PARAM1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("mymacro: param1=value1, body=body", outputHtml);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MacroProcessorFactory(com.enonic.xp.portal.macro.MacroProcessorFactory) SiteConfigsDataSerializer(com.enonic.xp.site.SiteConfigsDataSerializer) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ContentId(com.enonic.xp.content.ContentId) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Mockito.when(org.mockito.Mockito.when) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Test(org.junit.jupiter.api.Test) Form(com.enonic.xp.form.Form) MacroDescriptors(com.enonic.xp.macro.MacroDescriptors) Mockito(org.mockito.Mockito) MacroDescriptorService(com.enonic.xp.macro.MacroDescriptorService) InputTypeName(com.enonic.xp.inputtype.InputTypeName) Site(com.enonic.xp.site.Site) PrincipalKey(com.enonic.xp.security.PrincipalKey) SiteConfigs(com.enonic.xp.site.SiteConfigs) Input(com.enonic.xp.form.Input) MacroKey(com.enonic.xp.macro.MacroKey) MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Form(com.enonic.xp.form.Form) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) Test(org.junit.jupiter.api.Test)

Example 100 with PortalRequest

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

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