Previewer { public <T extends Previewable & Identifiable> ResponseEntity<String> render(String id, Repository<T> repository, HttpServletRequest httpServletRequest) { if (isBlank(id)) { throw new IllegalArgumentException("Need to specify the id of the page to preview."); } try { String html = generator.generateHtml(getResourceContext(httpServletRequest),repository.get(id)); return new ResponseEntity<>(html, HttpStatus.OK); } catch (GenerationException e) { LOGGER.error("Error during page generation", e); return new ResponseEntity<>("Error during page generation", HttpStatus.INTERNAL_SERVER_ERROR); } catch (NotFoundException e) { return new ResponseEntity<>("Page <" + id + "> not found", HttpStatus.NOT_FOUND); } }  ResponseEntity<String> render(String id, Repository<T> repository, HttpServletRequest httpServletRequest);  }
@Test public void should_generate_html_and_print_it_in_response() throws Exception { when(pageRepository.get(page.getId())).thenReturn(page); when(generator.generateHtml("/runtime/", page)).thenReturn("foobar"); ResponseEntity<String> response = previewer.render(page.getId(), pageRepository, request); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getBody()).isEqualTo("foobar"); }
@Test public void should_return_error_response_when_error_occur_on_generation() throws Exception { when(pageRepository.get(page.getId())).thenReturn(page); when(generator.generateHtml("/runtime/", page)).thenThrow(new GenerationException("error", new Exception())); ResponseEntity<String> response = previewer.render(page.getId(), pageRepository, request); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(response.getBody()).isEqualTo("Error during page generation"); }
@Test public void should_return_error_response_when_page_is_not_found() throws Exception { when(pageRepository.get("unexisting-page")).thenThrow(new NotFoundException("page not found")); ResponseEntity<String> response = previewer.render("unexisting-page", pageRepository, request); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(response.getBody()).isEqualTo("Page <unexisting-page> not found"); }