Skip to content

Commit 48fd9b5

Browse files
committedDec 9, 2013
Back porting to 1.9.7: PatientService.savePatientIdentifier still
requires location - TRUNK-4056
1 parent c6943c2 commit 48fd9b5

File tree

3 files changed

+86
-30
lines changed

3 files changed

+86
-30
lines changed
 

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -1436,9 +1436,16 @@ public void mergePatients(Patient preferred, List<Patient> notPreferred) throws
14361436
*/
14371437
public PatientIdentifier savePatientIdentifier(PatientIdentifier patientIdentifier) throws APIException {
14381438
//if the argument or the following required fields are not specified
1439-
if (patientIdentifier == null || patientIdentifier.getPatient() == null
1440-
|| patientIdentifier.getIdentifierType() == null || patientIdentifier.getLocation() == null
1441-
|| StringUtils.isBlank(patientIdentifier.getIdentifier()))
1439+
PatientIdentifierType.LocationBehavior locationBehavior = null;
1440+
if (patientIdentifier != null) {
1441+
locationBehavior = patientIdentifier.getIdentifierType().getLocationBehavior();
1442+
}
1443+
1444+
if (patientIdentifier == null
1445+
|| patientIdentifier.getPatient() == null
1446+
|| patientIdentifier.getIdentifierType() == null
1447+
|| StringUtils.isBlank(patientIdentifier.getIdentifier())
1448+
|| (locationBehavior == PatientIdentifierType.LocationBehavior.REQUIRED && patientIdentifier.getLocation() == null))
14421449
throw new APIException("PatientIdentifier argument or one of its required fields is null or invalid");
14431450
if (patientIdentifier.getPatientIdentifierId() == null) {
14441451
Context.requirePrivilege(PrivilegeConstants.ADD_PATIENT_IDENTIFIERS);

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

+52-27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@
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+
43+
import javax.validation.ValidationException;
44+
1645
import org.apache.commons.collections.CollectionUtils;
1746
import org.apache.commons.logging.Log;
1847
import org.apache.commons.logging.LogFactory;
@@ -55,33 +84,6 @@
5584
import org.openmrs.util.OpenmrsConstants;
5685
import org.openmrs.util.OpenmrsUtil;
5786

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-
8587
/**
8688
* This class tests methods in the PatientService class TODO Add methods to test all methods in
8789
* PatientService class
@@ -3220,4 +3222,27 @@ public void getCountOfPatients_shouldReturnTheRightCountOfPatientsWithAMatchingI
32203222
Assert.assertEquals(1, patientService.getCountOfPatients(identifier).intValue());
32213223
}
32223224

3225+
/**
3226+
* @see {@link PatientService#savePatientIdentifier(PatientIdentifier)}
3227+
*/
3228+
@Test
3229+
@Verifies(value = "should pass if patient identifer type's location behaviour is NOT_USED and location is null", method = "savePatientIdentifier(PatientIdentifierType)")
3230+
public void savePatientIdentifier_shouldAllowLocationToBeNullWhenLocationBehaviourIsNotUsed() {
3231+
PatientIdentifier patientIdentifier = patientService.getPatientIdentifier(7);
3232+
patientIdentifier.setLocation(null);
3233+
patientIdentifier.getIdentifierType().setLocationBehavior(PatientIdentifierType.LocationBehavior.NOT_USED);
3234+
patientService.savePatientIdentifier(patientIdentifier);
3235+
}
3236+
3237+
/**
3238+
* @see {@link PatientService#savePatientIdentifier(PatientIdentifier)}
3239+
*/
3240+
@Test(expected = APIException.class)
3241+
@Verifies(value = "should fail if patient identifer type's location behaviour is REQUIRED and location is null", method = "savePatientIdentifier(PatientIdentifierType)")
3242+
public void savePatientIdentifier_shouldAllowLocationToBeNullWhenLocationBehaviourIsRequired() {
3243+
PatientIdentifier patientIdentifier = patientService.getPatientIdentifier(7);
3244+
patientIdentifier.setLocation(null);
3245+
patientIdentifier.getIdentifierType().setLocationBehavior(PatientIdentifierType.LocationBehavior.REQUIRED);
3246+
patientService.savePatientIdentifier(patientIdentifier);
3247+
}
32233248
}

‎api/src/test/java/org/openmrs/validator/PatientIdentifierValidatorTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openmrs.api.IdentifierNotUniqueException;
2424
import org.openmrs.api.InvalidCheckDigitException;
2525
import org.openmrs.api.InvalidIdentifierFormatException;
26+
import org.openmrs.api.PatientIdentifierException;
2627
import org.openmrs.api.context.Context;
2728
import org.openmrs.patient.IdentifierValidator;
2829
import org.openmrs.patient.impl.LuhnIdentifierValidator;
@@ -167,4 +168,27 @@ public void validateIdentifier_shouldFailValidationIfIdentifierIsBlank() throws
167168
PatientIdentifierValidator.validateIdentifier(identifier);
168169
}
169170

171+
/**
172+
* @see {@link PatientIdentifierValidator#validateIdentifier(PatientIdentifier)}
173+
*/
174+
@Test
175+
@Verifies(value = "should pass if locationBehavior is NOT_USED and location is null", method = "validateIdentifier(PatientIdentifier)")
176+
public void validateIdentifier_shouldPassIfLocationBehaviorIsNotUsedAndLocationIsNull() throws Exception {
177+
PatientIdentifier pi = new PatientIdentifier("1TU-8", new PatientIdentifierType(1), null);
178+
PatientIdentifierType idType = pi.getIdentifierType();
179+
idType.setLocationBehavior(PatientIdentifierType.LocationBehavior.NOT_USED);
180+
PatientIdentifierValidator.validateIdentifier(pi);
181+
}
182+
183+
/**
184+
* @see {@link PatientIdentifierValidator#validateIdentifier(PatientIdentifier)}
185+
*/
186+
@Test(expected = PatientIdentifierException.class)
187+
@Verifies(value = "should fail validation if locationBehavior is REQUIRED and location is null", method = "validateIdentifier(PatientIdentifier)")
188+
public void validateIdentifier_shouldPassIfLocationBehaviorIsRequiredAndLocationIsNull() throws Exception {
189+
PatientIdentifier pi = new PatientIdentifier("1TU-8", new PatientIdentifierType(1), null);
190+
PatientIdentifierType idType = pi.getIdentifierType();
191+
idType.setLocationBehavior(PatientIdentifierType.LocationBehavior.REQUIRED);
192+
PatientIdentifierValidator.validateIdentifier(pi);
193+
}
170194
}

0 commit comments

Comments
 (0)
Please sign in to comment.