ClientLibraryCategoriesDataSourceServlet extends SlingSafeMethodsServlet { @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); }   final static String RESOURCE_TYPE; final static String PN_LIBRARY_TYPE; }
@Test public void testDataSource() throws Exception { context.currentResource("/apps/clientlibrarycategoriesinput"); dataSourceServlet.doGet(context.request(), context.response()); DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName()); assertNotNull(dataSource); dataSource.iterator().forEachRemaining(resource -> { assertTrue(TextValueDataResourceSource.class.isAssignableFrom(resource.getClass()), "Expected class"); TextValueDataResourceSource textValueDataResourceSource = (TextValueDataResourceSource) resource; assertTrue(Arrays.asList(CLIENTLIB_A_CATEGORIES[0], CLIENTLIB_B_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getText()), "Expected type in (clientlib.a.jsonly, clientlib.b.cssonly, clientlib.c.jsandcss)"); assertTrue(Arrays.asList(CLIENTLIB_A_CATEGORIES[0], CLIENTLIB_B_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getValue()), "Expected value in (clientlib.a.jsonly, clientlib.b.cssonly, clientlib.c.jsandcss)"); }); }
@Test public void testDataSourceTypeJS() throws Exception { context.currentResource("/apps/clientlibrarycategoriesinputjs"); dataSourceServlet.doGet(context.request(), context.response()); DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName()); assertNotNull(dataSource); dataSource.iterator().forEachRemaining(resource -> { assertTrue(TextValueDataResourceSource.class.isAssignableFrom(resource.getClass()), "Expected class"); TextValueDataResourceSource textValueDataResourceSource = (TextValueDataResourceSource) resource; assertTrue(Arrays.asList(CLIENTLIB_A_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getText()), "Expected type in (clientlib.a.jsonly, clientlib.c.jsandcss)"); assertTrue(Arrays.asList(CLIENTLIB_A_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getValue()), "Expected value in (clientlib.a.jsonly, clientlib.c.jsandcss)"); }); }
@Test public void testDataSourceTypeCSS() throws Exception { context.currentResource("/apps/clientlibrarycategoriesinputcss"); dataSourceServlet.doGet(context.request(), context.response()); DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName()); assertNotNull(dataSource); dataSource.iterator().forEachRemaining(resource -> { assertTrue(TextValueDataResourceSource.class.isAssignableFrom(resource.getClass()), "Expected class"); TextValueDataResourceSource textValueDataResourceSource = (TextValueDataResourceSource) resource; assertTrue(Arrays.asList(CLIENTLIB_B_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getText()), "Expected type in (clientlib.b.cssonly, clientlib.c.jsandcss)"); assertTrue(Arrays.asList(CLIENTLIB_B_CATEGORIES[0], CLIENTLIB_C_CATEGORIES[0]).contains(textValueDataResourceSource.getValue()), "Expected value in (clientlib.b.cssonly, clientlib.c.jsandcss)"); }); }