@Test public void testWithCurrentResource() throws Exception { context.currentResource( context.resourceResolver().getResource("/content/sample/en/jcr:content")); JSONArray result = getJsonResult(); assertEquals(2, result.length()); assertItem(result.getJSONObject(0), "/content/sample/en/page1", "title1"); assertItem(result.getJSONObject(1), "/content/sample/en/page2", "title2"); }
/** * Test bug #36640 - Resources are not searchable after moving/merging tags * * @see https://issues.adobe.com/browse/CQ5-13533 */ @Test @UseVersion(ANY_BUT_5_5) public void testPageViewSearchAfterMovingTags() throws JSONException, ClientException { TagClient tagClient = adminAuthor.getClient(TagClient.class); final String ns = "itnamespace"; final String tag = "tagOne"; final String tagID = getTagID(ns, tag); try { // create tag tagClient.createTag("Integration Tests", ns, "Created during IT tests.", null); tagClient.createTag("Test Tag", tag, "", getTagID(ns, null)); // tag some content tagClient.setTag(ROOT_PAGE_HANDLE, "+" + tagID); // test cf search JSONArray hits = searchPagesForTag(tagID); Assert.assertTrue("No hits found for tag ID '" + tagID + "'", hits.length() > 0); Assert.assertEquals(ROOT_PAGE_HANDLE, hits.getJSONObject(0).get("path")); // move tag final String newTag = "tagTwo"; final String newTagID = getTagID(ns, newTag); tagClient.moveTag(tagID, newTagID, 200); // test cf search with new tag id hits = searchPagesForTag(newTagID); Assert.assertTrue("No hits found for tag ID '" + newTagID + "'", hits.length() > 0); Assert.assertEquals(ROOT_PAGE_HANDLE, hits.getJSONObject(0).get("path")); // test cf search with old tag id hits = searchPagesForTag(tagID); Assert.assertTrue("No hits found for tag ID '" + tagID + "'", hits.length() > 0); Assert.assertEquals(ROOT_PAGE_HANDLE, hits.getJSONObject(0).get("path")); } finally { // raw node delete here for safe cleanup (deleteTag() does too much validation) tagClient.delete("/etc/tags/" + ns); } }
@Test public void testWithPath() throws Exception { context .request() .setParameterMap(ImmutableValueMap.of(AbstractPageProvider.RP_PATH, "/content/sample/en")); JSONArray result = getJsonResult(); assertEquals(2, result.length()); assertItem(result.getJSONObject(0), "/content/sample/en/page1", "title1"); assertItem(result.getJSONObject(1), "/content/sample/en/page2", "title2"); }
@Test public void testWithPredicate() throws Exception { context.registerService(PredicateProvider.class, new DummyPredicateProvider()); context .request() .setParameterMap( ImmutableValueMap.of( AbstractPageProvider.RP_PATH, "/content/sample/en", AbstractPageProvider.RP_PREDICATE, DummyPredicateProvider.PREDICATE_PAGENAME_PAGE1)); JSONArray result = getJsonResult(); assertEquals(1, result.length()); assertItem(result.getJSONObject(0), "/content/sample/en/page1", "title1"); }
/** * Takes the original request and starts the batching. * * @param request * @param response * @throws IOException */ protected void batchRequest( SlingHttpServletRequest request, SlingHttpServletResponse response, boolean allowModify) throws IOException { // Grab the JSON block out of it and convert it to RequestData objects we can use. String json = request.getParameter(REQUESTS_PARAMETER); List<RequestInfo> batchedRequests = new ArrayList<RequestInfo>(); try { JSONArray arr = new JSONArray(json); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); RequestInfo r = new RequestInfo(obj); if (allowModify || r.isSafe()) { batchedRequests.add(r); } } } catch (JSONException e) { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "Failed to parse the " + REQUESTS_PARAMETER + " parameter"); LOGGER.warn("Failed to parse the " + REQUESTS_PARAMETER + " parameter"); return; } // Loop over the requests and handle each one. try { StringWriter sw = new StringWriter(); JSONWriter write = new JSONWriter(sw); write.object(); write.key("results"); write.array(); for (RequestInfo r : batchedRequests) { doRequest(request, response, r, write); } write.endArray(); write.endObject(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(sw.getBuffer().toString()); } catch (JSONException e) { LOGGER.warn("Failed to create a JSON response"); response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to write JSON response"); } }