Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

ResourceResolverType.JCR_OAK individual test class failing

Avatar

Level 7

Hi,

 

In my project: mvn clean install -PautoInstallPackage working fine and unit test classes are passing.

When I go to individual unit test class in IntelliJ IDE and run test class it is failing with below error.

 

Here is my test class:

 

package com.comp.app.rxwa.pharmacy.filters;

import com.comp.app.rxwa.pharmacy.utils.CharArrayResponseWrapper;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.loader.ContentLoader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RunWith(MockitoJUnitRunner.class)
public class ContentJsonLinkRewriterFilterTest {
    protected static final String CONTENT_ROOT = "/content/experience-fragments/rxwa";

    public Map<String, Object> props = new HashMap<>();

    @Deleted Account
    public AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

    @Mock
    FilterChain chain;

    public ContentJsonLinkRewriterFilter filter;

    @Before
    public void setUp() {
        context.load().json("/test-page-content.json", "/content/experience-fragments/rxwa");
        context.requestPathInfo().setResourcePath("/content/experience-fragments/rxwa/templated-page");
        context.requestPathInfo().setSelectorString("model");
        context.requestPathInfo().setExtension("json");
        context.request().setMethod("GET");
        context.response().setCharacterEncoding("UTF-8");

        context.currentResource("/content/experience-fragments/rxwa/templated-page");

    }

    @test
    public void testDoFilter() throws ServletException, IOException {
        props.clear();
        props.put("enabled", "true");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpage"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        Mockito.doAnswer(invocation -> {
            CharArrayResponseWrapper httpServletResponseWrapper =
                    (CharArrayResponseWrapper) invocation.getArguments()[1];
            String testHtmlContent = IOUtils.toString(
                    ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
            );
            httpServletResponseWrapper.getWriter().write(testHtmlContent);
            httpServletResponseWrapper.getWriter().flush();

            return null;
        }).when(chain).doFilter(Mockito.any(), Mockito.any());

        filter.doFilter(context.request(), context.response(), chain);

        Mockito.verify(chain).doFilter(Mockito.any(), Mockito.any());

        String testHtmlContent2 = IOUtils.toString(
                ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
        );

        Assert.assertFalse(StringUtils.contains(context.response().getOutputAsString(),"/content/experience-fragments/rxwa/"));
    }

    @test
    public void testNoTransform() throws ServletException, IOException {

        props.clear();
        props.put("enabled", "false");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpage"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        Mockito.doAnswer(invocation -> {
            SlingHttpServletResponse httpServletResponseWrapper = (SlingHttpServletResponse)invocation.getArguments()[1];
            String testHtmlContent = IOUtils.toString(
                    ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
            );
            httpServletResponseWrapper.getWriter().write(testHtmlContent);
            httpServletResponseWrapper.getWriter().flush();

            return null;
        }).when(chain).doFilter(Mockito.any(), Mockito.any());

        filter.doFilter(context.request(), context.response(), chain);

        Mockito.verify(chain).doFilter(Mockito.any(), Mockito.any());

        String testHtmlContent2 = IOUtils.toString(
                ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
        );

        Assert.assertTrue(StringUtils.contains(context.response().getOutputAsString(),"/content/experience-fragments/rxwa/"));
    }


    @test
    public void testResourceTypeNoMatch() throws ServletException, IOException {

        props.clear();
        props.put("enabled", "true");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpagenomatch"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        Mockito.doAnswer(invocation -> {
            SlingHttpServletResponse httpServletResponseWrapper = (SlingHttpServletResponse)invocation.getArguments()[1];
            String testHtmlContent = IOUtils.toString(
                    ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
            );
            httpServletResponseWrapper.getWriter().write(testHtmlContent);
            httpServletResponseWrapper.getWriter().flush();

            return null;
        }).when(chain).doFilter(Mockito.any(), Mockito.any());

        filter.doFilter(context.request(), context.response(), chain);

        Mockito.verify(chain).doFilter(Mockito.any(), Mockito.any());

        String testHtmlContent2 = IOUtils.toString(
                ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
        );

        Assert.assertTrue(StringUtils.contains(context.response().getOutputAsString(),"/content/experience-fragments/rxwa/"));
    }


    @test(expected = IllegalStateException.class)
    public void testAndValidateException() throws Exception {
        HttpServletRequest httpReq = Mockito.mock(HttpServletRequest.class);
        HttpServletResponse httpResp = Mockito.mock(HttpServletResponse.class);

        props.clear();
        props.put("enabled", "true");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpagenomatch"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        filter.doFilter(httpReq, httpResp, chain);
    }

    @test
    public void testNoResponseString() throws ServletException, IOException {

        props.clear();
        props.put("enabled", "true");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpage"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        Mockito.doAnswer(invocation -> {
            SlingHttpServletResponse httpServletResponseWrapper = (SlingHttpServletResponse)invocation.getArguments()[1];
            String testHtmlContent = "";
            httpServletResponseWrapper.getWriter().write(testHtmlContent);
            httpServletResponseWrapper.getWriter().flush();

            return null;
        }).when(chain).doFilter(Mockito.any(), Mockito.any());

        filter.doFilter(context.request(), context.response(), chain);

        Mockito.verify(chain).doFilter(Mockito.any(), Mockito.any());

        String testHtmlContent2 = IOUtils.toString(
                ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
        );

        Assert.assertEquals("{}", context.response().getOutputAsString());
    }

    @test
    public void testDoFilterCaas() throws ServletException, IOException {

        props.clear();
        props.put("enabled", "true");
        props.put("resourceTypes", new String[] {"rxwa-pharmacy/components/structure/xfpage"});

        ContentJsonLinkRewriterFilter aei = context.registerInjectActivateService(new ContentJsonLinkRewriterFilter(), props);
        filter = Mockito.spy(aei);

        Mockito.doAnswer(invocation -> {
            CharArrayResponseWrapper httpServletResponseWrapper =
                    (CharArrayResponseWrapper) invocation.getArguments()[1];
            String testHtmlContent = IOUtils.toString(
                    ContentLoader.class.getResourceAsStream("/test-caas-response.json"), "UTF-8"
            );
            httpServletResponseWrapper.getWriter().write(testHtmlContent);
            httpServletResponseWrapper.getWriter().flush();

            return null;
        }).when(chain).doFilter(Mockito.any(), Mockito.any());


        filter.doFilter(context.request(), context.response(), chain);

        Mockito.verify(chain).doFilter(Mockito.any(), Mockito.any());

        String testHtmlContent2 = IOUtils.toString(
                ContentLoader.class.getResourceAsStream("/test-page-content.json"), "UTF-8"
        );

        Assert.assertFalse(StringUtils.contains(context.response().getOutputAsString(),"/content/experience-fragments/rxwa/"));
    }
}

 

