AllowedColorSwatchesDataSourceServlet extends SlingSafeMethodsServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException { SimpleDataSource allowedColorSwatchesDataSource = new SimpleDataSource(getAllowedColorSwatches(request).iterator()); request.setAttribute(DataSource.class.getName(), allowedColorSwatchesDataSource); }    }
@Test public void testDataSource() throws Exception { AllowedColorSwatchesDataSourceServlet dataSourceServlet = new AllowedColorSwatchesDataSourceServlet(); String[] expected = new String[] {COLOR_VALUE_1, COLOR_VALUE_2, COLOR_VALUE_3}; context.build().resource("/content", ImmutableMap.of("sling:resourceType", "/restype")).commit(); context.contentPolicyMapping("/restype", ImmutableMap.of(AllowedColorSwatchesDataSourceServlet.PN_ALLOWED_COLOR_SWATCHES, expected)); context.request().setAttribute(Value.CONTENTPATH_ATTRIBUTE, "/content"); dataSourceServlet.doGet(context.request(), context.response()); DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName()); Assertions.assertNotNull(dataSource); dataSource.iterator().forEachRemaining(resource -> { ValueMap props = resource.getValueMap(); String allowedColorSwatch = props.get(AllowedColorSwatchesDataSourceServlet.PN_COLOR_VALUE, String.class); Assertions.assertTrue(Arrays.asList(expected).contains(allowedColorSwatch), "Allowed color swatches values are not as expected"); }); }