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

AEM Mock page.getTags() return 0 Tags

Avatar

Avatar
Level 1
aemdevf50224445
Level 1

Likes

0 likes

Total Posts

1 post

Correct Reply

0 solutions
View profile

Avatar
Level 1
aemdevf50224445
Level 1

Likes

0 likes

Total Posts

1 post

Correct Reply

0 solutions
View profile
aemdevf50224445
Level 1

17-09-2018

Hi Fellow AEM Developers,

I'm writing unit test for the SlingModel for a project that uses AEM 6.1. Sling model check if the Page has a Tag or not in the Post construct method. During the test mocked a Page with json which contains the "cq:tags" property, however the mocked page returns 0 tags when page.getTags() called.

Here are the code snippets.

SlingModel

--------------

@Model(adaptables = SlingHttpServletRequest.class)

public class SomeModel {

  @Inject

    @Source("sling-object")

    private ResourceResolver resourceResolver;

  @PostConstruct

  public void postConstruct() {

  PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

  Page page = pageManager.getPage("/content/newssite/en/category");

  boolean hasTag = isPageHasTag(page);

  }

  public static boolean isPageHasTag(Page page) {

       

      Tag[] tags = page.getTags();

       for(Tag tag : tags) {

            if(tag.getTagID().equals("news-site:news-types/sports")) {

            return true;

            }

       }

       return false;

  }

}

Now i have to call this is in postConstruct() since the actual logic is lot more than this.

Page JSON

------------

{

  "jcr:primaryType": "cq:Page",

  "jcr:content": {

  "jcr:primaryType": "nt:unstructured",

  "jcr:title": "News",

  "imagePath": "/content/dam/news-site/image.png",

  "hideInNav": "false",

  "cq:tags": ["news-site:news-types/sports"],

  "sling:resourceType": "newssite/components/structure/generic-page"

  }

}

tried with

cq:tags": "[news-site:news-types/sports]" as well

Test Code

-------------

@RunWith(PowerMockRunner.class)

public class TopNavControllerTest {

  @Rule

  public AemContext context = new AemContext();

@Before

    public void setup() throws Exception {

  context.load().json("/test/category_page.json", "/content/newssite/en/category");

  Resource categoryPageResource = context.resourceResolver().getResource("/content/newssite/en/category");

  context.request().setResource(categoryPageResource);

}

@Test

    public void testPostConstruct() {

  //This should call postConstruct() and check Tag

  SomeModel someModel = context.request().adaptTo(SomeModel.class);

     //Assert

  }

}

Dependnecy

---------------

<dependency>

                <groupId>io.wcm</groupId>

                <artifactId>io.wcm.testing.aem-mock</artifactId>

                <version>1.9.8</version>

                <scope>test</scope>

            </dependency>

I'm not sure what's wrong.

Any help is much appreciated.

Thanks so much in Advance.

Replies

Avatar

Avatar
Boost 5
Level 2
ahmedmusallam
Level 2

Likes

7 likes

Total Posts

11 posts

Correct Reply

1 solution
Top badges earned
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Boost 5
Level 2
ahmedmusallam
Level 2

Likes

7 likes

Total Posts

11 posts

Correct Reply

1 solution
Top badges earned
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
ahmedmusallam
Level 2

04-02-2019

I ran into the same issue. in order to resolve Tags, they must exists under `/content/cq:tags/your/tag` or `/etc/tags` (legacy).

The Page#getTags implementation makes a call to TagManager#getTags which in turn tries to resolve the actual tag resource in the repo. Since you are testing in an AEM context, you have to load these tags in the appropriate location for the MockTagManager to resolve them.

What this means is that you need to load your tags into the AEM test context just like you've loaded your resources (via json).

Take a look at the aem-mock TagManager impl here: wcm-io-testing/MockTagManager.java at develop · wcm-io/wcm-io-testing · GitHub start with the `resolve` method and debug your way to figure out where you need to add those tags.

Avatar

Avatar
Level 1
rasleena1527058
Level 1

Likes

0 likes

Total Posts

4 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
rasleena1527058
Level 1

Likes

0 likes

Total Posts

4 posts

Correct Reply

0 solutions
View profile
rasleena1527058
Level 1

12-09-2019

Hey,

Is this issue resoled? Can you give an example on how it worked?