Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM Page adapt to experience fragment in test

JiaJian213
Level 1
Level 1

Hi, This problem, I've been bugging me around a bit. Whenever I am adapt to ExperienceFragmentVariation, it returns me a NULL. Anyone have some insight on this?

Screen Shot 2020-11-06 at 5.52.11 PM.png

Unit-test
1 Accepted Solution
SureshDhulipudi
Correct answer by
Community Advisor
Community Advisor

 

Is your page resource is Experience Fragment Page?

can you try like get Experience Fragment resource page and then get the variations list of that page resource and then adapto

 

ResourceResolver resourceResolver;
resourceResolver = resourceResolverFactory.getServiceResourceResolver(<<<inputparam>>>);
Resource res=resourceResolver.getResource(<<<<<<Page Path / Resource Path>>>>);
Page page=res.adaptTo(Page.class);
ExperienceFragmentsService expService=experienceFragmentsServiceFactory.getExperienceFragmentsService(<<<resourceResolver>>>);
            /// check if the page is Experience Fragment type or content Fragment type
if(page.getContentResource().isResourceType("cq/experience-fragments/components/experiencefragment")){ 
Iterator<Page> fragmentVariation=page.listChildren(new PageFilter(),false);
while(fragmentVariation.hasNext()){
Page fragmentPage=fragmentVariation.next();
if(null!=fragmentPage){
 ExperienceFragmentVariation fragmentVariation = fragmentPage.adaptTo(ExperienceFragmentVariation.class);
      if (null!=fragmentVariation)  
              <<< LOGIC to implement>>>
      else 
           logger.warn("The page at path cannot be adapted to an experience fragment variation");
}
 

View solution in original post

4 Replies
SureshDhulipudi
Correct answer by
Community Advisor
Community Advisor

 

Is your page resource is Experience Fragment Page?

can you try like get Experience Fragment resource page and then get the variations list of that page resource and then adapto

 

ResourceResolver resourceResolver;
resourceResolver = resourceResolverFactory.getServiceResourceResolver(<<<inputparam>>>);
Resource res=resourceResolver.getResource(<<<<<<Page Path / Resource Path>>>>);
Page page=res.adaptTo(Page.class);
ExperienceFragmentsService expService=experienceFragmentsServiceFactory.getExperienceFragmentsService(<<<resourceResolver>>>);
            /// check if the page is Experience Fragment type or content Fragment type
if(page.getContentResource().isResourceType("cq/experience-fragments/components/experiencefragment")){ 
Iterator<Page> fragmentVariation=page.listChildren(new PageFilter(),false);
while(fragmentVariation.hasNext()){
Page fragmentPage=fragmentVariation.next();
if(null!=fragmentPage){
 ExperienceFragmentVariation fragmentVariation = fragmentPage.adaptTo(ExperienceFragmentVariation.class);
      if (null!=fragmentVariation)  
              <<< LOGIC to implement>>>
      else 
           logger.warn("The page at path cannot be adapted to an experience fragment variation");
}
 

View solution in original post

JiaJian213
Level 1
Level 1
In Aem testing, the the resourceResolverFactory, should I do it like
JiaJian213
Level 1
Level 1

Hi, I am writing a Unit test, unfortunately, I don't have any logger for this, but my problem, it's that whenever I try adapting the resource Page to ExperienceFragmentVariation => null.