/** * Will only add PatientIdentifiers in this list that this patient does not have already * * @param patientIdentifiers */ public void addIdentifiers(Collection<PatientIdentifier> patientIdentifiers) { for (PatientIdentifier identifier : patientIdentifiers) { addIdentifier(identifier); } }
/** * @see Patient#addIdentifier(PatientIdentifier) */ @Test public void addIdentifier_shouldNotFailWithNullIdentifiersList() { Patient p = new Patient(); p.setIdentifiers(null); p.addIdentifier(new PatientIdentifier()); }
@Test public void getPatients_shouldFindIdentifierIgnoringCase() { Patient patient = patientService.getPatient(2); PatientIdentifier patientIdentifier = new PatientIdentifier("AS_567", patientService.getPatientIdentifierType(5), locationService.getLocation(1)); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); updateSearchIndex(); List<Patient> patients = patientService.getPatients("as_567"); assertThat(patients, contains(patient)); }
@Test public void processDeath_shouldMapValuesAndSavePatient() throws Exception { // given final Date dateDied = new Date(); final Concept causeOfDeath = new Concept(2); when(conceptService.getConcept(anyString())).thenReturn(new Concept()); when(locationService.getDefaultLocation()).thenReturn(new Location()); UserContext userContext = mock(UserContext.class); this.contextMockHelper.setUserContext(userContext); when(userContext.hasPrivilege(anyString())).thenReturn(true); ArgumentCaptor<Patient> argumentCaptor = ArgumentCaptor.forClass(Patient.class); when(patientDaoMock.savePatient(argumentCaptor.capture())).thenReturn(new Patient()); // when final Patient patient = new Patient(); patient.addIdentifier(new PatientIdentifier("an identifier", new PatientIdentifierType(1234), new Location())); patientService.processDeath(patient, dateDied, causeOfDeath, "unknown"); // then final Patient savedPatient = argumentCaptor.getValue(); assertEquals(true, savedPatient.getDead()); assertEquals(dateDied, savedPatient.getDeathDate()); assertEquals(causeOfDeath, savedPatient.getCauseOfDeath()); }
@Test public void checkPatientIdentifiers_shouldIgnoreAbsenceOfActiveIdentifiersGivenPatientIsVoided() throws Exception { // given Patient patient = new Patient(); patient.setVoided(true); patient.addIdentifier(createVoidedPatientIdentifier()); // when patientService.checkPatientIdentifiers(patient); // no exception }
@Test(expected = InsufficientIdentifiersException.class) public void checkPatientIdentifiers_shouldThrowInsufficientIdentifiersErrorGivenPatientHasNoActiveIdentifiers() throws Exception { // given Patient patient = new Patient(); patient.setVoided(false); patient.addIdentifier(createVoidedPatientIdentifier()); // when patientService.checkPatientIdentifiers(patient); // this patient only has a voided identifier, so saving is not allowed > exception }
/** * @see Patient#addIdentifier(PatientIdentifier) */ @Test public void addIdentifier_shouldNotAddIdentifierThatIsInListAlready() { Patient p = new Patient(); assertNotNull(p.getIdentifiers()); PatientIdentifier pa1 = new PatientIdentifier(); pa1.setIdentifier("firsttest"); pa1.setIdentifierType(new PatientIdentifierType(1)); pa1.setDateCreated(new Date()); pa1.setVoided(false); p.addIdentifier(pa1); // adding the same identifier should not increment the size p.addIdentifier(pa1); assertTrue( "There should be 1 identifier in the patient object but there is actually : " + p.getIdentifiers().size(), p .getIdentifiers().size() == 1); }
/** * @see PatientService#checkPatientIdentifiers(Patient) */ @Test(expected = BlankIdentifierException.class) public void checkPatientIdentifiers_shouldRemoveIdentifierAndThrowErrorWhenPatientHasBlankPatientIdentifier() throws Exception { Patient patient = new Patient(); PatientIdentifier patientIdentifier = new PatientIdentifier(); patientIdentifier.setIdentifierType(Context.getPatientService().getAllPatientIdentifierTypes(false).get(0)); patient.addIdentifier(patientIdentifier); // Should throw blank identifier exception Context.getPatientService().checkPatientIdentifiers(patient); }
/** * @see PatientService#getPatients(String,Integer,Integer) */ @Test public void getPatients_shouldFindAPatientsWithAMatchingIdentifierWithNoDigits() throws Exception { final String identifier = "XYZ"; Patient patient = patientService.getPatient(2); Assert.assertEquals(0, patientService.getPatients(identifier, null, null).size()); PatientIdentifier pId = new PatientIdentifier(identifier, patientService.getPatientIdentifierType(5), locationService.getLocation(1)); patient.addIdentifier(pId); patientService.savePatient(patient); updateSearchIndex(); Assert.assertEquals(1, patientService.getPatients(identifier).size()); }
/** * @see PatientService#getCountOfPatients(String) */ @Test public void getCountOfPatients_shouldReturnTheRightCountOfPatientsWithAMatchingIdentifierWithNoDigits() throws Exception { final String identifier = "XYZ"; Patient patient = patientService.getPatient(2); Assert.assertEquals(0, patientService.getCountOfPatients(identifier).intValue()); PatientIdentifier pId = new PatientIdentifier(identifier, patientService.getPatientIdentifierType(5), locationService.getLocation(1)); patient.addIdentifier(pId); patientService.savePatient(patient); updateSearchIndex(); Assert.assertEquals(1, patientService.getCountOfPatients(identifier).intValue()); }
/** * @see HibernatePatientDAO#getPatients(String, Integer, Integer) */ @Test public void getPatients_shouldNotGetExcessPatientsOnIdentifierAndNameMatch() { Patient patient = patientService.getPatient(2); PatientIdentifier patientIdentifier = new PatientIdentifier("alpha", patientService.getPatientIdentifierType(5), Context .getLocationService().getLocation(1)); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); updateSearchIndex(); List<Patient> patients = dao.getPatients("alpha", 0 , null); Assert.assertEquals(patient, patients.get(0)); Assert.assertEquals(3,patients.size()); List<Patient> first_patient = dao.getPatients("alpha", 0, 1); Assert.assertEquals(patient, first_patient.get(0)); Assert.assertEquals(1,first_patient.size()); List<Patient> two_patients_only = dao.getPatients("alpha", 0, 2); Assert.assertEquals(2, two_patients_only.size()); List<Patient> second_patient = dao.getPatients("alpha", 1, 1); Assert.assertEquals(1, second_patient.size()); }
/** * @see PatientService#checkPatientIdentifiers(Patient) */ @Test(expected = DuplicateIdentifierException.class) public void checkPatientIdentifiers_shouldThrowErrorWhenPatientHasIdenticalIdentifiers() throws Exception { PatientIdentifierType patientIdentifierType = Context.getPatientService().getAllPatientIdentifierTypes(false).get(0); Patient patient = new Patient(); // Identifier #1 PatientIdentifier patientIdentifier1 = new PatientIdentifier(); patientIdentifier1.setIdentifier("123456789"); patientIdentifier1.setLocation( new Location(2) ); patientIdentifier1.setIdentifierType(patientIdentifierType); patient.addIdentifier(patientIdentifier1); // Identifier #2 PatientIdentifier patientIdentifier2 = new PatientIdentifier(); patientIdentifier2.setIdentifier("123456789"); patientIdentifier2.setIdentifierType(patientIdentifierType); patientIdentifier2.setLocation( new Location(2) ); patient.addIdentifier(patientIdentifier2); patientService.checkPatientIdentifiers(patient); }
@Test public void getPatients_shouldGetNewPatientByIdentifierStartMatch() { String oldPropertyValue = globalPropertiesTestHelper.setGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE, OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_START); Patient patient = patientService.getPatient(2); PatientIdentifier patientIdentifier = new PatientIdentifier("OM292", patientService.getPatientIdentifierType(5), Context .getLocationService().getLocation(1)); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); updateSearchIndex(); //Check for partial identifier match List<Patient> patients = dao.getPatients("OM", false, 0, null); Assert.assertEquals(1, patients.size()); Assert.assertEquals("OM292", patients.get(0).getPatientIdentifier(5).toString()); if (oldPropertyValue != null) { globalPropertiesTestHelper.setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE, oldPropertyValue); } else { globalPropertiesTestHelper.purgeGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE); } }
@Test public void getPatients_shouldNotGetNewPatientByWrongIdentifierStartMatch() { String oldPropertyValue = globalPropertiesTestHelper.setGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE, OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_START); Patient patient = patientService.getPatient(2); PatientIdentifier patientIdentifier = new PatientIdentifier("OM292", patientService.getPatientIdentifierType(5), Context .getLocationService().getLocation(1)); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); updateSearchIndex(); List<Patient> patients = dao.getPatients("OM78", false, 0, null); Assert.assertEquals(0, patients.size()); if (oldPropertyValue != null) { globalPropertiesTestHelper.setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE, oldPropertyValue); } else { globalPropertiesTestHelper.purgeGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE); } }
/** * @see PatientService#getPatients(String, String, java.util.List, boolean) */ @Test public void getPatients_shouldSupportSimpleRegex() throws Exception { Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "^0*@SEARCH@([A-Z]+-[0-9])?$")); PatientIdentifier identifier = new PatientIdentifier("1234-4", new PatientIdentifierType(1), new Location(1)); identifier.setCreator(new User(1)); identifier.setDateCreated(new Date()); Patient patient = Context.getPatientService().getPatient(2); patient.addIdentifier(identifier); Context.getPatientService().savePatient(patient); updateSearchIndex(); assertEquals(1, Context.getPatientService().getPatients("1234-4").size()); }
/** * @see PatientService#getPatients(String, String, java.util.List, boolean) */ @Test public void getPatients_shouldSupportPatternUsingLastDigitAsCheckDigit() throws Exception { Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_PATTERN, "@SEARCH@,0@SEARCH@,@SEARCH-1@-@CHECKDIGIT@,0@SEARCH-1@-@CHECKDIGIT@")); // "^(0*@SEARCH-1@-@CHECKDIGIT@)$")); PatientIdentifier identifier = new PatientIdentifier("1234-4", new PatientIdentifierType(1), new Location(1)); identifier.setCreator(new User(1)); identifier.setDateCreated(new Date()); Patient patient = Context.getPatientService().getPatient(2); patient.addIdentifier(identifier); Context.getPatientService().savePatient(patient); updateSearchIndex(); assertEquals(1, Context.getPatientService().getPatients("12344").size()); assertEquals(1, Context.getPatientService().getPatients("1234-4").size()); }
/** * https://tickets.openmrs.org/browse/TRUNK-3728 * * @see PatientService#savePatient(Patient) */ @Test public void savePatient_shouldNotThrowNonUniqueObjectExceptionWhenCalledWithPersonPromotedToPatient() throws Exception { Person person = personService.getPerson(1); Patient patient = patientService.getPatientOrPromotePerson(person.getPersonId()); PatientIdentifier patientIdentifier = new PatientIdentifier("some identifier", new PatientIdentifierType(2), new Location(1)); patientIdentifier.setPreferred(true); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); }
/** * Regression test for ticket #1375: org.hibernate.NonUniqueObjectException caused by * PatientIdentifierValidator Manually construct a patient with a correctly-matching patientId * and patient identifier with validator. Calling PatientService.savePatient on that patient * leads to a call to PatientIdentifierValidator.validateIdentifier which used to load the * Patient for that identifier into the hibernate session, leading to a NonUniqueObjectException * when the calling saveOrUpdate on the manually constructed Patient. * * @see PatientService#savePatient(Patient) */ @Test public void savePatient_shouldNotThrowANonUniqueObjectExceptionWhenCalledWithAHandConstructedPatientRegression1375() { Patient patient = new Patient(); patient.setGender("M"); patient.setPatientId(2); patient.addName(new PersonName("This", "Isa", "Test")); PatientIdentifier patientIdentifier = new PatientIdentifier("101-6", new PatientIdentifierType(1), new Location(1)); patientIdentifier.setPreferred(true); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); }
/** * Regression test for http://dev.openmrs.org/ticket/1375 * * @see PatientService#savePatient(Patient) */ @Test public void savePatient_shouldNotThrowANonUniqueObjectExceptionWhenCalledWithAHandConstructedPatient() throws Exception { Patient patient = new Patient(); patient.setGender("M"); patient.setPatientId(2); // patient.setCreator(new User(1)); // patient.setDateCreated date_created="2005-09-22 00:00:00.0" // changed_by="1" date_changed="2008-08-18 12:29:59.0" patient.addName(new PersonName("This", "Isa", "Test")); PatientIdentifier patientIdentifier = new PatientIdentifier("101-6", new PatientIdentifierType(1), new Location(1)); patientIdentifier.setPreferred(true); patient.addIdentifier(patientIdentifier); Context.getPatientService().savePatient(patient); }
/** * @see PatientService#savePatient(Patient) */ @Test public void savePatient_shouldSetThePreferredNameAddressAndIdentifierIfNoneIsSpecified() throws Exception { Patient patient = new Patient(); patient.setGender("M"); PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(2), locationService.getLocation(1)); patient.addIdentifier(identifier); PersonName name = new PersonName("givenName", "middleName", "familyName"); patient.addName(name); PersonAddress address = new PersonAddress(); address.setAddress1("some address"); patient.addAddress(address); Context.getPatientService().savePatient(patient); Assert.assertTrue(identifier.getPreferred()); Assert.assertTrue(name.getPreferred()); Assert.assertTrue(address.getPreferred()); }