PatientService { public Patient updateOrCreatePatient(Attributes data, StoreParam storeParam) { AttributeFilter filter = storeParam.getAttributeFilter(Entity.Patient); IDWithIssuer pid = IDWithIssuer.pidWithIssuer(data, null); Patient patient; try { patient = findPatient(pid); Patient mergedWith = patient.getMergedWith(); if (mergedWith != null) throw new PatientMergedException("" + patient + " merged with " + mergedWith); Attributes patientAttrs = patient.getAttributes(); Attributes modified = new Attributes(); if (patientAttrs.updateSelected(data, modified, filter.getSelection())) { if (pid != null && pid.issuer != null) patient.setIssuerOfPatientID( findOrCreateIssuer(pid)); patient.setAttributes(patientAttrs, filter, storeParam.getFuzzyStr()); } } catch (NonUniqueResultException e) { throw new NonUniquePatientException(pid); } catch (NoResultException e) { patient = createNewPatient(data, pid, storeParam); try { findPatient(pid); } catch (NonUniqueResultException e2) { em.remove(patient); return updateOrCreatePatient(data, storeParam); } } return patient; }  Patient findUniqueOrCreatePatient(Attributes data,
            StoreParam storeParam, boolean followMergedWith,
            boolean mergeAttributes); void mergeAttributes(Patient patient, Attributes data,
            StoreParam storeParam); Patient updateOrCreatePatient(Attributes data,
            StoreParam storeParam); void mergePatient(Attributes attrs, Attributes priorAttrs,
            StoreParam storeParam); List<Patient> findPatients(IDWithIssuer pid); Patient deletePatient(IDWithIssuer pid);  }
@Test public void testUpdateOrCreatePatient() throws Exception { patientService.updateOrCreatePatient( attrs(TEST_1234, PID_1234, null), storeParam); Patient pat1234X = patientService.updateOrCreatePatient( attrs(TEST_1234_X, PID_1234, ISSUER_X), storeParam); Patient pat1234 = patientService.findUniqueOrCreatePatient( attrs(TEST_1234, PID_1234, null), storeParam, true, true); assertEquals(pat1234.getPk(), pat1234X.getPk()); assertEquals(TEST_1234_X, pat1234.getAttributes().getString(Tag.PatientName)); }