Skip to content
Snippets Groups Projects
Commit f354c68a authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4097 Test persist attachment service

parent 25eecc5b
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.nachrichten.postfach.osiv2.attachment;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentFileTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.DisplayName;
......@@ -97,7 +97,7 @@ class Osi2AttachmentFileMapperTest {
void shouldMapVorgangId() {
var result = doMapping();
assertThat(result.getVorgangId()).isEqualTo(UPLOAD_VORGANG_ID);
assertThat(result.getVorgangId()).isEqualTo(VORGANG_ID);
}
private OzgCloudUploadFile doMapping() {
......
package de.ozgcloud.nachrichten.postfach.osiv2.attachment;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.GrpcOzgFileTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.V1ReplyMessageTestFactory.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
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 org.springframework.core.io.Resource;
import de.ozgcloud.nachrichten.postfach.osiv2.exception.Osi2RuntimeException;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentFileTestFactory;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.AttachmentInfoTestFactory;
import de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2MessageTestFactory;
import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentFile;
import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentInfo;
import de.ozgcloud.nachrichten.postfach.osiv2.model.Osi2Message;
import de.ozgcloud.nachrichten.postfach.osiv2.transfer.PostfachApiFacadeService;
import lombok.SneakyThrows;
class Osi2PersistAttachmentServiceTest {
@Spy
@InjectMocks
private Osi2PersistAttachmentService service;
@Mock
private PostfachApiFacadeService postfachApiFacadeService;
@Mock
private Osi2AttachmentFileService attachmentFileService;
@DisplayName("persist attachments")
@Nested
class TestPersistAttachments {
@DisplayName("with no attachments")
@Nested
class TestWithNoAttachments {
private final Osi2Message osi2Message = Osi2MessageTestFactory.create();
@DisplayName("should return empty list")
@Test
void shouldReturnEmptyList() {
var result = service.persistAttachments(osi2Message);
assertThat(result).isEmpty();
}
}
@DisplayName("with two attachments")
@Nested
class TestWithTwoAttachments {
private final AttachmentInfo attachment1 = AttachmentInfoTestFactory.create();
private final AttachmentInfo attachment2 = AttachmentInfoTestFactory.createBuilder()
.guid(UPLOAD_GUID2)
.build();
private final Osi2Message osi2Message = Osi2MessageTestFactory.createBuilder()
.attachments(List.of(attachment1, attachment2))
.build();
@BeforeEach
void mock() {
doReturn(FILE_ID).when(service).persistAttachment(any(), eq(attachment1));
doReturn(FILE_ID_2).when(service).persistAttachment(any(), eq(attachment2));
}
@DisplayName("should call persistAttachment twice")
@Test
void shouldCallPersistAttachmentTwice() {
service.persistAttachments(osi2Message);
verify(service).persistAttachment(osi2Message, attachment1);
verify(service).persistAttachment(osi2Message, attachment2);
}
@DisplayName("should return")
@Test
void shouldReturn() {
var result = service.persistAttachments(osi2Message);
assertThat(result).containsExactly(FILE_ID, FILE_ID_2);
}
}
}
@DisplayName("persist attachment")
@Nested
class TestPersistAttachment {
private final Osi2Message osi2Message = Osi2MessageTestFactory.create();
private final AttachmentInfo attachment = AttachmentInfoTestFactory.create();
private final AttachmentFile attachmentFile = AttachmentFileTestFactory.create();
@Mock
private Resource content;
@BeforeEach
void mock() {
when(postfachApiFacadeService.downloadAttachment(any(), any())).thenReturn(content);
doReturn(attachmentFile).when(service).buildAttachmentFile(any(), any());
doReturn(FILE_ID).when(service).persistAttachmentFile(any(), any());
}
@DisplayName("should call persistAttachmentFile")
@Test
void shouldCallPersistAttachmentFile() {
service.persistAttachment(osi2Message, attachment);
verify(service).persistAttachmentFile(attachmentFile, content);
}
@DisplayName("should call buildAttachmentFile")
@Test
void shouldCallBuildAttachmentFile() {
service.persistAttachment(osi2Message, attachment);
verify(service).buildAttachmentFile(VORGANG_ID, attachment);
}
@DisplayName("should call downloadAttachment")
@Test
void shouldCallDownloadAttachment() {
service.persistAttachment(osi2Message, attachment);
verify(postfachApiFacadeService).downloadAttachment(MESSAGE_ID, UPLOAD_GUID);
}
}
@DisplayName("build attachment file")
@Nested
class TestBuildAttachmentFile {
private final AttachmentInfo attachment = AttachmentInfoTestFactory.create();
@DisplayName("should return")
@Test
void shouldReturn() {
var result = service.buildAttachmentFile(VORGANG_ID, attachment);
assertThat(result).usingRecursiveComparison()
.isEqualTo(AttachmentFileTestFactory.createBuilder()
.vorgangId(VORGANG_ID)
.build());
}
}
@DisplayName("persist attachment file")
@Nested
class TestPersistAttachmentFile {
private final AttachmentFile attachmentFile = AttachmentFileTestFactory.create();
@Mock
private Resource content;
@Mock
private InputStream inputStream;
@BeforeEach
@SneakyThrows
void mock() {
when(attachmentFileService.uploadFileAndReturnId(any(), any())).thenReturn(FILE_ID);
when(content.getInputStream()).thenReturn(inputStream);
}
@DisplayName("should close inputStream")
@Test
@SneakyThrows
void shouldCloseInputStream() {
service.persistAttachmentFile(attachmentFile, content);
verify(inputStream).close();
}
@DisplayName("should call uploadFileAndReturnId")
@Test
void shouldCallUploadFileAndReturnId() {
service.persistAttachmentFile(attachmentFile, content);
verify(attachmentFileService).uploadFileAndReturnId(attachmentFile, inputStream);
}
@DisplayName("should rethrow IOException")
@Test
@SneakyThrows
void shouldRethrowIoException() {
doThrow(new IOException("test")).when(inputStream).close();
assertThatThrownBy(() -> service.persistAttachmentFile(attachmentFile, content))
.isInstanceOf(Osi2RuntimeException.class);
}
@DisplayName("should return")
@Test
void shouldReturn() {
var result = service.persistAttachmentFile(attachmentFile, content);
assertThat(result).isEqualTo(FILE_ID);
}
}
}
\ No newline at end of file
package de.ozgcloud.nachrichten.postfach.osiv2.factory;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentFile;
public class AttachmentFileTestFactory {
public static final String UPLOAD_VORGANG_ID = "vorgangId1";
public static AttachmentFile create() {
return createBuilder().build();
}
......@@ -16,6 +15,6 @@ public class AttachmentFileTestFactory {
return AttachmentFile.builder()
.name(UPLOAD_NAME)
.contentType(UPLOAD_CONTENT_TYPE)
.vorgangId(UPLOAD_VORGANG_ID);
.vorgangId(VORGANG_ID);
}
}
package de.ozgcloud.nachrichten.postfach.osiv2.factory;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.Osi2FileUploadTestFactory.*;
import de.ozgcloud.nachrichten.postfach.osiv2.model.AttachmentInfo;
public class AttachmentInfoTestFactory {
public static AttachmentInfo create() {
return createBuilder().build();
}
public static AttachmentInfo.AttachmentInfoBuilder createBuilder() {
return AttachmentInfo.builder()
.name(UPLOAD_NAME)
.contentType(UPLOAD_CONTENT_TYPE)
.guid(UPLOAD_GUID)
.size(UPLOAD_SIZE);
}
}
......@@ -5,11 +5,11 @@ import de.ozgcloud.vorgang.grpc.file.GrpcOzgFile;
public class GrpcOzgFileTestFactory {
public static final String FILE_ID = "ozgfileId1";
public static final String FILE_ID_2 = "ozgfileId2";
public static final String FILE_NAME = "ozgfileName1";
public static final String FILE_CONTENT_TYPE = "ozgfileContentType1";
public static final Long FILE_SIZE = 124L;
public static GrpcOzgFile create() {
return createBuilder().build();
}
......
package de.ozgcloud.nachrichten.postfach.osiv2.factory;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory.*;
import static de.ozgcloud.nachrichten.postfach.osiv2.factory.V1ReplyMessageTestFactory.*;
import java.time.ZonedDateTime;
import java.util.List;
import de.ozgcloud.nachrichten.postfach.PostfachNachricht;
import de.ozgcloud.nachrichten.postfach.osiv2.model.Osi2Message;
......@@ -15,11 +17,13 @@ public class Osi2MessageTestFactory {
public static Osi2Message.Osi2MessageBuilder createBuilder() {
return Osi2Message.builder()
.messageId(MESSAGE_ID)
.mailBody(MAIL_BODY)
.subject(MAIL_SUBJECT)
.replyOption(PostfachNachricht.ReplyOption.FORBIDDEN)
.createdAt(ZonedDateTime.now())
.vorgangId(VORGANG_ID)
.attachments(List.of())
.postfachAddress(PostfachAddressTestFactory.create());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment