Skip to content
Snippets Groups Projects
Commit 857023dd authored by Martin Vogel's avatar Martin Vogel
Browse files

test: add tests for data extraction from attachment (planning#2251)

parent 032ddc85
No related branches found
No related tags found
1 merge request!434planning#2251 : Send large submission data as attachment
......@@ -3,6 +3,7 @@ package dev.fitko.fitconnect.api.domain.model.metadata.attachment;
import dev.fitko.fitconnect.api.domain.model.attachment.Fragment;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.File;
import java.util.Collections;
......@@ -11,6 +12,7 @@ import java.util.UUID;
@Data
@AllArgsConstructor
@NoArgsConstructor
public final class AttachmentForValidation {
private UUID attachmentId;
......
......@@ -13,6 +13,7 @@ public final class DataAttachmentUtil {
/**
* Extract submission data from an attachment with {@link Purpose#DATA}.
* The data attachment will be removed from the given list.
*
* @param attachments to be filtered
* @return optional byte[] with decrypted submission data
......
package dev.fitko.fitconnect.client.util;
import dev.fitko.fitconnect.api.domain.model.attachment.Fragment;
import dev.fitko.fitconnect.api.domain.model.metadata.Hash;
import dev.fitko.fitconnect.api.domain.model.metadata.attachment.ApiAttachment;
import dev.fitko.fitconnect.api.domain.model.metadata.attachment.AttachmentForValidation;
import dev.fitko.fitconnect.api.domain.model.metadata.attachment.Purpose;
import dev.fitko.fitconnect.core.crypto.constants.CryptoConstants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class DataAttachmentUtilTest {
@Test
void getDataForAttachmentWithDataPurposeAndNoFragments() throws IOException {
// Given
var data = new byte[1024];
new Random().nextBytes(data);
final Hash hash = new Hash();
hash.setContent(CryptoConstants.HASH_OF_ZERO_BYTES);
final ApiAttachment attachmentMetadata = new ApiAttachment();
attachmentMetadata.setAttachmentId(UUID.randomUUID());
attachmentMetadata.setPurpose(Purpose.DATA);
attachmentMetadata.setHash(hash);
final AttachmentForValidation attachment = AttachmentForValidation.forAttachmentWithoutFragments(attachmentMetadata, data, "");
final var attachments = new ArrayList<AttachmentForValidation>();
attachments.add(attachment);
// When
final Optional<byte[]> dataFromAttachment = DataAttachmentUtil.getDataFromAttachment(attachments);
// Then
assertTrue(dataFromAttachment.isPresent());
assertThat(attachments.size(), is(0));
assertThat(dataFromAttachment.get(), is(data));
}
@Test
void getDataForFragmentedAttachmentWithDataPurpose(@TempDir Path tempDir) throws IOException {
// Given
var data = new byte[1024];
new Random().nextBytes(data);
final File attachmentFile = Files.write(tempDir.resolve("attachment.pdf"), data).toFile();
final Hash hash = new Hash();
hash.setContent(CryptoConstants.HASH_OF_ZERO_BYTES);
final ApiAttachment attachmentMetadata = new ApiAttachment();
attachmentMetadata.setAttachmentId(UUID.randomUUID());
attachmentMetadata.setPurpose(Purpose.DATA);
attachmentMetadata.setHash(hash);
final List<Fragment> fragments = List.of(new Fragment(UUID.randomUUID(), tempDir.resolve("fragment.bin").toFile()));
final AttachmentForValidation chunkedAttachment = AttachmentForValidation.forFragmentedAttachment(attachmentMetadata, fragments, attachmentFile);
final var attachments = new ArrayList<AttachmentForValidation>();
attachments.add(chunkedAttachment);
// When
final Optional<byte[]> dataFromChunkedAttachment = DataAttachmentUtil.getDataFromAttachment(attachments);
// Then
assertTrue(dataFromChunkedAttachment.isPresent());
assertFalse(attachmentFile.exists());
assertThat(attachments.size(), is(0));
assertThat(dataFromChunkedAttachment.get(), is(data));
}
@Test
void getDataForAttachmentWithAttachmentPurpose() throws IOException {
// Given
final ApiAttachment attachmentMetadata = new ApiAttachment();
attachmentMetadata.setPurpose(Purpose.ATTACHMENT);
final AttachmentForValidation attachment = new AttachmentForValidation();
attachment.setAttachmentMetadata(attachmentMetadata);
final var attachments = new ArrayList<AttachmentForValidation>();
attachments.add(attachment);
// When
final Optional<byte[]> dataFromAttachment = DataAttachmentUtil.getDataFromAttachment(attachments);
// Then
assertTrue(dataFromAttachment.isEmpty());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment