diff --git a/aemdesign-aem-services/src/test/java/design/aem/models/v2/analytics/DataLayerTest.java b/aemdesign-aem-services/src/test/java/design/aem/models/v2/analytics/DataLayerTest.java new file mode 100644 index 000000000..254c768f6 --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/models/v2/analytics/DataLayerTest.java @@ -0,0 +1,106 @@ +package design.aem.models.v2.analytics; + +import com.day.cq.replication.ReplicationStatus; +import com.day.cq.wcm.api.Page; +import com.day.cq.wcm.api.components.Component; +import design.aem.components.ComponentProperties; +import design.aem.utils.components.CommonUtil; +import design.aem.utils.components.ComponentsUtil; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.api.scripting.SlingScriptHelper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.script.Bindings; +import java.util.Locale; + +import static design.aem.utils.components.CommonUtil.DEFAULT_LIST_DETAILS_SUFFIX; +import static design.aem.utils.components.ComponentsUtil.*; +import static java.text.MessageFormat.format; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ComponentsUtil.class, CommonUtil.class}) +public class DataLayerTest { + + @InjectMocks + DataLayer dataLayer = new DataLayer(); + + @Mock + Page page; + + @Mock + SlingHttpServletRequest request; + + @Mock + ValueMap properties; + + @Mock + Bindings bindings; + + @Mock + ResourceResolver resourceResolver; + + @Mock + Component component; + + @Mock + SlingScriptHelper helper; + + @Mock + Resource resource; + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(ComponentsUtil.class); + PowerMockito.mockStatic(CommonUtil.class); + when(bindings.get("resourcePage")).thenReturn(page); + when(bindings.get("properties")).thenReturn(properties); + when(bindings.get("request")).thenReturn(request); + when(bindings.get("components")).thenReturn(component); + when(bindings.get("sling")).thenReturn(helper); + when(request.getResourceResolver()).thenReturn(resourceResolver); + } + + @Test + public void testActivate_Success() throws Exception { + Locale locale = new Locale("en", "australia"); + ComponentProperties componentProperties = new ComponentProperties(); + when(page.getPath()).thenReturn("path"); + when(properties.get(ReplicationStatus.NODE_PROPERTY_LAST_REPLICATED, "")).thenReturn(""); + when(ComponentsUtil.getNewComponentProperties(dataLayer)).thenReturn(componentProperties); + when(page.getLanguage(false)).thenReturn(locale); + when(CommonUtil.findComponentInPage(page, DEFAULT_LIST_DETAILS_SUFFIX)).thenReturn("path"); + when(resourceResolver.getResource("path")).thenReturn(resource); + when(resource.getResourceType()).thenReturn("aemdesign/components/lists/list/v2"); + when(resource.adaptTo(ValueMap.class)).thenReturn(properties); + when(properties.get(DETAILS_ANALYTICS_PAGENAME, "")).thenReturn("name"); + when(properties.get(DETAILS_ANALYTICS_PAGETYPE, "")).thenReturn("full-width"); + when(properties.get(DETAILS_ANALYTICS_PLATFORM, "aem")).thenReturn("mobile"); + when(properties.get(DETAILS_ANALYTICS_ABORT, "false")).thenReturn("true"); + when(properties.get(DETAILS_ANALYTICS_VARIANT, DEFAULT_VARIANT)).thenReturn("variant"); + when(ComponentsUtil.getComponentVariantTemplate(component, format(COMPONENT_VARIANT_TEMPLATE_FORMAT, "variant"), helper)).thenReturn("template"); + dataLayer.activate(); + Assert.assertEquals("template", dataLayer.getComponentProperties().get("variantTemplate")); + Assert.assertEquals("full-width", dataLayer.getComponentProperties().get("pageType")); + Assert.assertEquals("true", dataLayer.getComponentProperties().get("abort")); + Assert.assertEquals("AUSTRALIA", dataLayer.getComponentProperties().get("contentCountry")); + Assert.assertEquals("english", dataLayer.getComponentProperties().get("contentLanguage")); + Assert.assertEquals("path", dataLayer.getComponentProperties().get("pagePath")); + Assert.assertEquals("name", dataLayer.getComponentProperties().get("pageName")); + Assert.assertEquals("", dataLayer.getComponentProperties().get("effectiveDate")); + Assert.assertEquals("mobile", dataLayer.getComponentProperties().get("platform")); + } +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/RedirectNotificationTest.java b/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/RedirectNotificationTest.java new file mode 100644 index 000000000..cee8b23bb --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/RedirectNotificationTest.java @@ -0,0 +1,95 @@ +package design.aem.models.v2.common; + +import com.day.cq.wcm.api.Page; +import com.day.cq.wcm.api.PageManager; +import design.aem.components.ComponentProperties; +import design.aem.utils.components.ComponentsUtil; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.ValueMap; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.script.Bindings; + +import static design.aem.utils.components.CommonUtil.PN_REDIRECT_TARGET; +import static design.aem.utils.components.ComponentsUtil.DEFAULT_FIELDS_ACCESSIBILITY; +import static design.aem.utils.components.ComponentsUtil.DEFAULT_FIELDS_STYLE; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ComponentsUtil.class}) +public class RedirectNotificationTest { + + @Mock + private Bindings bindings; + + @Mock + private ValueMap properties; + + @Mock + private SlingHttpServletRequest request; + + @Mock + PageManager pageManager; + + @Mock + Page page; + + @InjectMocks + RedirectNotification redirectNotification = new RedirectNotification(); + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(ComponentsUtil.class); + when(bindings.get("request")).thenReturn(request); + when(bindings.get("pageProperties")).thenReturn(properties); + when(bindings.get("pageManager")).thenReturn(pageManager); + } + + @Test + public void testReady_Success_ExternalUrl() { + when(properties.get(PN_REDIRECT_TARGET, "")).thenReturn("https://www.linkedin.com/"); + ComponentProperties componentProperties = new ComponentProperties(); + componentProperties.put("redirectUrl", "https://www.linkedin.com/"); + componentProperties.put("redirectTitle", "redirectTitle"); + when(ComponentsUtil.getComponentProperties(eq(redirectNotification), + any(Object[][].class), + eq(DEFAULT_FIELDS_STYLE), + eq(DEFAULT_FIELDS_ACCESSIBILITY))).thenReturn(componentProperties); + redirectNotification.ready(); + Assert.assertEquals("redirectTitle", redirectNotification.getComponentProperties().get("redirectTitle")); + Assert.assertEquals("https://www.linkedin.com/", redirectNotification.getComponentProperties().get("redirectUrl")); + Assert.assertEquals("redirectIsSet", redirectNotification.getComponentProperties().get("redirectIsSet")); + Assert.assertEquals("redirectIsNotSet", redirectNotification.getComponentProperties().get("redirectIsNotSet")); + } + + @Test + public void testReady_Success_InternalUrl() { + when(properties.get(PN_REDIRECT_TARGET, "")).thenReturn("/content/aem.design/home/about-us"); + when(pageManager.getPage("/content/aem.design/home/about")).thenReturn(page); + ComponentProperties componentProperties = new ComponentProperties(); + componentProperties.put("redirectUrl", "/content/aem.design/home/about"); + componentProperties.put("redirectTitle", "redirectTitle"); + when(ComponentsUtil.getComponentProperties(eq(redirectNotification), + any(Object[][].class), + eq(DEFAULT_FIELDS_STYLE), + eq(DEFAULT_FIELDS_ACCESSIBILITY))).thenReturn(componentProperties); + redirectNotification.ready(); + Assert.assertEquals("redirectTitle", redirectNotification.getComponentProperties().get("redirectTitle")); + Assert.assertEquals("/content/aem.design/home/about.html", redirectNotification.getComponentProperties().get("redirectUrl")); + Assert.assertEquals("redirectIsSet", redirectNotification.getComponentProperties().get("redirectIsSet")); + Assert.assertEquals("redirectIsNotSet", redirectNotification.getComponentProperties().get("redirectIsNotSet")); + } + +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/StaticIncludeTest.java b/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/StaticIncludeTest.java new file mode 100644 index 000000000..0c96b661d --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/models/v2/common/StaticIncludeTest.java @@ -0,0 +1,92 @@ +package design.aem.models.v2.common; + +import com.adobe.cq.sightly.SightlyWCMMode; +import com.day.cq.wcm.api.components.Component; +import design.aem.components.ComponentProperties; +import design.aem.utils.components.ComponentsUtil; +import org.apache.jackrabbit.vault.util.JcrConstants; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.script.Bindings; + +import static design.aem.utils.components.ComponentsUtil.DEFAULT_FIELDS_ACCESSIBILITY; +import static design.aem.utils.components.ComponentsUtil.DEFAULT_FIELDS_STYLE; +import static design.aem.utils.components.ConstantsUtil.SITE_INCLUDE_PATHS; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ComponentsUtil.class}) +public class StaticIncludeTest { + + @Mock + private SlingHttpServletRequest request; + + @Mock + private Bindings bindings; + + @Mock + Component component; + + @Mock + ValueMap valueMap; + + @Mock + SightlyWCMMode wcmMode; + + @Mock + ResourceResolver resourceResolver; + + @InjectMocks + StaticInclude staticInclude = new StaticInclude(); + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(ComponentsUtil.class); + when(bindings.get("request")).thenReturn(request); + when(bindings.get("component")).thenReturn(component); + when(bindings.get("properties")).thenReturn(valueMap); + when(bindings.get("wcmmode")).thenReturn(wcmMode); + when(component.getProperties()).thenReturn(valueMap); + when(request.getResourceResolver()).thenReturn(resourceResolver); + } + + @Test + public void testReady_Success() { + final String[] paths = new String[]{"/path1"}; + ComponentProperties componentProperties = new ComponentProperties(); + when(ComponentsUtil.getComponentProperties(eq(staticInclude), + any(Object[][].class), + eq(DEFAULT_FIELDS_STYLE), + eq(DEFAULT_FIELDS_ACCESSIBILITY))).thenReturn(componentProperties); + when(valueMap.get(JcrConstants.JCR_TITLE, "")).thenReturn("title"); + when(valueMap.get(SITE_INCLUDE_PATHS, new String[0])).thenReturn(paths); + when(valueMap.get("showContentPreview", "false")).thenReturn("true"); + when(valueMap.get("showContent", "false")).thenReturn("true"); + when(ComponentsUtil.getResourceContent(resourceResolver, paths, "")).thenReturn("resource-content"); + when(wcmMode.isEdit()).thenReturn(true); + staticInclude.ready(); + Assert.assertTrue((Boolean) staticInclude.getComponentProperties().get("hasContent")); + Assert.assertTrue((Boolean) staticInclude.getComponentProperties().get("showContentPreview")); + Assert.assertTrue((Boolean) staticInclude.getComponentProperties().get("showContentSet")); + Assert.assertTrue((Boolean) staticInclude.getComponentProperties().get("showContent")); + Assert.assertEquals("resource-content", staticInclude.getComponentProperties().get("includeContents")); + Assert.assertEquals("parentnotfound", staticInclude.getComponentProperties().get("parentnotfound")); + Assert.assertEquals("title", staticInclude.getComponentProperties().get("componentName")); + Assert.assertEquals("/path1", staticInclude.getComponentProperties().get("includePaths")); + } +}