use of uk.nhs.adaptors.pss.translator.model.EbxmlReference in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandlerTest method When_MIDFragmentRecordHasAnAttachmentLog_Expect_AttachmentLogToBeUpdated.
@Test
public void When_MIDFragmentRecordHasAnAttachmentLog_Expect_AttachmentLogToBeUpdated() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
var parentMid = "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1";
var childMid = "28B31-4245-4AFC-8DA2-8A40623A5101";
InboundMessage message = new InboundMessage();
prepareFragmentIndexMocks(message);
when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog(parentMid, null, true));
when(patientAttachmentLogService.findAttachmentLog(childMid, CONVERSATION_ID)).thenReturn(buildPartialPatientAttachmentLog(childMid, "text/plain"));
when(patientAttachmentLogService.findAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
EbxmlReference reference = new EbxmlReference("First instance is always a payload", "mid:xxxx-xxxx-xxxx-xxxx", "docId");
EbxmlReference reference2 = new EbxmlReference("desc", "mid:28B31-4245-4AFC-8DA2-8A40623A5101", "docId");
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.add(reference);
attachmentReferenceDescription.add(reference2);
when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(attachmentReferenceDescription);
// ACT
copcMessageHandler.handleMessage(message, CONVERSATION_ID);
// ASSERT
verify(patientAttachmentLogService).updateAttachmentLog(patientLogCaptor.capture(), conversationIdCaptor.capture());
PatientAttachmentLog actual = patientLogCaptor.getValue();
assertThat(actual.getBase64()).isTrue();
assertThat(actual.getLargeAttachment()).isTrue();
assertThat(actual.getCompressed()).isFalse();
assertEquals(0, actual.getOrderNum());
assertEquals("047C22B4-613F-47D3-9A72-44A1758464FB", actual.getParentMid());
assertEquals(CONVERSATION_ID, conversationIdCaptor.getValue());
}
use of uk.nhs.adaptors.pss.translator.model.EbxmlReference in project nia-patient-switching-standard-adaptor by NHSDigital.
the class XmlParseUtilService method getEbxmlAttachmentsData.
public List<EbxmlReference> getEbxmlAttachmentsData(InboundMessage inboundMessage) throws SAXException {
List<EbxmlReference> ebxmlAttachmentsIds = new ArrayList<>();
final String REFERENCES_ATTACHMENTS_PATH = "/Envelope/Body/Manifest/Reference";
Document ebXmlDocument = xPathService.parseDocumentFromXml(inboundMessage.getEbXML());
NodeList referencesAttachment = xPathService.getNodes(ebXmlDocument, REFERENCES_ATTACHMENTS_PATH);
for (int index = 0; index < referencesAttachment.getLength(); index++) {
// Reference
Node referenceNode = referencesAttachment.item(index);
if (referenceNode.getNodeType() == Node.ELEMENT_NODE) {
// description
Element referenceElement = (Element) referenceNode;
String description = referenceElement.getTextContent();
String hrefAttribute = referenceElement.getAttribute("xlink:href");
String documentId = referenceElement.getAttribute("eb:id");
ebxmlAttachmentsIds.add(new EbxmlReference(description, hrefAttribute, documentId));
}
}
return ebxmlAttachmentsIds;
}
use of uk.nhs.adaptors.pss.translator.model.EbxmlReference in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandlerTest method When_MIDFragmentRecordDoesNotHaveAnAttachmentLog_Expect_AttachmentLogToBeCreated.
@Test
public void When_MIDFragmentRecordDoesNotHaveAnAttachmentLog_Expect_AttachmentLogToBeCreated() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
var childMid = "28B31-4245-4AFC-8DA2-8A40623A5101";
InboundMessage message = new InboundMessage();
prepareFragmentIndexMocks(message);
when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", null, true));
when(patientAttachmentLogService.findAttachmentLog(childMid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
EbxmlReference reference = new EbxmlReference("First instance is always a payload", "mid:xxxx-xxxx-xxxx-xxxx", "docId");
EbxmlReference reference2 = new EbxmlReference("desc", "mid:28B31-4245-4AFC-8DA2-8A40623A5101", "docId");
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.add(reference);
attachmentReferenceDescription.add(reference2);
when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(attachmentReferenceDescription);
copcMessageHandler.handleMessage(message, CONVERSATION_ID);
verify(patientAttachmentLogService).addAttachmentLog(patientLogCaptor.capture());
PatientAttachmentLog actual = patientLogCaptor.getValue();
assertEquals(childMid, actual.getMid());
assertEquals("E39E79A2-FA96-48FF-9373-7BBCB9D036E7_0.messageattachment", actual.getFilename());
assertEquals("047C22B4-613F-47D3-9A72-44A1758464FB", actual.getParentMid());
assertEquals(1, actual.getPatientMigrationReqId());
assertEquals("text/plain", actual.getContentType());
assertThat(actual.getCompressed()).isFalse();
assertThat(actual.getLargeAttachment()).isTrue();
assertThat(actual.getBase64()).isTrue();
assertEquals(0, actual.getOrderNum());
}
use of uk.nhs.adaptors.pss.translator.model.EbxmlReference in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandlerTest method When_FragmentIndexIsRecievedWithCIDAndMIDParts_Expect_CIDMessageToBeProcessed.
@Test
public void When_FragmentIndexIsRecievedWithCIDAndMIDParts_Expect_CIDMessageToBeProcessed() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
var childMid = "28B31-4245-4AFC-8DA2-8A40623A5101";
var childCid = "435B1171-31F6-4EF2-AD7F-C7E64EEFF357";
InboundMessage message = new InboundMessage();
prepareFragmentIndexWithCidMocks(message);
when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", null, true));
when(patientAttachmentLogService.findAttachmentLog(childMid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog(childCid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
EbxmlReference reference = new EbxmlReference("First instance is always a payload", "mid:xxxx-xxxx-xxxx-xxxx", "docId");
EbxmlReference reference1 = new EbxmlReference("desc", "cid:435B1171-31F6-4EF2-AD7F-C7E64EEFF357", "doc1Id");
EbxmlReference reference2 = new EbxmlReference("desc", "mid:28B31-4245-4AFC-8DA2-8A40623A5101", "doc2Id");
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.add(reference);
attachmentReferenceDescription.add(reference1);
attachmentReferenceDescription.add(reference2);
when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(attachmentReferenceDescription);
copcMessageHandler.handleMessage(message, CONVERSATION_ID);
verify(attachmentHandlerService).storeAttachments(message.getAttachments(), CONVERSATION_ID);
verify(patientAttachmentLogService, times(2)).addAttachmentLog(patientLogCaptor.capture());
PatientAttachmentLog actualCidAttachmentLog = patientLogCaptor.getAllValues().get(0);
PatientAttachmentLog actualMidAttachmentLog = patientLogCaptor.getAllValues().get(1);
assertEquals(childCid, actualCidAttachmentLog.getMid());
assertEquals(childMid, actualMidAttachmentLog.getMid());
}
use of uk.nhs.adaptors.pss.translator.model.EbxmlReference in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandler method extractFragmentsAndLog.
private void extractFragmentsAndLog(PatientMigrationRequest migrationRequest, PatientAttachmentLog parentAttachmentLog, String conversationId, InboundMessage message) throws ParseException, SAXException, ValidationException, InlineAttachmentProcessingException {
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.addAll(xmlParseUtilService.getEbxmlAttachmentsData(message));
// first item is always the message payload reference so skip it
for (var index = 1; index < attachmentReferenceDescription.size(); index++) {
var payloadReference = attachmentReferenceDescription.get(index);
var descriptionString = "";
var messageId = "";
var fileUpload = false;
// in this instance there should only ever be one CID on a fragment index file
if (payloadReference.getHref().contains("cid:")) {
messageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("cid:") + "cid:".length());
descriptionString = message.getAttachments().get(0).getDescription();
// upload the file
attachmentHandlerService.storeAttachments(message.getAttachments(), conversationId);
fileUpload = true;
} else {
var localMessageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("mid:") + "mid:".length());
messageId = localMessageId;
var externalAttachmentResult = message.getExternalAttachments().stream().filter(attachment -> attachment.getMessageId().equals(localMessageId)).findFirst();
if (externalAttachmentResult == null || externalAttachmentResult.stream().count() != 1) {
throw new ValidationException("External Attachment in payload header does not match a received External Attachment ID");
}
var externalAttachment = externalAttachmentResult.get();
descriptionString = externalAttachment.getDescription();
}
PatientAttachmentLog fragmentLog = patientAttachmentLogService.findAttachmentLog(messageId, conversationId);
if (fragmentLog != null) {
updateFragmentLog(fragmentLog, parentAttachmentLog, descriptionString, index - 1, parentAttachmentLog.getLargeAttachment());
patientAttachmentLogService.updateAttachmentLog(fragmentLog, conversationId);
} else {
PatientAttachmentLog newFragmentLog = buildPatientAttachmentLog(messageId, descriptionString, parentAttachmentLog.getMid(), migrationRequest.getId(), index - 1, fileUpload, parentAttachmentLog.getLargeAttachment());
patientAttachmentLogService.addAttachmentLog(newFragmentLog);
}
}
}
Aggregations