RenderPageAction implements Action { @Override public String doAction(Resource resource) throws PersistenceException, AecuException { Page page = context.getPageManager().getContainingPage(resource); if (page == null) { throw new AecuException("Unable to find a page for resource " + resource.getPath()); } String successMessage = "Correct page rendering for " + page.getPath(); RequestResponseFactory requestResponseFactory = context.getRequestResponseFactory(); SlingRequestProcessor requestProcessor = context.getSlingRequestProcessor(); String requestPath = page.getPath() + ".html"; HttpServletRequest req = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, requestPath); WCMMode.DISABLED.toRequest(req); MockHttpServletResponse resp = createResponse(); try { requestProcessor.processRequest(req, resp, context.getResolver()); if (resp.getStatus() != statusCode) { throw new AecuException(requestPath + " returned " + resp.getStatus() + " instead of " + statusCode); } String html = resp.getOutput().toString(); if (StringUtils.isNotBlank(textPresent) && !html.contains(textPresent)) { throw new AecuException(requestPath + " did not include " + textPresent); } if (StringUtils.isNotBlank(textNotPresent) && html.contains(textNotPresent)) { throw new AecuException(requestPath + " did include " + textNotPresent); } } catch (ServletException | IOException e) { throw new PersistenceException("Unable to render " + requestPath); } return successMessage; } RenderPageAction(BindingContext context, int statusCode, String textPresent, String textNotPresent); @Override String doAction(Resource resource);  }
@Test public void doAction() throws PersistenceException, AecuException { RenderPageAction action = new RenderPageAction(context, 200, null, null); String result = action.doAction(resource); assertTrue(result.contains("Correct")); }
@Test(expected = AecuException.class) public void doAction_noPage() throws PersistenceException, AecuException { when(pageManager.getContainingPage(resource)).thenReturn(null); RenderPageAction action = new RenderPageAction(context, 200, null, null); action.doAction(resource); }
@Test public void doAction_textPresent() throws AecuException, IOException { actionSpy.doAction(resource); }
@Test(expected = AecuException.class) public void doAction_textPresentDoesNotMatch() throws AecuException, IOException { when(out.toString()).thenReturn("1111"); actionSpy.doAction(resource); }
@Test(expected = AecuException.class) public void doAction_textNotPresentDoesNotMatch() throws AecuException, IOException { when(out.toString()).thenReturn("test789"); actionSpy.doAction(resource); }