use of com.day.cq.search.result.SearchResult in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ListImplTest method testSearchListType.
@Test
protected void testSearchListType() throws Exception {
Session mockSession = mock(Session.class);
SimpleSearch mockSimpleSearch = mock(SimpleSearch.class);
context.registerAdapter(ResourceResolver.class, Session.class, mockSession);
context.registerAdapter(Resource.class, SimpleSearch.class, mockSimpleSearch);
SearchResult searchResult = mock(SearchResult.class);
when(mockSimpleSearch.getResult()).thenReturn(searchResult);
when(searchResult.getResources()).thenReturn(Collections.singletonList(Objects.requireNonNull(context.resourceResolver().getResource("/content/list/pages/page_1/jcr:content"))).iterator());
List list = getListUnderTest(LIST_6);
checkListConsistencyByPaths(list, new String[] { "/content/list/pages/page_1" });
Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_6));
}
use of com.day.cq.search.result.SearchResult 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.result.SearchResult 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);
}
use of com.day.cq.search.result.SearchResult in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class SearchResultServlet method getResults.
/**
* Gets the search results.
*
* @param request The search request.
* @param searchComponent The search component.
* @param pageManager A PageManager.
* @return List of search results.
*/
@NotNull
private List<ListItem> getResults(@NotNull final SlingHttpServletRequest request, @NotNull final Search searchComponent, @NotNull final PageManager pageManager) {
List<ListItem> results = new ArrayList<>();
String fulltext = request.getParameter(PARAM_FULLTEXT);
if (fulltext == null || fulltext.length() < searchComponent.getSearchTermMinimumLength()) {
return results;
}
long resultsOffset = Optional.ofNullable(request.getParameter(PARAM_RESULTS_OFFSET)).map(Long::parseLong).orElse(0L);
Map<String, String> predicatesMap = new HashMap<>();
predicatesMap.put(FulltextPredicateEvaluator.FULLTEXT, fulltext);
predicatesMap.put(PathPredicateEvaluator.PATH, searchComponent.getSearchRootPagePath());
predicatesMap.put(TypePredicateEvaluator.TYPE, NameConstants.NT_PAGE);
PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
ResourceResolver resourceResolver = request.getResource().getResourceResolver();
Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
if (searchComponent.getResultsSize() != 0) {
query.setHitsPerPage(searchComponent.getResultsSize());
}
if (resultsOffset != 0) {
query.setStart(resultsOffset);
}
SearchResult searchResult = query.getResult();
LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
// Query builder has a leaking resource resolver, so the following work around is required.
ResourceResolver leakingResourceResolver = null;
try {
Iterator<Resource> resourceIterator = searchResult.getResources();
while (resourceIterator.hasNext()) {
Resource resource = resourceIterator.next();
// Get a reference to QB's leaking resource resolver
if (leakingResourceResolver == null) {
leakingResourceResolver = resource.getResourceResolver();
}
Optional.of(resource).map(res -> resourceResolver.getResource(res.getPath())).map(pageManager::getContainingPage).map(page -> new PageListItemImpl(linkHandler, page, searchComponent.getId(), null)).ifPresent(results::add);
}
} finally {
if (leakingResourceResolver != null) {
leakingResourceResolver.close();
}
}
return results;
}
use of com.day.cq.search.result.SearchResult in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ListImpl method getSearchListItems.
/**
* Get the list items if using the search source type.
*
* @return The search list items.
*/
private Stream<Page> getSearchListItems() {
Optional<Page> searchRoot = getRootPage(PN_SEARCH_IN);
String query = properties.get(PN_SEARCH_QUERY, String.class);
if (!StringUtils.isBlank(query) && searchRoot.isPresent()) {
SimpleSearch search = resource.adaptTo(SimpleSearch.class);
if (search != null) {
search.setQuery(query);
search.setSearchIn(searchRoot.get().getPath());
search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));
int limit = properties.get(PN_SEARCH_LIMIT, SEARCH_LIMIT_DEFAULT);
search.setHitsPerPage(limit);
return safeGetSearchResult(search).map(SearchResult::getResources).map(it -> (Iterable<Resource>) () -> it).map(it -> StreamSupport.stream(it.spliterator(), false)).orElseGet(Stream::empty).filter(Objects::nonNull).map(currentPage.getPageManager()::getContainingPage).filter(Objects::nonNull);
}
}
return Stream.empty();
}
Aggregations