From 966e675a04d83d68d21e5ad351825c63664c28a7 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Mon, 9 Dec 2024 19:44:30 +0100 Subject: [PATCH] OZG-6810 refactor attachment upload --- nachrichten-manager-server/pom.xml | 2 +- .../NachrichtenManagerConfiguration.java | 8 + .../nachrichten/file/AttachmentFile.java | 2 +- .../file/AttachmentFileRemoteService.java | 53 --- .../file/AttachmentFileService.java | 71 ++- .../osi/MessageAttachmentService.java | 8 +- .../file/AttachmentFileServiceTest.java | 412 ++++++++++++++++-- .../osi/MessageAttachmentServiceTest.java | 12 +- 8 files changed, 463 insertions(+), 105 deletions(-) delete mode 100644 nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java diff --git a/nachrichten-manager-server/pom.xml b/nachrichten-manager-server/pom.xml index 2983ff7..b4ea2ea 100644 --- a/nachrichten-manager-server/pom.xml +++ b/nachrichten-manager-server/pom.xml @@ -48,7 +48,7 @@ <bayernid-proxy-interface.version>0.7.0</bayernid-proxy-interface.version> <vorgang-manager.version>2.17.0</vorgang-manager.version> <muk-postfach.version>0.1.0</muk-postfach.version> - <api-lib.version>0.13.0</api-lib.version> + <api-lib.version>0.15.0-SNAPSHOT</api-lib.version> <ozgcloud-common.version>4.5.0</ozgcloud-common.version> </properties> diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java index d564c5b..4a6d1a5 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/NachrichtenManagerConfiguration.java @@ -1,5 +1,7 @@ package de.ozgcloud.nachrichten; +import jakarta.activation.MimetypesFileTypeMap; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -49,4 +51,10 @@ public class NachrichtenManagerConfiguration { OzgCloudFileService grpcOzgCloudFileService(NachrichtenManagerCallContextProvider contextProvider, OzgCloudFileMapper mapper) { return new GrpcOzgCloudFileService(fileServiceBlockingStub, fileServiceAsyncServiceStub, contextProvider, mapper); } + + @Bean + MimetypesFileTypeMap mimetypesFileTypeMap() { + // uses map file: src/main/resources/mime.types + return new MimetypesFileTypeMap(); + } } \ No newline at end of file diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFile.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFile.java index 5ec6c99..dacbc05 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFile.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFile.java @@ -26,7 +26,7 @@ package de.ozgcloud.nachrichten.file; import lombok.Builder; import lombok.Getter; -@Builder +@Builder(toBuilder = true) @Getter public class AttachmentFile { 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 deleted file mode 100644 index a22105c..0000000 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/file/AttachmentFileRemoteService.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den - * Ministerpräsidenten des Landes Schleswig-Holstein - * Staatskanzlei - * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung - * - * Lizenziert unter der EUPL, Version 1.2 oder - sobald - * diese von der Europäischen Kommission genehmigt wurden - - * Folgeversionen der EUPL ("Lizenz"); - * Sie dürfen dieses Werk ausschließlich gemäß - * dieser Lizenz nutzen. - * Eine Kopie der Lizenz finden Sie hier: - * - * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * - * Sofern nicht durch anwendbare Rechtsvorschriften - * gefordert oder in schriftlicher Form vereinbart, wird - * die unter der Lizenz verbreitete Software "so wie sie - * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - - * ausdrücklich oder stillschweigend - verbreitet. - * Die sprachspezifischen Genehmigungen und Beschränkungen - * unter der Lizenz sind dem Lizenztext zu entnehmen. - */ -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 a531e38..1c88cb5 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 @@ -23,32 +23,89 @@ */ package de.ozgcloud.nachrichten.file; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; +import java.net.URLConnection; +import java.nio.charset.Charset; +import java.util.Base64; +import java.util.Optional; +import jakarta.activation.MimetypesFileTypeMap; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import de.ozgcloud.common.errorhandling.TechnicalException; +import de.ozgcloud.apilib.file.OzgCloudFileService; +import de.ozgcloud.apilib.vorgang.OzgCloudFileIdMapper; import de.ozgcloud.nachrichten.NachrichtenManagerConfiguration; import de.ozgcloud.nachrichten.postfach.FileId; import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; @Service(NachrichtenManagerConfiguration.BINARY_FILE_SERVICE_NAME) @RequiredArgsConstructor +@Log4j2 public class AttachmentFileService { - private final AttachmentFileRemoteService attachmentFileRemoteService; + static final String ATTACHMENT_NAME = "PostfachAttachment"; + + private final OzgCloudFileService fileService; + private final OzgCloudFileIdMapper fileIdMapper; + private final AttachmentFileMapper attachmentFileMapper; + + private final MimetypesFileTypeMap mimetypesFileTypeMap; public AttachmentFile getFile(FileId fileId) { - return attachmentFileRemoteService.findAttachmentFile(fileId) - .orElseThrow(() -> new TechnicalException("Can not find attachment with id " + fileId)); + var ozgCloudFile = fileService.getFile(fileIdMapper.toFileId(fileId.toString())); + return attachmentFileMapper.fromOzgCloudFile(ozgCloudFile); } public InputStream getFileContent(FileId fileId) { - return attachmentFileRemoteService.getFileContent(fileId); + return null; + } + + public String createAttachmentFile(AttachmentFile attachmentFile, String fileContent) { + var ozgCloudFileId = fileService.uploadFile(attachmentFileMapper.toOzgCloudUploadFile(enhanceAttachmentFile(attachmentFile, fileContent)), + toDecodedInputStream(fileContent)); + return ozgCloudFileId.toString(); + } + + AttachmentFile enhanceAttachmentFile(AttachmentFile attachmentFile, String fileContent) { + if (StringUtils.isBlank(attachmentFile.getContentType())) { + return attachmentFile.toBuilder().contentType(getContentType(attachmentFile, fileContent)).build(); + } + return attachmentFile; + } + + String getContentType(AttachmentFile attachmentFile, String fileContent) { + return getTypeByFileName(attachmentFile).or(() -> getTypeByContent(fileContent)).orElseGet(() -> getByMimeTypes(attachmentFile)); + } + + Optional<String> getTypeByFileName(AttachmentFile attachmentFile) { + var contentType = URLConnection.getFileNameMap().getContentTypeFor(attachmentFile.getName()); + return Optional.ofNullable(contentType); + } + + Optional<String> getTypeByContent(String fileContent) { + try (var contentStream = toInputStream(fileContent)) { + return Optional.ofNullable(URLConnection.guessContentTypeFromStream(contentStream)); + } catch (IOException e) { + LOG.warn("IO-Exception while guessing content type", e); + } + return Optional.empty(); + } + + String getByMimeTypes(AttachmentFile attachmentFile) { + return mimetypesFileTypeMap.getContentType(attachmentFile.getName()); } - public String createAttachmentFile(AttachmentFile binaryFile, InputStream fileContent) { - return attachmentFileRemoteService.createAttachmentFile(binaryFile, fileContent); + InputStream toDecodedInputStream(String content) { + return new ByteArrayInputStream(Base64.getDecoder().decode(content)); } + InputStream toInputStream(String content) { + return IOUtils.toInputStream(content, Charset.defaultCharset()); + } } diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentService.java index 16924f1..9d04ee4 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentService.java @@ -24,8 +24,6 @@ package de.ozgcloud.nachrichten.postfach.osi; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; import java.util.Base64; import org.apache.commons.io.IOUtils; @@ -71,7 +69,7 @@ public class MessageAttachmentService { } public String persistAttachment(String vorgangId, MessageAttachment attachment) { - return attachmentFileService.createAttachmentFile(buildAttachmentFile(vorgangId, attachment), getContent(attachment)); + return attachmentFileService.createAttachmentFile(buildAttachmentFile(vorgangId, attachment), attachment.getContent()); } AttachmentFile buildAttachmentFile(String vorgangId, MessageAttachment attachment) { @@ -81,8 +79,4 @@ public class MessageAttachmentService { .build(); } - InputStream getContent(MessageAttachment attachment) { - return IOUtils.toInputStream(attachment.getContent(), Charset.defaultCharset()); - } - } 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 222971e..7902e26 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 @@ -24,64 +24,99 @@ 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.IOException; import java.io.InputStream; +import java.net.FileNameMap; +import java.net.URLConnection; +import java.util.Base64; import java.util.Optional; import java.util.UUID; +import jakarta.activation.MimetypesFileTypeMap; + +import org.junit.jupiter.api.AfterEach; 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.MockedStatic; import org.mockito.Spy; -import de.ozgcloud.common.errorhandling.TechnicalException; +import com.thedeanda.lorem.LoremIpsum; + +import de.ozgcloud.apilib.file.OzgCloudFile; +import de.ozgcloud.apilib.file.OzgCloudFileId; +import de.ozgcloud.apilib.file.OzgCloudFileService; +import de.ozgcloud.apilib.file.OzgCloudUploadFile; +import de.ozgcloud.apilib.vorgang.OzgCloudFileIdMapper; import de.ozgcloud.nachrichten.postfach.FileId; class AttachmentFileServiceTest { - private static final FileId FILE_ID = FileId.createNew(); + private static final String FILE_ID = UUID.randomUUID().toString(); + private static final String FILE_CONTENT = LoremIpsum.getInstance().getWords(1); @Spy @InjectMocks private AttachmentFileService service; @Mock - private AttachmentFileRemoteService attachmentFileRemoteService; + private OzgCloudFileService ozgCloudFileService; + @Mock + private OzgCloudFileIdMapper fileIdMapper; + @Mock + private AttachmentFileMapper attachmentFileMapper; + @Mock + private MimetypesFileTypeMap mimetypesFileTypeMap; @Nested class TestGetFile { - private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); + @Mock + private OzgCloudFile ozgCloudFile; + @Mock + private AttachmentFile attachmentFile; - @Test - void shouldCallFindAttachmentFile() { - when(attachmentFileRemoteService.findAttachmentFile(any())).thenReturn(Optional.of(ATTACHMENT_FILE)); + @BeforeEach + void init() { + when(fileIdMapper.toFileId(any())).thenReturn(OzgCloudFileId.from(FILE_ID)); + when(attachmentFileMapper.fromOzgCloudFile(any())).thenReturn(attachmentFile); + when(ozgCloudFileService.getFile(any())).thenReturn(ozgCloudFile); + } + @Test + void shouldCallToFileId() { getFile(); - verify(attachmentFileRemoteService).findAttachmentFile(FILE_ID); + verify(fileIdMapper).toFileId(FILE_ID); } @Test - void shouldReturnAttachmentFile() { - when(attachmentFileRemoteService.findAttachmentFile(any())).thenReturn(Optional.of(ATTACHMENT_FILE)); + void shouldCallGetFile() { + getFile(); - var result = getFile(); + verify(ozgCloudFileService).getFile(OzgCloudFileId.from(FILE_ID)); + } + + @Test + void shouldCallFromOzgCloudFile() { + getFile(); - assertThat(result).isEqualTo(ATTACHMENT_FILE); + verify(attachmentFileMapper).fromOzgCloudFile(ozgCloudFile); } @Test - void shouldThrowExceptionIfFileNotFound() { - assertThrows(TechnicalException.class, this::getFile); + void shouldReturnAttachmentFile() { + var result = getFile(); + + assertThat(result).isEqualTo(attachmentFile); } private AttachmentFile getFile() { - return service.getFile(FILE_ID); + return service.getFile(FileId.from(FILE_ID)); } } @@ -93,55 +128,380 @@ class AttachmentFileServiceTest { @BeforeEach void init() { - when(attachmentFileRemoteService.getFileContent(any())).thenReturn(contentStream); } @Test void shouldCallGetFileContent() { getFileContent(); - verify(attachmentFileRemoteService).getFileContent(FILE_ID); + // verify(ozgCloudFileService).getFileContent(FILE_ID); } @Test void shouldReturnContentStream() { var result = getFileContent(); - assertThat(result).isEqualTo(contentStream); +// assertThat(result).isEqualTo(contentStream); } private InputStream getFileContent() { - return service.getFileContent(FILE_ID); + return service.getFileContent(FileId.from(FILE_ID)); } } @Nested class TestCreateAttachmentFile { - private static final String FILE_ID = UUID.randomUUID().toString(); private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); + @Mock + private AttachmentFile enhancedAttachmentFile; + @Mock + private OzgCloudUploadFile ozgCloudUploadFile; @Mock private InputStream contentStream; + @BeforeEach + void init() { + when(ozgCloudFileService.uploadFile(any(), any())).thenReturn(OzgCloudFileId.from(FILE_ID)); + when(attachmentFileMapper.toOzgCloudUploadFile(any())).thenReturn(ozgCloudUploadFile); + doReturn(enhancedAttachmentFile).when(service).enhanceAttachmentFile(any(), any()); + doReturn(contentStream).when(service).toDecodedInputStream(any()); + } + + @Test + void shouldCallEnhanceAttachmentFile() { + createAttachmentFile(); + + verify(service).enhanceAttachmentFile(ATTACHMENT_FILE, FILE_CONTENT); + } + + @Test + void shouldCallToOzgCloudUploadFile() { + createAttachmentFile(); + + verify(attachmentFileMapper).toOzgCloudUploadFile(enhancedAttachmentFile); + } + @Test - void shouldCallCreateAttachmentFile() { + void shouldCallToDecodedInputStream() { createAttachmentFile(); - verify(attachmentFileRemoteService).createAttachmentFile(ATTACHMENT_FILE, contentStream); + verify(service).toDecodedInputStream(FILE_CONTENT); } @Test - void shouldReturnAttachmentFileId() { - when(attachmentFileRemoteService.createAttachmentFile(any(), any())).thenReturn(FILE_ID); + void shouldCallUploadFile() { + createAttachmentFile(); + + verify(ozgCloudFileService).uploadFile(ozgCloudUploadFile, contentStream); + } + @Test + void shouldReturnFileId() { var result = createAttachmentFile(); assertThat(result).isEqualTo(FILE_ID); } private String createAttachmentFile() { - return attachmentFileRemoteService.createAttachmentFile(ATTACHMENT_FILE, contentStream); + return service.createAttachmentFile(ATTACHMENT_FILE, FILE_CONTENT); + } + } + + @Nested + class TestEnhanceAttachmentFile { + + private AttachmentFile attachmentFile; + + @Nested + class TestNoContentType { + + @BeforeEach + void init() { + attachmentFile = AttachmentFileTestFactory.createBuilder().contentType(null).build(); + doReturn(AttachmentFileTestFactory.CONTENT_TYPE).when(service).getContentType(any(), any()); + } + + @Test + void shouldCallGetContentType() { + enhanceAttachmentFile(); + + verify(service).getContentType(attachmentFile, FILE_CONTENT); + } + + @Test + void shouldReturnAttachmentFile() { + var result = enhanceAttachmentFile(); + + assertThat(result).usingRecursiveComparison().isEqualTo(AttachmentFileTestFactory.create()); + } + } + + @Test + void shouldReturnAttachmentFile() { + attachmentFile = AttachmentFileTestFactory.create(); + + var result = enhanceAttachmentFile(); + + assertThat(result).isSameAs(attachmentFile); + } + + private AttachmentFile enhanceAttachmentFile() { + return service.enhanceAttachmentFile(attachmentFile, FILE_CONTENT); + } + } + + @Nested + class TestGetContentType { + + private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); + + @Nested + class TestGetByFileName { + + @BeforeEach + void init() { + doReturn(Optional.of(AttachmentFileTestFactory.CONTENT_TYPE)).when(service).getTypeByFileName(any()); + } + + @Test + void shouldCallGetTypeByFileName() { + getContentType(); + + verify(service).getTypeByFileName(ATTACHMENT_FILE); + } + + @Test + void shouldReturnContentType() { + var result = getContentType(); + + assertThat(result).isEqualTo(AttachmentFileTestFactory.CONTENT_TYPE); + } + } + + @Nested + class TestTypeByContent { + + @BeforeEach + void init() { + doReturn(Optional.empty()).when(service).getTypeByFileName(any()); + doReturn(Optional.of(AttachmentFileTestFactory.CONTENT_TYPE)).when(service).getTypeByContent(any()); + } + + @Test + void shouldCallGetTypeByContent() { + getContentType(); + + verify(service).getTypeByContent(FILE_CONTENT); + } + + @Test + void shouldReturnContentType() { + var result = getContentType(); + + assertThat(result).isEqualTo(AttachmentFileTestFactory.CONTENT_TYPE); + } + } + + @Nested + class TestByMimeTypes { + + @BeforeEach + void init() { + doReturn(Optional.empty()).when(service).getTypeByFileName(any()); + doReturn(Optional.empty()).when(service).getTypeByContent(any()); + doReturn(AttachmentFileTestFactory.CONTENT_TYPE).when(service).getByMimeTypes(any()); + } + + @Test + void shouldCallGetByMimeTypes() { + getContentType(); + + verify(service).getByMimeTypes(ATTACHMENT_FILE); + } + + @Test + void shouldReturnContentType() { + var result = getContentType(); + + assertThat(result).isEqualTo(AttachmentFileTestFactory.CONTENT_TYPE); + } + } + + private String getContentType() { + return service.getContentType(ATTACHMENT_FILE, FILE_CONTENT); + } + } + + @Nested + class TestGetTypeByFileName { + + private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); + + @Mock + private FileNameMap fileNameMap; + private MockedStatic<URLConnection> urlConnectionMock; + + @BeforeEach + void init() { + urlConnectionMock = mockStatic(URLConnection.class); + urlConnectionMock.when(URLConnection::getFileNameMap).thenReturn(fileNameMap); + } + + @AfterEach + void close() { + urlConnectionMock.close(); + } + + @Test + void shouldCallGetContentTypeFor() { + getTypeByFileName(); + + verify(fileNameMap).getContentTypeFor(AttachmentFileTestFactory.NAME); + } + + @Test + void shouldReturnContentType() { + when(fileNameMap.getContentTypeFor(any())).thenReturn(AttachmentFileTestFactory.CONTENT_TYPE); + + var result = getTypeByFileName(); + + assertThat(result).contains(AttachmentFileTestFactory.CONTENT_TYPE); + } + + @Test + void shouldReturnEmpty() { + var result = getTypeByFileName(); + + assertThat(result).isEmpty(); + } + + private Optional<String> getTypeByFileName() { + return service.getTypeByFileName(ATTACHMENT_FILE); + } + } + + @Nested + class TestGetTypeByContent { + + private static final AttachmentFile ATTACHMENT_FILE = AttachmentFileTestFactory.create(); + + @Mock + private InputStream contentStream; + private MockedStatic<URLConnection> urlConnectionMock; + + @BeforeEach + void init() { + urlConnectionMock = mockStatic(URLConnection.class); + doReturn(contentStream).when(service).toInputStream(any()); + } + + @AfterEach + void close() { + urlConnectionMock.close(); + } + + @Test + void shouldCallToInputStream() { + getTypeByContent(); + + verify(service).toInputStream(FILE_CONTENT); + } + + @Test + void shouldCallGuessContentTypeFromStream() { + getTypeByContent(); + + urlConnectionMock.verify(() -> URLConnection.guessContentTypeFromStream(contentStream)); + } + + @Test + void shouldReturnContentType() { + urlConnectionMock.when(() -> URLConnection.guessContentTypeFromStream(any())).thenReturn(AttachmentFileTestFactory.CONTENT_TYPE); + + var result = getTypeByContent(); + + assertThat(result).contains(AttachmentFileTestFactory.CONTENT_TYPE); + } + + @Test + void shouldReturnEmpty() { + var result = getTypeByContent(); + + assertThat(result).isEmpty(); + } + + @Test + void shouldNotThrowException() { + urlConnectionMock.when(() -> URLConnection.guessContentTypeFromStream(any())).thenThrow(IOException.class); + + var result = getTypeByContent(); + + assertThat(result).isEmpty(); + } + + private Optional<String> getTypeByContent() { + return service.getTypeByContent(FILE_CONTENT); + } + } + + @Nested + class TestGetByMimeTypes { + + @BeforeEach + void init() { + } + + @AfterEach + void close() { + } + + @Test + void shouldCallGetContentType() { + getByMimeTypes(); + + verify(mimetypesFileTypeMap).getContentType(AttachmentFileTestFactory.NAME); + } + + @Test + void shouldReturnContentType() { + when(mimetypesFileTypeMap.getContentType(anyString())).thenReturn(AttachmentFileTestFactory.CONTENT_TYPE); + + var result = getByMimeTypes(); + + assertThat(result).isEqualTo(AttachmentFileTestFactory.CONTENT_TYPE); + } + + private String getByMimeTypes() { + return service.getByMimeTypes(AttachmentFileTestFactory.create()); + } + } + + @Nested + class TestToDecodedInputStream { + + @Mock + private Base64.Decoder decoder; + + private MockedStatic<Base64> base64Mock; + + @BeforeEach + void init() { + base64Mock = mockStatic(Base64.class); + base64Mock.when(Base64::getDecoder).thenReturn(decoder); + } + + @AfterEach + void close() { + base64Mock.close(); + } + + @Test + void shouldReturnInputStream() { + var result = service.toDecodedInputStream(FILE_CONTENT); + + assertThat(result).isNotNull(); } } } \ No newline at end of file diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentServiceTest.java index 89a19c3..7160dc4 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageAttachmentServiceTest.java @@ -198,29 +198,21 @@ class MessageAttachmentServiceTest { @BeforeEach void init() { doReturn(attachmentFile).when(service).buildAttachmentFile(any(), any()); - doReturn(inputStream).when(service).getContent(any(MessageAttachment.class)); when(attachmentFileService.createAttachmentFile(any(), any())).thenReturn(ATTACHMENT_FILE_ID); } @Test - void shouldCallMapAttachmentFile() { + void shouldCallBuildAttachmentFile() { persistAttachment(); verify(service).buildAttachmentFile(AttachmentFileTestFactory.VORGANG_ID, ATTACHMENT); } - @Test - void shouldCallGetContent() { - persistAttachment(); - - verify(service).getContent(ATTACHMENT); - } - @Test void shouldCallCreateAttachmentFile() { persistAttachment(); - verify(attachmentFileService).createAttachmentFile(attachmentFile, inputStream); + verify(attachmentFileService).createAttachmentFile(attachmentFile, MessageAttachmentTestFactory.CONTENT); } @Test -- GitLab