diff --git a/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/DepositDataMapperTest.java b/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/DepositDataMapperTest.java index f419421790ddbb78c551d2266d56455ab3c01054..bc6367e4cad7f32d3112836edb4678e49ccf8f16 100644 --- a/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/DepositDataMapperTest.java +++ b/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/DepositDataMapperTest.java @@ -136,9 +136,65 @@ class DepositDataMapperTest { } } - @DisplayName("with large attachments") + @DisplayName("with duplicate keys") @Nested - class TestWithLargeAttachments { + class TestWithDuplicateKeys { + @BeforeEach + void mock() { + depositData = DepositDataTestFactory.create(List.of( + withEmptyName(createXmlDaten()), + createXmlDaten(), + withEmptyName(createPdf()), + createPdf(), + withEmptyName(createPng()), + createPng() + )); + } + + private Attachment withEmptyName(Attachment attachment) { + attachment.setName(""); + return attachment; + } + + @DisplayName("should keep last entry for representations") + @Test + void shouldKeepLastEntryForRepresentations() { + var formData = doMapping(); + + var representationFiles = formData.getRepresentations(); + assertThat(getAttachmentVendorIds(representationFiles)).containsExactly(XML_ATTACHMENT_ID, PDF_ATTACHMENT_ID); + assertThat(getAttachmentFileNames(representationFiles)).containsExactly(XML_NAME, PDF_ATTACHMENT_NAME); + } + + @DisplayName("should keep last entry for attachments") + @Test + void shouldKeepLastEntryForAttachments() { + var formData = doMapping(); + + var attachmentFiles = formData.getAttachments().stream() + .map(IncomingFileGroup::getFiles) + .flatMap(List::stream) + .toList(); + assertThat(getAttachmentVendorIds(attachmentFiles)).containsExactly(PNG_ATTACHMENT_ID); + assertThat(getAttachmentFileNames(attachmentFiles)).containsExactly(PNG_ATTACHMENT_NAME); + } + + private List<String> getAttachmentFileNames(List<IncomingFile> incomingFileList) { + return incomingFileList.stream() + .map(IncomingFile::getName) + .toList(); + } + + private List<String> getAttachmentVendorIds(List<IncomingFile> incomingFileList) { + return incomingFileList.stream() + .map(IncomingFile::getVendorId) + .toList(); + } + } + + @DisplayName("with many attachments") + @Nested + class TestWithManyAttachments { @BeforeEach void mock() { depositData = DepositDataTestFactory.create(MANY_ATTACHMENTS); diff --git a/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/FormDataEndpointITCase.java b/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/FormDataEndpointITCase.java index 891d1578997efde8644f5322aa86ef368a7b2e52..524f388ca41061d7abcfe8b535d4f78853df2081 100644 --- a/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/FormDataEndpointITCase.java +++ b/intelliform-adapter/src/test/java/de/ozgcloud/eingang/intelliform/FormDataEndpointITCase.java @@ -34,6 +34,7 @@ import static org.mockito.Mockito.*; import java.io.StringReader; import java.io.StringWriter; import java.util.Collection; +import java.util.List; import java.util.Optional; import javax.xml.namespace.QName; @@ -80,6 +81,7 @@ class FormDataEndpointITCase { private final static String RESPONSE = "EinfachesFormularZweiAnhaengeSoapResponse.xml"; private static final String SOAP_REQUEST_OTHER_NAME = "XML-Daten-1-other_name_SoapRequest.xml"; + private static Jaxb2Marshaller marshaller; @Autowired private ApplicationContext applicationContext; @@ -96,6 +98,14 @@ class FormDataEndpointITCase { private MockWebServiceClient mockClient; + private DepositData depositData; + + @BeforeAll + static void setupMarshaller() { + marshaller = new Jaxb2Marshaller(); + marshaller.setPackagesToScan("de.ozgcloud.eingang.intelliform"); + } + @BeforeEach void initTest() { mockClient = MockWebServiceClient.createClient(applicationContext); @@ -105,16 +115,6 @@ class FormDataEndpointITCase { @Nested class TestSendAntragWithManyAttachments { - private DepositData depositData; - - private static Jaxb2Marshaller marshaller; - - @BeforeAll - static void setupMarshaller() { - marshaller = new Jaxb2Marshaller(); - marshaller.setPackagesToScan("de.ozgcloud.eingang.intelliform"); - } - @BeforeEach void mock() { depositData = DepositDataTestFactory.create(MANY_ATTACHMENTS); @@ -194,30 +194,88 @@ class FormDataEndpointITCase { assertThat(eingang.getZustaendigeStelle().getOrganisationseinheitenId()).isEqualTo(ORGANISATIONSEINHEITEN_ID); } - @SneakyThrows - private void sendDepositAndCaptureCreateVorgang() { - mockClient.sendRequest(RequestCreators.withPayload(createEnvelopeSourceFromDepositData(depositData))) - .andExpect(ResponseMatchers.noFault()) - .andExpect(ResponseMatchers.payload(getResource(RESPONSE))); + } + + @DisplayName("with duplicate keys") + @Nested + class TestWithDuplicateKeys { + @BeforeEach + void mock() { + depositData = DepositDataTestFactory.create(List.of( + withEmptyName(createXmlDaten()), + createXmlDaten(), + withEmptyName(createPdf()), + createPdf(), + withEmptyName(createPng()), + createPng() + )); + } - verify(vorgangRemoteService).createVorgang(formDataCaptor.capture(), grpcEingangCaptor.capture(), organisationsEinheitIdCaptor.capture()); + private Attachment withEmptyName(Attachment attachment) { + attachment.setName(""); + return attachment; } - @SneakyThrows - private Source createEnvelopeSourceFromDepositData(DepositData depositData) { - Deposit deposit = new Deposit(); - deposit.setData(depositData); - - JAXBElement<Deposit> depositJAXBElement = new JAXBElement<>( - new QName(NAMESPACE_URI, PAYLOAD_LOCAL_PART), - Deposit.class, - deposit); - - var stringWriter = new StringWriter(); - marshaller.marshal(depositJAXBElement, new StreamResult(stringWriter)); - return new StreamSource(new StringReader(stringWriter.toString())); + @DisplayName("should keep last entry for representations") + @Test + void shouldKeepLastEntryForRepresentations() { + sendDepositAndCaptureCreateVorgang(); + + var eingang = grpcEingangCaptor.getValue(); + var representationFiles = eingang.getRepresentationsList(); + assertThat(getAttachmentVendorIds(representationFiles)).containsExactly(XML_ATTACHMENT_ID, PDF_ATTACHMENT_ID); + assertThat(getAttachmentFileNames(representationFiles)).containsExactly(XML_NAME, PDF_ATTACHMENT_NAME); } + @DisplayName("should keep last entry for attachments") + @Test + void shouldKeepLastEntryForAttachments() { + sendDepositAndCaptureCreateVorgang(); + + var eingang = grpcEingangCaptor.getValue(); + var attachmentFiles = eingang.getAttachmentsList().stream() + .map(GrpcIncomingFileGroup::getFilesList) + .flatMap(List::stream) + .toList(); + assertThat(getAttachmentVendorIds(attachmentFiles)).containsExactly(PNG_ATTACHMENT_ID); + assertThat(getAttachmentFileNames(attachmentFiles)).containsExactly(PNG_ATTACHMENT_NAME); + } + + private List<String> getAttachmentFileNames(List<GrpcIncomingFile> incomingFileList) { + return incomingFileList.stream() + .map(GrpcIncomingFile::getName) + .toList(); + } + + private List<String> getAttachmentVendorIds(List<GrpcIncomingFile> incomingFileList) { + return incomingFileList.stream() + .map(GrpcIncomingFile::getVendorId) + .toList(); + } + } + + @SneakyThrows + private void sendDepositAndCaptureCreateVorgang() { + mockClient.sendRequest(RequestCreators.withPayload(createEnvelopeSourceFromDepositData(depositData))) + .andExpect(ResponseMatchers.noFault()) + .andExpect(ResponseMatchers.payload(getResource(RESPONSE))); + + verify(vorgangRemoteService).createVorgang(formDataCaptor.capture(), grpcEingangCaptor.capture(), organisationsEinheitIdCaptor.capture()); + } + + @SneakyThrows + private Source createEnvelopeSourceFromDepositData(DepositData depositData) { + Deposit deposit = new Deposit(); + deposit.setData(depositData); + + JAXBElement<Deposit> depositJAXBElement = new JAXBElement<>( + new QName(NAMESPACE_URI, PAYLOAD_LOCAL_PART), + Deposit.class, + deposit); + + var stringWriter = new StringWriter(); + marshaller.marshal(depositJAXBElement, new StreamResult(stringWriter)); + return new StreamSource(new StringReader(stringWriter.toString())); } @DisplayName("Send antrag with attachments")