Search in sources :

Example 1 with HtmlLibrary

use of com.adobe.granite.ui.clientlibs.HtmlLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class VersionedClientlibsTransformerFactoryTest method doFilter_notFoundInCacheWithDot_md5MisMatch.

@Test
public void doFilter_notFoundInCacheWithDot_md5MisMatch() throws Exception {
    when(slingRequest.getRequestURI()).thenReturn("/etc/clientlibs/some.path.min.ACSHASHfoobar.js");
    HtmlLibrary library = mock(HtmlLibrary.class);
    when(library.getInputStream(false)).thenReturn(INPUTSTREAM);
    when(library.getLibraryPath()).thenReturn("/etc/clientlibs/some.path.js");
    when(htmlLibraryManager.getLibrary(LibraryType.JS, "/etc/clientlibs/some.path")).thenReturn(library);
    filter.doFilter(slingRequest, slingResponse, filterChain);
    verify404();
}
Also used : HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) Test(org.junit.Test)

Example 2 with HtmlLibrary

use of com.adobe.granite.ui.clientlibs.HtmlLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class VersionedClientlibsTransformerFactoryTest method doFilter_foundInCacheWithDot_md5MisMatch.

@Test
public void doFilter_foundInCacheWithDot_md5MisMatch() throws Exception {
    when(slingRequest.getRequestURI()).thenReturn("/etc/clientlibs/some.path.min.ACSHASHfoobar.js");
    factory.getCache().put(new VersionedClientLibraryMd5CacheKey("/etc/clientlibs/some.path", LibraryType.JS), INPUTSTREAM_MD5);
    HtmlLibrary htmlLibrary = mock(HtmlLibrary.class);
    when(htmlLibrary.getLibraryPath()).thenReturn("/etc/clientlibs/some.path");
    when(htmlLibraryManager.getLibrary(LibraryType.JS, "/etc/clientlibs/some.path")).thenReturn(htmlLibrary);
    filter.doFilter(slingRequest, slingResponse, filterChain);
    verify404();
}
Also used : HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) Test(org.junit.Test)

Example 3 with HtmlLibrary

use of com.adobe.granite.ui.clientlibs.HtmlLibrary in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method setUp.

