diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterService.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterService.java index 804551540..ae72dc890 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterService.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterService.java @@ -32,7 +32,10 @@ public class AttachmentReferenceUpdaterService { private final StorageManagerService storageManagerService; - public String updateReferenceToAttachment(List attachments, String conversationId, String payloadStr) + public String replaceOriginalFilenameWithStorageFilenameInEhrExtract( + List attachments, + String conversationId, + String ehrExtract) throws ValidationException, AttachmentNotFoundException, InlineAttachmentProcessingException { if (conversationId == null || conversationId.isEmpty()) { @@ -40,10 +43,10 @@ public String updateReferenceToAttachment(List attach } if (attachments == null) { - return payloadStr; + return ehrExtract; } - String resultPayload = payloadStr; + String resultPayload = ehrExtract; Set expectedFilenames = attachments.stream() .filter(attachment -> !XmlParseUtilService.parseIsSkeleton(attachment.getDescription())) @@ -52,28 +55,27 @@ public String updateReferenceToAttachment(List attach .collect(Collectors.toCollection(HashSet::new)); Pattern pattern = Pattern.compile(wrapWithReferenceElement("file://localhost/([^\"]+)")); - Matcher matcher = pattern.matcher(payloadStr); + Matcher originalFilenameMatch = pattern.matcher(ehrExtract); - while (matcher.find()) { - String decodedFilename = UriUtils.decode(matcher.group(1), StandardCharsets.UTF_8); + while (originalFilenameMatch.find()) { + String decodedFilename = UriUtils.decode(originalFilenameMatch.group(1), StandardCharsets.UTF_8); if (expectedFilenames.contains(decodedFilename)) { String fileLocation = storageManagerService.getFileLocation(decodedFilename, conversationId); - String referenceElement = wrapWithReferenceElement(xmlEscape(fileLocation)); + String updatedReferenceElement = wrapWithReferenceElement(xmlEscape(fileLocation)); - resultPayload = resultPayload.replace(matcher.group(0), referenceElement); + resultPayload = resultPayload.replace(originalFilenameMatch.group(0), updatedReferenceElement); expectedFilenames.remove(decodedFilename); } } if (!expectedFilenames.isEmpty()) { - throw new AttachmentNotFoundException("Unable to find attachment(s): " + expectedFilenames); + throw new AttachmentNotFoundException("Unable to find attachment(s) in EhrExtract: " + expectedFilenames); } return resultPayload; - } private String xmlEscape(String str) { diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingService.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingService.java index 49c908e52..d31f5c1ff 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingService.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingService.java @@ -94,7 +94,7 @@ public void mergeAndBundleMessage(String conversationId) throws JAXBException, J Arrays.asList(bypassPayloadLoadingArray), conversationId ); - var newPayloadStr = attachmentReferenceUpdaterService.updateReferenceToAttachment( + var newPayloadStr = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( messageAttachments, conversationId, inboundMessage.getPayload() diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/task/EhrExtractMessageHandler.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/task/EhrExtractMessageHandler.java index 9df13e8ba..884f3fafe 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/task/EhrExtractMessageHandler.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/task/EhrExtractMessageHandler.java @@ -181,7 +181,7 @@ private void processAndCompleteEHRMessage(InboundMessage inboundMessage, } // upload all references to files in the inbound message - var fileUpdatedPayload = attachmentReferenceUpdaterService.updateReferenceToAttachment( + var fileUpdatedPayload = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( inboundMessage.getAttachments(), conversationId, inboundMessage.getPayload() diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterServiceTests.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterServiceTests.java index 36d92437f..45e4aede1 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterServiceTests.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/AttachmentReferenceUpdaterServiceTests.java @@ -140,7 +140,9 @@ public void When_NoAttachmentsGiven_Expect_NoChanges() var content = getFileContent(PAYLOAD_XML); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(null, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + null, CONVERSATION_ID, content + ); assertEquals(content, result); } @@ -153,7 +155,9 @@ public void When_AttachmentGiven_Expect_GetFileLocationHitOnce() when(storageManagerService.getFileLocation(any(), any())).thenReturn("https://location.com"); - attachmentReferenceUpdaterService.updateReferenceToAttachment(mockAttachment, CONVERSATION_ID, content); + attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockAttachment, CONVERSATION_ID, content + ); verify(storageManagerService, times(1)).getFileLocation(any(), any()); } @@ -165,7 +169,9 @@ public void When_AttachmentGiven_Expect_PayloadXmlChanged() var content = getFileContent(PAYLOAD_XML); when(storageManagerService.getFileLocation(any(), any())).thenReturn("https://location.com"); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(mockAttachment, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockAttachment, CONVERSATION_ID, content + ); assertNotEquals(content, result); assertTrue(result.contains("https://location.com")); @@ -180,7 +186,9 @@ public void When_AttachmentGiven_Expect_ValidXml() var content = getFileContent(FLAT_PAYLOAD_XML); when(storageManagerService.getFileLocation(any(), any())).thenReturn("https://location.com"); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(mockAttachment, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockAttachment, CONVERSATION_ID, content + ); assertNotEquals(content, result); assertTrue(result.contains("https://location.com")); @@ -199,7 +207,9 @@ public void When_MultipleAttachmentsGiven_Expect_GetFileLocationHitMultipleTimes var content = getFileContent(PAYLOAD_XML); when(storageManagerService.getFileLocation(any(), any())).thenReturn("https://location.com"); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(mockThreeAttachments, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockThreeAttachments, CONVERSATION_ID, content + ); verify(storageManagerService, times(mockThreeAttachments.size())).getFileLocation(any(), any()); assertTrue(result.contains("https://location.com")); @@ -215,7 +225,9 @@ public void When_TppStyleAttachmentGiven_Expect_PayloadXmlChanged() var content = getFileContent(FLAT_PAYLOAD_XML); when(storageManagerService.getFileLocation(any(), any())).thenReturn("https://location.com"); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(mockTppStyleAttachment, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockTppStyleAttachment, CONVERSATION_ID, content + ); verify(storageManagerService, times(mockTppStyleAttachment.size())).getFileLocation(any(), any()); assertTrue(result.contains("https://location.com")); @@ -236,7 +248,9 @@ public void When_EncodedUrlsPresent_Expect_PayloadXmlChanged() when(storageManagerService.getFileLocation(eq("8681AF4F-E577-4C8D-A2CE-43CABE3D5FB4_sample_mpeg4.mp4"), any())) .thenReturn("https://location.com/sampleMpeg4.mp4"); - var result = attachmentReferenceUpdaterService.updateReferenceToAttachment(mockEncodedAttachments, CONVERSATION_ID, content); + var result = attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockEncodedAttachments, CONVERSATION_ID, content + ); verify(storageManagerService, times(mockEncodedAttachments.size())).getFileLocation(any(), any()); @@ -248,10 +262,12 @@ public void When_MissingAttachmentGiven_Expect_AttachmentNotFoundException() { var content = getFileContent(PAYLOAD_XML); assertThatThrownBy( - () -> attachmentReferenceUpdaterService.updateReferenceToAttachment(mockMissingAttachment, CONVERSATION_ID, content) + () -> attachmentReferenceUpdaterService.replaceOriginalFilenameWithStorageFilenameInEhrExtract( + mockMissingAttachment, CONVERSATION_ID, content + ) ) .isInstanceOf(AttachmentNotFoundException.class) - .hasMessageContaining("Unable to find attachment(s): [missing_attachment.txt]"); + .hasMessageContaining("Unable to find attachment(s) in EhrExtract: [missing_attachment.txt]"); } private String getFileContent(String filename) { diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingServiceTests.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingServiceTests.java index a50f5e983..f39e2f12a 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingServiceTests.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/service/InboundMessageMergingServiceTests.java @@ -117,7 +117,8 @@ private void prepareMocks(InboundMessage inboundMessage, ArrayList