Skip to content

Commit 539307a

Browse files
committedMay 15, 2013
reverting TRUNK-3822: Search for patient by identifier fails when identifier is made of only letters
1 parent 57525b1 commit 539307a

File tree

8 files changed

+124
-202
lines changed

8 files changed

+124
-202
lines changed
 

‎api/src/main/java/org/openmrs/api/PatientService.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
*/
1414
package org.openmrs.api;
1515

16-
import java.util.Collection;
17-
import java.util.Date;
18-
import java.util.List;
19-
import java.util.Set;
20-
2116
import org.openmrs.Concept;
2217
import org.openmrs.Location;
2318
import org.openmrs.Patient;
@@ -35,6 +30,11 @@
3530
import org.openmrs.validator.PatientIdentifierValidator;
3631
import org.springframework.transaction.annotation.Transactional;
3732

33+
import java.util.Collection;
34+
import java.util.Date;
35+
import java.util.List;
36+
import java.util.Set;
37+
3838
/**
3939
* Contains methods pertaining to Patients in the system
4040
*
@@ -592,7 +592,6 @@ public PatientIdentifierType unretirePatientIdentifierType(PatientIdentifierType
592592
* @return a list of matching Patients
593593
* @throws APIException
594594
* @since 1.8
595-
* @should find a patients with a matching identifier with no digits
596595
*/
597596
@Transactional(readOnly = true)
598597
@Authorized( { PrivilegeConstants.VIEW_PATIENTS })
@@ -1011,7 +1010,6 @@ public void saveCauseOfDeathObs(Patient patient, Date dateDied, Concept causeOfD
10111010
* @return the number of patients matching the given search phrase
10121011
* @since 1.8
10131012
* @should return the right count when a patient has multiple matching person names
1014-
* @should return the right count of patients with a matching identifier with no digits
10151013
*/
10161014
@Transactional(readOnly = true)
10171015
@Authorized( { PrivilegeConstants.VIEW_PATIENTS })

‎api/src/main/java/org/openmrs/api/db/PatientDAO.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
*/
1414
package org.openmrs.api.db;
1515

16-
import java.util.List;
17-
1816
import org.openmrs.Location;
1917
import org.openmrs.Patient;
2018
import org.openmrs.PatientIdentifier;
2119
import org.openmrs.PatientIdentifierType;
2220
import org.openmrs.api.PatientService;
2321

22+
import java.util.List;
23+
2424
/**
2525
* Database methods for the PatientService
2626
*
@@ -55,8 +55,6 @@ public interface PatientDAO {
5555
public List<Patient> getAllPatients(boolean includeVoided) throws DAOException;
5656

5757
/**
58-
* @param searchOnNamesOrIdentifiers specifies if the logic should find patients that match the
59-
* name or identifier otherwise find patients that match both the name and identifier
6058
* @see org.openmrs.api.PatientService#getPatients(String, String, List, boolean, Integer,
6159
* Integer)
6260
* @should escape percentage character in name phrase
@@ -65,11 +63,9 @@ public interface PatientDAO {
6563
* @should escape percentage character in identifier phrase
6664
* @should escape underscore character in identifier phrase
6765
* @should escape an asterix character in identifier phrase
68-
* @should get patients with a matching identifier and type
6966
*/
7067
public List<Patient> getPatients(String name, String identifier, List<PatientIdentifierType> identifierTypes,
71-
boolean matchIdentifierExactly, Integer start, Integer length, boolean searchOnNamesOrIdentifiers)
72-
throws DAOException;
68+
boolean matchIdentifierExactly, Integer start, Integer length) throws DAOException;
7369

7470
/**
7571
* @see org.openmrs.api.PatientService#getPatientIdentifiers(java.lang.String, java.util.List,
@@ -150,10 +146,8 @@ public List<PatientIdentifierType> getPatientIdentifierTypes(String name, String
150146
public void deletePatientIdentifier(PatientIdentifier patientIdentifier) throws DAOException;
151147

152148
/**
153-
* @param searchOnNamesOrIdentifiers specifies if the logic should find patients that match the
154-
* name or identifier otherwise find patients that match both the name and identifier
155149
* @see PatientService#getCountOfPatients(String)
156150
*/
157151
public Long getCountOfPatients(String name, String identifier, List<PatientIdentifierType> identifierTypes,
158-
boolean matchIdentifierExactly, boolean searchOnNamesOrIdentifiers);
152+
boolean matchIdentifierExactly);
159153
}

