From 9ae28b796054f12e073b1306c86a32853d2bbbfd Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Thu, 5 Dec 2024 09:16:46 +0100
Subject: [PATCH] OZG-6810 create AttachmentFileService

---
 .../file/AttachmentFileRemoteService.java     |  19 +++
 .../file/AttachmentFileService.java           |  10 +-
 .../file/AttachmentFileServiceTest.java       | 117 ++++++++++++++++++
 3 files changed, 141 insertions(+), 5 deletions(-)

diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java
index 4595837..a22105c 100644
--- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java
+++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java
@@ -23,12 +23,31 @@
  */
 package de.ozgcloud.nachrichten.file;
 
+import java.io.InputStream;
+import java.util.Optional;
+
 import org.springframework.stereotype.Component;
 
 import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration;
+import de.ozgcloud.nachrichten.postfach.FileId;
+import lombok.RequiredArgsConstructor;
 
 @Component(NachrichtenManagerConfiguration.BINARY_FILE_REMOTE_SERVICE_NAME)
+@RequiredArgsConstructor
 class AttachmentFileRemoteService {
 
+	private final AttachmentFileMapper attachmentFileMapper;
+
+	public Optional<AttachmentFile> findAttachmentFile(FileId fileId) {
+		//		map(attachmentFileMapper::fromOzgFile)
+		return null;
+	}
+
+	public InputStream getFileContent(FileId fileId) {
+		return InputStream.nullInputStream();
+	}
 
+	public String createAttachmentFile(AttachmentFile attachmentFile, InputStream fileContent) {
+		return "";
+	}
 }
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 856cdde..a531e38 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
@@ -27,6 +27,7 @@ import java.io.InputStream;
 
 import org.springframework.stereotype.Service;
 
+import de.ozgcloud.common.errorhandling.TechnicalException;
 import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration;
 import de.ozgcloud.nachrichten.postfach.FileId;
 import lombok.RequiredArgsConstructor;
@@ -36,19 +37,18 @@ import lombok.RequiredArgsConstructor;
 public class AttachmentFileService {
 
 	private final AttachmentFileRemoteService attachmentFileRemoteService;
-	private final AttachmentFileMapper attachmentFileMapper;
 
 	public AttachmentFile getFile(FileId fileId) {
-		// new TechnicalException("Can not find attachment with id " + fileId));
-		return attachmentFileMapper.fromOzgFile(null);
+		return attachmentFileRemoteService.findAttachmentFile(fileId)
+				.orElseThrow(() -> new TechnicalException("Can not find attachment with id " + fileId));
 	}
 
 	public InputStream getFileContent(FileId fileId) {
-		return InputStream.nullInputStream();
+		return attachmentFileRemoteService.getFileContent(fileId);
 	}
 
 	public String createAttachmentFile(AttachmentFile binaryFile, InputStream fileContent) {
-		return "";
+		return attachmentFileRemoteService.createAttachmentFile(binaryFile, 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 1d1d53e..222971e 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
@@ -23,8 +23,125 @@
  */
 package de.ozgcloud.nachrichten.file;
 
+import static org.assertj.core.api.Assertions.*;
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import java.io.InputStream;
+import java.util.Optional;
+import java.util.UUID;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Spy;
+
+import de.ozgcloud.common.errorhandling.TechnicalException;
+import de.ozgcloud.nachrichten.postfach.FileId;
 
 class AttachmentFileServiceTest {
 
+	private static final FileId FILE_ID = FileId.createNew();
+
+	@Spy
+	@InjectMocks
+	private AttachmentFileService service;
+
+	@Mock
+	private AttachmentFileRemoteService attachmentFileRemoteService;
+
+	@Nested
+	class TestGetFile {
+
+		private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create();
+
+		@Test
+		void shouldCallFindAttachmentFile() {
+			when(attachmentFileRemoteService.findAttachmentFile(any())).thenReturn(Optional.of(ATTACHMENT_FILE));
+
+			getFile();
+
+			verify(attachmentFileRemoteService).findAttachmentFile(FILE_ID);
+		}
+
+		@Test
+		void shouldReturnAttachmentFile() {
+			when(attachmentFileRemoteService.findAttachmentFile(any())).thenReturn(Optional.of(ATTACHMENT_FILE));
+
+			var result = getFile();
+
+			assertThat(result).isEqualTo(ATTACHMENT_FILE);
+		}
+
+		@Test
+		void shouldThrowExceptionIfFileNotFound() {
+			assertThrows(TechnicalException.class, this::getFile);
+		}
+
+		private AttachmentFile getFile() {
+			return service.getFile(FILE_ID);
+		}
+	}
+
+	@Nested
+	class TestGetFileContent {
+
+		@Mock
+		private InputStream contentStream;
+
+		@BeforeEach
+		void init() {
+			when(attachmentFileRemoteService.getFileContent(any())).thenReturn(contentStream);
+		}
+
+		@Test
+		void shouldCallGetFileContent() {
+			getFileContent();
+
+			verify(attachmentFileRemoteService).getFileContent(FILE_ID);
+		}
+
+		@Test
+		void shouldReturnContentStream() {
+			var result = getFileContent();
+
+			assertThat(result).isEqualTo(contentStream);
+		}
+
+		private InputStream getFileContent() {
+			return service.getFileContent(FILE_ID);
+		}
+	}
+
+	@Nested
+	class TestCreateAttachmentFile {
+
+		private static final String FILE_ID = UUID.randomUUID().toString();
+		private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create();
+
+		@Mock
+		private InputStream contentStream;
+
+		@Test
+		void shouldCallCreateAttachmentFile() {
+			createAttachmentFile();
+
+			verify(attachmentFileRemoteService).createAttachmentFile(ATTACHMENT_FILE, contentStream);
+		}
+
+		@Test
+		void shouldReturnAttachmentFileId() {
+			when(attachmentFileRemoteService.createAttachmentFile(any(), any())).thenReturn(FILE_ID);
+
+			var result = createAttachmentFile();
+
+			assertThat(result).isEqualTo(FILE_ID);
+		}
+
+		private String createAttachmentFile() {
+			return attachmentFileRemoteService.createAttachmentFile(ATTACHMENT_FILE, contentStream);
+		}
+	}
 }
\ No newline at end of file
-- 
GitLab