Search in sources :

Example 1 with PatientAttachmentLog

use of uk.nhs.adaptors.connector.model.PatientAttachmentLog 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 PatientAttachmentLog

use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.

the class COPCMessageHandlerTest method When_CIDMessageFileIsProcessed_Expect_AttachmentLogUploadedColumnIsSetToTrue.

@Test
public void When_CIDMessageFileIsProcessed_Expect_AttachmentLogUploadedColumnIsSetToTrue() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
    InboundMessage message = new InboundMessage();
    prepareExpectedFragmentMocks(message);
    when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
    copcMessageHandler.handleMessage(message, CONVERSATION_ID);
    verify(patientAttachmentLogService).updateAttachmentLog(patientLogCaptor.capture(), conversationIdCaptor.capture());
    PatientAttachmentLog actual = patientLogCaptor.getAllValues().get(0);
    assertThat(actual.getUploaded()).isTrue();
    assertEquals(CONVERSATION_ID, conversationIdCaptor.getValue());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) Test(org.junit.jupiter.api.Test)

Example 3 with PatientAttachmentLog

use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.

the class COPCMessageHandlerTest method When_CIDFragmentPartIsReceivedBeforeFragmentIndex_Expect_PartialLogToBeCreated.

@Test
public void When_CIDFragmentPartIsReceivedBeforeFragmentIndex_Expect_PartialLogToBeCreated() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
    // fragmentAttachmentLog = buildPatientAttachmentLog("28B31-4245-4AFC-8DA2-8A40623A5101", "", 0, true);
    // ARRANGE
    var messageId = "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1";
    when(patientAttachmentLogService.findAttachmentLog(messageId, CONVERSATION_ID)).thenReturn(null).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", null, true));
    when(patientAttachmentLogService.findAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
    InboundMessage message = new InboundMessage();
    prepareFragmentMocks(message);
    // ACT
    copcMessageHandler.handleMessage(message, CONVERSATION_ID);
    // ASSERT
    verify(patientAttachmentLogService).addAttachmentLog(patientLogCaptor.capture());
    PatientAttachmentLog actual = patientLogCaptor.getAllValues().get(0);
    assertThat(actual.getUploaded()).isTrue();
    assertEquals("047C22B4-613F-47D3-9A72-44A1758464FB", actual.getMid());
    assertEquals("E39E79A2-FA96-48FF-9373-7BBCB9D036E7_1.messageattachment", actual.getFilename());
    assertEquals("xml/text", actual.getContentType());
    assertEquals(1, actual.getPatientMigrationReqId());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) Test(org.junit.jupiter.api.Test)

Example 4 with PatientAttachmentLog

use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.

the class AttachmentHandlerService method buildSingleFileStringFromPatientAttachmentLogs.

public String buildSingleFileStringFromPatientAttachmentLogs(List<PatientAttachmentLog> attachmentLogs, String conversationId) {
    StringBuilder combinedFile = new StringBuilder("");
    for (PatientAttachmentLog log : attachmentLogs) {
        var filename = log.getFilename();
        var attachmentBytes = getAttachment(filename, conversationId);
        combinedFile.append(new String(attachmentBytes, StandardCharsets.UTF_8));
    }
    return combinedFile.toString();
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog)

Example 5 with PatientAttachmentLog

use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.

the class COPCMessageHandler method checkAndMergeFileParts.

public void checkAndMergeFileParts(InboundMessage inboundMessage, String conversationId) throws SAXException, AttachmentLogException, ValidationException, InlineAttachmentProcessingException {
    Document ebXmlDocument = xPathService.parseDocumentFromXml(inboundMessage.getEbXML());
    var inboundMessageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
    var currentAttachmentLog = patientAttachmentLogService.findAttachmentLog(inboundMessageId, conversationId);
    if (currentAttachmentLog == null) {
        throw new AttachmentLogException("Given COPC message is missing an attachment log");
    }
    // if a message has arrived early, it will not have a parent ID so we can cancel early.
    if (currentAttachmentLog.getParentMid() == null) {
        return;
    }
    var conversationAttachmentLogs = patientAttachmentLogService.findAttachmentLogs(conversationId);
    var attachmentLogFragments = conversationAttachmentLogs.stream().sorted(Comparator.comparingInt(PatientAttachmentLog::getOrderNum)).filter(log -> !(log.getParentMid() == null) && log.getParentMid().equals(currentAttachmentLog.getParentMid())).toList();
    var parentLogMessageId = attachmentLogFragments.size() == 1 ? currentAttachmentLog.getMid() : currentAttachmentLog.getParentMid();
    attachmentLogFragments = conversationAttachmentLogs.stream().sorted(Comparator.comparingInt(PatientAttachmentLog::getOrderNum)).filter(log -> !(log.getParentMid() == null) && log.getParentMid().equals(parentLogMessageId)).toList();
    var allFragmentsHaveUploaded = attachmentLogFragments.stream().allMatch(PatientAttachmentLog::getUploaded);
    if (allFragmentsHaveUploaded) {
        String payload = attachmentHandlerService.buildSingleFileStringFromPatientAttachmentLogs(attachmentLogFragments, conversationId);
        var parentLogFile = conversationAttachmentLogs.stream().filter(log -> log.getMid().equals(parentLogMessageId)).findAny().orElse(null);
        var mergedLargeAttachment = createNewLargeAttachmentInList(parentLogFile, payload);
        attachmentHandlerService.storeAttachments(mergedLargeAttachment, conversationId);
        var updatedLog = PatientAttachmentLog.builder().mid(parentLogFile.getMid()).uploaded(true).build();
        patientAttachmentLogService.updateAttachmentLog(updatedLog, conversationId);
        attachmentLogFragments.forEach((PatientAttachmentLog log) -> {
            attachmentHandlerService.removeAttachment(log.getFilename(), conversationId);
            patientAttachmentLogService.deleteAttachmentLog(log.getMid(), conversationId);
        });
    }
}
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) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) Document(org.w3c.dom.Document) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException)

Aggregations

PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)11 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)8 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 EbxmlReference (uk.nhs.adaptors.pss.translator.model.EbxmlReference)5 XmlUnmarshallUtil.unmarshallString (uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString)5 ParseException (java.text.ParseException)4 Document (org.w3c.dom.Document)4 SAXException (org.xml.sax.SAXException)4 PatientMigrationRequest (uk.nhs.adaptors.connector.model.PatientMigrationRequest)4 InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ValidationException (javax.xml.bind.ValidationException)3 COPCIN000001UK01Message (org.hl7.v3.COPCIN000001UK01Message)3 AttachmentNotFoundException (uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException)3 BundleMappingException (uk.nhs.adaptors.pss.translator.exception.BundleMappingException)3 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 List (java.util.List)2 JAXBException (javax.xml.bind.JAXBException)2