‎api/src/main/java/org/openmrs/api/db/hibernate/HibernateEncounterDAO.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
*/
1414
package org.openmrs.api.db.hibernate;
1515

16-
import java.util.ArrayList;
17-
import java.util.Collection;
18-
import java.util.Collections;
19-
import java.util.Comparator;
20-
import java.util.Date;
21-
import java.util.HashMap;
22-
import java.util.List;
23-
import java.util.Map;
24-
2516
import org.apache.commons.lang.StringUtils;
2617
import org.apache.commons.logging.Log;
2718
import org.apache.commons.logging.LogFactory;
@@ -51,6 +42,15 @@
5142
import org.openmrs.api.db.DAOException;
5243
import org.openmrs.api.db.EncounterDAO;
5344

45+
import java.util.ArrayList;
46+
import java.util.Collection;
47+
import java.util.Collections;
48+
import java.util.Comparator;
49+
import java.util.Date;
50+
import java.util.HashMap;
51+
import java.util.List;
52+
import java.util.Map;
53+
5454
/**
5555
* Hibernate specific dao for the {@link EncounterService} All calls should be made on the
5656
* Context.getEncounterService() object
@@ -409,7 +409,7 @@ private Criteria createEncounterByQueryCriteria(String query, Integer patientId,
409409
name = query;
410410
}
411411
criteria = new PatientSearchCriteria(sessionFactory, criteria).prepareCriteria(name, identifier,
412-
new ArrayList<PatientIdentifierType>(), false, orderByNames, false);
412+
new ArrayList<PatientIdentifierType>(), false, orderByNames);
413413
}
414414

415415
return criteria;

‎api/src/main/java/org/openmrs/api/db/hibernate/HibernatePatientDAO.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,19 @@ private void insertPatientStubIfNeeded(Patient patient) {
191191

192192
/**
193193
* @see org.openmrs.api.db.PatientDAO#getPatients(String, String, List, boolean, Integer,
194-
* Integer, boolean)
194+
* Integer)
195195
*/
196196
@SuppressWarnings("unchecked")
197197
public List<Patient> getPatients(String name, String identifier, List<PatientIdentifierType> identifierTypes,
198-
boolean matchIdentifierExactly, Integer start, Integer length, boolean searchOnNamesOrIdentifiers)
199-
throws DAOException {
198+
boolean matchIdentifierExactly, Integer start, Integer length) throws DAOException {
200199
if (StringUtils.isBlank(name) && StringUtils.isBlank(identifier)
201200
&& (identifierTypes == null || identifierTypes.isEmpty())) {
202201
return Collections.emptyList();
203202
}
204203

205204
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class);
206205
criteria = new PatientSearchCriteria(sessionFactory, criteria).prepareCriteria(name, identifier, identifierTypes,
207-
matchIdentifierExactly, true, searchOnNamesOrIdentifiers);
206+
matchIdentifierExactly, true);
208207
// restricting the search to the max search results value
209208
if (start != null)
210209
criteria.setFirstResult(start);
@@ -574,16 +573,16 @@ public void deletePatientIdentifier(PatientIdentifier patientIdentifier) throws
574573
}
575574

576575
/**
577-
* @see PatientDAO#getCountOfPatients(String, String, List, boolean, boolean)
576+
* @see PatientDAO#getCountOfPatients(String, String, List, boolean)
578577
*/
579578
public Long getCountOfPatients(String name, String identifier, List<PatientIdentifierType> identifierTypes,
580-
boolean matchIdentifierExactly, boolean searchOnNamesOrIdentifiers) {
579+
boolean matchIdentifierExactly) {
581580
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class);
582581
//Skip the ordering of names because H2(and i think PostgreSQL) will require one of the ordered
583582
//columns to be in the resultset which then contradicts with the combination of
584583
//(Projections.rowCount() and Criteria.uniqueResult()) that expect back only one row with one column
585584
criteria = new PatientSearchCriteria(sessionFactory, criteria).prepareCriteria(name, identifier, identifierTypes,
586-
matchIdentifierExactly, false, searchOnNamesOrIdentifiers);
585+
matchIdentifierExactly, false);
587586
criteria.setProjection(Projections.countDistinct("patientId"));
588587
return (Long) criteria.uniqueResult();
589588
}

