ClientLibrariesImpl implements ClientLibraries { @NotNull protected Set<String> getCategoriesFromComponents() { try (ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, COMPONENTS_SERVICE))) { Set<String> categories = new HashSet<>(); for (ClientLibrary library : this.getAllClientLibraries(resourceResolver)) { for (String category : library.getCategories()) { if (pattern == null || pattern.matcher(category).matches()) { categories.add(category); } } } return categories; } catch (LoginException e) { LOG.error("Cannot login as a service user", e); return Collections.emptySet(); } }  @NotNull @Override String getJsInline(); @NotNull @Override String getCssInline(); @Override String getJsIncludes(); @Override String getCssIncludes(); @Override String getJsAndCssIncludes(); static final String COMPONENTS_SERVICE; }
@Test void testGetCategories() { PageManager pageManager = context.pageManager(); Page page = pageManager.getPage(ROOT_PAGE); Map<String, Object> attributes = new HashMap<>(); attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class))); ClientLibrariesImpl clientlibs = Objects.requireNonNull((ClientLibrariesImpl) getClientLibrariesUnderTest(ROOT_PAGE, attributes)); Set<String> categories = new HashSet<>(); categories.add(TEASER_CATEGORY); categories.add(ACCORDION_CATEGORY); categories.add(CAROUSEL_CATEGORY); assertEquals(categories, clientlibs.getCategoriesFromComponents()); }
@Test void testGetCategoriesWithLoginException() throws Exception { PageManager pageManager = context.pageManager(); Page page = pageManager.getPage(ROOT_PAGE); Map<String, Object> attributes = new HashMap<>(); attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class))); ClientLibrariesImpl clientlibs = Objects.requireNonNull((ClientLibrariesImpl) getClientLibrariesUnderTest(ROOT_PAGE, attributes)); ResourceResolverFactory factory = mock(ResourceResolverFactory.class); doThrow(new LoginException()).when(factory).getServiceResourceResolver(anyMap()); clientlibs.resolverFactory = factory; assertEquals(new HashSet<>(), clientlibs.getCategoriesFromComponents()); }
@Test void testGetCategoriesForComponent() { Resource accordionResource = context.currentResource(ACCORDION_PATH); Map<String, Object> attributes = new HashMap<>(); attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getResourceTypes(accordionResource, context.request(), mock(ModelFactory.class))); ClientLibraries clientlibs = getClientLibrariesUnderTest(ACCORDION_PATH, attributes); Set<String> categories = new HashSet<>(); categories.add(TEASER_CATEGORY); categories.add(ACCORDION_CATEGORY); assertEquals(categories, ((ClientLibrariesImpl)clientlibs).getCategoriesFromComponents()); }
@Test void testGetCategoriesWithInjectedFilter() { PageManager pageManager = context.resourceResolver().adaptTo(PageManager.class); Page page = pageManager.getPage(ROOT_PAGE); Map<String,Object> attributes = new HashMap<>(); attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class))); attributes.put("filter", ".*teaser.*"); ClientLibraries clientlibs = getClientLibrariesUnderTest(ROOT_PAGE, attributes); Set<String> categories = new HashSet<>(); categories.add(TEASER_CATEGORY); assertEquals(categories, ((ClientLibrariesImpl)clientlibs).getCategoriesFromComponents()); }