Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Code Coverage for Util Classses. #17

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}

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());
}

}
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());
}

}
Loading