From f6f9874c72fb2e54a92b6e6567545a243e463b43 Mon Sep 17 00:00:00 2001 From: Arpit Varshney Date: Tue, 28 Jan 2020 15:36:20 +0530 Subject: [PATCH] Added Code Coverage for Util Classses. --- .../components/ComponentDetailsUtilTest.java | 63 ++++ .../utils/components/ComponentsUtilTest.java | 97 ++++++ .../components/ContentFragmentUtilTest.java | 121 +++++++ .../aem/utils/components/ImagesUtilTest.java | 308 +++++++++++++++++- .../utils/components/ParagraphUtilTest.java | 57 ++++ .../utils/components/SlingPostUtilTest.java | 147 +++++++++ .../aem/utils/components/TagUtilTest.java | 305 +++++++++++++++++ 7 files changed, 1092 insertions(+), 6 deletions(-) create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentDetailsUtilTest.java create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentsUtilTest.java create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/ContentFragmentUtilTest.java create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/ParagraphUtilTest.java create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/SlingPostUtilTest.java create mode 100644 aemdesign-aem-services/src/test/java/design/aem/utils/components/TagUtilTest.java diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentDetailsUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentDetailsUtilTest.java new file mode 100644 index 000000000..b4dd319e3 --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentDetailsUtilTest.java @@ -0,0 +1,63 @@ +package design.aem.utils.components; + +import com.day.cq.wcm.api.Page; +import com.day.cq.wcm.api.PageManager; +import org.apache.sling.api.resource.Resource; +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.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static design.aem.utils.components.CommonUtil.PN_REDIRECT_TARGET; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(MockitoJUnitRunner.class) +public class ComponentDetailsUtilTest { + + @Mock + Page page, currentPage; + + @Mock + ResourceResolver resolver; + + @Mock + Resource contentResource; + + @Mock + ValueMap valueMap; + + @Mock + PageManager pageManager; + + @Before + public void before() { + initMocks(this); + } + + @Test + public void testCheckSelected() { + when(currentPage.getPath()).thenReturn("/content/aem-design/en/home"); + when(page.getPath()).thenReturn("/content/aem-design/en"); + boolean isSelected = ComponentDetailsUtil.checkSelected(page, currentPage, resolver); + Assert.assertTrue(isSelected); + } + + @Test + public void testCurrentPageIsRedirectTarget() { + final String REDIRECT_TARGET = "/content/aem.design/en/home/product"; + when(page.getContentResource()).thenReturn(contentResource); + when(contentResource.getValueMap()).thenReturn(valueMap); + when(valueMap.get(PN_REDIRECT_TARGET, String.class)).thenReturn(REDIRECT_TARGET); + when(resolver.adaptTo(PageManager.class)).thenReturn(pageManager); + when(pageManager.getPage(REDIRECT_TARGET)).thenReturn(currentPage); + boolean isCurrentPageIsRedirectTarget = ComponentDetailsUtil.currentPageIsRedirectTarget(page, currentPage, resolver); + Assert.assertTrue(isCurrentPageIsRedirectTarget); + } + +} + diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentsUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentsUtilTest.java new file mode 100644 index 000000000..4a91d4b5d --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentsUtilTest.java @@ -0,0 +1,97 @@ +package design.aem.utils.components; + +import org.apache.commons.imaging.common.BinaryInputStream; +import org.apache.commons.io.IOUtils; +import org.apache.jackrabbit.vault.util.JcrConstants; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.jcr.Binary; +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; +import javax.jcr.nodetype.NodeType; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(IOUtils.class) +public class ComponentsUtilTest { + + @Mock + ResourceResolver resourceResolver; + + @Mock + Resource resource; + + @Mock + Node node; + + @Mock + NodeType nodeType; + + @Mock + Property property; + + @Mock + Binary binary; + + @Mock + InputStream stream; + + @Mock + BinaryInputStream bin; + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(IOUtils.class); + } + + @Test + public void testGetResourceContent_Success_Asset() throws RepositoryException, IOException { + byte[] result = new byte[]{'a', 'b'}; + String[] paths = {"/content/dam/aem-design/campaigns/asset-test.jpg"}; + when(resourceResolver.getResource("/content/dam/aem-design/campaigns/asset-test.jpg")).thenReturn(resource); + when(resource.adaptTo(Node.class)).thenReturn(node); + when(node.getPrimaryNodeType()).thenReturn(nodeType); + when(nodeType.getName()).thenReturn("dam:Asset"); + when(node.hasNode("jcr:content/renditions/original/jcr:content")).thenReturn(true); + when(node.getNode("jcr:content/renditions/original/jcr:content")).thenReturn(node); + when(node.hasProperty(JcrConstants.JCR_DATA)).thenReturn(true); + when(node.getProperty("jcr:data")).thenReturn(property); + when(property.getBinary()).thenReturn(binary); + when(binary.getStream()).thenReturn(stream); + when(IOUtils.toByteArray(any(BufferedInputStream.class))).thenReturn(result); + String resourceContent = ComponentsUtil.getResourceContent(resourceResolver, paths, "."); + Assert.assertEquals("ab.", resourceContent); + } + + @Test + public void testGetResourceContent_Success_Page() throws RepositoryException, IOException { + String[] paths = {"/content/aem-design/home/products"}; + when(resourceResolver.getResource("/content/aem-design/home/products")).thenReturn(resource); + when(resource.adaptTo(Node.class)).thenReturn(node); + when(node.getPrimaryNodeType()).thenReturn(nodeType); + when(nodeType.getName()).thenReturn("cq:page"); + when(node.hasNode("jcr:content")).thenReturn(true); + when(node.getNode("jcr:content")).thenReturn(node); + when(node.hasProperty(JcrConstants.JCR_DATA)).thenReturn(false); + String resourceContent = ComponentsUtil.getResourceContent(resource); + Assert.assertEquals(0, resourceContent.length()); + } + +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ContentFragmentUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ContentFragmentUtilTest.java new file mode 100644 index 000000000..ad7e77301 --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ContentFragmentUtilTest.java @@ -0,0 +1,121 @@ +package design.aem.utils.components; + +import com.adobe.cq.dam.cfm.ContentElement; +import com.adobe.cq.dam.cfm.ContentFragment; +import com.adobe.cq.dam.cfm.ContentVariation; +import org.apache.sling.testing.resourceresolver.MockResource; +import org.apache.sling.testing.resourceresolver.MockResourceResolver; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.Iterator; +import java.util.Map; + +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(MockitoJUnitRunner.class) +public class ContentFragmentUtilTest { + + private static final String CONTENT_FRAGMENT_PATH = "/content/dam/aem-desigm/traditonals-shared"; + private static final String VARIATION = "VARIATION"; + + @Mock + MockResourceResolver resourceResolver; + + @Mock + ContentFragment contentFragment; + + @Mock + MockResource resource; + + @Mock + ContentElement contentElement; + + @Mock + ContentVariation variation; + + @Mock + Iterator contentElementIterator; + + @Before + public void before() { + initMocks(this); + } + + @Test + public void testGetComponentFragmentMap_EmptyFragmentPath() { + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap("", VARIATION, resourceResolver); + Assert.assertTrue(fragmentMap.isEmpty()); + } + + @Test + public void testGetComponentFragmentMap_NullFragmentPath() { + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(null, VARIATION, resourceResolver); + Assert.assertTrue(fragmentMap.isEmpty()); + } + + @Test + public void testGetComponentFragmentMap_NullContentFragment() { + when(resourceResolver.getResource(CONTENT_FRAGMENT_PATH)).thenReturn(resource); + when(resource.getResourceType()).thenReturn("/apps/valid"); + when(resource.adaptTo(ContentFragment.class)).thenReturn(null); + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, VARIATION, resourceResolver); + Assert.assertTrue(fragmentMap.isEmpty()); + } + + @Test + public void testGetComponentFragmentMap_NullVariation() { + when(resourceResolver.getResource(CONTENT_FRAGMENT_PATH)).thenReturn(resource); + when(resource.getResourceType()).thenReturn("/apps/valid"); + when(resource.adaptTo(ContentFragment.class)).thenReturn(contentFragment); + when(contentFragment.getElements()).thenReturn(contentElementIterator); + when(contentElementIterator.hasNext()).thenReturn(true, false); + when(contentElementIterator.next()).thenReturn(contentElement); + when(contentElement.getName()).thenReturn("name"); + when(contentElement.getVariation(VARIATION)).thenReturn(null); + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, VARIATION, resourceResolver); + Assert.assertNull(fragmentMap.get("name")); + } + + @Test + public void testGetComponentFragmentMap_Success_DefinedVariation() { + when(resourceResolver.getResource(CONTENT_FRAGMENT_PATH)).thenReturn(resource); + when(resource.getResourceType()).thenReturn("/apps/valid"); + when(resource.adaptTo(ContentFragment.class)).thenReturn(contentFragment); + when(contentFragment.getElements()).thenReturn(contentElementIterator); + when(contentElementIterator.hasNext()).thenReturn(true, false); + when(contentElementIterator.next()).thenReturn(contentElement); + when(contentElement.getName()).thenReturn("name"); + when(contentElement.getVariation(VARIATION)).thenReturn(variation); + when(variation.getContent()).thenReturn("content"); + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, VARIATION, resourceResolver); + Assert.assertEquals("content", fragmentMap.get("name")); + } + + @Test + public void testGetComponentFragmentMap_Success_DefaultVariation() { + when(resourceResolver.getResource(CONTENT_FRAGMENT_PATH)).thenReturn(resource); + when(resource.getResourceType()).thenReturn("/apps/valid"); + when(resource.adaptTo(ContentFragment.class)).thenReturn(contentFragment); + when(contentFragment.getElements()).thenReturn(contentElementIterator); + when(contentElementIterator.hasNext()).thenReturn(true, false); + when(contentElementIterator.next()).thenReturn(contentElement); + when(contentElement.getName()).thenReturn("name"); + when(contentElement.getContent()).thenReturn("content"); + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, null, resourceResolver); + Assert.assertEquals("content", fragmentMap.get("name")); + verify(contentElement, never()).getVariation(anyString()); + } + + @Test + public void testGetComponentFragmentMap_Exception() { + Map fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, VARIATION, null); + Assert.assertTrue(fragmentMap.isEmpty()); + } + +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ImagesUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ImagesUtilTest.java index b1221ff09..0809c59e4 100644 --- a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ImagesUtilTest.java +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ImagesUtilTest.java @@ -1,15 +1,91 @@ package design.aem.utils.components; +import com.adobe.xmp.XMPException; +import com.day.cq.dam.api.DamConstants; +import com.day.cq.dam.api.Rendition; +import com.day.cq.dam.commons.util.DamUtil; +import com.day.cq.wcm.api.Page; +import com.day.cq.wcm.foundation.Image; +import org.apache.jackrabbit.vault.util.JcrConstants; +import org.apache.sling.api.resource.Resource; +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.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; +import java.text.MessageFormat; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +import static design.aem.utils.components.ComponentsUtil.DEFAULT_IMAGE_GENERATED_FORMAT; +import static design.aem.utils.components.ComponentsUtil.DEFAULT_IMAGE_RESOURCETYPE; +import static design.aem.utils.components.ConstantsUtil.IMAGE_FILEREFERENCE; import static org.junit.Assert.*; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.whenNew; +@RunWith(PowerMockRunner.class) +@PrepareForTest({DamUtil.class, ImagesUtil.class, MessageFormat.class}) public class ImagesUtilTest { private static final Logger LOGGER = LoggerFactory.getLogger(ImagesUtilTest.class); + @Mock + Node node; + + @Mock + com.adobe.granite.asset.api.Asset graniteAsset; + + @Mock + com.day.cq.dam.api.Asset damAsset; + + @Mock + ResourceResolver resolver; + + @Mock + Resource resource; + + @Mock + Rendition rendition; + + @Mock + com.adobe.granite.asset.api.Rendition graniteRendition; + + @Mock + ValueMap valueMap; + + @Mock + Property property; + + @Mock + Page page; + + @Mock + Image image; + + private static final String PATH = "/content/aem.design/en/home"; + + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(DamUtil.class); + PowerMockito.mockStatic(MessageFormat.class); + } + @Test public void testClass() { // Run the test @@ -22,12 +98,232 @@ public void testClass() { @Test public void canRenderOnWeb() { - assertTrue(ImagesUtil.canRenderOnWeb("jpeg") ); - assertTrue(ImagesUtil.canRenderOnWeb("jpg") ); - assertTrue(ImagesUtil.canRenderOnWeb("gif") ); - assertTrue(ImagesUtil.canRenderOnWeb("png") ); + assertTrue(ImagesUtil.canRenderOnWeb("jpeg")); + assertTrue(ImagesUtil.canRenderOnWeb("jpg")); + assertTrue(ImagesUtil.canRenderOnWeb("gif")); + assertTrue(ImagesUtil.canRenderOnWeb("png")); + + assertFalse(ImagesUtil.canRenderOnWeb("bmp")); + } + + @Test + public void testGetMetadataStringForKey_Success() throws RepositoryException { + when(node.hasNode(ImagesUtil.ASSET_METADATA_FOLDER)).thenReturn(true); + when(node.getNode(ImagesUtil.ASSET_METADATA_FOLDER)).thenReturn(node); + when(DamUtil.getValue(node, "key", "defaultValue")).thenReturn("value"); + String actualValue = ImagesUtil.getMetadataStringForKey(node, "key", "defaultValue"); + Assert.assertEquals("value", actualValue); + } + + @Test + public void testGetMetadataStringForKey_NullNode() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey(null, "key", "defaultValue"); + Assert.assertNull(actualValue); + } + + @Test + public void testGetMetadataStringForKey_BlankKey() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey(node, "", "defaultValue"); + Assert.assertNull(actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromGraniteAsset_Success() throws RepositoryException { + when(graniteAsset.adaptTo(Node.class)).thenReturn(node); + when(graniteAsset.getResourceResolver()).thenReturn(resolver); + when(node.getPath()).thenReturn(PATH); + when(resolver.getResource(PATH.concat("/").concat(ImagesUtil.ASSET_METADATA_FOLDER))).thenReturn(resource); + when(resource.adaptTo(ValueMap.class)).thenReturn(valueMap); + when(valueMap.containsKey("key")).thenReturn(true); + when(valueMap.get("key")).thenReturn("value"); + String actualValue = ImagesUtil.getMetadataStringForKey(graniteAsset, "key"); + Assert.assertEquals("value", actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromGraniteAsset_NullAsset() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey((com.adobe.granite.asset.api.Asset) null, "key"); + Assert.assertNull(actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromGraniteAsset_BlankKey() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey(graniteAsset, ""); + Assert.assertNull(actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromDamAsset_Success() throws RepositoryException { + when(damAsset.getMetadata("key")).thenReturn("value"); + String actualValue = ImagesUtil.getMetadataStringForKey(damAsset, "key"); + Assert.assertEquals("value", actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromDamAsset_NullAsset() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey((com.day.cq.dam.api.Asset) null, "key"); + Assert.assertNull(actualValue); + } + + @Test + public void testGetMetadataStringForKey_FromDamAsset_BlankKey() throws RepositoryException { + String actualValue = ImagesUtil.getMetadataStringForKey(damAsset, ""); + Assert.assertNull(actualValue); + } + + @Test + public void testGetThumbnail_Success() { + List renditionList = new ArrayList<>(); + when(damAsset.getRenditions()).thenReturn(renditionList); + when(DamUtil.getBestFitRendition(300, renditionList)).thenReturn(rendition); + Rendition actualRendition = ImagesUtil.getThumbnail(damAsset, 300); + Assert.assertEquals(rendition, actualRendition); + } + + @Test + public void testGetThumbnailUrl_Success() { + when(page.getPath()).thenReturn(PATH); + when(resolver.map(PATH.concat(ImagesUtil.DEFAULT_THUMB_SELECTOR_MD))).thenReturn("/content/aem.design/en/home/jcr:content/image/file.sftmp"); + String actualUrl = ImagesUtil.getThumbnailUrl(page, resolver); + Assert.assertEquals("/content/aem.design/en/home/jcr:content/image/file.sftmp", actualUrl); + } + + @Test + public void testGetWidth_Success() throws RepositoryException { + when(node.hasNode(ImagesUtil.ASSET_METADATA_FOLDER)).thenReturn(true); + when(node.getNode(ImagesUtil.ASSET_METADATA_FOLDER)).thenReturn(node); + when(DamUtil.getValue(node, "exif:PixelXDimension", "")).thenReturn("300"); + when(DamUtil.getValue(node, "tiff:ImageWidth", "300")).thenReturn("320"); + int actualWidth = ImagesUtil.getWidth(node); + Assert.assertEquals(320, actualWidth); + } + + @Test + public void testGetProcessedImage_Success() throws Exception { + whenNew(Image.class).withArguments(resource, "/data/image").thenReturn(image); + Image actualImage = ImagesUtil.getProcessedImage(resource, "/data/image"); + Assert.assertEquals(image, actualImage); + } + + @Test + public void testGetProcessedImage_Success_NullPath() throws Exception { + whenNew(Image.class).withArguments(resource).thenReturn(image); + Image actualImage = ImagesUtil.getProcessedImage(resource, null); + Assert.assertEquals(image, actualImage); + } + + @Test + public void testGetResourceImageCustomHref_Success() { + when(resource.getChild("file")).thenReturn(resource); + when(resource.getChild(IMAGE_FILEREFERENCE)).thenReturn(resource); + when(resource.getResourceType()).thenReturn(DEFAULT_IMAGE_RESOURCETYPE); + when(resource.adaptTo(ValueMap.class)).thenReturn(valueMap); + when(valueMap.get(JcrConstants.JCR_LASTMODIFIED, Long.class)).thenReturn(1346524199000L); + when(resource.getPath()).thenReturn(PATH); + when(MessageFormat.format(DEFAULT_IMAGE_GENERATED_FORMAT, PATH, "1346524199000")).thenReturn("/content/url"); + when(resource.getResourceResolver()).thenReturn(resolver); + when(resolver.map("/content/url")).thenReturn("href"); + String actualHref = ImagesUtil.getResourceImageCustomHref(resource, "file"); + Assert.assertEquals("href", actualHref); + } + + @Test + public void testGetResourceImageCustomHref_NullResource() { + String actualHref = ImagesUtil.getResourceImageCustomHref(null, "file"); + Assert.assertEquals(0, actualHref.length()); + } + + @Test + public void testGetResourceImageCustomHref_BlankResourceName() { + String actualHref = ImagesUtil.getResourceImageCustomHref(resource, ""); + Assert.assertEquals(0, actualHref.length()); + } + + @Test + public void testGetPageImgReferencePath_Success() throws RepositoryException { + when(page.getContentResource()).thenReturn(resource); + when(resource.getChild("image")).thenReturn(resource); + when(resource.adaptTo(Node.class)).thenReturn(node); + when(node.hasProperty(IMAGE_FILEREFERENCE)).thenReturn(true); + when(node.getProperty(IMAGE_FILEREFERENCE)).thenReturn(property); + when(property.getString()).thenReturn("/content/aem.design/home/image"); + String actualPath = ImagesUtil.getPageImgReferencePath(page); + Assert.assertEquals("/content/aem.design/home/image", actualPath); + } + + @Test + public void testGetResourceImagePath_NullResource() throws RepositoryException { + String actualPath = ImagesUtil.getResourceImagePath(null, ImagesUtil.DEFAULT_IMAGE_NODE_NAME); + Assert.assertEquals(0, actualPath.length()); + } + + @Test + public void testGetAssetPropertyValueWithDefault_Success() { + when(damAsset.getMetadataValue("name")).thenReturn("value"); + String actualValue = ImagesUtil.getAssetPropertyValueWithDefault(damAsset, "name", "defaultValue"); + Assert.assertEquals("value", actualValue); + } + + @Test + public void testGetAssetPropertyValueWithDefault_NullAsset() { + String actualValue = ImagesUtil.getAssetPropertyValueWithDefault(null, "name", "defaultValue"); + Assert.assertEquals("defaultValue", actualValue); + } + + @Test + public void testGetAssetPropertyValueWithDefault_NullValue() { + when(damAsset.getMetadataValue("name")).thenReturn(null); + String actualValue = ImagesUtil.getAssetPropertyValueWithDefault(damAsset, "name", "defaultValue"); + Assert.assertEquals("defaultValue", actualValue); + } + + @Test + public void testGetAssetCopyrightInfo_Success() throws XMPException { + when(damAsset.getMetadataValue(DamConstants.DC_CREATOR)).thenReturn("assetCreator"); + when(damAsset.getMetadataValue(DamConstants.DC_CONTRIBUTOR)).thenReturn("assetContributor"); + when(damAsset.getMetadataValue(DamConstants.DC_RIGHTS)).thenReturn("assetLicense"); + when(damAsset.getMetadataValue(CommonUtil.DAM_FIELD_LICENSE_COPYRIGHT_OWNER)).thenReturn("assetCopyrightOwner"); + when(damAsset.getMetadataValue(CommonUtil.DAM_FIELD_LICENSE_USAGETERMS)).thenReturn("terms"); + when(damAsset.getMetadataValue(CommonUtil.DAM_FIELD_LICENSE_EXPIRY)).thenReturn(null); + when(MessageFormat.format("format", "assetCreator", "assetContributor", "assetLicense", "assetCopyrightOwner", "")).thenReturn("info"); + String actualInfo = ImagesUtil.getAssetCopyrightInfo(damAsset, "format"); + Assert.assertEquals("info", actualInfo); + } + + @Test + public void testGetDimension_Success_Original() throws RepositoryException { + when(graniteRendition.getName()).thenReturn("original"); + when(graniteRendition.adaptTo(com.adobe.granite.asset.api.Asset.class)).thenReturn(graniteAsset); + when(graniteAsset.adaptTo(Node.class)).thenReturn(node); + when(node.hasNode("jcr:content/metadata")).thenReturn(false); + when(node.getNode("jcr:content/metadata")).thenReturn(node); + when(node.hasProperty("tiff:ImageLength")).thenReturn(true); + when(node.getProperty("tiff:ImageLength")).thenReturn(property); + when(property.getString()).thenReturn("1000"); + int actualDimension = ImagesUtil.getDimension(graniteRendition, "tiff:ImageLength"); + Assert.assertEquals(1000, actualDimension); + } + + @Test + public void testGetWidth_Success_Rendition() throws RepositoryException { + when(graniteRendition.getName()).thenReturn("cq5dam.thumbnail.48.48.png"); + int actualDimension = ImagesUtil.getWidth(graniteRendition); + Assert.assertEquals(48, actualDimension); + } - assertFalse(ImagesUtil.canRenderOnWeb("bmp") ); + @Test + public void testGetAssetDuration_Success_NumericDMScale(){ + when(valueMap.get("xmpDM:scale", "")).thenReturn("31"); + when(valueMap.get("xmpDM:value", "")).thenReturn("123"); + Duration duration = ImagesUtil.getAssetDuration(valueMap); + Assert.assertEquals(123,duration.getSeconds()); } -} \ No newline at end of file + @Test + public void testGetAssetDuration_Success_NonNumericDMScale(){ + when(valueMap.get("xmpDM:scale", "")).thenReturn("1/44100"); + when(valueMap.get("xmpDM:value", "")).thenReturn("123"); + Duration duration = ImagesUtil.getAssetDuration(valueMap); + Assert.assertEquals(0,duration.getSeconds()); + } +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/ParagraphUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ParagraphUtilTest.java new file mode 100644 index 000000000..6cbcbf7ac --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/ParagraphUtilTest.java @@ -0,0 +1,57 @@ +package design.aem.utils.components; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import javax.servlet.http.Cookie; +import javax.servlet.jsp.JspWriter; +import java.io.IOException; + +import static design.aem.utils.components.ConstantsUtil.WCM_AUTHORING_MODE_COOKIE; +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(MockitoJUnitRunner.class) +public class ParagraphUtilTest { + + @Mock + JspWriter out; + + @Mock + SlingHttpServletRequest slingHttpServletRequest; + + @Mock + Cookie cookie; + + @Before + public void before() { + initMocks(this); + } + + @Test + public void testCloseRow_Success_WithClearFix() throws IOException { + ParagraphUtil.closeRow(out, true); + verify(out, times(2)).write(""); + verify(out).write("
"); + } + + @Test + public void testCloseRow_Success_WithoutClearFix() throws IOException { + ParagraphUtil.closeRow(out, false); + verify(out, times(2)).write(""); + verify(out, never()).write("
"); + } + + @Test + public void testUIMode_Success() { + when(slingHttpServletRequest.getCookie(WCM_AUTHORING_MODE_COOKIE)).thenReturn(cookie); + when(cookie.getValue()).thenReturn("TOUCH"); + String actualValue = ParagraphUtil.uiMode(slingHttpServletRequest); + Assert.assertEquals("TOUCH", actualValue); + } +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/SlingPostUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/SlingPostUtilTest.java new file mode 100644 index 000000000..90d0a25e3 --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/SlingPostUtilTest.java @@ -0,0 +1,147 @@ +package design.aem.utils.components; + +import com.day.cq.commons.jcr.JcrUtil; +import com.day.cq.tagging.Tag; +import com.day.cq.tagging.TagManager; +import edu.emory.mathcs.backport.java.util.Arrays; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +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.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.servlet.http.HttpServletRequest; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(JcrUtil.class) +public class SlingPostUtilTest { + + @Mock + HttpServletRequest request; + + @Mock + Node node; + + @Mock + TagManager tagManager; + + @Mock + Tag tag; + + @Mock + Property property; + + @Mock + Session session; + + @Before + public void before() { + initMocks(this); + PowerMockito.mockStatic(JcrUtil.class); + } + + @Test + public void testProcessDeletes_Success_HasProperty() throws Exception { + when(request.getParameterNames()).thenReturn(Collections.enumeration( + Arrays.asList(new String[]{"./name@Delete", "title"}))); + when(node.hasProperty("./name@Delete")).thenReturn(true); + when(node.getProperty("./name@Delete")).thenReturn(property); + SlingPostUtil.processDeletes(node, request); + verify(property).remove(); + } + + @Test + public void testProcessDeletes_Success_HasNode() throws Exception { + when(request.getParameterNames()).thenReturn(Collections.enumeration( + Arrays.asList(new String[]{"./name@Delete", "title"}))); + when(node.hasProperty("./name@Delete")).thenReturn(false); + when(node.hasNode("./name@Delete")).thenReturn(true); + when(node.getNode("./name@Delete")).thenReturn(node); + SlingPostUtil.processDeletes(node, request); + verify(node).remove(); + } + + @Test + public void testGetTagRequestParameters() { + when(request.getParameterNames()).thenReturn(Collections.enumeration( + Arrays.asList(new String[]{"tag1@cq:tags", "test1", "tag2@cq:tags", "test2"}))); + List tags = SlingPostUtil.getTagRequestParameters(request); + List expectedTags = Arrays.asList(new String[]{"tag1@cq:tags", "tag2@cq:tags"}); + Assert.assertTrue(expectedTags.equals(tags)); + } + + @Test + public void testGetProcessedTags_Success() throws Exception { + when(request.getParameterValues("search")).thenReturn(new String[]{"tag1", "tag2:tag3", "tag3"}); + List expectedTags = Arrays.asList(new String[]{"tag1", "tagId", "tag3"}); + when(tagManager.createTagByTitle("tag2:tag3", Locale.ENGLISH)).thenReturn(tag); + when(tag.getTagID()).thenReturn("tagId"); + List tags = SlingPostUtil.getProcessedTags(tagManager, "search", request); + Assert.assertTrue(expectedTags.equals(tags)); + } + + @Test + public void testGetPropertyName_Success() { + String actualValue = SlingPostUtil.getPropertyName("./name"); + Assert.assertEquals("name", actualValue); + } + + @Test + public void testGetPropertyName_Success_ContainsSlash() { + String actualValue = SlingPostUtil.getPropertyName("./image/name"); + Assert.assertEquals("name", actualValue); + } + + @Test + public void testGetParentNode_Success_AccessNode() throws RepositoryException { + when(node.hasNode("image")).thenReturn(true); + when(node.getNode("image")).thenReturn(node); + Node actualNode = SlingPostUtil.getParentNode(node, "./image/content"); + verify(node).getNode("image"); + Assert.assertEquals(node, actualNode); + } + + @Test + public void testGetParentNode_Success_CreateNode() throws RepositoryException { + when(node.hasNode("image")).thenReturn(false); + when(node.getSession()).thenReturn(session); + when(node.getPath()).thenReturn("/content/aem.design/en"); + when(JcrUtil.createPath("/content/aem.design/en/image", "nt:unstructured", session)).thenReturn(node); + Node actualNode = SlingPostUtil.getParentNode(node, "./image/content"); + Assert.assertEquals(node, actualNode); + } + + @Test + public void testWriteContent() throws RepositoryException { + final String authors[] = new String[]{"test1", "test2", "test3"}; + final String writer[] = new String[]{"test4"}; + when(request.getParameterNames()).thenReturn(Collections.enumeration( + Arrays.asList(new String[]{"./authors", "./writer"}))); + when(request.getParameterValues("./authors")).thenReturn(authors); + when(request.getParameterValues("./writer")).thenReturn(writer); + + SlingPostUtil.writeContent(node, request); + + ArgumentCaptor multiplesValues = ArgumentCaptor.forClass(String[].class); + verify(node).setProperty(eq("authors"), multiplesValues.capture(), eq(1)); + Assert.assertTrue(Arrays.equals(authors, multiplesValues.getValue())); + ArgumentCaptor singleValue = ArgumentCaptor.forClass(String.class); + verify(node).setProperty(eq("writer"), singleValue.capture(), eq(1)); + Assert.assertTrue(writer[0].equals(singleValue.getValue())); + } + +} diff --git a/aemdesign-aem-services/src/test/java/design/aem/utils/components/TagUtilTest.java b/aemdesign-aem-services/src/test/java/design/aem/utils/components/TagUtilTest.java new file mode 100644 index 000000000..a0400e501 --- /dev/null +++ b/aemdesign-aem-services/src/test/java/design/aem/utils/components/TagUtilTest.java @@ -0,0 +1,305 @@ +package design.aem.utils.components; + + +import com.day.cq.tagging.Tag; +import com.day.cq.tagging.TagManager; +import com.day.cq.wcm.api.Page; +import design.aem.services.ContentAccess; +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.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; + +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(MockitoJUnitRunner.class) +public class TagUtilTest { + + private static final String tagPath = "/content/cq:tags/men"; + + @Mock + SlingScriptHelper sling; + + @Mock + ContentAccess contentAccess; + + @Mock + ResourceResolver adminResourceResolver; + + @Mock + TagManager adminTagManager; + + @Mock + Resource resource1, resource2; + + @Mock + ValueMap tagVM1, tagVM2; + + @Mock + Tag tag1, tag2; + + @Mock + Page page; + + @Mock + Iterable resourceIterator; + + @Before + public void before() { + initMocks(this); + } + + @Test + public void testGetTagValueAsAdmin_Success() { + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve(tagPath)).thenReturn(tag1); + when(tag1.getName()).thenReturn("tag"); + when(tag1.adaptTo(Resource.class)).thenReturn(resource1); + when(resource1.getValueMap()).thenReturn(tagVM1); + when(tagVM1.containsKey("value")).thenReturn(true); + when(tagVM1.get("value", "tag")).thenReturn("tagValue"); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertEquals("tagValue", actualTag); + } + + @Test + public void testGetTagValueAsAdmin_EmptyTagPath() { + String actualTag = TagUtil.getTagValueAsAdmin("", sling); + Assert.assertTrue(actualTag.isEmpty()); + } + + @Test + public void testGetTagValueAsAdmin_NullTagPath() { + String actualTag = TagUtil.getTagValueAsAdmin(null, sling); + Assert.assertTrue(actualTag.isEmpty()); + } + + @Test + public void testGetTagValueAsAdmin_NullContentAccess() { + when(sling.getService(ContentAccess.class)).thenReturn(null); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertTrue(actualTag.isEmpty()); + } + + @Test + public void testGetTagValueAsAdmin_NullResourceResolver() { + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(null); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertTrue(actualTag.isEmpty()); + } + + @Test + public void testGetTagValueAsAdmin_NullTag() { + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve(tagPath)).thenReturn(null); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertTrue(actualTag.isEmpty()); + } + + @Test + public void testGetTagValueAsAdmin_NullResource() { + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve(tagPath)).thenReturn(tag1); + when(tag1.getName()).thenReturn("tag"); + when(tag1.adaptTo(Resource.class)).thenReturn(null); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertEquals("tag", actualTag); + } + + @Test + public void testGetTagValueAsAdmin_NullValueMap() { + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve(tagPath)).thenReturn(tag1); + when(tag1.getName()).thenReturn("tag"); + when(tag1.adaptTo(Resource.class)).thenReturn(resource1); + when(resource1.getValueMap()).thenReturn(null); + String actualTag = TagUtil.getTagValueAsAdmin(tagPath, sling); + Assert.assertEquals("tag", actualTag); + } + + @Test + public void testGetTagsAsAdmin_Success_GetTagChildren() { + String[] tagPaths = {"/content/cq:tags/gender"}; + String[] attributesToRead = {"title", "description"}; + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve("/content/cq:tags/gender")).thenReturn(tag1); + when(tag1.adaptTo(Resource.class)).thenReturn(resource1); + when(resource1.hasChildren()).thenReturn(true); + when(resource1.getChildren()).thenReturn(getResources()); + when(resource2.getPath()).thenReturn("/content/cq:tags/gender/male"); + when(adminTagManager.resolve("/content/cq:tags/gender/male")).thenReturn(tag2); + when(tag2.adaptTo(Resource.class)).thenReturn(resource1); + when(tag2.getPath()).thenReturn("/content/cq:tags/gender/male"); + when(tag2.getTitle()).thenReturn("Male"); + when(tag2.getDescription()).thenReturn("Male Clothing"); + when(tag2.getName()).thenReturn("Male"); + when(tag2.getLocalTagID()).thenReturn("localId"); + when(tag2.getTagID()).thenReturn("tagId"); + when(resource1.getValueMap()).thenReturn(tagVM1); + when(tagVM1.containsKey("value")).thenReturn(true); + when(tagVM1.get("value", "Male")).thenReturn("tagValue"); + when(tagVM1.get("title", null)).thenReturn("Male"); + when(tagVM1.get("description", null)).thenReturn("description"); + when(tagVM1.containsKey("title")).thenReturn(true); + when(tagVM1.containsKey("description")).thenReturn(true); + LinkedHashMap actualTagsMap = TagUtil.getTagsAsAdmin(sling, tagPaths, Locale.getDefault(), attributesToRead, true); + Assert.assertEquals("/content/cq:tags/gender/male", actualTagsMap.get("tagId").get("path")); + Assert.assertEquals("localId", actualTagsMap.get("tagId").get("tagid")); + Assert.assertEquals("description", actualTagsMap.get("tagId").get("description")); + Assert.assertEquals("Male", actualTagsMap.get("tagId").get("title")); + } + + @Test + public void testGetTagsAsAdmin_Success_NoTagChildren() { + String[] tagPaths = {"/content/cq:tags/male"}; + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve("/content/cq:tags/male")).thenReturn(tag1); + when(tag1.getTitle()).thenReturn("Male"); + when(tag1.getDescription()).thenReturn("Male Clothing"); + when(tag1.getPath()).thenReturn("/content/cq:tags/gender/male"); + when(tag1.getName()).thenReturn("Male"); + when(tag1.getLocalTagID()).thenReturn("localId"); + when(tag1.getTagID()).thenReturn("tagId"); + when(tag1.adaptTo(Resource.class)).thenReturn(resource1); + when(resource1.getValueMap()).thenReturn(tagVM1); + when(tagVM1.containsKey("value")).thenReturn(true); + when(tagVM1.get("value", "Male")).thenReturn("tagValue"); + LinkedHashMap actualTagsMap = TagUtil.getTagsAsAdmin(sling, tagPaths, Locale.getDefault()); + Assert.assertEquals("/content/cq:tags/gender/male", actualTagsMap.get("tagId").get("path")); + Assert.assertEquals("localId", actualTagsMap.get("tagId").get("tagid")); + Assert.assertEquals("Male Clothing", actualTagsMap.get("tagId").get("description")); + Assert.assertEquals("Male", actualTagsMap.get("tagId").get("title")); + Assert.assertEquals("tagValue", actualTagsMap.get("tagId").get("value")); + } + + @Test + public void testGetTagsAsValuesAsAdmin_Success() { + String[] tagPaths = {"/content/cq:tags/male", "/content/cq:tags/female"}; + when(sling.getService(ContentAccess.class)).thenReturn(contentAccess); + when(contentAccess.getAdminResourceResolver()).thenReturn(adminResourceResolver); + when(adminResourceResolver.adaptTo(TagManager.class)).thenReturn(adminTagManager); + when(adminTagManager.resolve("/content/cq:tags/male")).thenReturn(tag1); + when(adminTagManager.resolve("/content/cq:tags/female")).thenReturn(tag2); + when(tag1.getName()).thenReturn("tag1"); + when(tag2.getName()).thenReturn("tag2"); + mockTag(tag1, "tag1", "tag1", resource1, tagVM1); + mockTag(tag2, "tag2", "tag2", resource2, tagVM2); + String actualValue = TagUtil.getTagsAsValuesAsAdmin(sling, ":", tagPaths); + Assert.assertEquals("tag1:tag2", actualValue); + } + + @Test + public void testGetTagsAsValuesAsAdmin_NullTagPath() { + String actualValue = TagUtil.getTagsAsValuesAsAdmin(sling, ":", null); + Assert.assertNull(actualValue); + } + + @Test + public void testGetTagsAsValues_Success() { + String[] tagPaths = {"/content/cq:tags/male", "/content/cq:tags/female"}; + when(adminTagManager.resolve("/content/cq:tags/male")).thenReturn(tag1); + when(adminTagManager.resolve("/content/cq:tags/female")).thenReturn(tag2); + when(tag1.getName()).thenReturn("tag1"); + when(tag2.getName()).thenReturn("tag2"); + mockTag(tag1, "tag1", "tag1", resource1, tagVM1); + mockTag(tag2, "tag2", "tag2", resource2, tagVM2); + String actualValue = TagUtil.getTagsAsValues(adminTagManager, adminResourceResolver, ":", tagPaths); + Assert.assertEquals("tag1:tag2", actualValue); + } + + @Test + public void testGetTagsAsValues_NullTagPath() { + String actualValue = TagUtil.getTagsAsValues(adminTagManager, adminResourceResolver, ":", null); + Assert.assertNull(actualValue); + } + + @Test + public void testGetTagsValues_Success() { + String[] tagPaths = {"/content/cq:tags/male", "/content/cq:tags/female"}; + when(adminTagManager.resolve("/content/cq:tags/male")).thenReturn(tag1); + when(adminTagManager.resolve("/content/cq:tags/female")).thenReturn(tag2); + when(tag1.getName()).thenReturn("tag1"); + when(tag2.getName()).thenReturn("tag2"); + mockTag(tag1, "tag1", "tag1", resource1, tagVM1); + mockTag(tag2, "tag2", "tag2", resource2, tagVM2); + String[] actualValue = TagUtil.getTagsValues(adminTagManager, adminResourceResolver, ":", tagPaths); + Assert.assertEquals("tag1", actualValue[0]); + Assert.assertEquals("tag2", actualValue[1]); + } + + @Test + public void testGetTagsValues_NullTagPath() { + String[] actualValue = TagUtil.getTagsValues(adminTagManager, adminResourceResolver, ":", null); + Assert.assertNull(actualValue); + } + + @Test + public void testGetTagsValues_NullResource() { + String[] tagPaths = {"/content/cq:tags/male", "/content/cq:tags/female"}; + when(adminTagManager.resolve("/content/cq:tags/male")).thenReturn(tag1); + when(adminTagManager.resolve("/content/cq:tags/female")).thenReturn(tag2); + when(tag1.getName()).thenReturn("tag1"); + when(tag2.getName()).thenReturn("tag2"); + when(tag1.adaptTo(Resource.class)).thenReturn(null); + when(tag2.adaptTo(Resource.class)).thenReturn(null); + String[] actualValue = TagUtil.getTagsValues(adminTagManager, adminResourceResolver, ":", tagPaths); + Assert.assertEquals(0, actualValue.length); + } + + @Test + public void testGetPathFromTagId() { + String path = TagUtil.getPathFromTagId("gender:male/size", "/content/cq:tags"); + Assert.assertEquals("/content/cq:tags/gender/male/size", path); + } + + @Test + public void testGetPageTags_Success() { + when(page.getTags()).thenReturn(new Tag[]{tag1}); + Tag[] tags = TagUtil.getPageTags(page); + Assert.assertEquals(tag1, tags[0]); + } + + @Test + public void testGetPageTags_NullPage() { + Tag[] tags = TagUtil.getPageTags(null); + Assert.assertEquals(0, tags.length); + } + + private Iterable getResources() { + Iterable resources = Arrays.asList(resource2); + return resources; + } + + private void mockTag(Tag tag, final String value, final String name, final Resource resource, final ValueMap tagVM) { + when(tag.adaptTo(Resource.class)).thenReturn(resource); + when(resource.getValueMap()).thenReturn(tagVM); + when(tagVM.containsKey("value")).thenReturn(true); + when(tagVM.get("value", name)).thenReturn(value); + } + +}