‎api/src/main/java/org/openmrs/api/db/hibernate/PatientSearchCriteria.java

+26-60
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@
1313
*/
1414
package org.openmrs.api.db.hibernate;
1515

16-
import java.util.ArrayList;
17-
import java.util.List;
18-
import java.util.regex.Pattern;
19-
2016
import org.hibernate.Criteria;
2117
import org.hibernate.Hibernate;
2218
import org.hibernate.SessionFactory;
23-
import org.hibernate.criterion.Conjunction;
24-
import org.hibernate.criterion.Criterion;
2519
import org.hibernate.criterion.Expression;
2620
import org.hibernate.criterion.LogicalExpression;
2721
import org.hibernate.criterion.MatchMode;
@@ -34,6 +28,10 @@
3428
import org.openmrs.util.OpenmrsConstants;
3529
import org.springframework.util.StringUtils;
3630

31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.regex.Pattern;
34+
3735
/**
3836
* The PatientSearchCriteria class. It has API to return a criteria from the Patient Name and
3937
* identifier.
@@ -60,22 +58,10 @@ public PatientSearchCriteria(SessionFactory sessionFactory, Criteria criteria) {
6058
* @param identifier
6159
* @param identifierTypes
6260
* @param matchIdentifierExactly
63-
* @param searchOnNamesOrIdentifiers specifies if the logic should find patients that match the
64-
* name or identifier otherwise find patients that match both the name and identifier
6561
* @return {@link Criteria}
6662
*/
6763
public Criteria prepareCriteria(String name, String identifier, List<PatientIdentifierType> identifierTypes,
68-
boolean matchIdentifierExactly, boolean orderByNames, boolean searchOnNamesOrIdentifiers) {
69-
70-
//Find patients that match either the name or identifier if only
71-
//the name or identifier was passed in to this method
72-
if (searchOnNamesOrIdentifiers && (name == null || identifier == null)) {
73-
if (name == null)
74-
name = identifier;
75-
else
76-
identifier = name;
77-
}
78-
64+
boolean matchIdentifierExactly, boolean orderByNames) {
7965
name = HibernateUtil.escapeSqlWildcards(name, sessionFactory);
8066
identifier = HibernateUtil.escapeSqlWildcards(identifier, sessionFactory);
8167

@@ -88,33 +74,20 @@ public Criteria prepareCriteria(String name, String identifier, List<PatientIden
8874

8975
// get only distinct patients
9076
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
91-
Criterion nameCriterion = null;
77+
9278
if (name != null) {
93-
nameCriterion = prepareNameCriterion(name);
79+
addNameCriterias(criteria, name);
9480
}
9581

96-
Criterion identifierCriterion = null;
9782
// do the restriction on either identifier string or types
9883
if (identifier != null || identifierTypes.size() > 0) {
99-
identifierCriterion = prepareIdentifierCriterion(identifier, identifierTypes, matchIdentifierExactly,
100-
searchOnNamesOrIdentifiers);
101-
}
102-
103-
if (searchOnNamesOrIdentifiers) {
104-
criteria.add(Restrictions.or(nameCriterion, identifierCriterion));
105-
} else {
106-
if (nameCriterion != null) {
107-
criteria.add(nameCriterion);
108-
}
109-
if (identifierCriterion != null) {
110-
criteria.add(identifierCriterion);
111-
}
84+
addIdentifierCriterias(criteria, identifier, identifierTypes, matchIdentifierExactly);
11285
}
11386

11487
// TODO add junit test for searching on voided patients
11588

11689
// make sure the patient object isn't voided
117-
criteria.add(Restrictions.eq("voided", false));
90+
criteria.add(Expression.eq("voided", false));
11891

11992
return criteria;
12093
}
@@ -127,21 +100,19 @@ public Criteria prepareCriteria(String name, String identifier, List<PatientIden
127100
* @param identifierTypes
128101
* @param matchIdentifierExactly
129102
*/
130-
private Criterion prepareIdentifierCriterion(String identifier, List<PatientIdentifierType> identifierTypes,
131-
boolean matchIdentifierExactly, boolean searchOnNamesOrIdentifiers) {
132-
133-
Conjunction conjuction = Restrictions.conjunction();
103+
private void addIdentifierCriterias(Criteria criteria, String identifier, List<PatientIdentifierType> identifierTypes,
104+
boolean matchIdentifierExactly) {
134105
// TODO add junit test for searching on voided identifiers
135106

136107
// add the join on the identifiers table
137108
criteria.createAlias("identifiers", "ids");
109+
criteria.add(Expression.eq("ids.voided", false));
138110

139-
conjuction.add(Restrictions.eq("ids.voided", false));
140111
// do the identifier restriction
141112
if (identifier != null) {
142113
// if the user wants an exact search, match on that.
143114
if (matchIdentifierExactly) {
144-
conjuction.add(Restrictions.eq("ids.identifier", identifier).ignoreCase());
115+
criteria.add(Expression.eq("ids.identifier", identifier));
145116
} else {
146117
AdministrationService adminService = Context.getAdministrationService();
147118
String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "");
@@ -154,18 +125,18 @@ private Criterion prepareIdentifierCriterion(String identifier, List<PatientIden
154125
}
155126

156127
if (StringUtils.hasLength(patternSearch)) {
157-
conjuction.add(splitAndGetSearchPattern(identifier, patternSearch));
128+
splitAndAddSearchPattern(criteria, identifier, patternSearch);
158129
}
159130
// if the regex is empty, default to a simple "like" search or if
160131
// we're in hsql world, also only do the simple like search (because
161132
// hsql doesn't know how to deal with 'regexp'
162133
else if (regex.equals("") || HibernateUtil.isHSQLDialect(sessionFactory)) {
163-
conjuction.add(getCriterionForSimpleSearch(identifier, adminService));
134+
addCriterionForSimpleSearch(criteria, identifier, adminService);
164135
}
165136
// if the regex is present, search on that
166137
else {
167138
regex = replaceSearchString(regex, identifier);
168-
conjuction.add(Restrictions.sqlRestriction("identifier regexp ?", regex, Hibernate.STRING));
139+
criteria.add(Restrictions.sqlRestriction("identifier regexp ?", regex, Hibernate.STRING));
169140
}
170141
}
171142
}
@@ -174,38 +145,39 @@ else if (regex.equals("") || HibernateUtil.isHSQLDialect(sessionFactory)) {
174145

175146
// do the type restriction
176147
if (identifierTypes.size() > 0) {
177-
criteria.add(Restrictions.in("ids.identifierType", identifierTypes));
148+
criteria.add(Expression.in("ids.identifierType", identifierTypes));
178149
}
179150

180-
return conjuction;
181151
}
182152

183153
/**
184154
* Utility method to add prefix and suffix like expression
185155
*
156+
* @param criteria
186157
* @param identifier
187158
* @param adminService
188159
*/
189-
private Criterion getCriterionForSimpleSearch(String identifier, AdministrationService adminService) {
160+
private void addCriterionForSimpleSearch(Criteria criteria, String identifier, AdministrationService adminService) {
190161
String prefix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_PREFIX, "");
191162
String suffix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SUFFIX, "");
192163
StringBuffer likeString = new StringBuffer(prefix).append(identifier).append(suffix);
193-
return Restrictions.ilike("ids.identifier", likeString.toString());
164+
criteria.add(Expression.like("ids.identifier", likeString.toString()));
194165
}
195166

196167
/**
197168
* Utility method to add search pattern expression to identifier.
198169
*
170+
* @param criteria
199171
* @param identifier
200172
* @param patternSearch
201173
*/
202-
private Criterion splitAndGetSearchPattern(String identifier, String patternSearch) {
174+
private void splitAndAddSearchPattern(Criteria criteria, String identifier, String patternSearch) {
203175
// split the pattern before replacing in case the user searched on a comma
204176
List<String> searchPatterns = new ArrayList<String>();
205177
// replace the @SEARCH@, etc in all elements
206178
for (String pattern : patternSearch.split(","))
207179
searchPatterns.add(replaceSearchString(pattern, identifier));
208-
return Restrictions.in("ids.identifier", searchPatterns);
180+
criteria.add(Expression.in("ids.identifier", searchPatterns));
209181
}
210182

211183
/**
@@ -228,10 +200,7 @@ private String removePadding(String identifier, String regex) {
228200
* @param criteria
229201
* @param name
230202
*/
231-
private Criterion prepareNameCriterion(String name) {
232-
233-
Conjunction conjuction = Restrictions.conjunction();
234-
203+
private void addNameCriterias(Criteria criteria, String name) {
235204
// TODO simple name search to start testing, will need to make "real"
236205
// name search
237206
// i.e. split on whitespace, guess at first/last name, etc
@@ -254,14 +223,12 @@ private Criterion prepareNameCriterion(String name) {
254223
if (i > 0) {
255224
nameSoFar += " " + n;
256225
LogicalExpression fullNameSearch = getNameSearch(nameSoFar);
257-
searchExpression = Restrictions.or(oneNameSearch, fullNameSearch);
226+
searchExpression = Expression.or(oneNameSearch, fullNameSearch);
258227
}
259-
conjuction.add(searchExpression);
228+
criteria.add(searchExpression);
260229
}
261230
}
262231
}
263-
264-
return conjuction;
265232
}
266233

267234
/**
@@ -273,7 +240,6 @@ private Criterion prepareNameCriterion(String name) {
273240
* <pre>
274241
* ... where voided = false &amp;&amp; name in (familyName2, familyName, middleName, givenName)
275242
* </pre>
276-
*
277243
* Except when the name provided is less than min characters (usually 3) then we will look for
278244
* an EXACT match by default
279245
*

‎api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java

+29-16
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@
1313
*/
1414
package org.openmrs.api.impl;
1515

16-
import java.util.ArrayList;
17-
import java.util.Collection;
18-
import java.util.Collections;
19-
import java.util.Date;
20-
import java.util.HashSet;
21-
import java.util.LinkedHashMap;
22-
import java.util.List;
23-
import java.util.Map;
24-
import java.util.Set;
25-
import java.util.UUID;
26-
import java.util.Vector;
27-
2816
import org.apache.commons.lang.StringUtils;
2917
import org.apache.commons.logging.Log;
3018
import org.apache.commons.logging.LogFactory;
@@ -73,6 +61,18 @@
7361
import org.openmrs.util.PrivilegeConstants;
7462
import org.openmrs.validator.PatientIdentifierValidator;
7563

64+
import java.util.ArrayList;
65+
import java.util.Collection;
66+
import java.util.Collections;
67+
import java.util.Date;
68+
import java.util.HashSet;
69+
import java.util.LinkedHashMap;
70+
import java.util.List;
71+
import java.util.Map;
72+
import java.util.Set;
73+
import java.util.UUID;
74+
import java.util.Vector;
75+
7676
/**
7777
* Default implementation of the patient service. This class should not be used on its own. The
7878
* current OpenMRS implementation should be fetched from the Context via
@@ -1548,7 +1548,14 @@ public Integer getCountOfPatients(String query) {
15481548
if (StringUtils.isBlank(query))
15491549
return count;
15501550
List<PatientIdentifierType> emptyList = new Vector<PatientIdentifierType>();
1551-
return OpenmrsUtil.convertToInteger(dao.getCountOfPatients(null, query, emptyList, false, true));
1551+
// if there is a number in the query string
1552+
if (query.matches(".*\\d+.*")) {
1553+
log.debug("[Identifier search] Query: " + query);
1554+
return OpenmrsUtil.convertToInteger(dao.getCountOfPatients(null, query, emptyList, false));
1555+
} else {
1556+
// there is no number in the string, search on name
1557+
return OpenmrsUtil.convertToInteger(dao.getCountOfPatients(query, null, emptyList, false));
1558+
}
15521559
}
15531560

15541561
/**
@@ -1574,14 +1581,20 @@ private int getMinSearchCharacters() {
15741581
/**
15751582
* @see PatientService#getPatients(String, Integer, Integer)
15761583
*/
1577-
@SuppressWarnings("unchecked")
15781584
@Override
15791585
public List<Patient> getPatients(String query, Integer start, Integer length) throws APIException {
15801586
List<Patient> patients = new Vector<Patient>();
15811587
if (StringUtils.isBlank(query))
15821588
return patients;
15831589

1584-
return dao.getPatients(query, null, Collections.EMPTY_LIST, false, start, length, true);
1590+
// if there is a number in the query string
1591+
if (query.matches(".*\\d+.*")) {
1592+
log.debug("[Identifier search] Query: " + query);
1593+
return getPatients(null, query, null, false, start, length);
1594+
} else {
1595+
// there is no number in the string, search on name
1596+
return getPatients(query, null, null, false, start, length);
1597+
}
15851598
}
15861599

15871600
/**
@@ -1593,6 +1606,6 @@ public List<Patient> getPatients(String name, String identifier, List<PatientIde
15931606
if (identifierTypes == null)
15941607
identifierTypes = Collections.emptyList();
15951608

1596-
return dao.getPatients(name, identifier, identifierTypes, matchIdentifierExactly, start, length, false);
1609+
return dao.getPatients(name, identifier, identifierTypes, matchIdentifierExactly, start, length);
15971610
}
15981611
}

‎api/src/test/java/org/openmrs/api/PatientServiceTest.java

+28-63
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@
1313
*/
1414
package org.openmrs.api;
1515

16-
import static org.hamcrest.Matchers.equalTo;
17-
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
18-
import static org.hamcrest.core.Is.is;
19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertThat;
24-
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assert.fail;
26-
import static org.openmrs.test.TestUtil.assertCollectionContentsEquals;
27-
import static org.openmrs.util.AddressMatcher.containsAddress;
28-
import static org.openmrs.util.NameMatcher.containsFullName;
29-
30-
import java.util.ArrayList;
31-
import java.util.Arrays;
32-
import java.util.Calendar;
33-
import java.util.Collection;
34-
import java.util.Collections;
35-
import java.util.Date;
36-
import java.util.GregorianCalendar;
37-
import java.util.HashSet;
38-
import java.util.LinkedHashSet;
39-
import java.util.List;
40-
import java.util.Set;
41-
import java.util.Vector;
42-
4316
import org.apache.commons.collections.CollectionUtils;
4417
import org.apache.commons.logging.Log;
4518
import org.apache.commons.logging.LogFactory;
@@ -82,6 +55,33 @@
8255
import org.openmrs.util.OpenmrsConstants;
8356
import org.openmrs.util.OpenmrsUtil;
8457

58+
import java.util.ArrayList;
59+
import java.util.Arrays;
60+
import java.util.Calendar;
61+
import java.util.Collection;
62+
import java.util.Collections;
63+
import java.util.Date;
64+
import java.util.GregorianCalendar;
65+
import java.util.HashSet;
66+
import java.util.LinkedHashSet;
67+
import java.util.List;
68+
import java.util.Set;
69+
import java.util.Vector;
70+
71+
import static org.hamcrest.Matchers.equalTo;
72+
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
73+
import static org.hamcrest.core.Is.is;
74+
import static org.junit.Assert.assertEquals;
75+
import static org.junit.Assert.assertFalse;
76+
import static org.junit.Assert.assertNotNull;
77+
import static org.junit.Assert.assertNull;
78+
import static org.junit.Assert.assertThat;
79+
import static org.junit.Assert.assertTrue;
80+
import static org.junit.Assert.fail;
81+
import static org.openmrs.test.TestUtil.assertCollectionContentsEquals;
82+
import static org.openmrs.util.AddressMatcher.containsAddress;
83+
import static org.openmrs.util.NameMatcher.containsFullName;
84+
8585
/**
8686
* This class tests methods in the PatientService class TODO Add methods to test all methods in
8787
* PatientService class
@@ -424,6 +424,7 @@ public void shouldGetPatientsByIdentifierAndIdentifierType() throws Exception {
424424

425425
List<PatientIdentifierType> types = new Vector<PatientIdentifierType>();
426426
types.add(new PatientIdentifierType(1));
427+
427428
// make sure we get back only one patient
428429
List<Patient> patients = patientService.getPatients(null, "1234", types, false);
429430
assertEquals(1, patients.size());
@@ -566,8 +567,6 @@ public void getPatients_shouldAllowSearchStringToBeOneAccordingToMinsearchcharac
566567
public void getPatients_shouldAllowExactSearchOfForTwoCharacterName() throws Exception {
567568
initializeInMemoryDatabase();
568569
executeDataSet(FIND_PATIENTS_XML);
569-
Context.getAdministrationService().saveGlobalProperty(
570-
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MIN_SEARCH_CHARACTERS, "2"));
571570
assertEquals(1, Context.getPatientService().getPatients("Ho").size());
572571
}
573572

@@ -3186,38 +3185,4 @@ public void savePatient_shouldNotThrowNonUniqueObjectExceptionWhenCalledWithPers
31863185
patientService.savePatient(patient);
31873186
}
31883187

3189-
/**
3190-
* @see {@link PatientService#getPatients(String,Integer,Integer)}
3191-
*/
3192-
@Test
3193-
@Verifies(value = "should find a patients with a matching identifier with no digits", method = "getPatients(String,Integer,Integer)")
3194-
public void getPatients_shouldFindAPatientsWithAMatchingIdentifierWithNoDigits() throws Exception {
3195-
final String identifier = "XYZ";
3196-
Patient patient = patientService.getPatient(2);
3197-
Assert.assertEquals(0, patientService.getPatients(identifier, (Integer) null, (Integer) null).size());
3198-
PatientIdentifier pId = new PatientIdentifier(identifier, patientService.getPatientIdentifierType(2),
3199-
locationService.getLocation(1));
3200-
patient.addIdentifier(pId);
3201-
patientService.savePatient(patient);
3202-
3203-
Assert.assertEquals(1, patientService.getPatients(identifier).size());
3204-
}
3205-
3206-
/**
3207-
* @see {@link PatientService#getCountOfPatients(String)}
3208-
*/
3209-
@Test
3210-
@Verifies(value = "should return the right count of patients with a matching identifier with no digits", method = "getCountOfPatients(String)")
3211-
public void getCountOfPatients_shouldReturnTheRightCountOfPatientsWithAMatchingIdentifierWithNoDigits() throws Exception {
3212-
final String identifier = "XYZ";
3213-
Patient patient = patientService.getPatient(2);
3214-
Assert.assertEquals(0, patientService.getCountOfPatients(identifier).intValue());
3215-
PatientIdentifier pId = new PatientIdentifier(identifier, patientService.getPatientIdentifierType(2),
3216-
locationService.getLocation(1));
3217-
patient.addIdentifier(pId);
3218-
patientService.savePatient(patient);
3219-
3220-
Assert.assertEquals(1, patientService.getCountOfPatients(identifier).intValue());
3221-
}
3222-
32233188
}

‎api/src/test/java/org/openmrs/api/db/PatientDAOTest.java

+16-29
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
/**
2-
* The contents of this file are subject to the OpenMRS Public License
3-
* Version 1.0 (the "License"); you may not use this file except in
4-
* compliance with the License. You may obtain a copy of the License at
5-
* http://license.openmrs.org
6-
*
7-
* Software distributed under the License is distributed on an "AS IS"
8-
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9-
* License for the specific language governing rights and limitations
10-
* under the License.
11-
*
12-
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13-
*/
141
package org.openmrs.api.db;
152

16-
import java.util.ArrayList;
17-
import java.util.Collections;
18-
import java.util.List;
19-
203
import org.junit.Assert;
214
import org.junit.Before;
225
import org.junit.Test;
@@ -30,6 +13,10 @@
3013
import org.openmrs.test.BaseContextSensitiveTest;
3114
import org.openmrs.test.Verifies;
3215

16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.List;
19+
3320
public class PatientDAOTest extends BaseContextSensitiveTest {
3421

3522
private PatientDAO dao = null;
@@ -77,11 +64,11 @@ public void getPatients_shouldEscapeAnAsterixCharacterInIdentifierPhrase() throw
7764

7865
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
7966
//we expect only one matching patient
80-
int actualSize = dao.getPatients(null, "*567", identifierTypes, false, 0, null, false).size();
67+
int actualSize = dao.getPatients(null, "*567", identifierTypes, false, 0, null).size();
8168
Assert.assertEquals(1, actualSize);
8269

8370
//if actually the search returned the matching patient
84-
Patient actualPatient = dao.getPatients(null, "*567", identifierTypes, false, 0, null, false).get(0);
71+
Patient actualPatient = dao.getPatients(null, "*567", identifierTypes, false, 0, null).get(0);
8572

8673
Assert.assertEquals(patient2, actualPatient);
8774
}
@@ -109,11 +96,11 @@ public void getPatients_shouldEscapePercentageCharacterInIdentifierPhrase() thro
10996

11097
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
11198
//we expect only one matching patient
112-
int actualSize = dao.getPatients(null, "%567", identifierTypes, false, 0, null, false).size();
99+
int actualSize = dao.getPatients(null, "%567", identifierTypes, false, 0, null).size();
113100
Assert.assertEquals(1, actualSize);
114101

115102
//if actually the search returned the matching patient
116-
Patient actualPatient = dao.getPatients(null, "%567", identifierTypes, false, 0, null, false).get(0);
103+
Patient actualPatient = dao.getPatients(null, "%567", identifierTypes, false, 0, null).get(0);
117104

118105
Assert.assertEquals(patient2, actualPatient);
119106
}
@@ -141,11 +128,11 @@ public void getPatients_shouldEscapeUnderscoreCharacterInIdentifierPhrase() thro
141128

