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());
}
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());
}
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());
}
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();
}
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);
});
}
}
Aggregations