Skip to content

Commit

Permalink
TRUNK-4869: added deathDate validation if it is set in future. BirthD…
Browse files Browse the repository at this point in the history
…ate validation already existed

TRUNK-4869: added javadocs to all methods. Fixed naming

TRUNK-4869: fixing formatting
  • Loading branch information
samshuster committed Oct 15, 2016
1 parent 6e98a8c commit 8a25aa0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
61 changes: 49 additions & 12 deletions api/src/main/java/org/openmrs/validator/PersonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public boolean supports(Class<?> clazz) {
* org.springframework.validation.Errors)
* @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 deathdate 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
Expand Down Expand Up @@ -81,7 +82,7 @@ public void validate(Object target, Errors errors) {
errors.rejectValue("names", "Person.shouldHaveAtleastOneNonVoidedName");
}

//validate the personAddress
// validate the personAddress
index = 0;
for (PersonAddress address : person.getAddresses()) {
try {
Expand All @@ -95,6 +96,7 @@ public void validate(Object target, Errors errors) {
}

validateBirthDate(errors, person.getBirthdate());
validateDeathDate(errors, person.getDeathDate());

if (person.isVoided()) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
Expand All @@ -113,17 +115,52 @@ public void validate(Object target, Errors errors) {
* @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");
}
}
if (birthDate == null) {
return;
}
rejectIfFutureDate(errors, birthDate, "birthdate");
rejectDateIfBefore120YearsAgo(errors, birthDate, "birthdate");
}

/**
* Checks if the death date is in the future.
*
* @param errors Stores information about errors encountered during validation
* @param deathDate the deathdate to validate
*/
private void validateDeathDate(Errors errors, Date deathDate) {
if (deathDate == null) {
return;
}
rejectIfFutureDate(errors, deathDate, "deathDate");
}

/**
* Rejects a date if it is in the future.
*
* @param errors the error object
* @param date the date to check
* @param dateField the name of the field
*/
private void rejectIfFutureDate(Errors errors, Date date, String dateField) {
if (date.after(new Date())) {
errors.rejectValue(dateField, "error.date.future");
}
}

/**
* Rejects a date if it is before 120 years ago.
*
* @param errors the error object
* @param date the date to check
* @param dateField the name of the field
*/
private void rejectDateIfBefore120YearsAgo(Errors errors, Date date, String dateField) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -120);
if (date.before(c.getTime())) {
errors.rejectValue(dateField, "error.date.nonsensical");
}
}

Expand Down
22 changes: 20 additions & 2 deletions api/src/test/java/org/openmrs/validator/PersonValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public void validate_shouldFailValidationIfBirthdateIsAFutureDate() throws Excep
Assert.assertTrue(errors.hasFieldErrors("birthdate"));
}

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if deathdate is a future date
*/

@Test
@Verifies(value = "should fail validation if deathdate is a future date", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfDeathDateIsAFutureDate() throws Exception {
Patient pa = new Patient(1);
Calendar death = Calendar.getInstance();
death.setTime(new Date());
death.add(Calendar.YEAR, 20);
pa.setDeathDate(death.getTime());
Errors errors = new BindException(pa, "patient");
validator.validate(pa, errors);

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

/**
* @see PersonValidator#validate(Object,Errors)
* @verifies fail validation if birthdate makes patient older that 120 years old
Expand Down Expand Up @@ -166,8 +185,7 @@ public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() throws Ex
person.setBirthdate(new Date());
person.setGender("too long text too long too long text too long text too long text");
person.setPersonVoided(true);
person
.setPersonVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
person.setPersonVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");

Errors errors = new BindException(person, "person");
PersonValidator personValidator = new PersonValidator();
Expand Down

0 comments on commit 8a25aa0

Please sign in to comment.