142129
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
143130
//we expect only one matching patient
144-
int actualSize = dao.getPatients(null, "_567", identifierTypes, false, 0, null, false).size();
131+
int actualSize = dao.getPatients(null, "_567", identifierTypes, false, 0, null).size();
145132
Assert.assertEquals(1, actualSize);
146133

147134
//if actually the search returned the matching patient
148-
Patient actualPatient = dao.getPatients(null, "_567", identifierTypes, false, 0, null, false).get(0);
135+
Patient actualPatient = dao.getPatients(null, "_567", identifierTypes, false, 0, null).get(0);
149136

150137
Assert.assertEquals(patient2, actualPatient);
151138
}
@@ -171,10 +158,10 @@ public void getPatients_shouldEscapePercentageCharacterInNamePhrase() throws Exc
171158

172159
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
173160
//we expect only one matching patient
174-
int actualSize = dao.getPatients("%ca", null, identifierTypes, false, 0, null, false).size();
161+
int actualSize = dao.getPatients("%ca", null, identifierTypes, false, 0, null).size();
175162
Assert.assertEquals(1, actualSize);
176163

177-
Patient actualPatient = dao.getPatients("%ca", null, identifierTypes, false, 0, null, false).get(0);
164+
Patient actualPatient = dao.getPatients("%ca", null, identifierTypes, false, 0, null).get(0);
178165
//if actually the search returned the matching patient
179166
Assert.assertEquals(patient2, actualPatient);
180167
}
@@ -200,11 +187,11 @@ public void getPatients_shouldEscapeUnderscoreCharacterInNamePhrase() throws Exc
200187

201188
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
202189
//we expect only one matching patient
203-
int actualSize = dao.getPatients("_ca", null, identifierTypes, false, 0, null, false).size();
190+
int actualSize = dao.getPatients("_ca", null, identifierTypes, false, 0, null).size();
204191
Assert.assertEquals(1, actualSize);
205192

206193
//if actually the search returned the matching patient
207-
Patient actualPatient = dao.getPatients("_ca", null, identifierTypes, false, 0, null, false).get(0);
194+
Patient actualPatient = dao.getPatients("_ca", null, identifierTypes, false, 0, null).get(0);
208195
Assert.assertEquals(patient2, actualPatient);
209196

210197
}
@@ -230,11 +217,11 @@ public void getPatients_shouldEscapeAnAsterixCharacterInNamePhrase() throws Exce
230217

231218
List<PatientIdentifierType> identifierTypes = Collections.emptyList();
232219
//we expect only one matching patient
233-
int actualSize = dao.getPatients("*ca", null, identifierTypes, false, 0, null, false).size();
220+
int actualSize = dao.getPatients("*ca", null, identifierTypes, false, 0, null).size();
234221
Assert.assertEquals(1, actualSize);
235222

236223
//if actually the search returned the matching patient
237-
Patient actualPatient = dao.getPatients("*ca", null, identifierTypes, false, 0, null, false).get(0);
224+
Patient actualPatient = dao.getPatients("*ca", null, identifierTypes, false, 0, null).get(0);
238225
Assert.assertEquals(patient2, actualPatient);
239226
}
240227

0 commit comments

Comments
 (0)
Please sign in to comment.