Hi,
I'm writing a test case for a sling model. I'm able to load JSON and get the resource but when I'm trying to adapt that resource into class, it's coming as null.
Could you check and tell what I'm missing?
SlingModel class:
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import com.day.cq.wcm.api.Page;
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CarouselSlide {
final private static String CAROUSEL_TEMPLATE = "/apps/poc/templates/carousel";
@ScriptVariable
private Page currentPage;
@ValueMapValue
private String backgroundImage;
@ValueMapValue
private String gradientColor;
@ValueMapValue
private String gradientDirection;
@ValueMapValue
private String alignment;
private String gradientColorDirection;
private boolean carouselTemplateView;
@PostConstruct
protected void init() {
if(currentPage!=null){
if(currentPage.getTemplate() != null && StringUtils.equals(CAROUSEL_TEMPLATE, currentPage.getTemplate().getPath())){
carouselTemplateView = true;
}
}
}
public String getGradientColor() {
return gradientColor;
}
public String getGradientDirection() {
return gradientDirection;
}
public String getAlignment() {
return alignment;
}
public String getBackgroundImage() {
return backgroundImage;
}
public String getGradientColorDirection() {
if(StringUtils.isNotEmpty(gradientColor) && StringUtils.isNotEmpty(gradientDirection)){
gradientColorDirection = gradientColor + "-" + gradientDirection;
}else if(StringUtils.isNotEmpty(gradientColor) && StringUtils.isEmpty(gradientDirection)){
gradientColorDirection = gradientColor;
}
return gradientColorDirection;
}
public boolean isCarouselTemplateView() {
return carouselTemplateView;
}
}
Mock json:
{
"jcr:primaryType": "cq:Page",
"jcr:createdBy": "admin",
"jcr:created": "Mon Sep 11 2017 17:05:57 GMT+0530",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:mixinTypes": [
"mix:versionable"
],
"jcr:createdBy": "admin",
"jcr:title": "WRAC-103 Retest",
"cq:lastReplicationAction": "Activate",
"jcr:versionHistory": "6ea1a0b6-60b7-4bed-a0b5-3f83850d9a24",
"cq:template": "/apps/sample/templates/page-carousel-template",
"cq:lastReplicatedBy": "doris.tao",
"jcr:predecessors": [
"b7b0af89-1ffd-4292-b1dd-9e21037c4b7f"
],
"jcr:created": "Mon Sep 11 2017 17:05:57 GMT+0530",
"cq:lastReplicated": "Thu Aug 10 2017 21:12:54 GMT-0400",
"cq:lastModified": "Mon Aug 14 2017 13:13:18 GMT-0400",
"jcr:baseVersion": "b7b0af89-1ffd-4292-b1dd-9e21037c4b7f",
"jcr:isCheckedOut": true,
"hideInNav": "true",
"jcr:uuid": "8ff6a3bd-23ce-4d6e-9ea2-7d266e5d724a",
"sling:resourceType": "sample/components/pages/carouselPage",
"cq:lastModifiedBy": "admin",
"slides": {
"jcr:primaryType": "nt:unstructured",
"jcr:lastModifiedBy": "sysuser",
"jcr:lastModified": "Wed Aug 09 2017 21:43:20 GMT-0400",
"slideCount": "4",
"sling:resourceType": "sample/components/content/carouselContainer",
"par1": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "wcm/foundation/components/parsys",
"carouselslide": {
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "admin",
"gradientColor": "008000",
"jcr:lastModifiedBy": "admin",
"jcr:created": "Mon Aug 14 2017 12:52:15 GMT-0400",
"gradientDirection": "right",
"jcr:lastModified": "Mon Aug 14 2017 12:53:18 GMT-0400",
"sling:resourceType": "sample/components/content/carouselSlide",
"backgroundImage": "/content/dam/sample/new-images-cs/Playa Vista Urgent Care - Construction 021.jpg",
"alignment": "Left"
}
}
}
}
}
Test Class:
import static org.junit.Assert.assertEquals;
import org.apache.sling.api.resource.Resource;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.LoggerFactory;
import io.wcm.testing.mock.aem.junit.AemContext;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ CarouselSlide.class, LoggerFactory.class })
public class CarouselSlideTest {
private static final String TEST_TITLE_RESOURCE_CONTENT = "/carouselSlide/test-content.json";
private static final String TEST_CONTENT_ROOT = "/content/carousels/wrac-103-retest";
private static final String TITLE_RESOURCE = TEST_CONTENT_ROOT + "/jcr:content/slides/par1/carouselslide";
@Rule
public AemContext context = new AemContext();
private CarouselSlide carouselSlide;
private Resource resource;
@Before
public void setUp() throws Exception {
context.load().json(TEST_TITLE_RESOURCE_CONTENT, TEST_CONTENT_ROOT);
context.addModelsForClasses(CarouselSlide.class);
}
@Test
public void testGradientDirection() throws Exception {
resource = context.resourceResolver().getResource(TITLE_RESOURCE);
carouselSlide = resource.adaptTo(CarouselSlide.class);
assertEquals(carouselSlide.getGradientDirection(), "right");
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
Try adding @Model(adaptables = Resource.class) above CarouselSlide.
I would recommend you to follow this Helpx article:- Adobe Experience Manager Help | Working with Sling Models in Adobe Experience Manager
~kautuk
Views
Replies
Total Likes
Try adding @Model(adaptables = Resource.class) above CarouselSlide.
I would recommend you to follow this Helpx article:- Adobe Experience Manager Help | Working with Sling Models in Adobe Experience Manager
~kautuk
Views
Replies
Total Likes
See the default Sling Model class created here: Scott's Digital Community: Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archet...
That will help you!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies