Junit5 test case for accesscontrolManager functionality throws excpeption | Community
Skip to main content
New Participant
March 25, 2021
Solved

Junit5 test case for accesscontrolManager functionality throws excpeption

  • March 25, 2021
  • 1 reply
  • 1612 views

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/adobe/acs/commons/wrap/jcr/BaseSessionIWrapTest.java

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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.

1 reply

joerghoh
joerghohAccepted solution
Employee
March 25, 2021

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.

New Participant
March 26, 2021

Hi @joerghoh,

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.