use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidatorTest method validate_shouldPassIfStartDateIsEmpty.
/**
* @see RelationshipValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldPassIfStartDateIsEmpty() {
Map<String, String> map = new HashMap<>();
MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
Relationship relationship = new Relationship(1);
relationship.setStartDate(null);
new RelationshipValidator().validate(relationship, errors);
Assert.assertFalse(errors.hasErrors());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidatorTest method validate_shouldFailIfStartDateIsInFuture.
/**
* @see RelationshipValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldFailIfStartDateIsInFuture() {
Relationship relationship = new Relationship(1);
Map<String, String> map = new HashMap<>();
MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
Date nextYear = cal.getTime();
relationship.setStartDate(nextYear);
new RelationshipValidator().validate(relationship, errors);
Assert.assertTrue(errors.hasErrors());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidator method validate.
/**
* Checks that a given Relationship object is valid.
*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
*
* @should fail if end date is prior to the start date
* @should fail if start date is a future date
* @should pass validation if field lengths are correct
* @should fail validation if field lengths are not correct
* @param target Relationship object to be validate
* @param errors Error object to hold any errors encounter in the test
*/
@Override
public void validate(Object target, Errors errors) {
Relationship relationship = (Relationship) target;
if (relationship != null) {
Date startDate = relationship.getStartDate();
Date endDate = relationship.getEndDate();
if (startDate != null && endDate != null && startDate.after(endDate)) {
errors.reject("Relationship.InvalidEndDate.error");
}
ValidateUtil.validateFieldLengths(errors, target.getClass(), "voidReason");
if (startDate != null) {
Date currentDate = new Date();
if (startDate.after(currentDate)) {
errors.reject("error.date.future");
}
}
}
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class PersonServiceTest method voidRelationship_shouldVoidRelationshipWithVoidReasonNullIfGivenRelationshipIsNotVoided.
@Test
public void voidRelationship_shouldVoidRelationshipWithVoidReasonNullIfGivenRelationshipIsNotVoided() {
Relationship relationship = personService.getRelationship(1);
assertFalse("We need an unvoided relationship to test the method", relationship.getVoided());
String voidReason = null;
// TODO - voiding is done by the BaseVoidHandler called via AOP before voidRelationship
// is executed. Coverage of voidRelationship is low because relationship.getVoided() is true
// when entering voidRelationship
// Documented at TRUNK-5151
personService.voidRelationship(relationship, voidReason);
Relationship voidedRelationship = personService.getRelationship(1);
assertTrue(voidedRelationship.getVoided());
assertThat(voidedRelationship.getVoidReason(), is(voidReason));
assertNotNull(voidedRelationship.getDateVoided());
assertEquals(voidedRelationship.getVoidedBy(), Context.getAuthenticatedUser());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class PersonServiceTest method getRelationshipsByPerson_shouldOnlyGetUnvoidedRelationships.
/**
* Tests a voided relationship between personA and Person B to see if it is still listed when
* retrieving unvoided relationships for personA and if it is still listed when retrieving
* unvoided relationships for personB.
*
* @see PersonService#getRelationshipsByPerson(Person)
*/
@Test
public void getRelationshipsByPerson_shouldOnlyGetUnvoidedRelationships() {
executeDataSet(CREATE_PATIENT_XML);
executeDataSet(CREATE_RELATIONSHIP_XML);
Patient p1 = ps.getPatient(6);
Patient p2 = ps.getPatient(8);
// Create a sibling relationship between o1 and p2
Relationship sibling = new Relationship();
sibling.setPersonA(p1);
sibling.setPersonB(p2);
sibling.setRelationshipType(personService.getRelationshipType(4));
personService.saveRelationship(sibling);
// Make p2 the Doctor of p1.
Relationship doctor = new Relationship();
doctor.setPersonB(p1);
doctor.setPersonA(p2);
doctor.setRelationshipType(personService.getRelationshipType(3));
personService.saveRelationship(doctor);
// Void all relationships.
List<Relationship> allRels = personService.getAllRelationships();
for (Relationship r : allRels) {
personService.voidRelationship(r, "Because of a JUnit test.");
}
List<Relationship> updatedARels = personService.getRelationshipsByPerson(p1);
List<Relationship> updatedBRels = personService.getRelationshipsByPerson(p2);
// Neither p1 or p2 should have any relationships now.
assertEquals(0, updatedARels.size());
assertEquals(updatedARels, updatedBRels);
}
Aggregations