diff --git a/alfa-xdomea/src/main/java/de/ozgcloud/alfa/export/XDomeaService.java b/alfa-xdomea/src/main/java/de/ozgcloud/alfa/export/XDomeaService.java index c7012b1dd4b9d9d65186a25aaf3ceedf217864a1..0ae2cee646faa19dc7239d33ec0631dcdc7bf926 100644 --- a/alfa-xdomea/src/main/java/de/ozgcloud/alfa/export/XDomeaService.java +++ b/alfa-xdomea/src/main/java/de/ozgcloud/alfa/export/XDomeaService.java @@ -1,5 +1,8 @@ package de.ozgcloud.alfa.export; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; @@ -10,6 +13,7 @@ import java.util.zip.ZipOutputStream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import de.itvsh.kop.common.binaryfile.TempFileUtils; import de.itvsh.kop.common.errorhandling.TechnicalException; import de.ozgcloud.alfa.common.file.OzgFile; import de.ozgcloud.alfa.vorgang.VorgangController; @@ -39,13 +43,8 @@ class XDomeaService { var fileName = buildXmlFilename(filenameId); var xmlContent = createXmlContent(vorgang); var ozgFiles = exportFileService.getAllPdfs(vorgang.getEingang().getId()); - - try (var zipOutputStream = new ZipOutputStream(out)) { - putZipEntry(fileName, xmlContent, zipOutputStream); - putFilesIntoZip(ozgFiles, zipOutputStream); - } catch (Exception e) { - throw new TechnicalException("Error creating XDomea zip file", e); - } + var zipFile = createZipFile(fileName, xmlContent, ozgFiles); + writeZipFileContent(zipFile, out); } String buildXmlFilename(String filenameId) { @@ -56,6 +55,17 @@ class XDomeaService { return xDomeaXmlMarshaller.marshal(abgabeCreator.create(vorgang)); } + File createZipFile(String fileName, String xmlContent, Stream<OzgFile> ozgFiles) { + var file = TempFileUtils.createTmpFile().toFile(); + try (var zipOutputStream = new ZipOutputStream(new FileOutputStream(file))) { + putZipEntry(fileName, xmlContent, zipOutputStream); + putFilesIntoZip(ozgFiles, zipOutputStream); + return file; + } catch (Exception e) { + throw new TechnicalException("Error creating XDomea zip file", e); + } + } + void putZipEntry(String fileName, String fileData, ZipOutputStream zipOutputStream) throws IOException { var entry = new ZipEntry(fileName); zipOutputStream.putNextEntry(entry); @@ -75,4 +85,12 @@ class XDomeaService { exportFileService.writeOzgFile(ozgFile.getId(), zipOutputStream); zipOutputStream.closeEntry(); } + + void writeZipFileContent(File file, OutputStream outputStream) { + try (var fileInputStream = new FileInputStream(file)) { + fileInputStream.transferTo(outputStream); + } catch (Exception e) { + throw new TechnicalException("Error writting XDomea zip file to output stream", e); + } + } } diff --git a/alfa-xdomea/src/test/java/de/ozgcloud/alfa/export/XDomeaServiceTest.java b/alfa-xdomea/src/test/java/de/ozgcloud/alfa/export/XDomeaServiceTest.java index 1f204306638b352ecae3d1ec6f32ac1e70893a93..c5b15d26c762fc863a053a9feebb4aa50ae06c64 100644 --- a/alfa-xdomea/src/test/java/de/ozgcloud/alfa/export/XDomeaServiceTest.java +++ b/alfa-xdomea/src/test/java/de/ozgcloud/alfa/export/XDomeaServiceTest.java @@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -59,16 +60,20 @@ class XDomeaServiceTest { private static final String XML_STRING = "<xml>"; private static final String FILENAME_ID = UUID.randomUUID().toString(); - + private static final String FILE_NAME = "file.zip"; private final VorgangWithEingang vorgang = VorgangWithEingangTestFactory.create(); private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - private final OzgFile ozgFile = OzgFileTestFactory.create(); + private final Stream<OzgFile> ozgFiles = Stream.of(OzgFileTestFactory.create()); + private final File zipFile = mock(File.class); @BeforeEach void init() { when(vorgangController.getVorgang(VorgangHeaderTestFactory.ID)).thenReturn(vorgang); doReturn(XML_STRING).when(service).createXmlContent(vorgang); - when(exportFileService.getAllPdfs(EingangTestFactory.ID)).thenReturn(Stream.of(ozgFile)); + when(exportFileService.getAllPdfs(EingangTestFactory.ID)).thenReturn(ozgFiles); + doReturn(FILE_NAME).when(service).buildXmlFilename(FILENAME_ID); + doReturn(zipFile).when(service).createZipFile(FILE_NAME, XML_STRING, ozgFiles); + doNothing().when(service).writeZipFileContent(zipFile, outputStream); } @Test @@ -85,13 +90,6 @@ class XDomeaServiceTest { verify(service).createXmlContent(vorgang); } - @Test - void shouldCreateZipEntry() throws IOException { - callService(); - - verify(service).putZipEntry(anyString(), eq(XML_STRING), any(ZipOutputStream.class)); - } - @Test void shouldGenerateXmlFilename() { callService(); @@ -100,33 +98,24 @@ class XDomeaServiceTest { } @Test - void shouldWriteBytes() { + void shouldGetPdfFiles() { callService(); - assertThat(outputStream).isNotNull(); - assertThat(outputStream.toByteArray()).hasSizeGreaterThan(100); - } - - @Test - void shouldThrowTechnicalException() throws IOException { - doThrow(IOException.class).when(service).putZipEntry(anyString(), eq(XML_STRING), any(ZipOutputStream.class)); - - assertThatThrownBy(this::callService).isInstanceOf(TechnicalException.class); + verify(exportFileService).getAllPdfs(EingangTestFactory.ID); } @Test - void shouldGetPdfFiles() { + void shouldCreateZipFile() { callService(); - verify(exportFileService).getAllPdfs(EingangTestFactory.ID); + verify(service).createZipFile(FILE_NAME, XML_STRING, ozgFiles); } - @SneakyThrows @Test - void shouldWritePdfFiles() { + void shouldWriteZipFileContentToOutputStream() { callService(); - verify(service).putOzgFileIntoZip(eq(ozgFile), any(ZipOutputStream.class)); + verify(service).writeZipFileContent(zipFile, outputStream); } private void callService() { @@ -188,6 +177,60 @@ class XDomeaServiceTest { } } + @Nested + class TestCreateZipFile { + + private static final String XML_STRING = "<xml>"; + private static final String FILE_NAME = "file.zip"; + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + private final OzgFile ozgFile = OzgFileTestFactory.create(); + + @Captor + private ArgumentCaptor<ZipOutputStream> zipOutputStreamArgumentCaptor; + + @Test + void shouldCreateZipEntry() throws IOException { + callService(); + + verify(service).putZipEntry(eq(FILE_NAME), eq(XML_STRING), any(ZipOutputStream.class)); + } + + @Test + void shouldCreateZipOutputStream() throws IOException { + callService(); + + verify(service).putZipEntry(eq(FILE_NAME), eq(XML_STRING), zipOutputStreamArgumentCaptor.capture()); + assertThat(zipOutputStreamArgumentCaptor.getValue()).isInstanceOf(ZipOutputStream.class); + } + + @Test + void shouldWriteBytes() { + var file = callService(); + + assertThat(file).isNotEmpty().content().hasSizeGreaterThan(100); + } + + @SneakyThrows + @Test + void shouldWritePdfFiles() { + callService(); + + verify(service).putOzgFileIntoZip(eq(ozgFile), any(ZipOutputStream.class)); + } + + @Test + void shouldThrowTechnicalException() throws IOException { + doThrow(IOException.class).when(service).putZipEntry(anyString(), eq(XML_STRING), any(ZipOutputStream.class)); + + assertThatThrownBy(this::callService).isInstanceOf(TechnicalException.class); + } + + private File callService() { + return service.createZipFile(FILE_NAME, XML_STRING, Stream.of(ozgFile)); + } + + } + @Nested class TestPutZipEntry { diff --git a/pom.xml b/pom.xml index 46ae5cc7d1bce45bc36dd5ec8bf33208aa7dedea..36811a6facb041ce98f4f76a0bbf2545b4adcbac 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,7 @@ <parent> <groupId>de.itvsh.kop.common</groupId> <artifactId>kop-common-parent</artifactId> - <version>1.7.0</version> + <version>1.8.0</version> </parent> <modules>