/** * @see org.openmrs.hl7.db.HL7DAO#garbageCollect() */ @Override public void garbageCollect() { Context.clearSession(); }
Context.clearSession(); counter = 0;
@After public void clearSessionAfterEachTest() { // clear the session to make sure nothing is cached, etc Context.clearSession(); // needed because the authenticatedUser is the only object that sticks // around after tests and the clearSession call if (Context.isSessionOpen()) Context.logout(); }
/** * @see PatientService#getPatients(String) */ @Test public void getPatients_shouldForceSearchStringToBeGreaterThanMinsearchcharactersGlobalProperty() throws Exception { // make sure we can get patients with the default of 3 assertEquals(1, Context.getPatientService().getPatients("Colle").size()); Context.clearSession(); Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MIN_SEARCH_CHARACTERS, "4")); assertEquals(0, Context.getPatientService().getPatients("Col").size()); }
/** * Starts the OpenMRS System Should be called prior to any kind of activity * * @param props Runtime properties to use for startup * @throws InputRequiredException if the {@link DatabaseUpdater} has determined that updates * cannot continue without input from the user * @throws DatabaseUpdateException if database updates are required, see * {@link DatabaseUpdater#executeChangelog()} * @throws ModuleMustStartException if a module that should be started is not able to * @see InputRequiredException#getRequiredInput() InputRequiredException#getRequiredInput() for * the required question/datatypes */ public static synchronized void startup(Properties props) throws DatabaseUpdateException, InputRequiredException, ModuleMustStartException { // do any context database specific startup getContextDAO().startup(props); // find/set/check whether the current database version is compatible checkForDatabaseUpdates(props); // this should be first in the startup routines so that the application // data directory can be set from the runtime properties OpenmrsUtil.startup(props); openSession(); clearSession(); // add any privileges/roles that /must/ exist for openmrs to work // correctly. checkCoreDataset(); getContextDAO().setupSearchIndex(); // Loop over each module and startup each with these custom properties ModuleUtil.startup(props); }
/** * @see PatientService#getPatients(String) */ @Test public void getPatients_shouldAllowSearchStringToBeOneAccordingToMinsearchcharactersGlobalProperty() throws Exception { initializeInMemoryDatabase(); executeDataSet(FIND_PATIENTS_XML); updateSearchIndex(); // make sure the default of "2" kicks in and blocks any results assertEquals(0, Context.getPatientService().getPatients("J").size()); Context.clearSession(); Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MIN_SEARCH_CHARACTERS, "1")); // there is a patient will middle name "F", so this should generate a hit. assertEquals(1, Context.getPatientService().getPatients("F").size()); }
Context.clearSession();
@Test public void saveEncounter_shouldSaveEncounterWhenTopLevelObsIsUpdatedByRemovingChildObs() { executeDataSet(ENC_OBS_HIERARCHY_DATA_XML); EncounterService es = Context.getEncounterService(); ObsService os = Context.getObsService(); Encounter enc = es.getEncounter(100); Obs oParent = os.getObs(100); Obs oChild = os.getObs(101); oParent.removeGroupMember(oChild); es.saveEncounter(enc); Context.flushSession(); Context.clearSession(); enc = es.getEncounter(100); Set<Obs> obsAtTopLevel = enc.getObsAtTopLevel(true); assertEquals(2,obsAtTopLevel.size()); //oChild's new version is still associated to encounter but not part of the oParent anymore assertTrue(obsAtTopLevel.contains(os.getObs(100))); Obs newObs = obsAtTopLevel.stream().filter(obs -> obs.getObsId() != 100 ).findFirst().get(); //Find the other top level obs which is not 100. i.e. the new obs which is a clone of 101 assertNotNull(newObs.getPreviousVersion()); assertEquals(oChild.getObsId(), newObs.getPreviousVersion().getObsId()); assertTrue(os.getObs(100).getGroupMembers(true).contains(os.getObs(102))); //The oChild will still be associated to the parent because when we save oChild, we create a new instance //and void the oChild using ObsServiceImpl.voidExistingObs(). We use Context.evictFromSession(obs); to get a fresh copy //there by losing the change we made to oChild. assertTrue(os.getObs(100).getGroupMembers(true).contains(os.getObs(101))); }
/** * @see ConceptService#saveConceptNameTag(Object,Errors) */ @Test public void saveConceptNameTag_shouldSaveAnEditedNameTag() { ConceptService cs = Context.getConceptService(); ConceptNameTag cnt = cs.getConceptNameTag(1); cnt.setTag("dcba"); Integer id = cs.saveConceptNameTag(cnt).getId(); Context.flushSession(); Context.clearSession(); ConceptNameTag savedNameTag = cs.getConceptNameTag(id); assertEquals(savedNameTag.getTag(), "dcba"); }
/** * @throws ParseException * @see FormService#saveFormResource(org.openmrs.FormResource) */ @Test public void saveFormResource_shouldPersistAFormResource() throws ParseException { Form form = Context.getFormService().getForm(1); FormResource resource = new FormResource(); resource.setForm(form); resource.setName("Start Date"); resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype"); Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16"); resource.setValue(expected); Context.getFormService().saveFormResource(resource); Integer resourceId = resource.getFormResourceId(); Context.clearSession(); FormResource actual = Context.getFormService().getFormResource(resourceId); Assert.assertNotNull(actual); Assert.assertEquals(expected, actual.getValue()); }
@Test public void saveEncounter_shouldSaveEncounterWithComplexObs() { executeDataSet(ENC_OBS_HIERARCHY_DATA_XML); EncounterService es = Context.getEncounterService(); Encounter encounter = es.getEncounter(101); Obs observation = buildObs(); observation.setLocation(encounter.getLocation()); observation.setPerson(encounter.getPatient()); encounter.addObs(observation); es.saveEncounter(encounter); Context.flushSession(); Context.clearSession(); encounter = es.getEncounter(101); assertEquals(2, encounter.getObsAtTopLevel(true).size()); }
/** * @see VisitService#saveVisit(Visit) */ @Test public void saveVisit_shouldAssociateEncounterWithVisitOnSaveEncounter() { Visit visit = visitService.getVisit(1); EncounterService es = Context.getEncounterService(); Encounter encounter = new Encounter(); encounter.setEncounterDatetime(new Date()); encounter.setPatient(visit.getPatient()); encounter.setLocation(visit.getLocation()); encounter.setEncounterType(es.getEncounterType(1)); visit.addEncounter(encounter); Context.getEncounterService().saveEncounter(encounter); Integer encounterId = encounter.getEncounterId(); Context.flushSession(); Context.clearSession(); // reload the visit visit = visitService.getVisit(1); assertEquals(1, visit.getEncounters().size()); assertEquals(encounterId, ((Encounter) visit.getEncounters().toArray()[0]).getEncounterId()); }
/** * This test ensures that the getter for previous order field returns objects of the actual sub * types for subclasses instead proxies that are instances of Order * * @throws Exception */ @Test public void shouldNotReturnAProxyForPreviousOrder() { Order dcOrder = orderService.getOrder(22); Order previousOrder = dcOrder.getPreviousOrder(); assertNotNull(previousOrder); Order testOrder = orderService.getOrder(7); Order dcTestOrder = orderService.discontinueOrder(testOrder, "Testing", null, testOrder.getOrderer(), testOrder .getEncounter()); Context.flushSession(); Context.clearSession(); dcTestOrder = orderService.getOrder(dcTestOrder.getOrderId()).getPreviousOrder(); }
@Test @Ignore public void setImplementationId_shouldOverwriteImplementationIdInDatabaseIfExists() { executeDataSet("org/openmrs/api/include/AdministrationServiceTest-general.xml"); // sanity check to make sure we have an implementation id assertNotNull(adminService.getImplementationId()); Context.clearSession(); // so a NonUniqueObjectException doesn't occur on the global property later // save a second valid id ImplementationId validId2 = new ImplementationId(); validId2.setImplementationId("JUNIT-TEST 2"); validId2.setName("JUNIT-TEST (#2) implementation id"); validId2.setPassphrase("This is the junit test passphrase 2"); validId2.setDescription("This is the junit impl id (2) used for testing of the openmrs API only."); adminService.setImplementationId(validId2); assertEquals(validId2, adminService.getImplementationId()); }
Context.clearSession();
/** * @see ConceptService#saveConceptNameTag(Object,Errors) */ @Test public void saveConceptNameTag_shouldSaveATagIfItIsSupplied() { ConceptNameTag cnt = new ConceptNameTag(); cnt.setTag("abcd"); cnt.setDescription("test"); ConceptService cs = Context.getConceptService(); Integer id = cs.saveConceptNameTag(cnt).getId(); Context.flushSession(); Context.clearSession(); ConceptNameTag savedNameTag = cs.getConceptNameTag(id); assertEquals(savedNameTag.getTag(), "abcd"); assertEquals(savedNameTag.getDescription(), "test"); }
/** * @see PatientService#savePatient(Patient) */ @Test public void savePatient_shouldCreateNewPatientFromExistingPersonPlusUserObject() throws Exception { // sanity check, make sure there isn't a 501 patient already Patient oldPatient = patientService.getPatient(501); Assert.assertNull(oldPatient); // fetch Bruno from the database Person existingPerson = Context.getPersonService().getPerson(501); Context.clearSession(); Patient patient = new Patient(existingPerson); PatientIdentifier patientIdentifier = new PatientIdentifier("some identifier", new PatientIdentifierType(2), new Location(1)); patientIdentifier.setPreferred(true); patient.addIdentifier(patientIdentifier); patientService.savePatient(patient); Assert.assertEquals(501, patient.getPatientId().intValue()); // make sure a new row with a patient id WAS created Assert.assertNotNull(patientService.getPatient(501)); // make sure a new row with a new person id WASN'T created Assert.assertNull(patientService.getPatient(503)); }
/** * @see EncounterService#unvoidEncounter(Encounter) */ @Test public void saveEncounter_shouldSetDateStoppedOnTheOriginalAfterAddingReviseOrder() { EncounterService es = Context.getEncounterService(); TestOrder order = (TestOrder) Context.getOrderService().getOrder(7); Assert.assertNull(order.getDateStopped()); Encounter encounter = es.getEncounter(6); TestOrder reviseOrder = order.cloneForRevision(); reviseOrder.setOrderer(Context.getProviderService().getProvider(1)); encounter.addOrder(reviseOrder); es.saveEncounter(encounter); Context.flushSession(); Context.clearSession(); Date dateStopped = Context.getOrderService().getOrder(7).getDateStopped(); Assert.assertNotNull(dateStopped); }
@Test public void saveEncounter_shouldUpdateExistingEncounterWhenAChildObsIsEdited() { executeDataSet(ENC_OBS_HIERARCHY_DATA_XML); EncounterService es = Context.getEncounterService(); ObsService os = Context.getObsService(); Encounter enc = es.getEncounter(100); Obs o = os.getObs(101); o.setValueText("Obs value updated"); es.saveEncounter(enc); Context.flushSession(); Context.clearSession(); updateSearchIndex(); enc = es.getEncounter(100); Set<Obs> obsAtTopLevelUpdated = enc.getObsAtTopLevel(true); Obs oParent = os.getObs(100); final Obs editedObs = os.getObs(101); Obs o2 = os.getObs(102); Obs o3 = getNewVersionOfEditedObs(oParent,editedObs); assertEquals(1,obsAtTopLevelUpdated.size()); assertEquals(3, oParent.getGroupMembers(true).size()); assertTrue(editedObs.getVoided()); assertFalse(oParent.getVoided()); assertFalse(o2.getVoided()); assertNotNull(o3); assertFalse(o3.getVoided()); assertEquals("Obs value updated", o3.getValueText()); }
/** * @see PersonService#voidPerson(Person, String) */ @Test public void voidPerson_shouldReturnVoidedPersonWithGivenReason() throws Exception { executeDataSet("org/openmrs/api/include/PersonServiceTest-createPersonPurgeVoidTest.xml"); Person person = Context.getPersonService().getPerson(1001); Context.getPersonService().voidPerson(person, "Test Voiding Person"); Context.flushSession(); Context.clearSession(); Person voidedPerson = Context.getPersonService().getPerson(1001); assertTrue(voidedPerson.getVoided()); Assert.assertNotNull(voidedPerson.getVoidedBy()); Assert.assertNotNull(voidedPerson.getPersonVoidReason()); Assert.assertNotNull(voidedPerson.getPersonDateVoided()); Assert.assertEquals(voidedPerson.getPersonVoidReason(), "Test Voiding Person"); }