@Test public void testGenerateHtmlOutput() throws Exception { SlingHttpServletResponse servletResponse = Mockito.mock(SlingHttpServletResponse.class); Collection<Hit> hits = new ArrayList<>(); Hit hit = new Hit(); hit.set("path", "/hit/path1"); Hit hit2 = new Hit(); hit2.set("path", "/hit/path2"); hit2.set("resource", Mockito.mock(Resource.class)); RequestDispatcher requestDispatcher = Mockito.mock(RequestDispatcher.class); Mockito.when(servletRequest.getRequestDispatcher(Mockito.any(Resource.class), Mockito.any())).thenReturn(requestDispatcher); Mockito.doAnswer(invocation -> { StringResponseWrapper response = invocation.getArgumentAt(1, StringResponseWrapper.class); response.getWriter().println("Hit"); return null; }).when(requestDispatcher).include(Mockito.any(), Mockito.any(StringResponseWrapper.class)); String output = viewHandler.generateHtmlOutput(Collections.emptyList(), servletRequest, servletResponse); Assert.assertEquals("[]", output); hits.add(hit); output = viewHandler.generateHtmlOutput(hits, servletRequest, servletResponse); Assert.assertNotNull(output); Assert.assertTrue(output.startsWith("[com.day.cq.wcm.core.contentfinder.Hit@")); Mockito.when(servletRequest.getParameter("itemResourceType")).thenReturn("item/resource/type"); output = viewHandler.generateHtmlOutput(Collections.emptyList(), servletRequest, servletResponse); Assert.assertEquals("", output); output = viewHandler.generateHtmlOutput(hits, servletRequest, servletResponse); Assert.assertEquals("", output); hit.set("resource", Mockito.mock(Resource.class)); output = viewHandler.generateHtmlOutput(hits, servletRequest, servletResponse); Assert.assertEquals("Hit" + LINE_SEPARATOR, output); hits.add(hit2); output = viewHandler.generateHtmlOutput(hits, servletRequest, servletResponse); Assert.assertEquals("Hit" + LINE_SEPARATOR + "Hit" + LINE_SEPARATOR, output); }
protected String generateHtmlOutput(Collection<Hit> hits, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (request.getParameter("itemResourceType") == null) { return hits.toString(); } StringResponseWrapper hitResponse = new StringResponseWrapper(response); RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(null); requestDispatcherOptions.setForceResourceType(request.getParameter("itemResourceType")); for (Hit hit : hits) { Resource resource = (Resource) hit.get("resource"); if (resource != null) { request.setAttribute(Resource.class.getCanonicalName(), resource); RequestDispatcher dispatcher = request.getRequestDispatcher(resource, requestDispatcherOptions); dispatcher.include(request, hitResponse); request.removeAttribute(Resource.class.getCanonicalName()); } } return hitResponse.getString(); }
GraphqlProductViewHandler extends ViewHandler { protected String generateHtmlOutput(Collection<Hit> hits, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (request.getParameter("itemResourceType") == null) { return hits.toString(); } StringResponseWrapper hitResponse = new StringResponseWrapper(response); RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(null); requestDispatcherOptions.setForceResourceType(request.getParameter("itemResourceType")); for (Hit hit : hits) { Resource resource = (Resource) hit.get("resource"); if (resource != null) { request.setAttribute(Resource.class.getCanonicalName(), resource); RequestDispatcher dispatcher = request.getRequestDispatcher(resource, requestDispatcherOptions); dispatcher.include(request, hitResponse); request.removeAttribute(Resource.class.getCanonicalName()); } } return hitResponse.getString(); } }
GraphqlProductViewHandler extends ViewHandler { protected String generateHtmlOutput(Collection<Hit> hits, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (request.getParameter("itemResourceType") == null) { return hits.toString(); } StringResponseWrapper hitResponse = new StringResponseWrapper(response); RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(null); requestDispatcherOptions.setForceResourceType(request.getParameter("itemResourceType")); for (Hit hit : hits) { Resource resource = (Resource) hit.get("resource"); if (resource != null) { request.setAttribute(Resource.class.getCanonicalName(), resource); RequestDispatcher dispatcher = request.getRequestDispatcher(resource, requestDispatcherOptions); dispatcher.include(request, hitResponse); request.removeAttribute(Resource.class.getCanonicalName()); } } return hitResponse.getString(); }  }
GraphqlProductViewHandler extends ViewHandler { protected String generateHtmlOutput(Collection<Hit> hits, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (request.getParameter("itemResourceType") == null) { return hits.toString(); } StringResponseWrapper hitResponse = new StringResponseWrapper(response); RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(null); requestDispatcherOptions.setForceResourceType(request.getParameter("itemResourceType")); for (Hit hit : hits) { Resource resource = (Resource) hit.get("resource"); if (resource != null) { request.setAttribute(Resource.class.getCanonicalName(), resource); RequestDispatcher dispatcher = request.getRequestDispatcher(resource, requestDispatcherOptions); dispatcher.include(request, hitResponse); request.removeAttribute(Resource.class.getCanonicalName()); } } return hitResponse.getString(); }   }
GraphqlProductViewHandler extends ViewHandler { protected String generateHtmlOutput(Collection<Hit> hits, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (request.getParameter("itemResourceType") == null) { return hits.toString(); } StringResponseWrapper hitResponse = new StringResponseWrapper(response); RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions(null); requestDispatcherOptions.setForceResourceType(request.getParameter("itemResourceType")); for (Hit hit : hits) { Resource resource = (Resource) hit.get("resource"); if (resource != null) { request.setAttribute(Resource.class.getCanonicalName(), resource); RequestDispatcher dispatcher = request.getRequestDispatcher(resource, requestDispatcherOptions); dispatcher.include(request, hitResponse); request.removeAttribute(Resource.class.getCanonicalName()); } } return hitResponse.getString(); }    }