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 4595837fa1264a4c498d5d3749b8830066f6dff3..a22105cca733768fd2019dac3e1019b623272d91 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 856cdde14278a862045031f86e9c3dab2ef46093..a531e3825d3b5e5d51d1368cc9cf370c303f7104 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 1d1d53e6b67a1d858e6f6fcd37cf10f124b84815..222971e46a6924eb6fbda2dd27efa2e2b795ef59 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