Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading