From f354c68a70faa8feee6085ae4b3a9d2d5ef0ae86 Mon Sep 17 00:00:00 2001
From: Jan Zickermann <jan.zickermann@dataport.de>
Date: Fri, 21 Feb 2025 08:54:07 +0100
Subject: [PATCH] OZG-4097 Test persist attachment service

---
 .../Osi2AttachmentFileMapperTest.java         |   4 +-
 .../Osi2PersistAttachmentServiceTest.java     | 213 ++++++++++++++++++
 .../factory/AttachmentFileTestFactory.java    |   5 +-
 .../factory/AttachmentInfoTestFactory.java    |  20 ++
 .../osiv2/factory/GrpcOzgFileTestFactory.java |   2 +-
 .../osiv2/factory/Osi2MessageTestFactory.java |   4 +
 6 files changed, 242 insertions(+), 6 deletions(-)
 create mode 100644 src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2PersistAttachmentServiceTest.java
 create mode 100644 src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentInfoTestFactory.java

diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2AttachmentFileMapperTest.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2AttachmentFileMapperTest.java
index 8370879..a608d35 100644
--- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2AttachmentFileMapperTest.java
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2AttachmentFileMapperTest.java
@@ -1,7 +1,7 @@
 package de.ozgcloud.nachrichten.postfach.osiv2.attachment;
 
-import static de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentFileTestFactory.*;
 import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
 import static org.assertj.core.api.Assertions.*;
 
 import org.junit.jupiter.api.DisplayName;
