Expand my Community achievements bar.

Page.getTags() junits

Avatar

Level 6

HI Team,

I was able to get all properties of page when stubbed through json but not cq:tags,

when I do page.getPath() it's getting the path but it's returning null for page.getTags() however I was able to see cq:tags property is not null it has tags when i debug it 

Keerthi0555_0-1715954914095.png

 

8 Replies

Avatar

Community Advisor

Hi, 

How are you mocking the page? Can you post some code to see what is going on?




Esteban Bustamante

Avatar

Level 6

Sure,

junits 

aemContext.request().setAttribute("productModelPage", "/content/Page");

here is my model code:

productModelPage is a variable 

if(StringUtils.isNotBlank(productModelPage)){
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page productPage = pageManager.getPage(productModelPage);
String path = productPage.getPath();
Tag[] prodcutPageTags = productPage.getTags();
if(Objects.nonNull(prodcutPageTags)){

Avatar

Community Advisor

You should create the page and load its properties as part of the aemContext so below line actually can retrieve the page's properties:

pageManager.getPage(productModelPage);

Try something like this https://wcm.io/testing/aem-mock/usage-content-loader-builder.html:

EstebanBustamante_0-1715957847488.png

 

Hope this helps

 



Esteban Bustamante

Avatar

Level 6

ok, but productmodelPage is a string variable in my model so setting it as attribute.

Avatar

Community Advisor

Right, the attribute will tell which resource to look in a specific path, but you need to load such resource in the path, so the AEMcontext can get it



Esteban Bustamante

Avatar

Community Advisor

HI @Keerthi0555 ,

To write JUnit tests for `Page.getTags()`, you can use the AEM Mocks library provided by Adobe. Here's an example of how you can write JUnit tests for `Page.getTags()`:

1. Add the AEM Mocks dependency to your project's build.gradle or pom.xml file:

For Gradle:
```groovy
testImplementation 'com.adobe.aem:uber-jar:6.5.0:tests@jar'
```

For Maven:
```xml
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<version>6.5.0</version>
<classifier>tests</classifier>
<scope>test</scope>
<type>jar</type>
</dependency>
```

2. Write a JUnit test method to test `Page.getTags()`:

```java
import com.adobe.cq.wcm.core.components.models.Page;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PageTest {

@Rule
public final AemContext context = new AemContext();

private Page page;

@Before
public void setUp() {
// Create a mock Page object
page = mock(Page.class);

// Set up the AemContext with the mock Page
context.currentPage(page);
}

@Test
public void testGetTags() {
// Set up the mock behavior for getTags()
String[] tags = {"tag1", "tag2"};
when(page.getTags()).thenReturn(tags);

// Call the method being tested
String[] result = page.getTags();

// Assert the expected result
assertEquals(tags, result);
}
}
```

In this example, we use the AemContext provided by AEM Mocks to set up a mock Page object. We then use Mockito to mock the behavior of `Page.getTags()` and return a predefined array of tags. Finally, we call `page.getTags()` and assert that the result matches the expected tags.

Make sure to import the necessary classes and adjust the version of the AEM Mocks library according to your AEM version.

Note: The AEM Mocks library provides a comprehensive set of mock objects and utilities for testing AEM components and services. You can explore more features and examples in the AEM Mocks documentation.

Avatar

Community Advisor

When using the wcm.io mocks library in your JUnit5 tests for AEM, ensuring that cq:tags are correctly retrieved requires proper setup of the mock objects and their properties. Here's how you can make sure cq:tags are correctly mocked and accessible in your tests.

Steps to Mock cq:tags Property Correctly:

  1. Include Necessary Dependencies: Ensure you have the correct dependencies for wcm.io mocks and JUnit5 in your project. Add these dependencies to your pom.xml or build.gradle:

    <dependency>
        <groupId>io.wcm</groupId>
        <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
    </dependency>
    
  2. Set Up the Mock AEM Context: Use AemContext to set up your test context, and ensure that you properly set the cq:tags property.

Here's an example of how to set up your test to mock the cq:tags property using wcm.io mocks and JUnit5:

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import io.wcm.testing.mock.aem.junit5.AemContext;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(AemContextExtension.class)
public class PageTagsTest {

    private final AemContext context = new AemContext();

    @BeforeEach
    public void setUp() {
        // Create a test page with cq:tags
        context.load().json("/sample-content.json", "/content/sample");

        // Optionally, mock the TagManager if needed
        TagManager tagManager = mock(TagManager.class);
        context.registerService(TagManager.class, tagManager);

        Tag tag = mock(Tag.class);
        when(tag.getTagID()).thenReturn("sample:tag");
        when(tagManager.resolve("sample:tag")).thenReturn(tag);
    }

    @test
    public void testPageTags() {
        // Get the page
        PageManager pageManager = context.pageManager();
        Page page = pageManager.getPage("/content/sample");

        assertNotNull(page, "Page should not be null");
        assertEquals("/content/sample", page.getPath());

        // Retrieve the tags
        Tag[] tags = page.getTags();
        assertNotNull(tags, "Tags should not be null");
        assertEquals(1, tags.length);
        assertEquals("sample:tag", tags[0].getTagID());
    }
}

 

JSON Content Setup:

Ensure your sample-content.json includes the cq:tags property. Here’s an example JSON snippet:

{
  "jcr:primaryType": "cq:Page",
  "jcr:content": {
    "jcr:primaryType": "cq:PageContent",
    "cq:tags": ["sample:tag"]
  }
}

Explanation:

  1. Dependencies: The dependencies for io.wcm.testing.aem-mock.junit5 are included to enable AEM mocking for JUnit5.

  2. Setup: The setUp() method loads a JSON file that represents the content structure, including the cq:tags property.

  3. Mocking TagManager: The TagManager is mocked to resolve the tag ID correctly. This ensures that when page.getTags() is called, it returns the appropriate tag.

  4. Test Case: The test case retrieves the page and asserts that the cq:tags property is correctly mocked and accessible.

By following these steps and ensuring the proper setup of your mock context and JSON content, you should be able to retrieve the cq:tags property in your unit tests using the wcm.io mocks library in JUnit5.

 

Avatar

Administrator

@Keerthi0555 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni