diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingService.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingService.java index 7d26af0c5..29a478f9f 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingService.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingService.java @@ -75,7 +75,7 @@ private InboundMessage insertSkeletonIntoInboundMessagePayload(PatientAttachment throw new IllegalArgumentException("inboundMessage does not contain a skeleton attachment reference"); } - var skeletonDocumentId = ebxmlSkeletonReference.get().getDocumentId(); + var skeletonDocumentId = ebxmlSkeletonReference.get().getDocumentId().replaceFirst("^_", ""); var payloadXml = xPathService.parseDocumentFromXml(inboundMessage.getPayload()); var valueNodes = xPathService.getNodes(payloadXml, "//*/@*[.='" + skeletonDocumentId + "']/parent::*/parent::*"); var payloadNodeToReplace = valueNodes.item(0); diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingServiceTests.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingServiceTests.java index 186126ba9..ebfe1e2c9 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingServiceTests.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/SkeletonProcessingServiceTests.java @@ -5,9 +5,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.when; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doThrow; import static uk.nhs.adaptors.common.util.FileUtil.readResourceAsString; import java.nio.charset.StandardCharsets; @@ -253,6 +255,43 @@ PatientAttachmentLog createSkeletonPatientAttachmentLog() { .build(); } + @Test + void When_SkeletonDocumentIdHasLeadingUnderscore_Expect_UnderscoreIsStrippedBeforeXPathQuery() + throws SAXException, TransformerException { + + var inboundMessage = new InboundMessage(); + var attachmentLog = createSkeletonPatientAttachmentLog(); + + inboundMessage.setPayload(readInboundMessagePayloadFromFile()); + inboundMessage.setEbXML(readInboundMessageEbXmlFromFile()); + + var reference = new EbxmlReference( + "First instance is always a payload", + "mid:1", + "_C3866E77-41E2-4593-A133-AB622F54684F" + ); + var ebXmlAttachments = List.of(reference); + var fileAsBytes = readInboundMessageSkeletonPayloadFromFile().getBytes(StandardCharsets.UTF_8); + + when(attachmentHandlerService.getAttachment(any(), any())).thenReturn(fileAsBytes); + when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(ebXmlAttachments); + when(xPathService.parseDocumentFromXml(any())).thenReturn(ebXmlDocument); + when(xPathService.getNodes(any(), any())).thenReturn(nodeList); + when(nodeList.item(0)).thenReturn(node); + when(ebXmlDocument.getElementsByTagName("*")).thenReturn(nodeList); + when(xmlParseUtilService.getStringFromDocument(any())).thenReturn(inboundMessage.getPayload()); + when(node.getOwnerDocument()).thenReturn(ebXmlDocument); + when(node.getParentNode()).thenReturn(node); + + skeletonProcessingService.updateInboundMessageWithSkeleton( + attachmentLog, inboundMessage, CONVERSATION_ID); + + var expectedDocumentId = "C3866E77-41E2-4593-A133-AB622F54684F"; + var expectedXPath = "//*/@*[.='" + expectedDocumentId + "']/parent::*/parent::*"; + + verify(xPathService).getNodes(any(), eq(expectedXPath)); + } + @SneakyThrows private String readInboundMessagePayloadFromFile() { return readResourceAsString("/xml/inbound_message_payload.xml").replace("{{nhsNumber}}", NHS_NUMBER);