PatientService { public Patient findUniqueOrCreatePatient(Attributes data, StoreParam storeParam, boolean followMergedWith, boolean mergeAttributes) { IDWithIssuer pid = IDWithIssuer.pidWithIssuer(data, null); if (pid == null) return createNewPatient(data, null, storeParam); Patient patient; try { patient = findPatient(pid); Patient mergedWith = patient.getMergedWith(); if (mergedWith != null) if (followMergedWith) patient = followMergedWith(patient); else throw new PatientMergedException( "" + patient + " merged with " + mergedWith); if (mergeAttributes) mergeAttributes(patient, data, pid, storeParam); } catch (NonUniqueResultException e) { patient = createNewPatient(data, pid, storeParam); } catch (NoResultException e) { patient = createNewPatient(data, pid, storeParam); try { findPatient(pid); } catch (NonUniqueResultException e2) { em.remove(patient); return findUniqueOrCreatePatient( data, storeParam, followMergedWith, mergeAttributes); } } 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 testFindUniqueOrCreatePatient() throws Exception { Patient pat1234 = patientService.findUniqueOrCreatePatient( attrs(TEST_1234, PID_1234, null), storeParam, true, true); Patient pat1234X = patientService.findUniqueOrCreatePatient( attrs(TEST_1234_X, PID_1234, ISSUER_X), storeParam, true, true); Patient pat1234Y = patientService.findUniqueOrCreatePatient( attrs(TEST_1234_Y, PID_1234, ISSUER_Y), storeParam, true, true); assertEquals(pat1234.getPk(), pat1234X.getPk()); assertEquals(TEST_1234, pat1234X.getAttributes().getString(Tag.PatientName)); assertTrue(pat1234X.getPk() != pat1234Y.getPk()); }