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());
}
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());
}
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());
}
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()));
}
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());
}
Aggregations