Expand my Community achievements bar.

SOLVED

Junit5 test case for accesscontrolManager functionality throws excpeption

Avatar

Level 2

I am reposting this question as my previous question was closed by mistake as resolved.

This test case throws unsupported exception when using the JUNIT5 version as per below

I am instantiating the aem context at class level with below code

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })

class TrashcanServletTest {

 

private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);

 

Testcase method is provided here :

 

void testDoGetSlingHttpServletRequestSlingHttpServletResponse() throws UnsupportedRepositoryOperationException, RepositoryException {

 

aemContext.create().page("/content/ey-unified-site/language-masters/en/blueprintpage2/page1", "/", "We Retail");

MockSlingHttpServletRequestmockSlingRequest = aemContext.request();

MockSlingHttpServletResponsemockSlingResponse = aemContext.response();

Resource payloadResource = mockSlingRequest.getResourceResolver().resolve("/content/ey-unified-site/language-masters/en/blueprintpage2/page1");

ResourceResolver rr = payloadResource.getResourceResolver();

    Session session = rr.adaptTo(Session.class);

    final AccessControlManager accessControlManager = session.getAccessControlManager();

    final Privilege moveToTrashCanPrivilege = accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_NODE);

    if(accessControlManager.hasPrivileges(payloadResource.getPath(), new Privilege[]{moveToTrashCanPrivilege})) {

            //todo use full work

        }

 

This line-->     final AccessControlManager accessControlManager = session.getAccessControlManager(); throws java.lang.UnsupportedOperationException

 

Where as I found the test cases in below URL where JUNIT 4 class works

https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad...

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Looks like the JCR_MOCK does not support ACLs. Can you please try this version:

 

private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_OAK);

 

It fill startup a full in-memory repository, which supports ACLs. But it has an impact on the performance of the testcase, so it's recommended to the OAK repo implementation for unittests only when required.

View solution in original post

5 Replies

Avatar

Correct answer by
Employee Advisor

Looks like the JCR_MOCK does not support ACLs. Can you please try this version:

 

private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_OAK);

 

It fill startup a full in-memory repository, which supports ACLs. But it has an impact on the performance of the testcase, so it's recommended to the OAK repo implementation for unittests only when required.

Avatar

Level 2

Hi @Jörg_Hoh,

I modified the context  as 

private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_OAK);

I am getting this below exception:

java.lang.RuntimeException: Unable to initialize JCR_OAK resource resolver factory: Unable to instantiate resourcer resolver: org.apache.sling.testing.mock.sling.oak.OakMockResourceResolverAdapter. Make sure this maven dependency is included: org.apache.sling:org.apache.sling.testing.sling-mock-oak

 I am using the Archetype 25 and below are entires in my core pom.xml

 

<dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-core</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-junit-jupiter</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>junit-addons</groupId>

            <artifactId>junit-addons</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>io.wcm</groupId>

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

            <exclusions>

                <exclusion>

                    <groupId>org.apache.sling</groupId>

                    <artifactId>org.apache.sling.models.impl</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>org.slf4j</groupId>

                    <artifactId>slf4j-simple</artifactId>

                </exclusion>

            </exclusions>

            <scope>test</scope>

        </dependency>

 

Please let me know how to resolve this exception as i tried adding JUNIT4 dependencies provided  below:

   <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.commons.testing</artifactId>

            <version>2.0.24</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-all</artifactId>

            <version>1.9.5</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.testing.sling-mock</artifactId>

            <version>2.6.2</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>

            <version>2.1.10-1.16.0</version>

            <scope>test</scope>

        </dependency>

        

        <dependency>

            <groupId>junit-addons</groupId>

            <artifactId>junit-addons</artifactId>

            <version>1.4</version>

            <scope>test</scope>

        </dependency>

        But unable to get the test case working for when the aemContext uses JCR_OAK.

 

Request you to help me resolve this issue.

 

Avatar

Employee Advisor

The documentation on this is here: https://sling.apache.org/documentation/development/sling-mock.html#resource-resolver-types-1

 

It mentions that you should have the bundle org.apache.sling:org.apache.sling.testing.sling-mock-oak in your dependencies. I see it in your post mentioned, but it's not clear if it is part of the <dependencyManagement> or not.

 

You can find a working example for this inclusion in https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/pom.xml.

Avatar

Level 2

Hi @Jörg_Hoh,

 

 There is a solution provided here for this issue  here : [SLING-7195] Sling Mock Oak not working with AEM uber-jar 6.3.0 - ASF JIRA (apache.org)

They have provided a sample zip package file which I could download and verify.

But this sample package uses uses JUNIT 4 and not JUNIT 5.

Below is the entry to be made to my pom.xml as per the provided solution there( this is a working solution that uses JUNIT4)

<dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.testing.sling-mock</artifactId>

            <version>2.2.12</version>

        </dependency>

        <dependency>

            <groupId>org.apache.sling</groupId>

            <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>

            <version>2.0.2</version>

        </dependency>

 

My question on JUNIT5 is still not resolved since I need to use OAK repo implementation for JUNIT5, since the source class  AccessControlManager code that needs to be tested.

But looks like OAK repo implementation does currently work with JUNIT 5 or it is broken.

I get this error I get when I run JUNIT5 test case 

java.lang.VerifyError: class io.wcm.testing.mock.aem.context.AemContextImpl overrides final method currentResource.(Lorg/apache/sling/api/resource/Resource;)Lorg/apache/sling/api/resource/Resource;

 

 

 

Avatar

Employee Advisor
Maybe it's best if you ask the developers of aemMock at wcm.io, I experienced them as quite helpful.