Here is mvn dependency:tree from core bundle module

 

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ rxwa-pharmacy.core ---
[INFO] com.safeway.app.rxwa:rxwa-pharmacy.core:bundle:0.0.5-SNAPSHOT
[INFO] +- org.osgi:osgi.core:jar:6.0.0:provided
[INFO] +- org.osgi:osgi.cmpn:jar:6.0.0:provided
[INFO] +- org.osgi:osgi.annotation:jar:6.0.0:provided
[INFO] +- org.osgi:org.osgi.service.component.annotations:jar:1.3.0:provided
[INFO] +- org.osgi:org.osgi.service.metatype.annotations:jar:1.3.0:provided
[INFO] +- org.apache.felix:org.apache.felix.scr.annotations:jar:1.12.0:provided
[INFO] +- org.apache.commons:commons-lang3:jar:3.5:provided
[INFO] +- commons-codec:commons-codec:jar:1.5:provided
[INFO] +- commons-io:commons-io:jar:2.4:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.1:provided
[INFO] +- javax.jcr:jcr:jar:2.0:provided
[INFO] +- org.apache.sling:org.apache.sling.testing.sling-mock-oak:jar:2.1.6:test
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- io.wcm:io.wcm.testing.aem-mock.junit4:jar:3.0.2:test
[INFO] |  +- io.wcm:io.wcm.testing.aem-mock.core:jar:3.0.2:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.api:jar:2.16.2:test
[INFO] |  |  +- org.apache.jackrabbit:jackrabbit-jcr-commons:jar:2.14.0:test
[INFO] |  |  |  \- com.google.guava:guava:jar:15.0:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.commons.osgi:jar:2.4.0:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.resourceresolver:jar:1.5.22:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.serviceusermapper:jar:1.2.4:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.jcr.api:jar:2.4.0:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.jcr.resource:jar:2.9.2:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.commons.mime:jar:2.1.10:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.commons.json:jar:2.0.20:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.commons.classloader:jar:1.3.8:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.settings:jar:1.3.8:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.i18n:jar:2.5.6:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.models.impl:jar:1.3.8:test
[INFO] |  |  |  \- commons-beanutils:commons-beanutils:jar:1.8.3:test
[INFO] |  |  +- org.apache.jackrabbit:oak-jcr:jar:1.6.1:test
[INFO] |  |  |  +- org.apache.jackrabbit:oak-core:jar:1.6.1:test
[INFO] |  |  |  |  \- org.apache.jackrabbit:oak-blob:jar:1.6.1:test
[INFO] |  |  |  |     \- org.apache.jackrabbit:jackrabbit-data:jar:2.14.0:test
[INFO] |  |  |  |        \- org.slf4j:jcl-over-slf4j:jar:1.7.4:test
[INFO] |  |  |  +- org.apache.jackrabbit:oak-commons:jar:1.6.1:test
[INFO] |  |  |  +- org.apache.jackrabbit:jackrabbit-api:jar:2.14.0:test
[INFO] |  |  |  \- org.slf4j:slf4j-api:jar:1.7.6:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.testing.jcr-mock:jar:1.4.6:test
[INFO] |  |  |  \- org.apache.commons:commons-collections4:jar:4.1:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.testing.osgi-mock.core:jar:2.4.16:test
[INFO] |  |  |  \- org.reflections:reflections:jar:0.9.12:test
[INFO] |  |  |     \- org.javassist:javassist:jar:3.26.0-GA:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.testing.sling-mock.core:jar:2.5.0:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.scripting.api:jar:2.1.12:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.scripting.core:jar:2.0.44:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.contentparser.api:jar:2.0.0:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.contentparser.json:jar:2.0.0:test
[INFO] |  |  |  +- org.apache.johnzon:johnzon-core:jar:1.0.0:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.adapter:jar:2.1.6:test
[INFO] |  |  |  +- org.apache.sling:org.apache.sling.resourcebuilder:jar:1.0.4:test
[INFO] |  |  |  +- commons-lang:commons-lang:jar:2.5:provided
[INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-atinject_1.0_spec:jar:1.0:test
[INFO] |  |  |  \- org.apache.geronimo.specs:geronimo-json_1.1_spec:jar:1.0:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.testing.resourceresolver-mock:jar:1.1.26:test
[INFO] |  |  +- org.apache.sling:org.apache.sling.servlet-helpers:jar:1.3.0:test
[INFO] |  |  +- org.apache.jackrabbit.vault:org.apache.jackrabbit.vault:jar:3.1.18:test
[INFO] |  |  +- org.apache.commons:commons-imaging:jar:1.0-R1534292:test
[INFO] |  |  +- com.day.commons:day-commons-gfx:jar:2.1.28:test
[INFO] |  |  |  \- com.day.commons:day-commons-any:jar:2.0.0:test
[INFO] |  |  |     \- com.day.commons:day-commons-text:jar:1.1.3:test
[INFO] |  |  \- org.apache.sling:org.apache.sling.testing.hamcrest:jar:1.0.2:test
[INFO] |  |     \- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.apache.sling:org.apache.sling.testing.sling-mock.junit4:jar:2.5.0:test
[INFO] |  +- org.apache.sling:org.apache.sling.testing.osgi-mock.junit4:jar:3.1.2:test
[INFO] |  \- io.wcm:io.wcm.testing.junit-commons:jar:1.0.4:test
[INFO] +- org.mockito:mockito-core:jar:3.0.0:compile
[INFO] |  +- net.bytebuddy:byte-buddy:jar:1.9.10:compile
[INFO] |  +- net.bytebuddy:byte-buddy-agent:jar:1.9.10:compile
[INFO] |  \- org.objenesis:objenesis:jar:2.6:compile
[INFO] +- junit-addons:junit-addons:jar:1.4:test
[INFO] |  +- xerces:xercesImpl:jar:2.6.2:test
[INFO] |  \- xerces:xmlParserAPIs:jar:2.6.2:test
[INFO] +- org.apache.sling:org.apache.sling.xss:jar:2.2.12:test
[INFO] +- com.google.code.findbugs:jsr305:jar:3.0.2:provided
[INFO] +- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] +- javax.inject:javax.inject:jar:1:provided
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.4.3:provided
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.2:provided
[INFO] |  \- commons-logging:commons-logging:jar:1.2:provided
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- com.adobe.acs:acs-aem-commons-bundle:jar:4.3.2:provided
[INFO] +- biz.aQute.bnd:bndlib:jar:2.4.0:provided
[INFO] |  \- org.osgi:org.osgi.core:jar:4.3.1:provided
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.codehaus.sonar-plugins.java:sonar-jacoco-listeners:jar:2.3:test
[INFO] \- com.adobe.aem:uber-jar:jar:6.5.7:provided
[INFO] ------------------------------------------------------------------------

 

 

Here is core bundle POM dependecies :

 

       <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.cmpn</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.annotation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.component.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.metatype.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>io.wcm</groupId>
            <artifactId>io.wcm.testing.aem-mock.junit4</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit-addons</groupId>
            <artifactId>junit-addons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.xss</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.adobe.acs</groupId>
            <artifactId>acs-aem-commons-bundle</artifactId>
        </dependency>
        <dependency>
            <groupId>biz.aQute.bnd</groupId>
            <artifactId>bndlib</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.sonar-plugins.java</groupId>
            <artifactId>sonar-jacoco-listeners</artifactId>
            <version>${sonar-jacoco-listeners.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.adobe.aem</groupId>
            <artifactId>uber-jar</artifactId>
        </dependency>

    </dependencies>
3 Replies

Avatar

Level 4

Could you please check the compiler in IntelliJ? and what's your java version?

raushan123_0-1652517625610.png

 

 

Avatar

Employee Advisor

I haven't found the actual error message, can you provide it as well?

 

Thanks,

Jörg

Avatar

Level 2

I was able to get my node property request's working by switching to ResourceResolverType.JCR_MOCK instead, and correcting my test decencies to match the pom.xml in the example https://github.com/adobe/aem-guides-wknd