use of com.adobe.granite.ui.clientlibs.LibraryType 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()));
}
}
use of com.adobe.granite.ui.clientlibs.LibraryType in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ClientLibraryCategoriesDataSourceServlet method doGet.
@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
LibraryType libraryType = null;
Resource dataSourceResource = request.getResource().getChild(Config.DATASOURCE);
if (dataSourceResource != null) {
ValueMap dataSourceValueMap = ResourceUtil.getValueMap(dataSourceResource);
if (dataSourceValueMap != null) {
String type = dataSourceValueMap.get(PN_LIBRARY_TYPE, String.class);
if (type != null) {
type = type.toUpperCase();
libraryType = LibraryType.valueOf(type);
}
}
}
SimpleDataSource clientLibraryCategoriesDataSource = new SimpleDataSource(getCategoryResourceList(request, libraryType).iterator());
request.setAttribute(DataSource.class.getName(), clientLibraryCategoriesDataSource);
}
use of com.adobe.granite.ui.clientlibs.LibraryType in project acs-aem-commons by Adobe-Consulting-Services.
the class VersionedClientlibsTransformerFactory method getUriInfo.
@Nonnull
UriInfo getUriInfo(@Nullable final String uri, @Nonnull ResourceResolver resourceResolver) {
if (uri != null) {
Matcher matcher = FILTER_PATTERN.matcher(uri);
if (matcher.matches()) {
final String libraryPath = matcher.group(1);
final String md5 = matcher.group(2);
final String extension = matcher.group(3);
LibraryType libraryType;
if (LibraryType.CSS.extension.substring(1).equals(extension)) {
libraryType = LibraryType.CSS;
} else {
libraryType = LibraryType.JS;
}
final HtmlLibrary htmlLibrary = getLibrary(libraryType, libraryPath, resourceResolver);
return new UriInfo(libraryPath + "." + extension, md5, libraryType, htmlLibrary);
}
}
return new UriInfo("", "", null, null);
}
use of com.adobe.granite.ui.clientlibs.LibraryType in project acs-aem-commons by Adobe-Consulting-Services.
the class OptionsServlet method doGet.
@Override
@SuppressWarnings({ "squid:S3776", "squid:S1141" })
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
final JsonWriter writer = new JsonWriter(response.getWriter());
writer.beginArray();
String type = request.getRequestPathInfo().getSelectorString();
if (type != null) {
try {
Set<String> categories = new TreeSet<String>();
LibraryType libraryType = LibraryType.valueOf(type.toUpperCase());
Map<String, ClientLibrary> libraries = libraryManager.getLibraries();
for (ClientLibrary library : libraries.values()) {
if (library.getTypes() != null && library.getTypes().contains(libraryType)) {
String[] libraryCats = library.getCategories();
if (libraryCats != null) {
for (String cat : libraryCats) {
categories.add(cat);
}
}
}
}
for (String cat : categories) {
writer.beginObject();
writer.name("value");
writer.value(cat);
writer.name("text");
writer.value(cat);
writer.endObject();
}
} catch (IllegalArgumentException e) {
// no matching type. no need to log, just return empty array.
}
}
writer.endArray();
writer.close();
}
Aggregations