Skip to content

Commit

Permalink
TRUNK-4208: Removed gender as a requirement during PersonValidation, …
Browse files Browse the repository at this point in the history
…added it instead to PatientValidation.

- Moved unit tests from PersonValidatorTest to PatientValidatorTest to reflect this new behaviour.

- Birthdate is already *not* a requirement for Persons.

- Added test for PersonValidator to check that it's not validating gender requirement.

TRUNK-4208: Refactored PersonValidator. Extracted birth date validation into it's own method.
  • Loading branch information
Priya Chandran committed Jul 29, 2014
1 parent 600ac3a commit 795ad41
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/validator/PatientValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.springframework.validation.ValidationUtils;
import org.openmrs.annotation.Handler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
Expand Down Expand Up @@ -84,6 +85,8 @@ public void validate(Object obj, Errors errors) {

Patient patient = (Patient) obj;

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "Person.gender.required");

if (PatientIdentifierValidator.hasMoreThanOneIdentifierForSameIdentifierType(patient.getActiveIdentifiers())) {
errors.reject("error.duplicateIdentifierTypes");
}
Expand Down
39 changes: 23 additions & 16 deletions api/src/main/java/org/openmrs/validator/PersonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public boolean supports(Class<?> clazz) {
/**
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail validation if gender is blank
* @should fail validation if birthdate makes patient older that 120 years old
* @should fail validation if birthdate is a future date
* @should fail validation if voidReason is blank when patient is voided
* @should fail validation if causeOfDeath is blank when patient is dead
* @should pass validation if gender is blank for Persons
*/
@Override
public void validate(Object target, Errors errors) {
Expand All @@ -68,8 +68,6 @@ public void validate(Object target, Errors errors) {

Person person = (Person) target;

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "Person.gender.required");

boolean atLeastOneNonVoidPersonNameLeft = false;
for (PersonName personName : person.getNames()) {
personNameValidator.validate(personName, errors);
Expand All @@ -94,19 +92,7 @@ public void validate(Object target, Errors errors) {
}
}

// check birth date against future dates and really old dates
if (person.getBirthdate() != null) {
if (person.getBirthdate().after(new Date())) {
errors.rejectValue("birthdate", "error.date.future");
} else {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -120); // person cannot be older than 120 years old
if (person.getBirthdate().before(c.getTime())) {
errors.rejectValue("birthdate", "error.date.nonsensical");
}
}
}
validateBirthDate(errors, person.getBirthdate());

if (person.isVoided()) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
Expand All @@ -118,4 +104,25 @@ public void validate(Object target, Errors errors) {
ValidateUtil.validateFieldLengths(errors, Person.class, "gender");
}

/**
* Checks if the birth date specified is in the future or older than 120 years old..
*
* @param birthDate The birthdate to validate.
* @param errors Stores information about errors encountered during validation.
*/
private void validateBirthDate(Errors errors, Date birthDate) {
if (birthDate != null) {
if (birthDate.after(new Date())) {
errors.rejectValue("birthdate", "error.date.future");
} else {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -120);
if (birthDate.before(c.getTime())) {
errors.rejectValue("birthdate", "error.date.nonsensical");
}
}
}
}

}
15 changes: 15 additions & 0 deletions api/src/test/java/org/openmrs/validator/PatientValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,19 @@ public void validate_shouldPassTheValidatePatientIfSameIdentifierTypesAreAddedOn

Assert.assertFalse(errors.hasErrors());
}

/**
* @see {@link org.openmrs.validator.PatientValidator#validate(Object,Errors)}
* @verifies fail validation if gender is blank
*/
@Test
@Verifies(value = "should fail validation if gender is blank", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfGenderIsBlank() throws Exception {
Patient pa = new Patient(1);
Errors errors = new BindException(pa, "patient");
validator.validate(pa, errors);

Assert.assertTrue(errors.hasFieldErrors("gender"));
}

}
32 changes: 17 additions & 15 deletions api/src/test/java/org/openmrs/validator/PersonValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
Expand Down Expand Up @@ -94,21 +95,6 @@ public void validate_shouldFailValidationIfCauseOfDeathIsBlankWhenPatientIsDead(
Assert.assertTrue(errors.hasFieldErrors("causeOfDeath"));
}

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if gender is blank
*/
@Test
@Verifies(value = "should fail validation if gender is blank", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfGenderIsBlank() throws Exception {
Patient pa = new Patient(1);
Errors errors = new BindException(pa, "patient");
validator.validate(pa, errors);

Assert.assertTrue(errors.hasFieldErrors("gender"));

}

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if voidReason is blank when patient is voided
Expand Down Expand Up @@ -136,4 +122,20 @@ public void validate_shouldFailValidationIfPersonDoesNotHaveAtleastOneNonVoidedN
validator.validate(pa, errors);
Assert.assertTrue(errors.hasFieldErrors("names"));
}

/**
* @see {@link org.openmrs.validator.PersonValidator#validate(Object,Errors)}
* @verifies pass validation if gender is blank for Persons
*/
@Test
@Verifies(value = "should pass validation if gender is blank for Persons", method = "validate(Object,Errors)")
public void validate_shouldPassValidationIfGenderIsBlankForPersons() throws Exception {
Person person = new Person(1);
Errors errors = new BindException(person, "person");
PersonValidator personValidator = new PersonValidator();
personValidator.validate(person, errors);

Assert.assertFalse(errors.hasFieldErrors("gender"));
}

}

0 comments on commit 795ad41

Please sign in to comment.