From b09b8fa05a542473d1b3112ffa9a30e51e9ec81f Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 10 Dec 2024 06:40:35 +0100
Subject: [PATCH] OZG-6810 get file content

---
 .../file/AttachmentFileService.java           | 25 ++++++++-
 .../file/AttachmentFileServiceTest.java       | 51 +++++++++++++++++--
 2 files changed, 70 insertions(+), 6 deletions(-)

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 1c88cb5..2e57636 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
@@ -26,6 +26,9 @@ package de.ozgcloud.nachrichten.file;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+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;
@@ -35,10 +38,12 @@ import jakarta.activation.MimetypesFileTypeMap;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.core.task.TaskExecutor;
 import org.springframework.stereotype.Service;
 
 import de.ozgcloud.apilib.file.OzgCloudFileService;
 import de.ozgcloud.apilib.vorgang.OzgCloudFileIdMapper;
+import de.ozgcloud.common.errorhandling.TechnicalException;
 import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration;
 import de.ozgcloud.nachrichten.postfach.FileId;
 import lombok.RequiredArgsConstructor;
@@ -49,12 +54,15 @@ import lombok.extern.log4j.Log4j2;
 @Log4j2
 public class AttachmentFileService {
 
+	private static final int BUFFER_SIZE = 4 * 1024;
 	static final String ATTACHMENT_NAME = "PostfachAttachment";
 
 	private final OzgCloudFileService fileService;
 	private final OzgCloudFileIdMapper fileIdMapper;
 	private final AttachmentFileMapper attachmentFileMapper;
 
+	private final TaskExecutor taskExecutor;
+
 	private final MimetypesFileTypeMap mimetypesFileTypeMap;
 
 	public AttachmentFile getFile(FileId fileId) {
@@ -63,7 +71,22 @@ public class AttachmentFileService {
 	}
 
 	public InputStream getFileContent(FileId fileId) {
-		return null;
+		var inputStream = createInputStream();
+		var outputStream = connectToOutputStream(inputStream);
+		taskExecutor.execute(() -> fileService.writeFileDataToStream(fileIdMapper.toFileId(fileId.toString()), outputStream));
+		return inputStream;
+	}
+
+	PipedInputStream createInputStream() {
+		return new PipedInputStream(BUFFER_SIZE);
+	}
+
+	OutputStream connectToOutputStream(PipedInputStream inputStream) {
+		try {
+			return new PipedOutputStream(inputStream);
+		} catch (IOException e) {
+			throw new TechnicalException("Cannot read file content", e);
+		}
 	}
 
 	public String createAttachmentFile(AttachmentFile attachmentFile, String fileContent) {
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 7902e26..86484bf 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
@@ -28,6 +28,8 @@ import static org.mockito.Mockito.*;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PipedInputStream;
 import java.net.FileNameMap;
 import java.net.URLConnection;
 import java.util.Base64;
@@ -40,10 +42,13 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockedStatic;
 import org.mockito.Spy;
+import org.springframework.core.task.TaskExecutor;
 
 import com.thedeanda.lorem.LoremIpsum;
 
@@ -70,6 +75,8 @@ class AttachmentFileServiceTest {
 	@Mock
 	private AttachmentFileMapper attachmentFileMapper;
 	@Mock
+	private TaskExecutor taskExecutor;
+	@Mock
 	private MimetypesFileTypeMap mimetypesFileTypeMap;
 
 	@Nested
@@ -123,29 +130,63 @@ class AttachmentFileServiceTest {
 	@Nested
 	class TestGetFileContent {
 
+		private static final OzgCloudFileId OZG_CLOUD_FILE_ID = OzgCloudFileId.from(FILE_ID);
+
 		@Mock
-		private InputStream contentStream;
+		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);
+		}
+
+		@Test
+		void shouldCallCreateInputStream() {
+			getFileContent();
+
+			verify(service).createInputStream();
+		}
+
+		@Test
+		void shouldCallConnectToOutputStream() {
+			getFileContent();
+
+			verify(service).connectToOutputStream(inputStream);
+		}
+
+		@Test
+		void shouldCallToFileId() {
+			getFileContent();
+
+			verify(fileIdMapper).toFileId(FILE_ID);
 		}
 
 		@Test
-		void shouldCallGetFileContent() {
+		void shouldCallWriteFileDataToStream() {
 			getFileContent();
 
-			//			verify(ozgCloudFileService).getFileContent(FILE_ID);
+			verify(ozgCloudFileService).writeFileDataToStream(OZG_CLOUD_FILE_ID, outputStream);
 		}
 
 		@Test
 		void shouldReturnContentStream() {
 			var result = getFileContent();
 
-//			assertThat(result).isEqualTo(contentStream);
+			assertThat(result).isEqualTo(inputStream);
 		}
 
 		private InputStream getFileContent() {
-			return service.getFileContent(FileId.from(FILE_ID));
+			var fileContent = service.getFileContent(FileId.from(FILE_ID));
+			verify(taskExecutor).execute(runnableCaptor.capture());
+			runnableCaptor.getValue().run();
+			return fileContent;
 		}
 	}
 
-- 
GitLab