Search in sources :

Example 1 with QueryBuilder

use of com.day.cq.search.QueryBuilder in project acs-aem-commons by Adobe-Consulting-Services.

the class OnDeployScriptBaseTest method setup.

@Before
public void setup() throws Exception {
    resourceResolver = spy(context.resourceResolver());
    Session session = resourceResolver.adaptTo(Session.class);
    Node nodeContent = session.getRootNode().addNode("content", "nt:resource");
    nodeContent.addNode("to-delete");
    Node nodeContentResType1 = nodeContent.addNode("resource-type-update1");
    nodeContentResType1.setProperty("sling:resourceType", "test/component/comp1");
    nodeContentResType1.setProperty("text", "hello world");
    Node nodeContentResType2 = nodeContent.addNode("resource-type-update2");
    nodeContentResType2.setProperty("sling:resourceType", "test/component/comp2");
    nodeContent.addNode("resource-type-missing");
    // Create the test class instance
    onDeployScript = new OnDeployScriptBaseExt();
    queryBuilder = mock(QueryBuilder.class);
    doReturn(queryBuilder).when(resourceResolver).adaptTo(QueryBuilder.class);
    onDeployScript.execute(resourceResolver);
    // Reset the LogTester
    LogTester.reset();
}
Also used : Node(javax.jcr.Node) QueryBuilder(com.day.cq.search.QueryBuilder) Session(javax.jcr.Session) Before(org.junit.Before)

Example 2 with QueryBuilder

use of com.day.cq.search.QueryBuilder in project acs-aem-commons by Adobe-Consulting-Services.

the class QueryBuilderBindingsValuesProviderTest method test.

@Test
public void test() throws Exception {
    QueryBuilderBindingsValuesProvider provider = new QueryBuilderBindingsValuesProvider();
    QueryBuilder qb = mock(QueryBuilder.class);
    PrivateAccessor.setField(provider, "queryBuilder", qb);
    Bindings bindings = mock(Bindings.class);
    provider.addBindings(bindings);
    verify(bindings).put("queryBuilder", qb);
}
Also used : QueryBuilder(com.day.cq.search.QueryBuilder) Bindings(javax.script.Bindings) Test(org.junit.Test)

Example 3 with QueryBuilder

use of com.day.cq.search.QueryBuilder 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;
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) Session(javax.jcr.Session)

Example 4 with QueryBuilder

use of com.day.cq.search.QueryBuilder 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"));
}
Also used : Query(com.day.cq.search.Query) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 5 with QueryBuilder

use of com.day.cq.search.QueryBuilder in project acs-aem-commons by Adobe-Consulting-Services.

the class QueryBuilderViewHandler method createQuery.

@Override
protected ViewQuery createQuery(SlingHttpServletRequest slingRequest, Session session, String queryString) throws Exception {
    final ResourceResolver resolver = slingRequest.getResourceResolver();
    final QueryBuilder qb = resolver.adaptTo(QueryBuilder.class);
    Map<String, String> map;
    if (GQLToQueryBuilderConverter.convertToQueryBuilder(slingRequest)) {
        map = this.convertToQueryBuilderParams(slingRequest, queryString);
        log.debug("Forced QueryBuilder Parameter Map: {}", map);
    } else {
        map = this.getQueryBuilderParams(slingRequest, queryString);
        log.debug("Converted QueryBuilder Parameter Map: {}", map);
    }
    final Query query = qb.createQuery(PredicateGroup.create(map), session);
    return new QueryBuilderViewQuery(query);
}
Also used : ViewQuery(com.day.cq.wcm.core.contentfinder.ViewQuery) Query(com.day.cq.search.Query) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) QueryBuilder(com.day.cq.search.QueryBuilder)

Aggregations

QueryBuilder (com.day.cq.search.QueryBuilder)7 Query (com.day.cq.search.Query)5 SearchResult (com.day.cq.search.result.SearchResult)4 Session (javax.jcr.Session)4 Resource (org.apache.sling.api.resource.Resource)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)2 PredicateGroup (com.day.cq.search.PredicateGroup)2 HashMap (java.util.HashMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 DAMContentFragment (com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)1 ViewQuery (com.day.cq.wcm.core.contentfinder.ViewQuery)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 PostConstruct (javax.annotation.PostConstruct)1 Node (javax.jcr.Node)1 Bindings (javax.script.Bindings)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1