Skip to content
Snippets Groups Projects
Commit 632d736b authored by OZG-Cloud Team's avatar OZG-Cloud Team
Browse files

OZG-5412 intelliform: Cleanup test factory

parent c66283ea
Branches
Tags
No related merge requests found
......@@ -25,13 +25,12 @@ package de.ozgcloud.eingang.intelliform;
import static java.util.stream.Collectors.*;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import de.ozgcloud.common.test.TestUtils;
import lombok.Builder;
public class AttachmentTestFactory {
......@@ -53,24 +52,29 @@ public class AttachmentTestFactory {
<file content-type="image/png" description="" id="VendorId3333" length="155251">Image.png</file>
</Upload1>
</myForm>""";
public static final byte[] XML_CONTENT = XML_CONTENT_STRING.getBytes();
public static final String XML_CONTENT = XML_CONTENT_STRING;
public static final String XML_ATTACHMENT_ID = "myForm-xml";
public static final String XML_NAME = "XML-Daten.xml";
public static final String PDF_ATTACHMENT_CONTENT_TYPE = "application/pdf";
public static final byte[] PDF_ATTACHMENT_CONTENT = "TestContent2".getBytes();
public static final String PDF_ATTACHMENT_CONTENT = "TestContent2";
public static final String PDF_ATTACHMENT_NAME = "Scan1.pdf";
public static final String PDF_ATTACHMENT_ID = "VendorId2222";
public static final String PDF2_ATTACHMENT_ID = "VendorIdpdf2";
public static final String PNG_ATTACHMENT_CONTENT_TYPE = "application/pdf";
public static final byte[] PNG_ATTACHMENT_CONTENT = "TestContent3".getBytes();
public static final String PNG_ATTACHMENT_CONTENT = "TestContent3";
public static final String PNG_ATTACHMENT_NAME = "Image.png";
public static final String PNG_ATTACHMENT_ID = "VendorId3333";
public static final String JPG_ATTACHMENT_ID = "VendorIdjpg1";
public static final String ODT_ATTACHMENT_ID = "VendorIdodt1";
public static final String ODT_ATTACHMENT_CONTENT_TYPE = "application/vnd.oasis.opendocument.text";
public static final String DOCX_ATTACHMENT_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
public static final String DOCX_ATTACHMENT_CONTENT = "TestContent4";
public static final String DOCX1_ATTACHMENT_ID = "VendorIddocx1";
public static final String DOCX2_ATTACHMENT_ID = "VendorIddocx2";
public static final String DOCX3_ATTACHMENT_ID = "VendorIddocx3";
......@@ -81,7 +85,6 @@ public class AttachmentTestFactory {
public static final String DOCX8_ATTACHMENT_ID = "VendorIddocx8";
public static final String ORGANISATIONSEINHEITEN_ID = "10363455";
public static final String ANTRAGSTELLER_ANREDE = "Herr";
public static final String ANTRAGSTELLER_ANREDE_CODE = "03";
public static final String ANTRAGSTELLER_NACHNAME = "Mustermann";
......@@ -94,22 +97,30 @@ public class AttachmentTestFactory {
}
public static Attachment createPdf() {
var attachment = new Attachment();
attachment.getAttributes().add(createAttributesEntry());
attachment.setContent(PDF_ATTACHMENT_CONTENT);
attachment.setContentType(PDF_ATTACHMENT_CONTENT_TYPE);
attachment.setId(PDF_ATTACHMENT_ID);
attachment.setName(PDF_ATTACHMENT_NAME);
return attachment;
return createAttachment(MetaAttachment.builder()
.id(PDF_ATTACHMENT_ID)
.name(PDF_ATTACHMENT_NAME)
.contentType(PDF_ATTACHMENT_CONTENT_TYPE)
.content(PDF_ATTACHMENT_CONTENT)
.build());
}
public static Attachment createPng() {
return createAttachment(MetaAttachment.builder()
.id(PNG_ATTACHMENT_ID)
.name(PNG_ATTACHMENT_NAME)
.contentType(PNG_ATTACHMENT_CONTENT_TYPE)
.content(PNG_ATTACHMENT_CONTENT)
.build());
}
private static Attachment createAttachment(MetaAttachment metaAttachment) {
var attachment = new Attachment();
attachment.getAttributes().add(createAttributesEntry());
attachment.setContent(PNG_ATTACHMENT_CONTENT);
attachment.setContentType(PNG_ATTACHMENT_CONTENT_TYPE);
attachment.setId(PNG_ATTACHMENT_ID);
attachment.setName(PNG_ATTACHMENT_NAME);
attachment.setContent(metaAttachment.content.getBytes());
attachment.setContentType(metaAttachment.contentType);
attachment.setId(metaAttachment.id);
attachment.setName(metaAttachment.name);
return attachment;
}
......@@ -120,87 +131,52 @@ public class AttachmentTestFactory {
return attributesEntry;
}
public static Attachment createWithIdAndName(String attachmentId, String attachmentName) {
var attachment = new Attachment();
attachment.getAttributes().add(createAttributesEntry());
attachment.setContent("test content".getBytes());
attachment.setContentType("test content type");
attachment.setId(attachmentId);
attachment.setName(attachmentName);
return attachment;
}
public static List<Attachment> createManyAttachments(String xmlTemplateString, Map<String, String> templateValues,
String... attachmentParameterMatrix) {
public static Stream<Attachment> createAttachmentsForLargeXml() {
String[] templateValues = {
DOCX1_ATTACHMENT_ID, "Document1.docx",
PDF_ATTACHMENT_ID, "Document2.pdf",
DOCX2_ATTACHMENT_ID, "Document3.docx",
DOCX3_ATTACHMENT_ID, "Document4.docx",
DOCX4_ATTACHMENT_ID, "Document5.docx",
DOCX5_ATTACHMENT_ID, "Document6.docx",
DOCX6_ATTACHMENT_ID, "Document7.docx",
DOCX7_ATTACHMENT_ID, "Document8.docx",
DOCX8_ATTACHMENT_ID, "Document9.docx",
PDF2_ATTACHMENT_ID, "Document9.pdf",
ODT_ATTACHMENT_ID, "Document10.odt",
JPG_ATTACHMENT_ID, "Document11.jpg",
PNG_ATTACHMENT_ID, "Document12.png",
};
return arrayToMap(templateValues).entrySet().stream()
.map(entry -> createWithIdAndName(entry.getKey(), entry.getValue()));
}
var metaAttachments = arrayToMetaAttachments(attachmentParameterMatrix);
public static Attachment createLargeXmlDaten() {
String[] templateValues = {
"fileid-docx1", DOCX1_ATTACHMENT_ID,
"fileid-docx2", DOCX2_ATTACHMENT_ID,
"fileid-docx3", DOCX3_ATTACHMENT_ID,
"fileid-docx4", DOCX4_ATTACHMENT_ID,
"fileid-docx5", DOCX5_ATTACHMENT_ID,
"fileid-docx6", DOCX6_ATTACHMENT_ID,
"fileid-docx7", DOCX7_ATTACHMENT_ID,
"fileid-docx8", DOCX8_ATTACHMENT_ID,
"fileid-pdf1", PDF_ATTACHMENT_ID,
"fileid-pdf2", PDF2_ATTACHMENT_ID,
"fileid-png1", PNG_ATTACHMENT_ID,
"fileid-jpg1", JPG_ATTACHMENT_ID,
"fileid-odt1", ODT_ATTACHMENT_ID,
"oeid", ORGANISATIONSEINHEITEN_ID,
"uuid", UUID.randomUUID().toString(),
"transactionId", UUID.randomUUID().toString(),
"now", Instant.now().toString(),
"antragsteller_anrede", ANTRAGSTELLER_ANREDE,
"anrede_code", ANTRAGSTELLER_ANREDE_CODE,
"antragsteller_nachname", ANTRAGSTELLER_NACHNAME,
"antragsteller_vorname", "Max",
"antragsteller_email", "max.mustermann@example.com"
};
var allTemplateValues = Stream.concat(
templateValues.entrySet().stream(),
metaAttachments.stream().map(metaAttachment -> Map.entry(metaAttachment.templateId, metaAttachment.id))
).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
var xmlFormString = fillTemplateString(
TestUtils.loadTextFile("intelliform/XML-Daten-complex.template.xml"),
arrayToMap(templateValues));
return createXmlDatenWithContent(xmlFormString.getBytes());
xmlTemplateString,
allTemplateValues
);
return Stream.concat(
Stream.of(createXmlDatenWithContent(xmlFormString)),
metaAttachments.stream().map(AttachmentTestFactory::createAttachment)
).toList();
}
private static Attachment createXmlDatenWithContent(byte[] content) {
var attachment = new Attachment();
attachment.getAttributes().add(createAttributesEntry());
attachment.setContentType(XML_CONTENT_TYPE);
attachment.setId(XML_ATTACHMENT_ID);
attachment.setName(XML_NAME);
attachment.setContent(content);
return attachment;
private static Attachment createXmlDatenWithContent(String content) {
return createAttachment(MetaAttachment.builder()
.id(XML_ATTACHMENT_ID)
.name(XML_NAME)
.contentType(XML_CONTENT_TYPE)
.content(content)
.build()
);
}
@Builder
private record MetaAttachment(String id, String name, String contentType, String content, String templateId) {
}
public static Map<String, String> arrayToMap(String[] array) {
if (array.length % 2 != 0) {
throw new IllegalArgumentException("Array length must be even to form key-value pairs.");
private static List<MetaAttachment> arrayToMetaAttachments(String[] array) {
int parameterCount = 5;
if (array.length % parameterCount != 0) {
throw new IllegalArgumentException("Matrix length must be divisible by %d.".formatted(parameterCount));
}
return IntStream.range(0, array.length / 2)
return IntStream.range(0, array.length / parameterCount)
.boxed()
.collect(toMap(i -> array[2 * i], i -> array[2 * i + 1]));
.map(row -> row * parameterCount)
.map(offset -> new MetaAttachment(array[offset], array[offset + 1], array[offset + 2], array[offset + 3], array[offset + 4]))
.toList();
}
private static String fillTemplateString(String templateString, Map<String, String> templateValues) {
......
......@@ -326,7 +326,7 @@ class DepositDataMapperTest {
void shouldHaveContentType() {
var incomingFile = doMapping();
assertThat(incomingFile.getName()).isEqualTo(XML_NAME);
assertThat(incomingFile.getContentType()).isEqualTo(XML_CONTENT_TYPE);
}
@DisplayName("should have size")
......@@ -334,7 +334,7 @@ class DepositDataMapperTest {
void shouldHaveSize() {
var incomingFile = doMapping();
assertThat(incomingFile.getSize()).isEqualTo(XML_CONTENT.length);
assertThat(incomingFile.getSize()).isEqualTo(XML_CONTENT.getBytes().length);
}
@DisplayName("should have file with content")
......
......@@ -23,9 +23,15 @@
*/
package de.ozgcloud.eingang.intelliform;
import static de.ozgcloud.eingang.intelliform.AttachmentTestFactory.*;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import java.util.Map;
import java.util.UUID;
import de.ozgcloud.common.test.TestUtils;
public class DepositDataTestFactory {
......@@ -35,10 +41,32 @@ public class DepositDataTestFactory {
AttachmentTestFactory.createPng()
);
public static final List<Attachment> MANY_ATTACHMENTS = Stream.concat(
Stream.of(AttachmentTestFactory.createLargeXmlDaten()),
AttachmentTestFactory.createAttachmentsForLargeXml()
).toList();
public static final List<Attachment> MANY_ATTACHMENTS = createManyAttachments(
TestUtils.loadTextFile("intelliform/XML-Daten-complex.template.xml"),
Map.of(
"oeid", ORGANISATIONSEINHEITEN_ID,
"uuid", UUID.randomUUID().toString(),
"transactionId", UUID.randomUUID().toString(),
"now", Instant.now().toString(),
"antragsteller_anrede", ANTRAGSTELLER_ANREDE,
"anrede_code", ANTRAGSTELLER_ANREDE_CODE,
"antragsteller_nachname", ANTRAGSTELLER_NACHNAME,
"antragsteller_vorname", "Max",
"antragsteller_email", "max.mustermann@example.com"
),
DOCX1_ATTACHMENT_ID, "Document1.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx1",
PDF_ATTACHMENT_ID, "Document2.pdf", PDF_ATTACHMENT_CONTENT_TYPE, PDF_ATTACHMENT_CONTENT, "fileid-pdf1",
DOCX2_ATTACHMENT_ID, "Document3.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx2",
DOCX3_ATTACHMENT_ID, "Document4.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx3",
DOCX4_ATTACHMENT_ID, "Document5.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx4",
DOCX5_ATTACHMENT_ID, "Document6.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx5",
DOCX6_ATTACHMENT_ID, "Document7.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx6",
DOCX7_ATTACHMENT_ID, "Document8.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx7",
DOCX8_ATTACHMENT_ID, "Document9.docx", DOCX_ATTACHMENT_CONTENT_TYPE, DOCX_ATTACHMENT_CONTENT, "fileid-docx8",
PDF2_ATTACHMENT_ID, "Document9.pdf", PDF_ATTACHMENT_CONTENT_TYPE, PDF_ATTACHMENT_CONTENT, "fileid-pdf2",
ODT_ATTACHMENT_ID, "Document10.odt", ODT_ATTACHMENT_CONTENT_TYPE, "TestContent5", "fileid-odt1",
JPG_ATTACHMENT_ID, "Document11.jpg", "image/jpeg", "TestContent6", "fileid-jpg1",
PNG_ATTACHMENT_ID, "Document12.png", "image/png", "TestContent7", "fileid-png1");
public static DepositData create(Collection<Attachment> attachments) {
var depositData = new DepositData();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment