Search in sources :

Example 11 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroInstruction method resolveMacroDescriptorCaseInsensitive.

private MacroDescriptor resolveMacroDescriptorCaseInsensitive(final SiteConfigs siteConfigs, final String macroName) {
    for (SiteConfig siteConfig : siteConfigs) {
        final MacroDescriptors macroDescriptors = macroDescriptorService.getByApplication(siteConfig.getApplicationKey());
        final MacroDescriptor macroDescriptor = macroDescriptors.stream().filter((md) -> md.getName().equalsIgnoreCase(macroName)).findFirst().orElse(null);
        if (macroDescriptor != null) {
            return macroDescriptor;
        }
    }
    return null;
}
Also used : MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) SiteConfig(com.enonic.xp.site.SiteConfig) MacroDescriptors(com.enonic.xp.macro.MacroDescriptors)

Example 12 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor 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 13 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor 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 14 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor 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 15 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroInstructionTest method testInstructionMissingMacro.

@Test
public void testInstructionMissingMacro() throws Exception {
    MacroKey key = MacroKey.from("myapp:somemacro");
    Form form = Form.create().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));
    String outputHtml = macroInstruction.evaluate(portalRequest, "MACRO _name=\"mymacro\" param1=\"value1\" _body=\"body\"").getAsString();
    assertEquals("[mymacro param1=\"value1\"]body[/mymacro]", outputHtml);
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Form(com.enonic.xp.form.Form) Test(org.junit.jupiter.api.Test)

Aggregations

MacroDescriptor (com.enonic.xp.macro.MacroDescriptor)17 MacroKey (com.enonic.xp.macro.MacroKey)12 Test (org.junit.jupiter.api.Test)9 MacroDescriptors (com.enonic.xp.macro.MacroDescriptors)7 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)6 ApplicationKey (com.enonic.xp.app.ApplicationKey)5 Form (com.enonic.xp.form.Form)5 Input (com.enonic.xp.form.Input)5 MacroProcessor (com.enonic.xp.portal.macro.MacroProcessor)5 Site (com.enonic.xp.site.Site)5 SiteConfig (com.enonic.xp.site.SiteConfig)5 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 InputTypeName (com.enonic.xp.inputtype.InputTypeName)4 MacroDescriptorService (com.enonic.xp.macro.MacroDescriptorService)4 PortalRequest (com.enonic.xp.portal.PortalRequest)4 PortalResponse (com.enonic.xp.portal.PortalResponse)4 MacroProcessorFactory (com.enonic.xp.portal.macro.MacroProcessorFactory)4 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)4