Skip to content
Snippets Groups Projects
Commit 95b33bd6 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6810 refactor MessageAttachmentService

parent 1a3b519f
Branches
Tags
No related merge requests found
...@@ -23,66 +23,66 @@ ...@@ -23,66 +23,66 @@
*/ */
package de.ozgcloud.nachrichten.postfach.osi; package de.ozgcloud.nachrichten.postfach.osi;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Base64; import java.util.Base64;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.common.errorhandling.TechnicalException;
import de.ozgcloud.nachrichten.postfach.AttachmentFile; import de.ozgcloud.nachrichten.file.AttachmentFile;
import de.ozgcloud.nachrichten.postfach.BinaryFileService; import de.ozgcloud.nachrichten.file.AttachmentFileService;
import de.ozgcloud.nachrichten.postfach.FileId; import de.ozgcloud.nachrichten.postfach.FileId;
import de.ozgcloud.nachrichten.postfach.PersistPostfachNachrichtService; import lombok.RequiredArgsConstructor;
import lombok.val;
@Service @Service
@RequiredArgsConstructor
public class MessageAttachmentService { public class MessageAttachmentService {
@Autowired private final AttachmentFileService attachmentFileService;
private PersistPostfachNachrichtService persistPostfachNachrichtService;
@Autowired
private BinaryFileService binaryFileService;
public MessageAttachment getMessageAttachment(FileId fileId) { public MessageAttachment getMessageAttachment(FileId fileId) {
return buildMessageAttachment(attachmentFileService.getFile(fileId), getAttachmentContent(fileId));
}
MessageAttachment buildMessageAttachment(AttachmentFile attachmentFile, String attachmentContent) {
return MessageAttachment.builder()
.fileName(attachmentFile.getName())
.content(attachmentContent)
.build();
}
String getAttachmentContent(FileId fileId) {
return encodeAttachmentContent(getContent(fileId));
}
byte[] getContent(FileId fileId) {
try { try {
val metadata = binaryFileService.getFile(fileId).getMetadata(); return IOUtils.toByteArray(attachmentFileService.getFileContent(fileId));
return MessageAttachment.builder()
.fileName(metadata.getString("name"))
.content(getAttachmentContent(fileId))
.build();
} catch (IOException e) { } catch (IOException e) {
throw new TechnicalException(e.getMessage(), e); throw new TechnicalException("Cannot get file content", e);
} }
} }
String getAttachmentContent(FileId fileId) throws IOException { String encodeAttachmentContent(byte[] content) {
ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
IOUtils.copy(getAttachmentContentStream(fileId), contentStream);
return encodeAttachmentContent(contentStream.toByteArray());
}
private String encodeAttachmentContent(byte[] content) {
return new String(Base64.getEncoder().encode(content)); return new String(Base64.getEncoder().encode(content));
} }
public String persistAttachment(String vorgangId, MessageAttachment attachment) { public String persistAttachment(String vorgangId, MessageAttachment attachment) {
return persistPostfachNachrichtService.persistAttachment(vorgangId, mapAttachmentFile(attachment)); return attachmentFileService.createAttachmentFile(buildAttachmentFile(vorgangId, attachment), getContent(attachment));
} }
AttachmentFile mapAttachmentFile(MessageAttachment attachment) { AttachmentFile buildAttachmentFile(String vorgangId, MessageAttachment attachment) {
return AttachmentFile.builder() return AttachmentFile.builder()
.name(attachment.getFileName()) .name(attachment.getFileName())
.content(() -> IOUtils.toInputStream(attachment.getContent(), Charset.defaultCharset())).build(); .vorgangId(vorgangId)
.build();
} }
InputStream getAttachmentContentStream(FileId fileId) { InputStream getContent(MessageAttachment attachment) {
return binaryFileService.getUploadedFileStream(fileId); return IOUtils.toInputStream(attachment.getContent(), Charset.defaultCharset());
} }
} }
...@@ -28,104 +28,225 @@ import static org.mockito.ArgumentMatchers.*; ...@@ -28,104 +28,225 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Date;
import java.time.Instant;
import org.bson.BsonObjectId;
import org.bson.BsonValue;
import org.bson.Document;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy;
import com.mongodb.client.gridfs.model.GridFSFile; import com.thedeanda.lorem.LoremIpsum;
import de.ozgcloud.nachrichten.postfach.BinaryFileService; import de.ozgcloud.nachrichten.file.AttachmentFile;
import de.ozgcloud.nachrichten.file.AttachmentFileService;
import de.ozgcloud.nachrichten.file.AttachmentFileTestFactory;
import de.ozgcloud.nachrichten.postfach.FileId; import de.ozgcloud.nachrichten.postfach.FileId;
class MessageAttachmentServiceTest { class MessageAttachmentServiceTest {
private static final FileId FILE_ID = FileId.from("42");
private static final FileId FILE_ID = FileId.from(LoremIpsum.getInstance().getWords(1));
@Spy
@InjectMocks @InjectMocks
private MessageAttachmentService messageAttachmentService; private MessageAttachmentService service;
@Mock @Mock
private BinaryFileService fileService; private AttachmentFileService attachmentFileService;
private GridFSFile gridFsfile;
@Nested @Nested
class TestLoadingAttachment { class TestGetMessageAttachment {
@Mock
private AttachmentFile attachmentFile;
@Mock
private MessageAttachment messageAttachment;
@BeforeEach @BeforeEach
void init() { void init() {
Document metadata = new Document(); when(attachmentFileService.getFile(any())).thenReturn(attachmentFile);
metadata.put("name", MessageAttachmentTestFactory.FILENAME); doReturn(MessageAttachmentTestFactory.CONTENT).when(service).getAttachmentContent(any());
BsonValue id = new BsonObjectId(); doReturn(messageAttachment).when(service).buildMessageAttachment(any(), any());
gridFsfile = new GridFSFile(id, FILE_ID.toString(), 0, 0, Date.from(Instant.now()), metadata); }
when(fileService.getFile(any())).thenReturn(gridFsfile); @Test
when(fileService.getUploadedFileStream(any())) void shouldCallGetFile() {
.thenReturn(new ByteArrayInputStream(MessageAttachmentTestFactory.DECODED_CONTENT.getBytes())); getMessageAttachment();
verify(attachmentFileService).getFile(FILE_ID);
} }
@Test @Test
void shouldHaveAttachmentWithFileName() { void shouldCallGetAttachmentContent() {
MessageAttachment attachment = messageAttachmentService.getMessageAttachment(FILE_ID); getMessageAttachment();
assertThat(attachment.getFileName()).isEqualTo(MessageAttachmentTestFactory.FILENAME); verify(service).getAttachmentContent(FILE_ID);
} }
@Test @Test
void shouldHaveAttachmentContent() { void shouldCallBuildMessageAttachment() {
MessageAttachment attachment = messageAttachmentService.getMessageAttachment(FILE_ID); getMessageAttachment();
assertThat(attachment.getContent()).isEqualTo(MessageAttachmentTestFactory.CONTENT); verify(service).buildMessageAttachment(attachmentFile, MessageAttachmentTestFactory.CONTENT);
} }
@Test
void shouldReturnMessageAttachment() {
var result = getMessageAttachment();
assertThat(result).isSameAs(messageAttachment);
}
private MessageAttachment getMessageAttachment() {
return service.getMessageAttachment(FILE_ID);
}
} }
@Nested @Nested
class TestLoadingAttachmentContent { class TestBuildMessageAttachment {
@Test
void shouldBuildMessageAttachment() {
var attachment = buildMessageAttachment();
assertThat(attachment).usingRecursiveComparison().isEqualTo(MessageAttachmentTestFactory.create());
}
private MessageAttachment buildMessageAttachment() {
return service.buildMessageAttachment(AttachmentFileTestFactory.create(), MessageAttachmentTestFactory.CONTENT);
}
}
@Nested
class TestGetAttachmentContent {
private static final byte[] CONTENT = MessageAttachmentTestFactory.CONTENT.getBytes();
@BeforeEach @BeforeEach
void init() { void init() {
when(fileService.getUploadedFileStream(any())) doReturn(CONTENT).when(service).getContent(any(FileId.class));
.thenReturn(new ByteArrayInputStream(MessageAttachmentTestFactory.DECODED_CONTENT.getBytes())); doReturn(MessageAttachmentTestFactory.CONTENT).when(service).encodeAttachmentContent(any());
} }
@Test @Test
void shouldGetInputStream() { void shouldCallGetContent() {
InputStream input = messageAttachmentService.getAttachmentContentStream(FILE_ID); service.getAttachmentContent(FILE_ID);
assertThat(input).isNotNull(); verify(service).getContent(FILE_ID);
} }
@Test @Test
void shouldGetContent() throws IOException { void shouldCallEncodeAttachmentContent() {
String input = messageAttachmentService.getAttachmentContent(FILE_ID); service.getAttachmentContent(FILE_ID);
assertThat(input).isEqualTo(MessageAttachmentTestFactory.CONTENT); verify(service).encodeAttachmentContent(CONTENT);
}
@Test
void shouldReturnContent() {
var result = service.getAttachmentContent(FILE_ID);
assertThat(result).isEqualTo(MessageAttachmentTestFactory.CONTENT);
} }
} }
@Nested @Nested
class TestMapAttachmentFile { class TestGetContent {
@Test @Test
void shouldMapFileName() { void shouldCallGetFileContent() {
var attachmentFile = messageAttachmentService.mapAttachmentFile(MessageAttachmentTestFactory.create()); when(attachmentFileService.getFileContent(any())).thenReturn(new ByteArrayInputStream(MessageAttachmentTestFactory.CONTENT.getBytes()));
service.getContent(FILE_ID);
verify(attachmentFileService).getFileContent(FILE_ID);
}
@Test
void shouldReturnContent() {
when(attachmentFileService.getFileContent(any())).thenReturn(new ByteArrayInputStream(MessageAttachmentTestFactory.CONTENT.getBytes()));
var result = service.getContent(FILE_ID);
assertThat(result).isEqualTo(MessageAttachmentTestFactory.CONTENT.getBytes());
}
}
assertThat(attachmentFile.getName()).isEqualTo(MessageAttachmentTestFactory.FILENAME); @Nested
class TestEncodeAttachmentContent {
@Test
void shouldEncodeContent() {
var result = service.encodeAttachmentContent(MessageAttachmentTestFactory.DECODED_CONTENT.getBytes());
assertThat(result).isEqualTo(MessageAttachmentTestFactory.CONTENT);
}
}
@Nested
class TestPersistAttachment {
private static final String ATTACHMENT_FILE_ID = LoremIpsum.getInstance().getWords(1);
private static final MessageAttachment ATTACHMENT = MessageAttachmentTestFactory.create();
@Mock
private AttachmentFile attachmentFile;
@Mock
private InputStream inputStream;
@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 @Test
void shouldMapContent() { void shouldCallMapAttachmentFile() {
var attachmentFile = messageAttachmentService.mapAttachmentFile(MessageAttachmentTestFactory.create()); 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);
}
@Test
void shouldReturnAttachmentFileId() {
var result = persistAttachment();
assertThat(result).isEqualTo(ATTACHMENT_FILE_ID);
}
private String persistAttachment() {
return service.persistAttachment(AttachmentFileTestFactory.VORGANG_ID, ATTACHMENT);
}
}
@Nested
class TestBuildAttachmentFile {
@Test
void shouldMapFileName() {
var attachmentFile = buildAttachmentFile();
assertThat(attachmentFile).usingRecursiveComparison().ignoringFields("contentType").isEqualTo(AttachmentFileTestFactory.create());
}
assertThat(attachmentFile.getContent()).hasContent(MessageAttachmentTestFactory.CONTENT); private AttachmentFile buildAttachmentFile() {
return service.buildAttachmentFile(AttachmentFileTestFactory.VORGANG_ID, MessageAttachmentTestFactory.create());
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment