Search in sources :

Example 36 with Relationship

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());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 37 with Relationship

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());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) Calendar(java.util.Calendar) MapBindingResult(org.springframework.validation.MapBindingResult) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 38 with Relationship

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");
            }
        }
    }
}
Also used : Relationship(org.openmrs.Relationship) Date(java.util.Date)

Example 39 with Relationship

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());
}
Also used : Relationship(org.openmrs.Relationship) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 40 with Relationship

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);
}
Also used : Relationship(org.openmrs.Relationship) Patient(org.openmrs.Patient) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Relationship (org.openmrs.Relationship)55 Test (org.junit.Test)49 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)49 Person (org.openmrs.Person)19 Date (java.util.Date)14 Patient (org.openmrs.Patient)13 RelationshipType (org.openmrs.RelationshipType)13 HashMap (java.util.HashMap)7 PersonService (org.openmrs.api.PersonService)6 MapBindingResult (org.springframework.validation.MapBindingResult)6 Message (ca.uhn.hl7v2.model.Message)4 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)4 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)3 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 List (java.util.List)2 User (org.openmrs.User)2 HL7Exception (ca.uhn.hl7v2.HL7Exception)1