use of com.day.cq.search.Query in project acs-aem-commons by Adobe-Consulting-Services.
the class OnDeployScriptBase method searchAndUpdateResourceType.
/**
* Searches for the current sling:resourceType under /content and replaces any nodes it finds
* with the newResourceType.
*
* @param oldResourceType The current sling:resourceType.
* @param newResourceType The new sling:resourceType to be used.
*/
protected final void searchAndUpdateResourceType(String oldResourceType, String newResourceType) throws RepositoryException {
Map<String, String> map = new HashMap<>();
map.put("p.limit", "-1");
map.put("path", "/content");
map.put("1_property", SLING_RESOURCE_TYPE);
map.put("1_property.value", oldResourceType);
logger.info("Finding all nodes under /content with resource type: {}", oldResourceType);
Query query = resourceResolver.adaptTo(QueryBuilder.class).createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
Iterator<Node> nodeItr = result.getNodes();
if (nodeItr.hasNext()) {
while (nodeItr.hasNext()) {
Node node = nodeItr.next();
updateResourceType(node, newResourceType);
}
} else {
logger.info("No nodes found with resource type: {}", oldResourceType);
}
}
use of com.day.cq.search.Query in project acs-aem-commons by Adobe-Consulting-Services.
the class TwitterFeedUpdaterImpl method findTwitterResources.
private List<Resource> findTwitterResources(ResourceResolver resourceResolver) {
List<Resource> twitterResources = new ArrayList<>();
Map<String, String> predicateMap = new HashMap<>();
predicateMap.put("path", "/content");
predicateMap.put("property", "sling:resourceType");
int counter = 1;
for (String path : twitterComponentPaths) {
predicateMap.put("property." + (counter++) + "_value", path);
}
predicateMap.put("p.limit", "-1");
QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Session session = resourceResolver.adaptTo(Session.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
SearchResult result = query.getResult();
Iterator<Resource> resources = result.getResources();
while (resources.hasNext()) {
twitterResources.add(resources.next());
}
return twitterResources;
}
use of com.day.cq.search.Query in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ModelDataSourceServletTest method verifyDataSource.
@Test
public void verifyDataSource() throws Exception {
// GIVEN
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());
// WHEN
modelDatasourceServlet.doGet(request, context.response());
// THEN
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"));
}
use of com.day.cq.search.Query in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ContentFragmentListImplTest method testListWithOneFragment.
@SuppressWarnings("unchecked")
private void testListWithOneFragment(String listName) {
Resource resource = context.resourceResolver().getResource("/content/dam/contentfragments-for-list/text-only");
if (resource != null) {
Resource DAMFragment = Mockito.spy(resource);
Query query = Mockito.mock(Query.class);
SearchResult searchResult = Mockito.mock(SearchResult.class);
Iterator<Resource> iterator = Mockito.mock(Iterator.class);
ResourceResolver spyResolver = Mockito.spy(DAMFragment.getResourceResolver());
when(query.getResult()).thenReturn(searchResult);
when(searchResult.getResources()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(true, false);
when(iterator.next()).thenReturn(DAMFragment);
when(DAMFragment.getResourceResolver()).thenReturn(spyResolver);
Mockito.doNothing().when(spyResolver).close();
when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
ContentFragmentList contentFragmentList = getModelInstanceUnderTest(listName);
assertEquals(ContentFragmentListImpl.RESOURCE_TYPE_V1, contentFragmentList.getExportedType());
assertEquals(contentFragmentList.getListItems().size(), 1);
Utils.testJSONExport(contentFragmentList, Utils.getTestExporterJSONPath(TEST_BASE, listName));
Mockito.doCallRealMethod().when(spyResolver).close();
}
}
use of com.day.cq.search.Query in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ContentFragmentListImplTest method setUp.
@BeforeEach
@SuppressWarnings("unchecked")
void setUp() throws NoSuchFieldException, IllegalAccessException {
super.setUp();
// Load additional content for content list model
context.load().json(TEST_BASE + "/test-content.json", "/content/tests");
context.load().json("/contentfragmentlist/test-content-dam-contentfragments.json", "/content/dam/contentfragments-for-list");
Query query = Mockito.mock(Query.class);
SearchResult searchResult = Mockito.mock(SearchResult.class);
Iterator<Resource> iterator = Mockito.mock(Iterator.class);
Resource resource = Mockito.mock(Resource.class);
leakingResourceResolverMock = Mockito.mock(ResourceResolver.class);
when(query.getResult()).thenReturn(searchResult);
when(searchResult.getResources()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(true, false);
when(iterator.next()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(leakingResourceResolverMock);
when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class))).thenReturn(query);
}
Aggregations