@BeforeEach
void setUp() {
    context.load().json(BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
    context.load().json(BASE + CoreComponentTestContext.TEST_APPS_JSON, APPS_ROOT);
    context.load().json(BASE + TEST_CONTENT_XF_JSON, XF_ROOT);
    context.load().json(BASE + TEST_CONF_TEMPLATES_JSON, TEMPLATES_ROOT);
    jsIncludes = new HashMap<>();
    jsIncludes.put(TEASER_CATEGORY, "<script src=\"" + TEASER_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludes.put(ACCORDION_CATEGORY, "<script src=\"" + ACCORDION_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludes.put(CAROUSEL_CATEGORY, "<script src=\"" + CAROUSEL_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes = new HashMap<>();
    jsIncludesWithAttributes.put(TEASER_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + TEASER_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes.put(ACCORDION_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + ACCORDION_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + CAROUSEL_CLIENTLIB_PATH + ".js\"></script>");
    cssIncludes = new HashMap<>();
    cssIncludes.put(TEASER_CATEGORY, "<link rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludes.put(ACCORDION_CATEGORY, "<link rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludes.put(CAROUSEL_CATEGORY, "<link rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes = new HashMap<>();
    cssIncludesWithAttributes.put(TEASER_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes.put(ACCORDION_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    jsInlines = new HashMap<>();
    jsInlines.put(TEASER_CATEGORY, "console.log('teaser clientlib js');");
    jsInlines.put(ACCORDION_CATEGORY, "console.log('accordion clientlib js');");
    jsInlines.put(CAROUSEL_CATEGORY, "console.log('carousel clientlib js');");
    cssInlines = new HashMap<>();
    cssInlines.put(TEASER_CATEGORY, "html, .teaser { \n box-sizing: border-box;\n font-size: 14px; \n}");
    cssInlines.put(ACCORDION_CATEGORY, "html, .accordion { \n box-sizing: border-box;\n font-size: 14px; \n}");
    cssInlines.put(CAROUSEL_CATEGORY, "html, .carousel { \n box-sizing: border-box;\n font-size: 14px; \n}");
    // Mock ClientLibrary
    ClientLibrary teaserClientLibrary = mock(ClientLibrary.class);
    ClientLibrary accordionClientLibrary = mock(ClientLibrary.class);
    ClientLibrary carouselClientLibrary = mock(ClientLibrary.class);
    String[] teaserCategories = new String[] { TEASER_CATEGORY };
    when(teaserClientLibrary.getCategories()).thenReturn(teaserCategories);
    when(teaserClientLibrary.getPath()).thenReturn(TEASER_CLIENTLIB_PATH);
    String[] accordionCategories = new String[] { ACCORDION_CATEGORY };
    when(accordionClientLibrary.getCategories()).thenReturn(accordionCategories);
    when(accordionClientLibrary.getPath()).thenReturn(ACCORDION_CLIENTLIB_PATH);
    String[] carouselCategories = new String[] { CAROUSEL_CATEGORY };
    when(carouselClientLibrary.getCategories()).thenReturn(carouselCategories);
    when(carouselClientLibrary.getPath()).thenReturn(CAROUSEL_CLIENTLIB_PATH);
    librariesMap = new HashMap<>();
    librariesMap.put(TEASER_CATEGORY, teaserClientLibrary);
    librariesMap.put(ACCORDION_CATEGORY, accordionClientLibrary);
    librariesMap.put(CAROUSEL_CATEGORY, carouselClientLibrary);
    // Mock HtmlLibrary
    HtmlLibrary teaserJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary accordionJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary carouselJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary teaserCssHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary accordionCssHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary carouselCssHtmlLibrary = mock(HtmlLibrary.class);
    Resource teaserJsClientLibResource = context.currentResource(TEASER_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource accordionJsClientLibResource = context.currentResource(ACCORDION_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource carouselJsClientLibResource = context.currentResource(CAROUSEL_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource teaserCssClientLibResource = context.currentResource(TEASER_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    Resource accordionCssClientLibResource = context.currentResource(ACCORDION_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    Resource carouselCssClientLibResource = context.currentResource(CAROUSEL_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    try {
        when(teaserJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(teaserJsClientLibResource.adaptTo(InputStream.class));
        when(accordionJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(accordionJsClientLibResource.adaptTo(InputStream.class));
        when(carouselJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(carouselJsClientLibResource.adaptTo(InputStream.class));
        when(teaserCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(teaserCssClientLibResource.adaptTo(InputStream.class));
        when(accordionCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(accordionCssClientLibResource.adaptTo(InputStream.class));
        when(carouselCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(carouselCssClientLibResource.adaptTo(InputStream.class));
    } catch (IOException e) {
        fail(String.format("Unable to get input stream from the library: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.getLibraries()
    allLibraries = new HashMap<>();
    allLibraries.put(TEASER_CLIENTLIB_PATH, teaserClientLibrary);
    allLibraries.put(ACCORDION_CLIENTLIB_PATH, accordionClientLibrary);
    allLibraries.put(CAROUSEL_CLIENTLIB_PATH, carouselClientLibrary);
    HtmlLibraryManager htmlLibraryManager = context.registerInjectActivateService(mock(MockHtmlLibraryManager.class));
    when(htmlLibraryManager.getLibraries()).thenReturn(allLibraries);
    // Mock htmlLibraryManager.getLibraries(a, b, c, d)
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String[] categories = (String[]) args[0];
        Collection<ClientLibrary> libraries = new ArrayList<>();
        for (String category : categories) {
            libraries.add(librariesMap.get(category));
        }
        return libraries;
    }).when(htmlLibraryManager).getLibraries(any(String[].class), any(LibraryType.class), anyBoolean(), anyBoolean());
    // Mock htmlLibraryManager.getLibrary(libraryType, clientlib.getPath())
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(TEASER_CLIENTLIB_PATH))).thenReturn(teaserJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(ACCORDION_CLIENTLIB_PATH))).thenReturn(accordionJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(CAROUSEL_CLIENTLIB_PATH))).thenReturn(carouselJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(TEASER_CLIENTLIB_PATH))).thenReturn(teaserCssHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(ACCORDION_CLIENTLIB_PATH))).thenReturn(accordionCssHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(CAROUSEL_CLIENTLIB_PATH))).thenReturn(carouselCssHtmlLibrary);
    // Mock htmlLibraryManager.writeJsInclude
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder scriptIncludes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String script = jsIncludes.get(category);
                scriptIncludes.append(script);
            }
            ((PrintWriter) args[1]).write(scriptIncludes.toString());
            return null;
        }).when(htmlLibraryManager).writeJsInclude(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write JS include: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.writeCssInclude
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder linkIncludes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String link = cssIncludes.get(category);
                linkIncludes.append(link);
            }
            ((PrintWriter) args[1]).write(linkIncludes.toString());
            return null;
        }).when(htmlLibraryManager).writeCssInclude(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write CSS include: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.writeIncludes
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder includes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String script = jsIncludes.get(category);
                includes.append(script);
            }
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String link = cssIncludes.get(category);
                includes.append(link);
            }
            ((PrintWriter) args[1]).write(includes.toString());
            return null;
        }).when(htmlLibraryManager).writeIncludes(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write include: %s", e.getMessage()));
    }
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) HtmlLibraryManager(com.adobe.granite.ui.clientlibs.HtmlLibraryManager) MockHtmlLibraryManager(com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager) InputStream(java.io.InputStream) LibraryType(com.adobe.granite.ui.clientlibs.LibraryType) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) MockHtmlLibraryManager(com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with HtmlLibrary

use of com.adobe.granite.ui.clientlibs.HtmlLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class VersionedClientlibsTransformerFactoryTest method doFilter_notFoundInCache_md5Match.

@Test
public void doFilter_notFoundInCache_md5Match() throws Exception {
    when(slingRequest.getRequestURI()).thenReturn("/etc/clientlibs/some.min.ACSHASH" + INPUTSTREAM_MD5 + ".js");
    HtmlLibrary library = mock(HtmlLibrary.class);
    when(library.getInputStream(false)).thenReturn(INPUTSTREAM);
    when(library.getLibraryPath()).thenReturn("/etc/clientlibs/some.js");
    when(htmlLibraryManager.getLibrary(LibraryType.JS, "/etc/clientlibs/some")).thenReturn(library);
    filter.doFilter(slingRequest, slingResponse, filterChain);
    verifyNo404();
}
Also used : HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) Test(org.junit.Test)

Example 5 with HtmlLibrary

use of com.adobe.granite.ui.clientlibs.HtmlLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class VersionedClientlibsTransformerFactoryTest method doFilter_foundInCacheWithDot_md5Match.

@Test
public void doFilter_foundInCacheWithDot_md5Match() throws Exception {
    when(slingRequest.getRequestURI()).thenReturn("/etc/clientlibs/some.path.min.ACSHASH" + INPUTSTREAM_MD5 + ".js");
    factory.getCache().put(new VersionedClientLibraryMd5CacheKey("/etc/clientlibs/some.path", LibraryType.JS), INPUTSTREAM_MD5);
    HtmlLibrary htmlLibrary = mock(HtmlLibrary.class);
    when(htmlLibrary.getLibraryPath()).thenReturn("/etc/clientlibs/some.path");
    when(htmlLibraryManager.getLibrary(LibraryType.JS, "/etc/clientlibs/some.path")).thenReturn(htmlLibrary);
    filter.doFilter(slingRequest, slingResponse, filterChain);
    verifyNo404();
}
Also used : HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) Test(org.junit.Test)

Aggregations

HtmlLibrary (com.adobe.granite.ui.clientlibs.HtmlLibrary)12 Test (org.junit.Test)7 IOException (java.io.IOException)3 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)2 LibraryType (com.adobe.granite.ui.clientlibs.LibraryType)2 CacheMBeanException (com.adobe.acs.commons.util.impl.exception.CacheMBeanException)1 MockHtmlLibraryManager (com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager)1 HtmlLibraryManager (com.adobe.granite.ui.clientlibs.HtmlLibraryManager)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 Nonnull (javax.annotation.Nonnull)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 OpenDataException (javax.management.openmbean.OpenDataException)1 ServletException (javax.servlet.ServletException)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 Resource (org.apache.sling.api.resource.Resource)1