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 e871af9489874014f4ae7030e6c278e7c4d1a21b..f4af9f46eca2d195d2b694d1dfa2af0cea119cbd 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 8199cc2d152770910874d58c0f1a8f42ad3e4189..70e2f333a6e2658b93776876f73fd40f94f2b56f 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)); } }