Search in sources :

Example 1 with EbxmlReference

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());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) Test(org.junit.jupiter.api.Test)

Example 2 with EbxmlReference

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;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference)

Example 3 with EbxmlReference

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());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) Test(org.junit.jupiter.api.Test)

Example 4 with EbxmlReference

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());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) Test(org.junit.jupiter.api.Test)

Example 5 with EbxmlReference

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) NackAckPreparationService(uk.nhs.adaptors.pss.translator.service.NackAckPreparationService) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) COPCIN000001UK01Message(org.hl7.v3.COPCIN000001UK01Message) ArrayList(java.util.ArrayList) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) Document(org.w3c.dom.Document) ParseException(java.text.ParseException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) InboundMessageMergingService(uk.nhs.adaptors.pss.translator.service.InboundMessageMergingService) PatientAttachmentLogService(uk.nhs.adaptors.connector.service.PatientAttachmentLogService) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) AttachmentHandlerService(uk.nhs.adaptors.pss.translator.service.AttachmentHandlerService) XPathService(uk.nhs.adaptors.pss.translator.service.XPathService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) SAXException(org.xml.sax.SAXException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) EHR_EXTRACT_CANNOT_BE_PROCESSED(uk.nhs.adaptors.pss.translator.model.NACKReason.EHR_EXTRACT_CANNOT_BE_PROCESSED) ValidationException(javax.xml.bind.ValidationException) PatientMigrationRequestDao(uk.nhs.adaptors.connector.dao.PatientMigrationRequestDao) Comparator(java.util.Comparator) XmlParseUtilService(uk.nhs.adaptors.pss.translator.util.XmlParseUtilService) ValidationException(javax.xml.bind.ValidationException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference)

Aggregations

ArrayList (java.util.ArrayList)5 EbxmlReference (uk.nhs.adaptors.pss.translator.model.EbxmlReference)5 PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)4 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)4 Test (org.junit.jupiter.api.Test)3 Document (org.w3c.dom.Document)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ParseException (java.text.ParseException)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 JAXBException (javax.xml.bind.JAXBException)1 ValidationException (javax.xml.bind.ValidationException)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 COPCIN000001UK01Message (org.hl7.v3.COPCIN000001UK01Message)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1