diff --git a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/model/FileChunkInfo.java b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/model/FileChunkInfo.java index b1e049b26e8286e4add4476eda29943449333ac6..d6c61670c4ee59886aac569220533d9aa2ec3b5f 100644 --- a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/model/FileChunkInfo.java +++ b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/model/FileChunkInfo.java @@ -18,16 +18,20 @@ public record FileChunkInfo( return new AbstractResource() { @Override public String getDescription() { - return FileChunkInfo.this.toString(); + return "File chunk " + chunkIndex + " of " + upload.getLoggableString(); } @Override public InputStream getInputStream() { - return new LimitedInputStream(fileInputStream, CHUNK_SIZE); + return new LimitedInputStream(fileInputStream, contentLength()); } @Override public long contentLength() { + return chunkIndex == upload.numberOfChunks() ? 0 : getChunkContentLength(); + } + + private long getChunkContentLength() { return chunkIndex == upload.numberOfChunks() - 1 ? upload.file().getSize() % CHUNK_SIZE : CHUNK_SIZE; diff --git a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineService.java b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineService.java index 2c5253ee2e934cf0ecb6d775a0bebf3aa953192c..50a5a5272f51c427f97caa85018e106f2852ca76 100644 --- a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineService.java +++ b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineService.java @@ -80,7 +80,7 @@ public class Osi2QuarantineService { } Stream<FileChunkInfo> streamFileChunkInfos(Osi2FileUpload upload) { - return LongStream.range(0, upload.numberOfChunks()) + return LongStream.range(0, upload.numberOfChunks() + 1) .mapToObj(chunkIndex -> buildFileChunkInfo(upload, chunkIndex)); } diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java index 71c3be1335a5bbc768248ed0f2a210953945a9c7..82b3ca20b4f34a58e598a6a9af043d0639d1e508 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.*; import java.time.OffsetDateTime; import java.util.List; import java.util.UUID; +import java.util.function.Function; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -115,13 +116,17 @@ class OsiPostfachRemoteServiceITCase { osiPostfachRemoteService.sendMessage(postfachNachrichtWithAttachment); + var requests = postfachFacadeMockServer.findAll( + postRequestedFor(urlPathTemplate("/Quarantine/v1/Upload/Chunked"))); + assertThat(requests).hasSize(2); + Function<Integer, byte[]> requestBodyBytes = requestIndex -> requests.get(requestIndex).getPart("file").getBody().asBytes(); + // should send file in one chunk + assertThat(requestBodyBytes.apply(0)).isEqualTo(AttachmentExampleUploadUtil.EXAMPLE_TEXT_DATA); + // should send empty chunk to complete transfer + assertThat(requestBodyBytes.apply(1)).isEmpty(); postfachFacadeMockServer.verify( exactly(1), - postRequestedFor(urlPathTemplate("/Quarantine/v1/Upload/Chunked")) - ); - postfachFacadeMockServer.verify( - exactly(1), - postRequestedFor(urlPathTemplate("/Quarantine/v1/Upload/{guid}")) + getRequestedFor(urlPathTemplate("/Quarantine/v1/Upload/{guid}")) ); postfachFacadeMockServer.verify( exactly(1), diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/extension/AttachmentExampleUploadUtil.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/extension/AttachmentExampleUploadUtil.java index 3b3f4bacfeacb0b992df34205f19602994214c3a..bde195f730816430c4e4598566087932aba98bbc 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/extension/AttachmentExampleUploadUtil.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/extension/AttachmentExampleUploadUtil.java @@ -12,13 +12,15 @@ import lombok.extern.log4j.Log4j2; @Log4j2 public class AttachmentExampleUploadUtil { + public static final byte[] EXAMPLE_TEXT_DATA = LoremIpsum.getInstance().getParagraphs(5,100).getBytes(); + public static String uploadTextFile(Osi2AttachmentFileService remoteService) { return remoteService.uploadFileAndReturnId(AttachmentFile.builder() .contentType("test/plain") .name("test.txt") .vorgangId(UUID.randomUUID().toString()) - .build(), () -> new ByteArrayInputStream(LoremIpsum.getInstance().getParagraphs(5,100).getBytes())); + .build(), () -> new ByteArrayInputStream(EXAMPLE_TEXT_DATA)); } } diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineServiceTest.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineServiceTest.java index 0b903983a12284bb6997ae44d7c16c233efa690a..8e6e0a1abb2bfaafcbd90082b350a877d2e3361c 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineServiceTest.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/transfer/Osi2QuarantineServiceTest.java @@ -329,13 +329,17 @@ class Osi2QuarantineServiceTest { .upload(upload) .chunkIndex(1) .build(); + private final FileChunkInfo emptyChunkInfo = FileChunkInfo.builder() + .upload(upload) + .chunkIndex(2) + .build(); @DisplayName("should stream chunk infos") @Test void shouldStreamChunkInfos() { var result = service.streamFileChunkInfos(upload).toList(); - assertThat(result).containsExactly(chunkInfo1, chunkInfo2); + assertThat(result).containsExactly(chunkInfo1, chunkInfo2, emptyChunkInfo); } }