AllowedHeadingElementsDataSourceServlet extends SlingSafeMethodsServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException { SimpleDataSource allowedHeadingElementsDataSource = new SimpleDataSource(getAllowedHeadingElements(request).iterator()); request.setAttribute(DataSource.class.getName(), allowedHeadingElementsDataSource); }   final static String RESOURCE_TYPE_V1; final static String RESOURCE_TYPE_TITLE_V1; final static String PN_ALLOWED_HEADING_ELEMENTS; final static String PN_DEFAULT_HEADING_ELEMENT; final static String PN_ALLOWED_TYPES; final static String PN_DEFAULT_TYPE; }
@Test void testDataSource() throws Exception { context.contentPolicyMapping("core/wcm/components/title/v1/title", "type", "h3", AllowedHeadingElementsDataSourceServlet.PN_ALLOWED_TYPES, new String[]{"h3", "h4"}); 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(textValueDataResourceSource.getText().matches("h[3|4]"), "Expected type in (h3, h4)"); assertTrue(textValueDataResourceSource.getValue().matches("h[3|4]"), "Expected value in (h3, h4)"); if (textValueDataResourceSource.getValue().equals("h3")) { assertTrue(textValueDataResourceSource.getSelected()); } else { assertFalse(textValueDataResourceSource.getSelected()); } }); }
@Test void testDataSourceWithInvalidValues() throws Exception { context.contentPolicyMapping("core/wcm/components/title/v1/title", AllowedHeadingElementsDataSourceServlet.PN_ALLOWED_TYPES, new String[]{"foo", "h10"}); 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; assertNull(textValueDataResourceSource.getText(), "Expected null type"); assertTrue(textValueDataResourceSource.getValue().matches("foo|h10"), "Expected value in (foo, h10)"); }); }