AdapterComponentDocRepositoryOrchImpl { public DocumentMetadata saveExtrinsicObject(ExtrinsicObjectType extrinsicObject, RegistryErrorList errorList, Map<String, DataHandler> docMap, boolean hasReplacementRequest) { List<ExternalIdentifierType> externalIdentifiers = extrinsicObject.getExternalIdentifier(); if (externalIdentifiers == null || externalIdentifiers.isEmpty()) { RegistryError error = docRepoHelper.setRegistryError(FIND_A_REQUIRED_ELEMENT, "", DocRepoConstants.XDS_ERROR_CODE_MISSING_DOCUMENT_METADATA, DocRepoConstants.XDS_MISSING_DOCUMENT_METADATA + " extrinsicObject.getExternalIdentifier() element is null or empty."); errorList.getRegistryError().add(error); return null; } DocumentMetadata doc = new DocumentMetadata(); String documentUniqueId = extractMetadataFromExternalIdentifiers(externalIdentifiers, DocRepoConstants.XDS_DOCUMENT_UNIQUE_ID); if (documentUniqueId != null) { LOG.debug("DocumentUniqueId for ExtrinsicObject: {}", documentUniqueId); doc.setDocumentUniqueId(documentUniqueId); doc.setDocumentUri(documentUniqueId); } else { RegistryError error = docRepoHelper.setRegistryError(FIND_A_REQUIRED_ELEMENT, " extractDocumentId", DocRepoConstants.XDS_ERROR_CODE_MISSING_DOCUMENT_METADATA, DocRepoConstants.XDS_MISSING_DOCUMENT_METADATA + " DocumentUniqueId was missing."); errorList.getRegistryError().add(error); } String patientId = extractMetadataFromExternalIdentifiers(externalIdentifiers, DocRepoConstants.XDS_PATIENT_ID); if (patientId != null) { LOG.debug("patientId for ExtrinsicObject: {}", patientId); String patientIdReformatted = PatientIdFormatUtil.stripQuotesFromPatientId(patientId); LOG.debug("Reformatted patientId for ExtrinsicObject: {}", patientIdReformatted); doc.setPatientId(patientIdReformatted); } else { RegistryError error = docRepoHelper.setRegistryError(FIND_A_REQUIRED_ELEMENT, " extractPatientId", DocRepoConstants.XDS_ERROR_CODE_MISSING_DOCUMENT_METADATA, DocRepoConstants.XDS_MISSING_DOCUMENT_METADATA + " PatientId was missing."); errorList.getRegistryError().add(error); } if (errorList.getRegistryError().isEmpty()) { doc.setDocumentUniqueId(getDocumentService().getNextID()); doc.setNewRepositoryUniqueId(getDefaultRepositoryId()); InternationalStringType docTitle = extrinsicObject.getName(); if (docTitle != null && CollectionUtils.isNotEmpty(docTitle.getLocalizedString())) { String docTitleValue = docTitle.getLocalizedString().get(0).getValue(); LOG.debug("DocumentTitle for ExtrinsicObject: {}", docTitleValue); doc.setDocumentTitle(docTitleValue); } InternationalStringType docComments = extrinsicObject.getDescription(); if (docComments != null && CollectionUtils.isNotEmpty(docComments.getLocalizedString())) { String docCommentsValue = docComments.getLocalizedString().get(0).getValue(); LOG.debug("DocumentComments for ExtrinsicObject: {}", docCommentsValue); doc.setComments(docCommentsValue); } LOG.debug("Document mimeType for ExtrinsicObject: {}", extrinsicObject.getMimeType()); doc.setMimeType(extrinsicObject.getMimeType()); List<ClassificationType> classifications = extrinsicObject.getClassification(); setDocumentObjectsFromClassifications(doc, classifications); extractEventCodes(doc, classifications); extractDocumentBinary(extrinsicObject, docMap, doc); extractFromDocumentSlots(doc, extrinsicObject.getSlot()); String availabilityStatus = extrinsicObject.getStatus(); LOG.debug("Availability status received in message: {}", availabilityStatus); doc.setAvailablityStatus(StringUtils.isEmpty(availabilityStatus) ? DocRepoConstants.XDS_AVAILABLILTY_STATUS_APPROVED : availabilityStatus); doc.setStatus(DocRepoConstants.XDS_STATUS); saveDocument(doc, hasReplacementRequest, documentUniqueId, errorList); } else { return null; } return doc; } AdapterComponentDocRepositoryOrchImpl(); DocumentService getDocumentService(); LargeFileUtils getLargeFileUtils(); UTCDateUtil getDateUtil(); RetrieveDocumentSetResponseType documentRepositoryRetrieveDocumentSet(RetrieveDocumentSetRequestType body); RegistryResponseType registerDocumentSet(RegisterDocumentSetRequestType body); RegistryResponseType documentRepositoryProvideAndRegisterDocumentSet(
        ProvideAndRegisterDocumentSetRequestType body); DocumentMetadata saveExtrinsicObject(ExtrinsicObjectType extrinsicObject, RegistryErrorList errorList,
        Map<String, DataHandler> docMap, boolean hasReplacementRequest);  }
