### Question:
CohortValidator implements Validator { @Override public void validate(Object obj, Errors errors) { if (obj == null || !(obj instanceof Cohort)) { throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + Cohort.class); } Cohort cohort = (Cohort) obj; if (!cohort.getVoided()) { Collection<CohortMembership> members = cohort.getMemberships(); if (!CollectionUtils.isEmpty(members)) { for (CohortMembership member : members) { Patient p = Context.getPatientService().getPatient(member.getPatientId()); int dateCompare = OpenmrsUtil.compareWithNullAsLatest(member.getStartDate(), member.getEndDate()); if (p != null && p.getVoided() && !member.getVoided()) { String message = "Patient " + p.getPatientId() + " is voided, cannot add voided members to a cohort"; errors.rejectValue("memberships", "Cohort.patientAndMemberShouldBeVoided", message); } if (dateCompare == 1) { String message = "Start date is null or end date is before start date"; errors.rejectValue("memberships", "Cohort.startDateShouldNotBeNullOrBeforeEndDate", message); } } } } }  @Override boolean supports(Class<?> c); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void shouldFailIfGivenNull() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(nullOrIncompatibleObjErrorMessage); validator.validate(null, errors); }
@Test public void shouldFailIfGivenInstanceOfOtherClassThanCohort() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(nullOrIncompatibleObjErrorMessage); validator.validate(new Patient(), errors); }
@Test public void validate_shouldFailIfPatientIsVoided() { patient.setVoided(true); validator.validate(cohort, errors); Assert.assertTrue(errors.hasFieldErrors("memberships")); String eMessage = "Patient " + patient.getPatientId() + " is voided, cannot add voided members to a cohort"; Assert.assertEquals(eMessage, errors.getFieldError("memberships").getDefaultMessage()); }
@Test public void validate_shouldPassIfPatientIsNonVoided() { validator.validate(cohort, errors); Assert.assertFalse(errors.hasErrors()); Assert.assertFalse(errors.hasFieldErrors("memberships")); }
@Test public void validate_shouldPassIfMembershipisVoided() { cohortMembership.setVoided(true); validator.validate(cohort, errors); Assert.assertFalse(errors.hasErrors()); Assert.assertFalse(errors.hasFieldErrors("memberships")); }
@Test public void validate_shouldPassIfPatientAndMembershipAreVoided() { patient.setVoided(true); cohortMembership.setVoided(true); validator.validate(cohort, errors); Assert.assertFalse(errors.hasErrors()); Assert.assertFalse(errors.hasFieldErrors("memberships")); }
@Test public void validate_shouldPassIfMembershipStartDateIsAfterEndDate() throws Exception { Cohort cohort = new Cohort(2); CohortMembership membership = new CohortMembership(patient.getPatientId()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormat.parse("2016-11-01 00:00:00"); Date endDate = dateFormat.parse("2015-01-01 00:00:00"); membership.setStartDate(startDate); membership.setEndDate(endDate); Errors errors = new BindException(cohort, "cohort"); new CohortValidator().validate(cohort, errors); Assert.assertFalse(errors.hasFieldErrors("memberships")); }