ModelDataSourceServlet extends AbstractDataSourceServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException { DataSource dataSource = EmptyDataSource.instance(); ResourceResolver resourceResolver = request.getResourceResolver(); QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class); if (queryBuilder != null) { Map<String, String> parameterMap = new HashMap<>(); parameterMap.put("path", "/conf"); parameterMap.put("type", NT_TEMPLATE); parameterMap.put("p.limit", "-1"); parameterMap.put("1_property", JcrConstants.JCR_CONTENT + "/model/" + ResourceResolver.PROPERTY_RESOURCE_TYPE); parameterMap.put("1_property.value", "wcm/scaffolding/components/scaffolding"); PredicateGroup predicateGroup = PredicateGroup.create(parameterMap); Session session = resourceResolver.adaptTo(Session.class); Query query = queryBuilder.createQuery(predicateGroup, session); SearchResult searchResult = query.getResult(); ResourceResolver leakingResourceResolver = null; try { List<Resource> resources = new LinkedList<>(); for (Iterator<Resource> resourceIterator = searchResult.getResources(); resourceIterator.hasNext(); ) { Resource resource = resourceIterator.next(); if (leakingResourceResolver == null) { leakingResourceResolver = resource.getResourceResolver(); } ValueMap modelValueMap = resource.getValueMap(); String modelTitle = modelValueMap.get(JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_TITLE, resource.getName()); String modelPath = resource.getPath(); Resource syntheticResource = createResource(resourceResolver, modelTitle, modelPath); resources.add(syntheticResource); } dataSource = new SimpleDataSource(resources.iterator()); } finally { if (leakingResourceResolver != null) { leakingResourceResolver.close(); } } } request.setAttribute(DataSource.class.getName(), dataSource); }   static final String RESOURCE_TYPE; }
@Test public void verifyDataSourceWhenSessionIsNull() throws Exception { SlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext()); modelDatasourceServlet.doGet(request, context.response()); assertThat(request.getAttribute(DataSource.class.getName()), IsInstanceOf.instanceOf(EmptyDataSource.class)); }
@Test public void verifyDataSource() throws Exception { context.load().json(getClass().getResourceAsStream("test-content.json"), "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel"); ResourceResolver resourceResolver = Mockito.spy(context.resourceResolver()); Resource resource = resourceResolver.getResource("/conf/foobar/settings/dam/cfm/models/yetanothercfmodel"); Resource leakingResource = Mockito.spy(resource); QueryBuilder queryBuilder = Mockito.mock(QueryBuilder.class); Query query = Mockito.mock(Query.class); SearchResult searchResult = Mockito.mock(SearchResult.class); Iterator<Resource> iterator = Mockito.mock(Iterator.class); ResourceResolver leakingResourceResolver = Mockito.mock(ResourceResolver.class); Mockito.doReturn(leakingResourceResolver).when(leakingResource).getResourceResolver(); Mockito.doReturn(queryBuilder).when(resourceResolver).adaptTo(Mockito.eq(QueryBuilder.class)); Mockito.when(queryBuilder.createQuery(Mockito.any(), Mockito.any())).thenReturn(query); Mockito.when(query.getResult()).thenReturn(searchResult); Mockito.when(searchResult.getResources()).thenReturn(iterator); Mockito.when(iterator.hasNext()).thenReturn(true, false); Mockito.when(iterator.next()).thenReturn(leakingResource); SlingHttpServletRequest request = new MockSlingHttpServletRequest(resourceResolver, context.bundleContext()); modelDatasourceServlet.doGet(request, context.response()); SimpleDataSource simpleDataSource = (SimpleDataSource) request.getAttribute(DataSource.class.getName()); assertThat(simpleDataSource.iterator().hasNext(), CoreMatchers.is(true)); ValueMap valueMap = simpleDataSource.iterator().next().getValueMap(); assertThat(valueMap.get("text"), CoreMatchers.is("YetAnotherCFModel")); assertThat(valueMap.get("value"), CoreMatchers.is("/conf/foobar/settings/dam/cfm/models/yetanothercfmodel")); }