@Test public void testSaveExtrinsicObject_SuccessALL() throws IOException { final ExtrinsicObjectType extrinsicObject = mock(ExtrinsicObjectType.class); List<ExternalIdentifierType> externalIdentifierList = new ArrayList<>(); ExternalIdentifierType externalIdentifier = mock(ExternalIdentifierType.class); externalIdentifierList.add(externalIdentifier); InternationalStringType name = mock(InternationalStringType.class); List<LocalizedStringType> nameValueList = mock(ArrayList.class); LocalizedStringType nameValue = mock(LocalizedStringType.class); List<SlotType1> documentSlots = new ArrayList<>(); SlotType1 slot = mock(SlotType1.class); documentSlots.add(slot); ValueListType valueListType = mock(ValueListType.class); List<String> valueList = mock(ArrayList.class); RegistryErrorList errorList = mock(RegistryErrorList.class); final String DOC_UNIQUE_ID = "Doc_ID_1"; final String DOC_UNIQUE_PERSISTED_ID = "CONNECT0"; final String PATIENT_ID = "'Patient_ID_1'"; final String PATIENT_ID_NO_QUOTES = "Patient_ID_1"; final String DOCUMENT_TITLE = "Document_Title"; final String DOCUMENT_DESCRIPTION = "Document Description"; final String MIME_TYPE = "Mime Type"; final String XDS_INTENDED_RECIPIENT = "Organization|Person"; final String XDS_INTENDED_ORG = "Organization"; final String XDS_INTENDED_PERSON = "Person"; final String XDS_LANGUAGE_CODE = "Language Code"; final String XDS_LEGAL_AUTHENTICATOR = "Legal Authenticator"; final String CREATION_TIME_STRING = "100000"; final long CREATION_TIME = 100000L; Date createTimeDate = new Date(); createTimeDate.setTime(CREATION_TIME); final String START_TIME_STRING = "120000"; final String STOP_TIME_STRING = "240000"; final long START_TIME = 120000L; Date startTimeDate = new Date(); startTimeDate.setTime(START_TIME); final long STOP_TIME = 240000L; Date stopTimeDate = new Date(); stopTimeDate.setTime(STOP_TIME); final String XDS_PATIENT_ID = "'Patient_ID_2'"; final String XDS_PATIENT_ID_NO_QUOTES = "Patient_ID_2"; HashMap<String, DataHandler> docMap = mock(HashMap.class); DataHandler dataHandler = mock(DataHandler.class); final String RAW_DATA = "Raw Data"; final String STATUS = "Status"; AdapterComponentDocRepositoryOrchImpl docRepo = getDocRepoWithMutipleOverrides(); when(extrinsicObject.getExternalIdentifier()).thenReturn(externalIdentifierList); when(externalIdentifier.getName()).thenReturn(name); when(name.getLocalizedString()).thenReturn(nameValueList); when(nameValueList.get(0)).thenReturn(nameValue); when(nameValue.getValue()).thenReturn(DocRepoConstants.XDS_DOCUMENT_UNIQUE_ID, DocRepoConstants.XDS_PATIENT_ID, DOCUMENT_TITLE, DOCUMENT_DESCRIPTION); when(externalIdentifier.getValue()).thenReturn(DOC_UNIQUE_ID, PATIENT_ID); when(extrinsicObject.getName()).thenReturn(name); when(extrinsicObject.getDescription()).thenReturn(name); when(extrinsicObject.getMimeType()).thenReturn(MIME_TYPE); when(extrinsicObject.getSlot()).thenReturn(documentSlots); when(slot.getName()).thenReturn(DocRepoConstants.XDS_INTENDED_RECIPIENT_SLOT, DocRepoConstants.XDS_LANGUAGE_CODE_SLOT, DocRepoConstants.XDS_LEGAL_AUTHENTICATOR_SLOT, DocRepoConstants.XDS_CREATION_TIME_SLOT, DocRepoConstants.XDS_START_TIME_SLOT, DocRepoConstants.XDS_STOP_TIME_SLOT, DocRepoConstants.XDS_SOURCE_PATIENT_ID_SLOT); when(slot.getValueList()).thenReturn(valueListType); when(valueListType.getValue()).thenReturn(valueList); when(valueList.size()).thenReturn(1); when(valueList.get(0)).thenReturn(XDS_INTENDED_RECIPIENT, XDS_LANGUAGE_CODE, XDS_LEGAL_AUTHENTICATOR, CREATION_TIME_STRING, START_TIME_STRING, STOP_TIME_STRING, XDS_PATIENT_ID); when(utcDateUtil.parseUTCDateOptionalTimeZone(CREATION_TIME_STRING)).thenReturn(createTimeDate); when(utcDateUtil.parseUTCDateOptionalTimeZone(START_TIME_STRING)).thenReturn(startTimeDate); when(utcDateUtil.parseUTCDateOptionalTimeZone(STOP_TIME_STRING)).thenReturn(stopTimeDate); when(extrinsicObject.getId()).thenReturn(DOC_UNIQUE_ID); when(docMap.get(DOC_UNIQUE_ID)).thenReturn(dataHandler); when(largeFileUtils.convertToBytes(dataHandler)).thenReturn(RAW_DATA.getBytes()); when(extrinsicObject.getStatus()).thenReturn(STATUS); when(docService.getNextID()).thenReturn(DOC_UNIQUE_PERSISTED_ID); DocumentMetadata doc = docRepo.saveExtrinsicObject(extrinsicObject, errorList, docMap, true); assertEquals(PATIENT_ID_NO_QUOTES, doc.getPatientId()); assertEquals(DOCUMENT_TITLE, doc.getDocumentTitle()); assertEquals(DOCUMENT_DESCRIPTION, doc.getComments()); assertEquals(MIME_TYPE, doc.getMimeType()); assertEquals(XDS_INTENDED_ORG, doc.getIntendedRecipientOrganization()); assertEquals(XDS_INTENDED_PERSON, doc.getIntendedRecipientPerson()); assertEquals(XDS_LANGUAGE_CODE, doc.getLanguageCode()); assertEquals(XDS_LEGAL_AUTHENTICATOR, doc.getLegalAuthenticator()); assertEquals(CREATION_TIME, doc.getCreationTime().getTime()); assertEquals(START_TIME, doc.getServiceStartTime().getTime()); assertEquals(STOP_TIME, doc.getServiceStopTime().getTime()); assertEquals(XDS_PATIENT_ID_NO_QUOTES, doc.getSourcePatientId()); assertTrue(Arrays.equals(doc.getRawData(), RAW_DATA.getBytes())); assertEquals(STATUS, doc.getAvailablityStatus()); assertEquals(DocRepoConstants.XDS_STATUS, doc.getStatus()); assertEquals(DOC_UNIQUE_PERSISTED_ID, doc.getDocumentUniqueId()); assertEquals((Integer) RAW_DATA.getBytes().length, doc.getSize()); }