From e21d93db5812a25ced0987437d5cde16a682e740 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Mon, 21 Nov 2022 03:38:42 +0100
Subject: [PATCH] OZG-2966 OZG-3171 handle null createdBy(from system) as
 fallbackname

---
 .../goofy/postfach/PostfachMailService.java   |  8 ++-
 .../postfach/PostfachMailServiceTest.java     | 62 ++++++++++++++++++-
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
index e871af9489..f4af9f46ec 100644
--- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
+++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailService.java
@@ -38,6 +38,8 @@ import de.itvsh.goofy.common.binaryfile.BinaryFileService;
 import de.itvsh.goofy.common.binaryfile.FileId;
 import de.itvsh.goofy.common.errorhandling.ResourceNotFoundException;
 import de.itvsh.goofy.common.file.OzgFile;
+import de.itvsh.goofy.common.user.UserId;
+import de.itvsh.goofy.common.user.UserProfile;
 import de.itvsh.goofy.common.user.UserService;
 import de.itvsh.goofy.vorgang.VorgangWithEingang;
 import lombok.extern.log4j.Log4j2;
@@ -115,10 +117,14 @@ class PostfachMailService {
 			Map<FileId, String> ozgFileIdOzgFileMap) {
 		return PostfachNachrichtPdfData.builder()
 				.createdAt(postfachNachricht.getCreatedAt())
-				.user(userService.getById(postfachNachricht.getCreatedBy()))
+				.user(getUser(postfachNachricht.getCreatedBy()))
 				.mailBody(postfachNachricht.getMailBody())
 				.subject(postfachNachricht.getSubject())
 				.attachmentNames(postfachNachricht.getAttachments().stream().map(ozgFileIdOzgFileMap::get).toList())
 				.build();
 	}
+
+	private UserProfile getUser(UserId createdBy) {
+		return Objects.nonNull(createdBy) ? userService.getById(createdBy) : null;
+	}
 }
\ No newline at end of file
diff --git a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
index 8199cc2d15..70e2f333a6 100644
--- a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
+++ b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailServiceTest.java
@@ -285,10 +285,68 @@ class PostfachMailServiceTest {
 
 					assertThat(postfachNachricht.getAttachmentNames()).isEqualTo(List.of(OzgFileTestFactory.NAME));
 				}
+			}
+
+			@DisplayName("user")
+			@Nested
+			class TestUser {
+
+				@DisplayName("exists")
+				@Nested
+				class TestOnNonNull {
+
+					private final UserProfile user = UserProfileTestFactory.create();
 
-				private PostfachNachrichtPdfData buildPostfachNachrichtPdfData() {
-					return service.buildPostfachNachrichtPdfData(postfachNachricht, Map.of(BinaryFileTestFactory.FILE_ID, OzgFileTestFactory.NAME));
+					@BeforeEach
+					void mock() {
+						when(userService.getById(any(UserId.class))).thenReturn(user);
+					}
+
+					@Test
+					void shouldCallUserService() {
+						buildPostfachNachrichtPdfData();
+
+						verify(userService).getById(UserProfileTestFactory.ID);
+					}
+
+					@Test
+					void shouldHaveSetCreatedByName() {
+						var postfachNachricht = buildPostfachNachrichtPdfData();
+
+						assertThat(postfachNachricht.getUser()).isEqualTo(user);
+					}
 				}
+
+				@DisplayName("not exists")
+				@Nested
+				class TestOnNull {
+
+					private final PostfachMail postfachNachrichtWithoutCreatedBy = PostfachMailTestFactory.createBuilder().createdBy(null)
+							.build();
+
+					@Test
+					void shouldNotCallUserService() {
+						buildPostfachNachrichtPdfDataWithoutUser();
+
+						verify(userService, never()).getById(any());
+					}
+
+					@Test
+					void shouldHaveSetAsEmptyStringOnNull() {
+						var postfachNachricht = buildPostfachNachrichtPdfDataWithoutUser();
+
+						assertThat(postfachNachricht.getUser()).isNull();
+					}
+
+					private PostfachNachrichtPdfData buildPostfachNachrichtPdfDataWithoutUser() {
+						return service.buildPostfachNachrichtPdfData(postfachNachrichtWithoutCreatedBy,
+								Map.of(BinaryFileTestFactory.FILE_ID, OzgFileTestFactory.NAME));
+					}
+				}
+			}
+
+			private PostfachNachrichtPdfData buildPostfachNachrichtPdfData() {
+				return service.buildPostfachNachrichtPdfData(postfachNachricht, Map.of(BinaryFileTestFactory.FILE_ID, OzgFileTestFactory.NAME));
 			}
 		}
 
-- 
GitLab