Search in sources :

Example 91 with Encounter

use of org.openmrs.Encounter in project openmrs-core by openmrs.

the class EncounterServiceTest method saveEncounter_shouldNotOverwriteDateCreatedIfNonNull.

/**
 * @see EncounterService#saveEncounter(Encounter)
 */
@Test
public void saveEncounter_shouldNotOverwriteDateCreatedIfNonNull() {
    EncounterService encounterService = Context.getEncounterService();
    // the encounter to save without a dateCreated
    Encounter encounter = buildEncounter();
    encounter.setCreator(new User(4));
    // make sure we
    Date date = new Date(System.currentTimeMillis() - 5000);
    // have a
    // date that
    // isn't
    // "right now"
    encounter.setDateCreated(date);
    encounterService.saveEncounter(encounter);
    // make sure the encounter creator is user 4 not user 1
    assertEquals(4, encounter.getCreator().getId().intValue());
    assertNotSame(encounter.getCreator(), Context.getAuthenticatedUser());
    // make sure the encounter date created wasn't overwritten
    assertEquals(DateUtil.truncateToSeconds(date), encounter.getDateCreated());
    // make sure we can fetch this new encounter
    // from the database and its values are the same as the passed in ones
    Encounter newEncounter = encounterService.getEncounter(encounter.getEncounterId());
    assertNotNull(newEncounter);
    assertEquals(DateUtil.truncateToSeconds(date), encounter.getDateCreated());
}
Also used : User(org.openmrs.User) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 92 with Encounter

use of org.openmrs.Encounter in project openmrs-core by openmrs.

the class EncounterServiceTest method saveEncounter_shouldVoidAndCreateNewObsWhenSavingEncounter.

/**
 * @see EncounterService#saveEncounter(Encounter)
 */
@Test
public void saveEncounter_shouldVoidAndCreateNewObsWhenSavingEncounter() {
    // create an encounter
    EncounterService es = Context.getEncounterService();
    Encounter encounter = new Encounter();
    encounter.setLocation(Context.getLocationService().getLocation(1));
    encounter.setEncounterType(es.getEncounterType(1));
    encounter.setEncounterDatetime(new Date());
    encounter.setPatient(Context.getPatientService().getPatient(3));
    // Now add an obs to it
    Obs obs = new Obs();
    obs.setConcept(Context.getConceptService().getConcept(1));
    obs.setValueNumeric(50d);
    encounter.addObs(obs);
    // save the encounter
    es.saveEncounter(encounter);
    // get the id of this obs
    int oldObsId = obs.getObsId();
    // now change the obs value
    obs.setValueNumeric(100d);
    // resave the encounters
    encounter = es.saveEncounter(encounter);
    encounter = es.getEncounter(encounter.getEncounterId());
    // get the new obs id
    int newObsId = encounter.getAllObs().iterator().next().getId();
    Assert.assertTrue(oldObsId != newObsId);
    Assert.assertEquals(2, encounter.getAllObs(true).size());
    Assert.assertEquals(1, encounter.getAllObs().size());
}
Also used : Obs(org.openmrs.Obs) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 93 with Encounter

use of org.openmrs.Encounter in project openmrs-core by openmrs.

the class EncounterServiceTest method saveEncounter_shouldCascadeSaveToContainedObs.

/**
 * You should be able to add an obs to an encounter, save the encounter, and have the obs
 * automatically persisted. Added to test bug reported in ticket #827
 *
 * @see EncounterService#saveEncounter(Encounter)
 */
@Test
public void saveEncounter_shouldCascadeSaveToContainedObs() {
    EncounterService es = Context.getEncounterService();
    // First, create a new Encounter
    Encounter enc = buildEncounter();
    // add an obs to the encounter
    Obs groupObs = new Obs();
    Concept c = Context.getConceptService().getConcept(1);
    groupObs.setConcept(c);
    // add an obs to the group
    Obs childObs = new Obs();
    childObs.setConcept(c);
    childObs.setValueNumeric(50d);
    groupObs.addGroupMember(childObs);
    enc.addObs(groupObs);
    // confirm that save and new enc id are cascaded to obs groupMembers
    // even though childObs aren't directly associated to enc
    assertNotNull("save succeeds without error", es.saveEncounter(enc));
    assertTrue("enc save succeeds", enc.getId() > 0);
    assertNotNull("obs save succeeds", groupObs.getObsId());
    assertEquals("encounter id propogated", groupObs.getEncounter().getId(), enc.getId());
    assertEquals("encounter time propogated", groupObs.getObsDatetime(), enc.getEncounterDatetime());
    assertNotNull("obs save succeeds", childObs.getObsId());
    assertEquals("encounter id propogated", childObs.getEncounter().getId(), enc.getId());
    assertEquals("encounter time propogated", childObs.getObsDatetime(), enc.getEncounterDatetime());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 94 with Encounter

use of org.openmrs.Encounter in project openmrs-core by openmrs.

the class EncounterServiceTest method getEncounter_shouldFailIfUserIsNotAllowedToViewEncounterByGivenId.

/**
 * @see EncounterService#getEncounter(Integer)
 */
@Test(expected = APIException.class)
public void getEncounter_shouldFailIfUserIsNotAllowedToViewEncounterByGivenId() {
    // get encounter that has type with view privilege set
    Encounter encounter = getEncounterWithViewPrivilege();
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    // left this user as is - i.e. without required privilege
    // and authenticate under it's account
    Context.becomeUser(user.getSystemId());
    // have to add privilege in order to be able to call getEncounter(Integer) method
    Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS);
    assertNull(Context.getEncounterService().getEncounter(encounter.getId()));
}
Also used : User(org.openmrs.User) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 95 with Encounter

use of org.openmrs.Encounter in project openmrs-core by openmrs.

the class EncounterServiceTest method getEncountersByVisit_shouldGetActiveEncountersByVisit.

/**
 * @see EncounterService#getEncountersByVisit(Visit, boolean)
 */
@Test
public void getEncountersByVisit_shouldGetActiveEncountersByVisit() {
    List<Encounter> encounters = Context.getEncounterService().getEncountersByVisit(new Visit(1), false);
    assertEquals(1, encounters.size());
}
Also used : Visit(org.openmrs.Visit) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

Encounter (org.openmrs.Encounter)261 Test (org.junit.Test)204 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)153 Patient (org.openmrs.Patient)91 Date (java.util.Date)87 Order (org.openmrs.Order)46 Obs (org.openmrs.Obs)41 Visit (org.openmrs.Visit)40 Location (org.openmrs.Location)38 OrderUtilTest (org.openmrs.order.OrderUtilTest)38 DrugOrder (org.openmrs.DrugOrder)37 TestOrder (org.openmrs.TestOrder)34 EncounterType (org.openmrs.EncounterType)27 ArrayList (java.util.ArrayList)25 Concept (org.openmrs.Concept)25 BindException (org.springframework.validation.BindException)25 Calendar (java.util.Calendar)21 Errors (org.springframework.validation.Errors)21 User (org.openmrs.User)20 PihCoreContextSensitiveTest (org.openmrs.module.pihcore.PihCoreContextSensitiveTest)19