@@ -97,7 +97,7 @@ class Osi2AttachmentFileMapperTest {
 		void shouldMapVorgangId() {
 			var result = doMapping();
 
-			assertThat(result.getVorgangId()).isEqualTo(UPLOAD_VORGANG_ID);
+			assertThat(result.getVorgangId()).isEqualTo(VORGANG_ID);
 		}
 
 		private OzgCloudUploadFile doMapping() {
diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2PersistAttachmentServiceTest.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2PersistAttachmentServiceTest.java
new file mode 100644
index 0000000..2fcf400
--- /dev/null
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/attachment/Osi2PersistAttachmentServiceTest.java
@@ -0,0 +1,213 @@
+package de.ozgcloud.nachrichten.postfach.osiv2.attachment;
+
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.GrpcOzgFileTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.V1ReplyMessageTestFactory.*;
+import static org.assertj.core.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+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 org.springframework.core.io.Resource;
+
+import de.ozgcloud.nachrichten.postfach.osiv2.exception.Osi2RuntimeException;
+import de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentFileTestFactory;
+import de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentInfoTestFactory;
+import de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2MessageTestFactory;
+import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentFile;
+import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentInfo;
+import de.ozgcloud.nachrichten.postfach.osiv2.model.Osi2Message;
+import de.ozgcloud.nachrichten.postfach.osiv2.transfer.PostfachApiFacadeService;
+import lombok.SneakyThrows;
+
+class Osi2PersistAttachmentServiceTest {
+
+	@Spy
+	@InjectMocks
+	private Osi2PersistAttachmentService service;
+
+	@Mock
+	private PostfachApiFacadeService postfachApiFacadeService;
+
+	@Mock
+	private Osi2AttachmentFileService attachmentFileService;
+
+	@DisplayName("persist attachments")
+	@Nested
+	class TestPersistAttachments {
+
+		@DisplayName("with no attachments")
+		@Nested
+		class TestWithNoAttachments {
+			private final Osi2Message osi2Message = Osi2MessageTestFactory.create();
+
+			@DisplayName("should return empty list")
+			@Test
+			void shouldReturnEmptyList() {
+				var result = service.persistAttachments(osi2Message);
+
+				assertThat(result).isEmpty();
+			}
+
+		}
+
+		@DisplayName("with two attachments")
+		@Nested
+		class TestWithTwoAttachments {
+
+			private final AttachmentInfo attachment1 = AttachmentInfoTestFactory.create();
+			private final AttachmentInfo attachment2 = AttachmentInfoTestFactory.createBuilder()
+					.guid(UPLOAD_GUID2)
+					.build();
+			private final Osi2Message osi2Message = Osi2MessageTestFactory.createBuilder()
+					.attachments(List.of(attachment1, attachment2))
+					.build();
+
+			@BeforeEach
+			void mock() {
+				doReturn(FILE_ID).when(service).persistAttachment(any(), eq(attachment1));
+				doReturn(FILE_ID_2).when(service).persistAttachment(any(), eq(attachment2));
+			}
+
+			@DisplayName("should call persistAttachment twice")
+			@Test
+			void shouldCallPersistAttachmentTwice() {
+				service.persistAttachments(osi2Message);
+
+				verify(service).persistAttachment(osi2Message, attachment1);
+				verify(service).persistAttachment(osi2Message, attachment2);
+			}
+
+			@DisplayName("should return")
+			@Test
+			void shouldReturn() {
+				var result = service.persistAttachments(osi2Message);
+
+				assertThat(result).containsExactly(FILE_ID, FILE_ID_2);
+			}
+		}
+	}
+
+	@DisplayName("persist attachment")
+	@Nested
+	class TestPersistAttachment {
+		private final Osi2Message osi2Message = Osi2MessageTestFactory.create();
+		private final AttachmentInfo attachment = AttachmentInfoTestFactory.create();
+		private final AttachmentFile attachmentFile = AttachmentFileTestFactory.create();
+
+		@Mock
+		private Resource content;
+
+		@BeforeEach
+		void mock() {
+			when(postfachApiFacadeService.downloadAttachment(any(), any())).thenReturn(content);
+			doReturn(attachmentFile).when(service).buildAttachmentFile(any(), any());
+			doReturn(FILE_ID).when(service).persistAttachmentFile(any(), any());
+		}
+
+		@DisplayName("should call persistAttachmentFile")
+		@Test
+		void shouldCallPersistAttachmentFile() {
+			service.persistAttachment(osi2Message, attachment);
+
+			verify(service).persistAttachmentFile(attachmentFile, content);
+		}
+
+		@DisplayName("should call buildAttachmentFile")
+		@Test
+		void shouldCallBuildAttachmentFile() {
+			service.persistAttachment(osi2Message, attachment);
+
+			verify(service).buildAttachmentFile(VORGANG_ID, attachment);
+		}
+
+		@DisplayName("should call downloadAttachment")
+		@Test
+		void shouldCallDownloadAttachment() {
+			service.persistAttachment(osi2Message, attachment);
+
+			verify(postfachApiFacadeService).downloadAttachment(MESSAGE_ID, UPLOAD_GUID);
+		}
+	}
+
+	@DisplayName("build attachment file")
+	@Nested
+	class TestBuildAttachmentFile {
+		private final AttachmentInfo attachment = AttachmentInfoTestFactory.create();
+
+		@DisplayName("should return")
+		@Test
+		void shouldReturn() {
+			var result = service.buildAttachmentFile(VORGANG_ID, attachment);
+
+			assertThat(result).usingRecursiveComparison()
+					.isEqualTo(AttachmentFileTestFactory.createBuilder()
+							.vorgangId(VORGANG_ID)
+							.build());
+		}
+	}
+
+	@DisplayName("persist attachment file")
+	@Nested
+	class TestPersistAttachmentFile {
+		private final AttachmentFile attachmentFile = AttachmentFileTestFactory.create();
+		@Mock
+		private Resource content;
+
+		@Mock
+		private InputStream inputStream;
+
+		@BeforeEach
+		@SneakyThrows
+		void mock() {
+			when(attachmentFileService.uploadFileAndReturnId(any(), any())).thenReturn(FILE_ID);
+			when(content.getInputStream()).thenReturn(inputStream);
+		}
+
+		@DisplayName("should close inputStream")
+		@Test
+		@SneakyThrows
+		void shouldCloseInputStream() {
+			service.persistAttachmentFile(attachmentFile, content);
+
+			verify(inputStream).close();
+		}
+
+		@DisplayName("should call uploadFileAndReturnId")
+		@Test
+		void shouldCallUploadFileAndReturnId() {
+			service.persistAttachmentFile(attachmentFile, content);
+
+			verify(attachmentFileService).uploadFileAndReturnId(attachmentFile, inputStream);
+		}
+
+		@DisplayName("should rethrow IOException")
+		@Test
+		@SneakyThrows
+		void shouldRethrowIoException() {
+			doThrow(new IOException("test")).when(inputStream).close();
+
+			assertThatThrownBy(() -> service.persistAttachmentFile(attachmentFile, content))
+					.isInstanceOf(Osi2RuntimeException.class);
+		}
+
+		@DisplayName("should return")
+		@Test
+		void shouldReturn() {
+			var result = service.persistAttachmentFile(attachmentFile, content);
+
+			assertThat(result).isEqualTo(FILE_ID);
+		}
+	}
+
+}
\ No newline at end of file
diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentFileTestFactory.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentFileTestFactory.java
index 1d80639..5d8056e 100644
--- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentFileTestFactory.java
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentFileTestFactory.java
@@ -1,13 +1,12 @@
 package de.ozgcloud.nachrichten.postfach.osiv2.factory;
 
 import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
 
 import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentFile;
 
 public class AttachmentFileTestFactory {
 
-	public static final String UPLOAD_VORGANG_ID = "vorgangId1";
-
 	public static AttachmentFile create() {
 		return createBuilder().build();
 	}
@@ -16,6 +15,6 @@ public class AttachmentFileTestFactory {
 		return AttachmentFile.builder()
 				.name(UPLOAD_NAME)
 				.contentType(UPLOAD_CONTENT_TYPE)
-				.vorgangId(UPLOAD_VORGANG_ID);
+				.vorgangId(VORGANG_ID);
 	}
 }
diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentInfoTestFactory.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentInfoTestFactory.java
new file mode 100644
index 0000000..8758edf
--- /dev/null
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/AttachmentInfoTestFactory.java
@@ -0,0 +1,20 @@
+package de.ozgcloud.nachrichten.postfach.osiv2.factory;
+
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
+
+import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentInfo;
+
+public class AttachmentInfoTestFactory {
+
+	public static AttachmentInfo create() {
+		return createBuilder().build();
+	}
+
+	public static AttachmentInfo.AttachmentInfoBuilder createBuilder() {
+		return AttachmentInfo.builder()
+				.name(UPLOAD_NAME)
+				.contentType(UPLOAD_CONTENT_TYPE)
+				.guid(UPLOAD_GUID)
+				.size(UPLOAD_SIZE);
+	}
+}
diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/GrpcOzgFileTestFactory.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/GrpcOzgFileTestFactory.java
index d26f681..c72aa9d 100644
--- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/GrpcOzgFileTestFactory.java
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/GrpcOzgFileTestFactory.java
@@ -5,11 +5,11 @@ import de.ozgcloud.vorgang.grpc.file.GrpcOzgFile;
 public class GrpcOzgFileTestFactory {
 
 	public static final String FILE_ID = "ozgfileId1";
+	public static final String FILE_ID_2 = "ozgfileId2";
 	public static final String FILE_NAME = "ozgfileName1";
 	public static final String FILE_CONTENT_TYPE = "ozgfileContentType1";
 	public static final Long FILE_SIZE = 124L;
 
-
 	public static GrpcOzgFile create() {
 		return createBuilder().build();
 	}
diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/Osi2MessageTestFactory.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/Osi2MessageTestFactory.java
index 0192762..8d40f11 100644
--- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/Osi2MessageTestFactory.java
+++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/factory/Osi2MessageTestFactory.java
@@ -1,8 +1,10 @@
 package de.ozgcloud.nachrichten.postfach.osiv2.factory;
 
 import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
+import static de.ozgcloud.nachrichten.postfach.osiv2.factory.V1ReplyMessageTestFactory.*;
 
 import java.time.ZonedDateTime;
+import java.util.List;
 
 import de.ozgcloud.nachrichten.postfach.PostfachNachricht;
 import de.ozgcloud.nachrichten.postfach.osiv2.model.Osi2Message;
@@ -15,11 +17,13 @@ public class Osi2MessageTestFactory {
 
 	public static Osi2Message.Osi2MessageBuilder createBuilder() {
 		return Osi2Message.builder()
+				.messageId(MESSAGE_ID)
 				.mailBody(MAIL_BODY)
 				.subject(MAIL_SUBJECT)
 				.replyOption(PostfachNachricht.ReplyOption.FORBIDDEN)
 				.createdAt(ZonedDateTime.now())
 				.vorgangId(VORGANG_ID)
+				.attachments(List.of())
 				.postfachAddress(PostfachAddressTestFactory.create());
 	}
 }
-- 
GitLab