-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from aem-design/feature/code-coverage
Added Code Coverage for Util Classses.
- Loading branch information
Showing
7 changed files
with
1,092 additions
and
6 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...sign-aem-services/src/test/java/design/aem/utils/components/ComponentDetailsUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
97 changes: 97 additions & 0 deletions
97
aemdesign-aem-services/src/test/java/design/aem/utils/components/ComponentsUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
...esign-aem-services/src/test/java/design/aem/utils/components/ContentFragmentUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ContentElement> contentElementIterator; | ||
|
||
@Before | ||
public void before() { | ||
initMocks(this); | ||
} | ||
|
||
@Test | ||
public void testGetComponentFragmentMap_EmptyFragmentPath() { | ||
Map<String, Object> fragmentMap = ContentFragmentUtil.getComponentFragmentMap("", VARIATION, resourceResolver); | ||
Assert.assertTrue(fragmentMap.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testGetComponentFragmentMap_NullFragmentPath() { | ||
Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> fragmentMap = ContentFragmentUtil.getComponentFragmentMap(CONTENT_FRAGMENT_PATH, VARIATION, null); | ||
Assert.assertTrue(fragmentMap.isEmpty()); | ||
} | ||
|
||
} |
Oops, something went wrong.