Exporter { public void handleFileExport(String id, HttpServletResponse resp) throws ServletException { if (isBlank(id)) { throw new IllegalArgumentException("Id is needed to successfully export a component"); } ByteArrayOutputStream zipStream = null; String filename = null; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Zipper zipper = new Zipper(outputStream)) { zipStream = outputStream; T identifiable = artifactService.get(id); filename = getFileName(identifiable); zipper.addToZip(readAllBytes(repository.resolvePath(id).resolve(format("%s.json", id))), format("%s/%s.json", RESOURCES, repository.getComponentName())); for (ExportStep exporter : exportSteps) { logger.info("export step " + exporter.getClass().getName()); exporter.execute(zipper, identifiable); } } catch (Exception e) { logger.error(format("Technical error on zip creation %s with id %s", repository.getComponentName(), id), e); throw new ExportException(e); } if (zipStream != null) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(zipStream.toByteArray()); ServletOutputStream servletOutputStream = resp.getOutputStream();) { resp.setContentType(MimeType.APPLICATION_ZIP.toString()); resp.setHeader("Content-Disposition", format("inline; filename=%s;", filename)); copy(inputStream, servletOutputStream); } catch (Exception e) { logger.error(format("Technical error when exporting %s with id %s", repository.getComponentName(), id), e); throw new ExportException(e); } } } Exporter(Repository<T> repository, ArtifactService<T> artifactService, ExportStep<T>... exportSteps); void handleFileExport(String id, HttpServletResponse resp);  }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_when_id_is_null() throws Exception { exporter.handleFileExport(null, response); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_when_id_is_blank() throws Exception { exporter.handleFileExport(" ", response); }
@Test public void should_throw_exception_when_artefact_to_export_is_not_found() throws Exception { NotFoundException cause = new NotFoundException("Page not found"); when(pageService.get("unknown-id")).thenThrow(cause); exception.expect(ExportException.class); exception.expectCause(is(cause)); exporter.handleFileExport("unknown-id", response); }
@Test public void should_set_zip_content_type_to_response() throws Exception { Page page = create(aPage().build()); exporter.handleFileExport(page.getId(), response); assertThat(response.getContentType()).isEqualTo("application/zip"); }
@Test public void should_set_formatted_filename_in_header() throws Exception { Page page = create(aPage().withName("é&az zer/è\"").build()); exporter.handleFileExport(page.getId(), response); assertThat(response.getHeader("Content-Disposition")).isEqualTo("inline; filename=page-az-zer.zip;"); }
@Test public void should_set__artifact_type_in_filename() throws Exception { Page page = create(aPage().withType("layout").withName("thelayout").build()); exporter.handleFileExport(page.getId(), response); assertThat(response.getHeader("Content-Disposition")).isEqualTo("inline; filename=layout-thelayout.zip;"); }
@Test public void should_export_json_model_of_the_exported_artefact() throws Exception { Page page = create(aPage().withId("myPage").build()); exporter.handleFileExport(page.getId(), response); Path unzipped = unzip(response); assertThat(readAllBytes(unzipped.resolve("resources/page.json"))).isEqualTo("foobar".getBytes()); deleteDirectory(unzipped.toFile()); }
@Test public void should_execute_export_steps() throws Exception { FakeStep fakeStep1 = new FakeStep("This is some content", "resources/file1.json"); FakeStep fakeStep2 = new FakeStep("This is another content", "resources/deep/file2.json"); Exporter<Page> exporter = new Exporter<>(pageRepository, pageService, fakeStep1, fakeStep2); Page page = create(aPage().build()); exporter.handleFileExport(page.getId(), response); Path unzipped = unzip(response); assertThat(readAllBytes(unzipped.resolve("resources/file1.json"))).isEqualTo("This is some content".getBytes()); assertThat(readAllBytes(unzipped.resolve("resources/deep/file2.json"))).isEqualTo("This is another content".getBytes()); deleteDirectory(unzipped.toFile()); }