diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileService.java index a4ef01bb4e6c538a887d6b331001331df1dbb3e6..2e2b1ccef2727daaa8ce8edeb577412a26281016 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileService.java @@ -30,7 +30,6 @@ import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.URLConnection; -import java.nio.charset.Charset; import java.util.Base64; import java.util.Optional; @@ -75,8 +74,7 @@ public class AttachmentFileService { public InputStream getFileContent(FileId fileId) { var inputStream = createInputStream(); - var outputStream = connectToOutputStream(inputStream); - taskExecutor.execute(() -> ozgCloudFileService.writeFileDataToStream(fileIdMapper.toFileId(fileId.toString()), outputStream)); + writeFileContent(fileId.toString(), connectToOutputStream(inputStream)); return inputStream; } @@ -92,6 +90,13 @@ public class AttachmentFileService { } } + void writeFileContent(String fileId,OutputStream outputStream) { + taskExecutor.execute(() -> { + ozgCloudFileService.writeFileDataToStream(fileIdMapper.toFileId(fileId), outputStream); + IOUtils.closeQuietly(outputStream, e -> LOG.warn("Cannot close output stream", e)); + }); + } + public String uploadAttachmentFile(AttachmentFile attachmentFile, String fileContent) { var decodedContent = Base64.getDecoder().decode(fileContent); var ozgCloudFileId = ozgCloudFileService.uploadFile(attachmentFileMapper.toOzgCloudUploadFile( diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/file/AttachmentFileServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/file/AttachmentFileServiceTest.java index 9b5f7b8a6d0a655baa41a808218a1e7c6ffb90bf..a5936b2e23f732afcd1ad73e2b23722d04c09016 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/file/AttachmentFileServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/file/AttachmentFileServiceTest.java @@ -35,9 +35,11 @@ import java.net.URLConnection; import java.util.Base64; import java.util.Optional; import java.util.UUID; +import java.util.function.Consumer; import jakarta.activation.MimetypesFileTypeMap; +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; @@ -132,21 +134,16 @@ class AttachmentFileServiceTest { @Nested class TestGetFileContent { - private static final OzgCloudFileId OZG_CLOUD_FILE_ID = OzgCloudFileId.from(FILE_ID); - @Mock private PipedInputStream inputStream; @Mock private OutputStream outputStream; - @Captor - private ArgumentCaptor<Runnable> runnableCaptor; - @BeforeEach void init() { doReturn(inputStream).when(service).createInputStream(); doReturn(outputStream).when(service).connectToOutputStream(any()); - when(fileIdMapper.toFileId(any())).thenReturn(OZG_CLOUD_FILE_ID); + doNothing().when(service).writeFileContent(any(), any()); } @Test @@ -164,17 +161,10 @@ class AttachmentFileServiceTest { } @Test - void shouldCallToFileId() { - getFileContent(); - - verify(fileIdMapper).toFileId(FILE_ID); - } - - @Test - void shouldCallWriteFileDataToStream() { + void shoulcCallWriteFileContent() { getFileContent(); - verify(ozgCloudFileService).writeFileDataToStream(OZG_CLOUD_FILE_ID, outputStream); + verify(service).writeFileContent(FILE_ID, outputStream); } @Test @@ -185,10 +175,7 @@ class AttachmentFileServiceTest { } private InputStream getFileContent() { - var fileContent = service.getFileContent(FileId.from(FILE_ID)); - verify(taskExecutor).execute(runnableCaptor.capture()); - runnableCaptor.getValue().run(); - return fileContent; + return service.getFileContent(FileId.from(FILE_ID)); } } @@ -209,11 +196,58 @@ class AttachmentFileServiceTest { } } + @Nested + class TestWriteFileContent { + + private static final OzgCloudFileId OZG_CLOUD_FILE_ID = OzgCloudFileId.from(FILE_ID); + + @Mock + private OutputStream outputStream; + + @Captor + private ArgumentCaptor<Runnable> runnableCaptor; + + @BeforeEach + void init() { + when(fileIdMapper.toFileId(any())).thenReturn(OZG_CLOUD_FILE_ID); + } + + @Test + void shouldCallToFileId() { + writeFileContent(); + + verify(fileIdMapper).toFileId(FILE_ID); + } + + @Test + void shouldCallWriteFileDataToStream() { + writeFileContent(); + + verify(ozgCloudFileService).writeFileDataToStream(OZG_CLOUD_FILE_ID, outputStream); + } + + @Test + void shouldCallCloseOutputStream() { + try (var ioUtilsMock = mockStatic(IOUtils.class)) { + writeFileContent(); + + ioUtilsMock.verify(() -> IOUtils.closeQuietly(eq(outputStream), any(Consumer.class))); + } + } + + private void writeFileContent() { + service.writeFileContent(FILE_ID, outputStream); + verify(taskExecutor).execute(runnableCaptor.capture()); + runnableCaptor.getValue().run(); + } + + } + @Nested class TestUploadAttachmentFile { private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); - private static final String FILE_CONTENT_STR = new String(FILE_CONTENT); + private static final String FILE_CONTENT_STR = new String(FILE_CONTENT); @Mock private AttachmentFile enhancedAttachmentFile;