k-joseph

Untitled

Aug 27th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 104.71 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs.api;
  15.  
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNotSame;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22.  
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Calendar;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Vector;
  32.  
  33. import org.junit.Assert;
  34. import org.junit.Before;
  35. import org.junit.Ignore;
  36. import org.junit.Test;
  37. import org.openmrs.Cohort;
  38. import org.openmrs.Concept;
  39. import org.openmrs.Encounter;
  40. import org.openmrs.EncounterRole;
  41. import org.openmrs.EncounterType;
  42. import org.openmrs.Form;
  43. import org.openmrs.GlobalProperty;
  44. import org.openmrs.Location;
  45. import org.openmrs.Obs;
  46. import org.openmrs.Order;
  47. import org.openmrs.Patient;
  48. import org.openmrs.Privilege;
  49. import org.openmrs.Provider;
  50. import org.openmrs.Role;
  51. import org.openmrs.User;
  52. import org.openmrs.Visit;
  53. import org.openmrs.VisitType;
  54. import org.openmrs.api.context.Context;
  55. import org.openmrs.api.handler.EncounterVisitHandler;
  56. import org.openmrs.api.handler.ExistingOrNewVisitAssignmentHandler;
  57. import org.openmrs.api.handler.ExistingVisitAssignmentHandler;
  58. import org.openmrs.api.handler.NoVisitAssignmentHandler;
  59. import org.openmrs.test.BaseContextSensitiveTest;
  60. import org.openmrs.test.Verifies;
  61. import org.openmrs.util.OpenmrsConstants;
  62. import org.openmrs.util.PrivilegeConstants;
  63.  
  64. /**
  65. * Tests all methods in the {@link EncounterService}
  66. */
  67. public class EncounterServiceTest extends BaseContextSensitiveTest {
  68.  
  69. protected static final String ENC_INITIAL_DATA_XML = "org/openmrs/api/include/EncounterServiceTest-initialData.xml";
  70.  
  71. protected static final String UNIQUE_ENC_WITH_PAGING_XML = "org/openmrs/api/include/EncounterServiceTest-pagingWithUniqueEncounters.xml";
  72.  
  73. /**
  74. * This method is run before all of the tests in this class because it has the @Before
  75. * annotation on it. This will add the contents of {@link #ENC_INITIAL_DATA_XML} to the current
  76. * database
  77. *
  78. * @see BaseContextSensitiveTest#runBeforeAllUnitTests()
  79. * @throws Exception
  80. */
  81. @Before
  82. public void runBeforeEachTest() throws Exception {
  83. executeDataSet(ENC_INITIAL_DATA_XML);
  84. }
  85.  
  86. /**
  87. * @see {@link EncounterService#saveEncounter(Encounter)}
  88. */
  89. @Test
  90. @Verifies(value = "should save encounter with basic details", method = "saveEncounter(Encounter)")
  91. public void saveEncounter_shouldSaveEncounterWithBasicDetails() throws Exception {
  92. Encounter encounter = buildEncounter();
  93.  
  94. EncounterService es = Context.getEncounterService();
  95. es.saveEncounter(encounter);
  96.  
  97. assertNotNull("The saved encounter should have an encounter id now", encounter.getEncounterId());
  98. Encounter newSavedEncounter = es.getEncounter(encounter.getEncounterId());
  99. assertNotNull("We should get back an encounter", newSavedEncounter);
  100. assertTrue("The created encounter needs to equal the pojo encounter", encounter.equals(newSavedEncounter));
  101. }
  102.  
  103. /**
  104. * @see {@link EncounterService#saveEncounter(Encounter)}
  105. */
  106. @Test
  107. @Verifies(value = "should update encounter successfully", method = "saveEncounter(Encounter)")
  108. public void saveEncounter_shouldUpdateEncounterSuccessfully() throws Exception {
  109. EncounterService es = Context.getEncounterService();
  110.  
  111. // get the encounter from the database
  112. Encounter encounter = es.getEncounter(1);
  113.  
  114. // save the current values for comparison later
  115. Patient origPatient = encounter.getPatient();
  116. Location origLocation = encounter.getLocation();
  117. Date origDate = encounter.getEncounterDatetime();
  118. EncounterType origEncType = encounter.getEncounterType();
  119.  
  120. // add values that are different than the ones in the initialData.xml
  121. // file
  122. Location loc2 = new Location(2);
  123. EncounterType encType2 = new EncounterType(2);
  124. Date d2 = new Date();
  125. Patient pat2 = new Patient(2);
  126.  
  127. encounter.setLocation(loc2);
  128. encounter.setEncounterType(encType2);
  129. encounter.setEncounterDatetime(d2);
  130. encounter.setPatient(pat2);
  131.  
  132. // save to the db
  133. es.saveEncounter(encounter);
  134.  
  135. // fetch that encounter from the db
  136. Encounter newestEnc = es.getEncounter(encounter.getEncounterId());
  137.  
  138. assertFalse("The location should be different", origLocation.equals(loc2));
  139. assertTrue("The location should be different", newestEnc.getLocation().equals(loc2));
  140. assertFalse("The enc should have changed", origEncType.equals(encType2));
  141. assertTrue("The enc type needs to have been set", newestEnc.getEncounterType().equals(encType2));
  142. assertFalse("Make sure the dates changed slightly", origDate.equals(d2));
  143. assertTrue("The date needs to have been set", newestEnc.getEncounterDatetime().equals(d2));
  144. assertFalse("The patient should be different", origPatient.equals(pat2));
  145. assertTrue("The patient should have been set", newestEnc.getPatient().equals(pat2));
  146. }
  147.  
  148. /**
  149. * You should be able to add an obs to an encounter, save the encounter, and have the obs
  150. * automatically persisted. Added to test bug reported in ticket #827
  151. *
  152. * @see {@link EncounterService#saveEncounter(Encounter)}
  153. */
  154. @Test
  155. @Verifies(value = "should cascade save to contained obs", method = "saveEncounter(Encounter)")
  156. public void saveEncounter_shouldCascadeSaveToContainedObs() throws Exception {
  157. EncounterService es = Context.getEncounterService();
  158.  
  159. // First, create a new Encounter
  160. Encounter enc = buildEncounter();
  161.  
  162. //add an obs to the encounter
  163. Obs groupObs = new Obs();
  164. groupObs.setConcept(new Concept(1));
  165. groupObs.setValueNumeric(50d);
  166.  
  167. // add an obs to the group
  168. Obs childObs = new Obs();
  169. childObs.setConcept(new Concept(1));
  170. childObs.setValueNumeric(50d);
  171. groupObs.addGroupMember(childObs);
  172. enc.addObs(groupObs);
  173.  
  174. //confirm that save and new enc id are cascaded to obs groupMembers
  175. //even though childObs aren't directly associated to enc
  176. assertNotNull("save succeeds without error", es.saveEncounter(enc));
  177. assertTrue("enc save succeeds", enc.getId() > 0);
  178.  
  179. assertNotNull("obs save succeeds", groupObs.getObsId());
  180. assertEquals("encounter id propogated", groupObs.getEncounter().getId(), enc.getId());
  181. assertEquals("encounter time propogated", groupObs.getObsDatetime(), enc.getEncounterDatetime());
  182. assertNotNull("obs save succeeds", childObs.getObsId());
  183. assertEquals("encounter id propogated", childObs.getEncounter().getId(), enc.getId());
  184. assertEquals("encounter time propogated", childObs.getObsDatetime(), enc.getEncounterDatetime());
  185.  
  186. }
  187.  
  188. /**
  189. * When you save the encounter with a changed location, the location change should be cascaded
  190. * to all the obs associated with the encounter that had the same location as the encounter.
  191. *
  192. * @see {@link EncounterService#saveEncounter(Encounter)}
  193. */
  194. @Test
  195. @Verifies(value = "should cascade location change in encounter to contained obs", method = "saveEncounter(Encounter)")
  196. public void saveEncounter_shouldCascadeChangeOfLocationInEncounterToContainedObs() throws Exception {
  197. EncounterService es = Context.getEncounterService();
  198. Encounter enc = buildEncounter();
  199.  
  200. es.saveEncounter(enc);
  201.  
  202. // Now add an obs to it
  203. Obs newObs = new Obs();
  204. newObs.setConcept(new Concept(1));
  205. newObs.setValueNumeric(50d);
  206. Location location = new Location(1);
  207. newObs.setLocation(location);
  208.  
  209. enc.addObs(newObs);
  210. es.saveEncounter(enc);
  211.  
  212. enc.setLocation(location);
  213. es.saveEncounter(enc);
  214. assertEquals(enc.getLocation(), newObs.getLocation());
  215. }
  216.  
  217. private Encounter buildEncounter() {
  218. // First, create a new Encounter
  219. Encounter enc = new Encounter();
  220. enc.setLocation(Context.getLocationService().getLocation(1));
  221. enc.setEncounterType(Context.getEncounterService().getEncounterType(1));
  222. enc.setEncounterDatetime(new Date());
  223. enc.setPatient(Context.getPatientService().getPatient(3));
  224. enc.addProvider(Context.getEncounterService().getEncounterRole(1), Context.getProviderService().getProvider(1));
  225. return enc;
  226. }
  227.  
  228. /**
  229. * When you save the encounter with a changed location, the location change should not be
  230. * cascaded to all the obs associated with the encounter that had a different location from
  231. * encounter's location.
  232. *
  233. * @see {@link EncounterService#saveEncounter(Encounter)}
  234. */
  235. @Test
  236. @Verifies(value = "should not cascade location change in encounter to contained obs if the initial location of the encounter and the obs are different", method = "saveEncounter(Encounter)")
  237. public void saveEncounter_shouldNotCascadeLocationChangeForDifferentInitialLocations() throws Exception {
  238. EncounterService es = Context.getEncounterService();
  239.  
  240. // First, create a new Encounter
  241. Encounter enc = buildEncounter();
  242. es.saveEncounter(enc);
  243.  
  244. // Now add an obs to it
  245. Obs newObs = buildObs();
  246.  
  247. enc.addObs(newObs);
  248. es.saveEncounter(enc);
  249.  
  250. enc.setLocation(new Location(2));
  251. es.saveEncounter(enc);
  252. assertNotSame(enc.getLocation(), newObs.getLocation());
  253. }
  254.  
  255. private Obs buildObs() {
  256. Obs newObs = new Obs();
  257. newObs.setConcept(new Concept(1));
  258. newObs.setValueNumeric(50d);
  259. newObs.setLocation(new Location(2));
  260. return newObs;
  261. }
  262.  
  263. /**
  264. * Make sure that purging an encounter removes the row from the database
  265. *
  266. * @see {@link EncounterService#purgeEncounter(Encounter)}
  267. */
  268. @Test
  269. @Verifies(value = "should purgeEncounter", method = "purgeEncounter(Encounter)")
  270. public void purgeEncounter_shouldPurgeEncounter() throws Exception {
  271.  
  272. EncounterService es = Context.getEncounterService();
  273.  
  274. // fetch the encounter to delete from the db
  275. Encounter encounterToDelete = es.getEncounter(1);
  276.  
  277. es.purgeEncounter(encounterToDelete);
  278.  
  279. // try to refetch the encounter. should get a null object
  280. Encounter e = es.getEncounter(encounterToDelete.getEncounterId());
  281. assertNull("We shouldn't find the encounter after deletion", e);
  282. }
  283.  
  284. /**
  285. * Make sure that purging an encounter removes the row from the database
  286. *
  287. * @see {@link EncounterService#purgeEncounter(Encounter,null)}
  288. */
  289. @Test
  290. @Verifies(value = "should cascade purge to obs and orders", method = "purgeEncounter(Encounter,null)")
  291. public void purgeEncounter_shouldCascadePurgeToObsAndOrders() throws Exception {
  292.  
  293. EncounterService es = Context.getEncounterService();
  294.  
  295. // fetch the encounter to delete from the db
  296. Encounter encounterToDelete = es.getEncounter(1);
  297.  
  298. es.purgeEncounter(encounterToDelete, true);
  299.  
  300. // try to refetch the encounter. should get a null object
  301. Encounter e = es.getEncounter(encounterToDelete.getEncounterId());
  302. assertNull("We shouldn't find the encounter after deletion", e);
  303.  
  304. ObsService obsService = Context.getObsService();
  305. assertNull(obsService.getObs(1));
  306. assertNull(obsService.getObs(2));
  307. assertNull(obsService.getObs(3));
  308.  
  309. assertNull(Context.getOrderService().getOrder(1));
  310. }
  311.  
  312. /**
  313. * You should be able to add an obs to an encounter, save the encounter, and have the obs
  314. * automatically persisted.
  315. *
  316. * @see {@link EncounterService#saveEncounter(Encounter)}
  317. */
  318. @Test
  319. @Verifies(value = "should cascade save to contained obs when encounter already exists", method = "saveEncounter(Encounter)")
  320. public void saveEncounter_shouldCascadeSaveToContainedObsWhenEncounterAlreadyExists() throws Exception {
  321. EncounterService es = Context.getEncounterService();
  322.  
  323. // get an encounter from the database
  324. Encounter encounter = es.getEncounter(1);
  325. assertNotNull(encounter.getEncounterDatetime());
  326.  
  327. // Now add an obs to it
  328. Obs obs = new Obs();
  329. obs.setConcept(new Concept(1));
  330. obs.setValueNumeric(50d);
  331. obs.setObsDatetime(new Date());
  332. encounter.addObs(obs);
  333.  
  334. // there should not be an obs id before saving the encounter
  335. assertNull(obs.getObsId());
  336.  
  337. es.saveEncounter(encounter);
  338.  
  339. // the obs id should have been populated during the save
  340. assertNotNull(obs.getObsId());
  341. }
  342.  
  343. /**
  344. * You should be able to add an obs without an obsDatetime to an encounter, save the encounter,
  345. * and have the obs automatically persisted with the same date as the encounter. Added to test
  346. * bug reported in {@link Ticket#827}
  347. *
  348. * @see {@link EncounterService#saveEncounter(Encounter)}
  349. */
  350. @Test
  351. @Verifies(value = "should cascade encounter datetime to obs", method = "saveEncounter(Encounter)")
  352. public void saveEncounter_shouldCascadeEncounterDatetimeToObs() throws Exception {
  353. EncounterService es = Context.getEncounterService();
  354.  
  355. // get an encounter from the database
  356. Encounter encounter = es.getEncounter(1);
  357. assertNotNull(encounter.getEncounterDatetime());
  358.  
  359. // Now add an obs to it and do NOT set the obs datetime
  360. Obs obs = new Obs();
  361. obs.setConcept(new Concept(1));
  362. obs.setValueNumeric(50d);
  363. encounter.addObs(obs);
  364.  
  365. es.saveEncounter(encounter);
  366.  
  367. assertTrue(encounter.getEncounterDatetime().equals(obs.getObsDatetime()));
  368. }
  369.  
  370. /**
  371. * When the date on an encounter is modified and then saved, the encounterservice changes all of
  372. * the obsdatetimes to the new datetime. This test is showing error
  373. * http://dev.openmrs.org/ticket/934
  374. *
  375. * @see {@link EncounterService#saveEncounter(Encounter)}
  376. */
  377. @Test
  378. @Verifies(value = "should only cascade the obsdatetimes to obs with different initial obsdatetimes", method = "saveEncounter(Encounter)")
  379. public void saveEncounter_shouldOnlyCascadeTheObsdatetimesToObsWithDifferentInitialObsdatetimes() throws Exception {
  380. EncounterService es = Context.getEncounterService();
  381. Encounter enc = es.getEncounter(1);
  382. Date newDate = new Date();
  383. enc.setEncounterDatetime(newDate);
  384.  
  385. // save the encounter. The obs should pick up the encounter's date
  386. es.saveEncounter(enc);
  387.  
  388. boolean foundObs3 = false;
  389. for (Obs obs : enc.getAllObs()) {
  390. if (obs.getObsId().equals(3)) {
  391. // make sure different obs datetimes from the encounter datetime
  392. // are not edited to the new time
  393. assertNotSame(newDate, obs.getObsDatetime());
  394. foundObs3 = true;
  395. } else
  396. // make sure all obs were changed
  397. assertEquals(newDate, obs.getObsDatetime());
  398. }
  399. assertTrue(foundObs3);
  400. }
  401.  
  402. /**
  403. * @see {@link EncounterService#saveEncounter(Encounter)}
  404. */
  405. @Test
  406. @Verifies(value = "should not overwrite creator if non null", method = "saveEncounter(Encounter)")
  407. public void saveEncounter_shouldNotOverwriteCreatorIfNonNull() throws Exception {
  408. EncounterService encounterService = Context.getEncounterService();
  409.  
  410. // the encounter to save with a non null creator
  411. Encounter encounter = buildEncounter();
  412. encounter.setCreator(new User(4));
  413.  
  414. // make sure the logged in user isn't the user we're testing with
  415. assertNotSame(encounter.getCreator(), Context.getAuthenticatedUser());
  416.  
  417. encounterService.saveEncounter(encounter);
  418.  
  419. // make sure the encounter creator is user 4 not user 1
  420. assertEquals(4, encounter.getCreator().getId().intValue());
  421.  
  422. // make sure we can fetch this new encounter
  423. // from the database and its values are the same as the passed in ones
  424. Encounter newEncounter = encounterService.getEncounter(encounter.getEncounterId());
  425. assertNotNull(newEncounter);
  426. assertEquals(4, encounter.getCreator().getId().intValue());
  427. assertNotSame(encounter.getCreator(), Context.getAuthenticatedUser());
  428. }
  429.  
  430. /**
  431. * @see {@link EncounterService#saveEncounter(Encounter)}
  432. */
  433. @Test
  434. @Verifies(value = "should not overwrite dateCreated if non null", method = "saveEncounter(Encounter)")
  435. public void saveEncounter_shouldNotOverwriteDateCreatedIfNonNull() throws Exception {
  436. EncounterService encounterService = Context.getEncounterService();
  437.  
  438. // the encounter to save without a dateCreated
  439. Encounter encounter = buildEncounter();
  440. encounter.setCreator(new User(4));
  441. Date date = new Date(System.currentTimeMillis() - 5000); // make sure we
  442. // have a
  443. // date that
  444. // isn't
  445. // "right now"
  446. encounter.setDateCreated(date);
  447.  
  448. encounterService.saveEncounter(encounter);
  449.  
  450. // make sure the encounter creator is user 4 not user 1
  451. assertEquals(4, encounter.getCreator().getId().intValue());
  452. assertNotSame(encounter.getCreator(), Context.getAuthenticatedUser());
  453.  
  454. // make sure the encounter date created wasn't overwritten
  455. assertEquals(date, encounter.getDateCreated());
  456.  
  457. // make sure we can fetch this new encounter
  458. // from the database and its values are the same as the passed in ones
  459. Encounter newEncounter = encounterService.getEncounter(encounter.getEncounterId());
  460. assertNotNull(newEncounter);
  461. assertEquals(date, encounter.getDateCreated());
  462. }
  463.  
  464. /**
  465. * Make sure the obs and order creator and dateCreated is preserved when passed into
  466. * {@link EncounterService#saveEncounter(Encounter)}
  467. *
  468. * @see {@link EncounterService#saveEncounter(Encounter)}
  469. */
  470. @Test
  471. @Verifies(value = "should not overwrite obs and orders creator or dateCreated", method = "saveEncounter(Encounter)")
  472. public void saveEncounter_shouldNotOverwriteObsAndOrdersCreatorOrDateCreated() throws Exception {
  473. EncounterService encounterService = Context.getEncounterService();
  474.  
  475. // the encounter to save without a dateCreated
  476. Encounter encounter = buildEncounter();
  477. Date date = new Date(System.currentTimeMillis() - 5000); // make sure we
  478. // have a
  479. // date that
  480. // isn't
  481. // "right now"
  482. encounter.setDateCreated(date);
  483. User creator = new User(1);
  484. encounter.setCreator(creator);
  485.  
  486. // create and add an obs to this encounter
  487. Obs obs = new Obs(new Patient(2), new Concept(1), new Date(), new Location(1));
  488. obs.setDateCreated(date);
  489. obs.setCreator(creator);
  490. encounter.addObs(obs);
  491.  
  492. // create and add an order to this encounter
  493. Order order = new Order();
  494. order.setConcept(new Concept(1));
  495. order.setPatient(new Patient(2));
  496. order.setStartDate(new Date());
  497. order.setDateCreated(date);
  498. order.setCreator(creator);
  499. encounter.addOrder(order);
  500.  
  501. // make sure the logged in user isn't the user we're testing with
  502. assertNotSame(encounter.getCreator(), Context.getAuthenticatedUser());
  503.  
  504. encounterService.saveEncounter(encounter);
  505.  
  506. // make sure the obs date created and creator are the same as what we
  507. // set
  508. Obs createdObs = Context.getObsService().getObs(obs.getObsId());
  509. assertEquals(date, createdObs.getDateCreated());
  510. assertEquals(creator, createdObs.getCreator());
  511.  
  512. // make sure the order date created and creator are the same as what we
  513. // set
  514. Order createdOrder = Context.getOrderService().getOrder(order.getOrderId());
  515. assertEquals(date, createdOrder.getDateCreated());
  516. assertEquals(creator, createdOrder.getCreator());
  517. }
  518.  
  519. /**
  520. * @see {@link EncounterService#getEncountersByPatient(Patient)}
  521. */
  522. @Test
  523. @Verifies(value = "should not get voided encounters", method = "getEncountersByPatient(Patient)")
  524. public void getEncountersByPatient_shouldNotGetVoidedEncounters() throws Exception {
  525. EncounterService encounterService = Context.getEncounterService();
  526.  
  527. List<Encounter> encounters = encounterService.getEncountersByPatient(new Patient(3));
  528. assertEquals(2, encounters.size());
  529. }
  530.  
  531. /**
  532. * @see {@link EncounterService#getEncountersByPatient(Patient)}
  533. */
  534. @Test(expected = IllegalArgumentException.class)
  535. @Verifies(value = "should throw error when given null parameter", method = "getEncountersByPatient(Patient)")
  536. public void getEncountersByPatient_shouldThrowErrorWhenGivenNullParameter() throws Exception {
  537. Context.getEncounterService().getEncountersByPatient((Patient) null);
  538. }
  539.  
  540. /**
  541. * @see {@link EncounterService#getEncountersByPatientId(Integer)}
  542. */
  543. @Test
  544. @Verifies(value = "should not get voided encounters", method = "getEncountersByPatientId(Integer)")
  545. public void getEncountersByPatientId_shouldNotGetVoidedEncounters() throws Exception {
  546. EncounterService encounterService = Context.getEncounterService();
  547.  
  548. List<Encounter> encounters = encounterService.getEncountersByPatientId(3);
  549. assertEquals(2, encounters.size());
  550. }
  551.  
  552. /**
  553. * @see {@link EncounterService#getEncountersByPatientId(Integer)}
  554. */
  555. @Test(expected = IllegalArgumentException.class)
  556. @Verifies(value = "should throw error if given a null parameter", method = "getEncountersByPatientId(Integer)")
  557. public void getEncountersByPatientId_shouldThrowErrorIfGivenANullParameter() throws Exception {
  558. Context.getEncounterService().getEncountersByPatientId(null);
  559. }
  560.  
  561. /**
  562. * @see {@link EncounterService#getEncountersByPatientIdentifier(String)}
  563. */
  564. @Test
  565. @Verifies(value = "should not get voided encounters", method = "getEncountersByPatientIdentifier(String)")
  566. public void getEncountersByPatientIdentifier_shouldNotGetVoidedEncounters() throws Exception {
  567. EncounterService encounterService = Context.getEncounterService();
  568.  
  569. List<Encounter> encounters = encounterService.getEncountersByPatientIdentifier("12345");
  570. assertEquals(2, encounters.size());
  571. }
  572.  
  573. /**
  574. * @see {@link EncounterService#getEncountersByPatientIdentifier(String)}
  575. */
  576. @Test(expected = IllegalArgumentException.class)
  577. @Verifies(value = "should throw error if given null parameter", method = "getEncountersByPatientIdentifier(String)")
  578. public void getEncountersByPatientIdentifier_shouldThrowErrorIfGivenNullParameter() throws Exception {
  579. Context.getEncounterService().getEncountersByPatientIdentifier(null);
  580. }
  581.  
  582. /**
  583. * Make sure {@link EncounterService#voidEncounter(Encounter, String)} marks all the voided
  584. * stuff correctly
  585. *
  586. * @see {@link EncounterService#voidEncounter(Encounter,String)}
  587. */
  588. @Test
  589. @Verifies(value = "should void encounter and set attributes", method = "voidEncounter(Encounter,String)")
  590. public void voidEncounter_shouldVoidEncounterAndSetAttributes() throws Exception {
  591. EncounterService encounterService = Context.getEncounterService();
  592.  
  593. // get a nonvoided encounter
  594. Encounter encounter = encounterService.getEncounter(1);
  595. assertFalse(encounter.isVoided());
  596. assertNull(encounter.getVoidedBy());
  597. assertNull(encounter.getVoidReason());
  598. assertNull(encounter.getDateVoided());
  599.  
  600. Encounter voidedEnc = encounterService.voidEncounter(encounter, "Just Testing");
  601.  
  602. // make sure its still the same object
  603. assertEquals(voidedEnc, encounter);
  604.  
  605. // make sure that all the values were filled in
  606. assertTrue(voidedEnc.isVoided());
  607. assertNotNull(voidedEnc.getDateVoided());
  608. assertEquals(Context.getAuthenticatedUser(), voidedEnc.getVoidedBy());
  609. assertEquals("Just Testing", voidedEnc.getVoidReason());
  610. }
  611.  
  612. /**
  613. * @see {@link EncounterService#voidEncounter(Encounter,String)}
  614. */
  615. @Test
  616. @Verifies(value = "should cascade to obs", method = "voidEncounter(Encounter,String)")
  617. public void voidEncounter_shouldCascadeToObs() throws Exception {
  618. EncounterService encounterService = Context.getEncounterService();
  619.  
  620. // get a nonvoided encounter that has some obs
  621. Encounter encounter = encounterService.getEncounter(1);
  622. encounterService.voidEncounter(encounter, "Just Testing");
  623.  
  624. Obs obs = Context.getObsService().getObs(1);
  625. assertTrue(obs.isVoided());
  626. assertEquals("Just Testing", obs.getVoidReason());
  627. }
  628.  
  629. /**
  630. * @see {@link EncounterService#voidEncounter(Encounter,String)}
  631. */
  632. @Test
  633. @Verifies(value = "should cascade to orders", method = "voidEncounter(Encounter,String)")
  634. public void voidEncounter_shouldCascadeToOrders() throws Exception {
  635. EncounterService encounterService = Context.getEncounterService();
  636.  
  637. // get a nonvoided encounter that has some obs
  638. Encounter encounter = encounterService.getEncounter(1);
  639. encounterService.voidEncounter(encounter, "Just Testing");
  640.  
  641. Order order = Context.getOrderService().getOrder(1);
  642. assertTrue(order.isVoided());
  643. assertEquals("Just Testing", order.getVoidReason());
  644. }
  645.  
  646. /**
  647. * @see {@link EncounterService#unvoidEncounter(Encounter)}
  648. */
  649. @Test
  650. @Verifies(value = "should cascade unvoid to obs", method = "unvoidEncounter(Encounter)")
  651. public void unvoidEncounter_shouldCascadeUnvoidToObs() throws Exception {
  652. EncounterService encounterService = Context.getEncounterService();
  653.  
  654. // get a voided encounter that has some voided obs
  655. Encounter encounter = encounterService.getEncounter(2);
  656. encounterService.unvoidEncounter(encounter);
  657.  
  658. Obs obs = Context.getObsService().getObs(4);
  659. assertFalse(obs.isVoided());
  660. assertNull(obs.getVoidReason());
  661. }
  662.  
  663. /**
  664. * @see {@link EncounterService#unvoidEncounter(Encounter)}
  665. */
  666. @Test
  667. @Verifies(value = "should cascade unvoid to orders", method = "unvoidEncounter(Encounter)")
  668. public void unvoidEncounter_shouldCascadeUnvoidToOrders() throws Exception {
  669. EncounterService encounterService = Context.getEncounterService();
  670.  
  671. // get a voided encounter that has some voided obs
  672. Encounter encounter = encounterService.getEncounter(2);
  673. encounterService.unvoidEncounter(encounter);
  674.  
  675. Order order = Context.getOrderService().getOrder(2);
  676. assertFalse(order.isVoided());
  677. assertNull(order.getVoidReason());
  678. }
  679.  
  680. /**
  681. * @see {@link EncounterService#voidEncounter(Encounter,String)}
  682. */
  683. @Test(expected = IllegalArgumentException.class)
  684. @Verifies(value = "should throw error with null reason parameter", method = "voidEncounter(Encounter,String)")
  685. public void voidEncounter_shouldThrowErrorWithNullReasonParameter() throws Exception {
  686. EncounterService encounterService = Context.getEncounterService();
  687. Encounter type = encounterService.getEncounter(1);
  688. encounterService.voidEncounter(type, null);
  689. }
  690.  
  691. /**
  692. * @see {@link EncounterService#unvoidEncounter(Encounter)}
  693. */
  694. @Test
  695. @Verifies(value = "should unvoid and unmark all attributes", method = "unvoidEncounter(Encounter)")
  696. public void unvoidEncounter_shouldUnvoidAndUnmarkAllAttributes() throws Exception {
  697. EncounterService encounterService = Context.getEncounterService();
  698.  
  699. // get an already voided encounter
  700. Encounter encounter = encounterService.getEncounter(2);
  701. assertTrue(encounter.isVoided());
  702. assertNotNull(encounter.getVoidedBy());
  703. assertNotNull(encounter.getVoidReason());
  704. assertNotNull(encounter.getDateVoided());
  705.  
  706. Encounter unvoidedEnc = encounterService.unvoidEncounter(encounter);
  707.  
  708. // make sure its still the same object
  709. assertEquals(unvoidedEnc, encounter);
  710.  
  711. // make sure that all the values were unfilled in
  712. assertFalse(unvoidedEnc.isVoided());
  713. assertNull(unvoidedEnc.getDateVoided());
  714. assertNull(unvoidedEnc.getVoidedBy());
  715. assertNull(unvoidedEnc.getVoidReason());
  716. }
  717.  
  718. /**
  719. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  720. */
  721. @Test
  722. @Verifies(value = "should get encounters by location", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  723. public void getEncounters_shouldGetEncountersByLocation() throws Exception {
  724. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, new Location(1), null, null, null,
  725. null, null, true);
  726. assertEquals(6, encounters.size());
  727. }
  728.  
  729. /**
  730. * Get encounters that are after a certain date, and ensure the comparison is INCLUSIVE of the
  731. * given date
  732. *
  733. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  734. */
  735. @Test
  736. @Verifies(value = "should get encounters on or after date", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  737. public void getEncounters_shouldGetEncountersOnOrAfterDate() throws Exception {
  738. // there is only one nonvoided encounter, on 2005-01-01
  739. DateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
  740.  
  741. // test for a min date long before all dates
  742. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, ymd.parse("2004-12-31"), null,
  743. null, null, null, false);
  744. assertEquals(4, encounters.size());
  745. assertEquals(1, encounters.get(0).getEncounterId().intValue());
  746.  
  747. // test for exact date search
  748. encounters = Context.getEncounterService().getEncounters(null, null, ymd.parse("2005-01-01"), null, null, null,
  749. null, false);
  750. assertEquals(4, encounters.size());
  751.  
  752. // test for one day later
  753. encounters = Context.getEncounterService().getEncounters(null, null, ymd.parse("2005-01-02"), null, null, null,
  754. null, false);
  755. assertEquals(3, encounters.size());
  756. assertEquals(3, encounters.get(0).getEncounterId().intValue());
  757. assertEquals(4, encounters.get(1).getEncounterId().intValue());
  758. assertEquals(5, encounters.get(2).getEncounterId().intValue());
  759. }
  760.  
  761. /**
  762. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  763. */
  764. @Test
  765. @Verifies(value = "should get encounters on or up to a date", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  766. public void getEncounters_shouldGetEncountersOnOrUpToADate() throws Exception {
  767. Date toDate = new SimpleDateFormat("yyyy-dd-MM").parse("2006-01-01");
  768. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, toDate, null, null, null,
  769. true);
  770. assertEquals(2, encounters.size());
  771. assertEquals(15, encounters.get(0).getEncounterId().intValue());
  772. assertEquals(1, encounters.get(1).getEncounterId().intValue());
  773. }
  774.  
  775. /**
  776. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  777. */
  778. @Test
  779. @Verifies(value = "should get encounters by form", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  780. public void getEncounters_shouldGetEncountersByForm() throws Exception {
  781. List<Form> forms = new Vector<Form>();
  782. forms.add(new Form(1));
  783. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, null, forms, null, null,
  784. true);
  785. assertEquals(7, encounters.size());
  786. }
  787.  
  788. /**
  789. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  790. */
  791. @Test
  792. @Verifies(value = "should get encounters by provider", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  793. public void getEncounters_shouldGetEncountersByProvider() throws Exception {
  794. List<User> providers = new ArrayList<User>();
  795. providers.add(Context.getUserService().getUser(1));
  796. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, null, null, null,
  797. providers, true);
  798. assertEquals(2, encounters.size()); // should be encounters 15 and 16
  799. }
  800.  
  801. /**
  802. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  803. */
  804. @Test
  805. @Verifies(value = "should get encounters by type", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  806. public void getEncounters_shouldGetEncountersByType() throws Exception {
  807. List<EncounterType> types = new Vector<EncounterType>();
  808. types.add(new EncounterType(1));
  809. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, null, null, types, null,
  810. true);
  811. assertEquals(6, encounters.size());
  812. }
  813.  
  814. /**
  815. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  816. */
  817. @Test
  818. @Verifies(value = "should exclude voided encounters", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  819. public void getEncounters_shouldExcludeVoidedEncounters() throws Exception {
  820. assertEquals(5, Context.getEncounterService().getEncounters(null, null, null, null, null, null, null, false).size());
  821. }
  822.  
  823. /**
  824. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  825. */
  826. @Test
  827. @Verifies(value = "should include voided encounters", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QUser;>,null)")
  828. public void getEncounters_shouldIncludeVoidedEncounters() throws Exception {
  829. assertEquals(7, Context.getEncounterService().getEncounters(null, null, null, null, null, null, null, true).size());
  830. }
  831.  
  832. /**
  833. * @see {@link EncounterService#saveEncounterType(EncounterType)}
  834. */
  835. @Test
  836. @Verifies(value = "should save encounter type", method = "saveEncounterType(EncounterType)")
  837. public void saveEncounterType_shouldSaveEncounterType() throws Exception {
  838. EncounterService encounterService = Context.getEncounterService();
  839.  
  840. EncounterType encounterType = new EncounterType("testing", "desc");
  841. encounterService.saveEncounterType(encounterType);
  842.  
  843. // make sure an encounter type id was created
  844. assertNotNull(encounterType.getEncounterTypeId());
  845.  
  846. // make sure we can fetch this new encounter type
  847. // from the database
  848. EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId());
  849. assertNotNull(newEncounterType);
  850. }
  851.  
  852. /**
  853. * @see {@link EncounterService#saveEncounterType(EncounterType)}
  854. */
  855. @Test
  856. @Verifies(value = "should not overwrite creator", method = "saveEncounterType(EncounterType)")
  857. public void saveEncounterType_shouldNotOverwriteCreator() throws Exception {
  858. EncounterService encounterService = Context.getEncounterService();
  859.  
  860. // the encounter to save without a dateCreated
  861. EncounterType encounterType = new EncounterType("testing", "desc");
  862. encounterType.setCreator(new User(4));
  863.  
  864. // make sure the logged in user isn't the user we're testing with
  865. assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser());
  866.  
  867. encounterService.saveEncounterType(encounterType);
  868.  
  869. // make sure the encounter type creator is user 4 not user 1
  870. assertEquals(4, encounterType.getCreator().getId().intValue());
  871. assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser());
  872.  
  873. // make sure we can fetch this new encounter type
  874. // from the database and its values are the same as the passed in ones
  875. EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId());
  876. assertNotNull(newEncounterType);
  877. assertEquals(4, encounterType.getCreator().getId().intValue());
  878. assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser());
  879. }
  880.  
  881. /**
  882. * @see {@link EncounterService#saveEncounterType(EncounterType)}
  883. */
  884. @Test
  885. @Verifies(value = "should not overwrite creator or date created", method = "saveEncounterType(EncounterType)")
  886. public void saveEncounterType_shouldNotOverwriteCreatorOrDateCreated() throws Exception {
  887. EncounterService encounterService = Context.getEncounterService();
  888.  
  889. // the encounter to save without a dateCreated
  890. EncounterType encounterType = new EncounterType("testing", "desc");
  891. encounterType.setCreator(new User(4));
  892. Date date = new Date(System.currentTimeMillis() - 5000); // make sure we
  893. // have a
  894. // date that
  895. // isn't
  896. // "right now"
  897. encounterType.setDateCreated(date);
  898.  
  899. // make sure the logged in user isn't the user we're testing with
  900. assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser());
  901.  
  902. encounterService.saveEncounterType(encounterType);
  903.  
  904. // make sure the encounter type creator is user 4 not user 1
  905. assertNotSame(encounterType.getCreator().getId(), Context.getAuthenticatedUser().getId());
  906.  
  907. // make sure the encounter type date created wasn't overwritten
  908. assertEquals(date, encounterType.getDateCreated());
  909.  
  910. // make sure we can fetch this new encounter type
  911. // from the database and its values are the same as the passed in ones
  912. EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId());
  913. assertNotNull(newEncounterType);
  914. assertEquals(4, encounterType.getCreator().getId().intValue());
  915. assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser());
  916. assertEquals(date, encounterType.getDateCreated());
  917. }
  918.  
  919. /**
  920. * @see {@link EncounterService#saveEncounterType(EncounterType)}
  921. */
  922. @Test
  923. @Verifies(value = "should not overwrite date created", method = "saveEncounterType(EncounterType)")
  924. public void saveEncounterType_shouldNotOverwriteDateCreated() throws Exception {
  925. EncounterService encounterService = Context.getEncounterService();
  926.  
  927. // the encounter to save without a dateCreated
  928. EncounterType encounterType = new EncounterType();
  929. encounterType.setName("testing");
  930. encounterType.setDescription("desc");
  931. Date date = new Date(System.currentTimeMillis() - 5000); // make sure we
  932. // have a
  933. // date that
  934. // isn't
  935. // "right now"
  936. encounterType.setDateCreated(date);
  937.  
  938. encounterService.saveEncounterType(encounterType);
  939.  
  940. // make sure the encounter type date created wasn't overwritten
  941. assertEquals(date, encounterType.getDateCreated());
  942.  
  943. // make sure we can fetch this new encounter type
  944. // from the database and its values are the same as the passed in ones
  945. EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId());
  946. assertNotNull(newEncounterType);
  947. assertEquals(date, encounterType.getDateCreated());
  948. }
  949.  
  950. /**
  951. * There should be two encounters in the system with the name "Test Enc Type A" and one should
  952. * be retired and one not. Only the non retired one should be returned here
  953. *
  954. * @see {@link EncounterService#getEncounterType(String)}
  955. */
  956. @Test
  957. @Verifies(value = "should not get retired types", method = "getEncounterType(String)")
  958. public void getEncounterType_shouldNotGetRetiredTypes() throws Exception {
  959. EncounterService encounterService = Context.getEncounterService();
  960.  
  961. // loop over retired and nonretired types to make sure
  962. // that there are two "Test Enc Type A" types (one retired, one not)
  963. boolean foundRetired = false;
  964. boolean foundNonRetired = false;
  965. int countOfTestEncType2s = 0;
  966. for (EncounterType encType : encounterService.getAllEncounterTypes(true)) {
  967. if (encType.getName().equals("Test Enc Type A")) {
  968. countOfTestEncType2s++;
  969. if (encType.isRetired())
  970. foundRetired = true;
  971. else
  972. foundNonRetired = true;
  973. }
  974. }
  975. // check that both were set to true
  976. assertEquals("We are only expecting to have two types: one retired, one not", 2, countOfTestEncType2s);
  977. assertTrue("No retired type was found in the db", foundRetired);
  978. assertTrue("No non-retired type was found in the db", foundNonRetired);
  979.  
  980. // we should not get two types here, the second one is retired
  981. EncounterType type = encounterService.getEncounterType("Test Enc Type A");
  982. assertEquals(2, type.getEncounterTypeId().intValue());
  983. assertFalse(type.isRetired());
  984. }
  985.  
  986. /**
  987. * Make sure that the "Some Retired Type" type is not returned because it is retired in
  988. * {@link EncounterService#getEncounterType(String)}
  989. *
  990. * @see {@link EncounterService#getEncounterType(String)}
  991. */
  992. @Test
  993. @Verifies(value = "should return null if only retired type found", method = "getEncounterType(String)")
  994. public void getEncounterType_shouldReturnNullIfOnlyRetiredTypeFound() throws Exception {
  995. EncounterService encounterService = Context.getEncounterService();
  996.  
  997. // sanity check to make sure 'some retired type' is in the dataset
  998. assertTrue(encounterService.getEncounterType(4).isRetired());
  999. assertEquals("Some Retired Type", encounterService.getEncounterType(4).getName());
  1000.  
  1001. // we should get a null here because this named type is retired
  1002. EncounterType type = encounterService.getEncounterType("Some Retired Type");
  1003. assertNull(type);
  1004. }
  1005.  
  1006. /**
  1007. * Make sure that we are matching on exact name and not partial name in
  1008. *
  1009. * @see {@link EncounterService#getEncounterType(String)}
  1010. */
  1011. @Test
  1012. @Verifies(value = "should not get by inexact name", method = "getEncounterType(String)")
  1013. public void getEncounterType_shouldNotGetByInexactName() throws Exception {
  1014. EncounterService encounterService = Context.getEncounterService();
  1015.  
  1016. // we should not get two types here, the second one is retired
  1017. EncounterType type = encounterService.getEncounterType("Test Enc Type A");
  1018. assertEquals(2, type.getEncounterTypeId().intValue());
  1019.  
  1020. // we should not get any encounters here even though "Test Enc" is
  1021. // similar
  1022. // to a name that is in the db
  1023. EncounterType typeByInExactName = encounterService.getEncounterType("Test Enc");
  1024. assertNull(typeByInExactName);
  1025. }
  1026.  
  1027. /**
  1028. * Make sure that we are not throwing an error with a null parameter to
  1029. *
  1030. * @see {@link EncounterService#getEncounterType(String)}
  1031. */
  1032. @Test
  1033. @Verifies(value = "should return null with null name parameter", method = "getEncounterType(String)")
  1034. public void getEncounterType_shouldReturnNullWithNullNameParameter() throws Exception {
  1035. EncounterService encounterService = Context.getEncounterService();
  1036.  
  1037. // we should not get an error here...but silently return nothing
  1038. EncounterType type = encounterService.getEncounterType((String) null);
  1039. assertNull(type);
  1040. }
  1041.  
  1042. /**
  1043. * @see {@link EncounterService#getAllEncounterTypes(boolean)}
  1044. */
  1045. @Test
  1046. @Verifies(value = "should not return retired types", method = "getAllEncounterTypes(boolean)")
  1047. public void getAllEncounterTypes_shouldNotReturnRetiredTypes() throws Exception {
  1048. EncounterService encounterService = Context.getEncounterService();
  1049. List<EncounterType> encounterTypes = encounterService.getAllEncounterTypes(false);
  1050.  
  1051. // make sure we get a list
  1052. assertNotNull(encounterTypes);
  1053.  
  1054. // make sure we only get the two non retired encounter types
  1055. // defined in the initialData.xml
  1056. assertEquals(2, encounterTypes.size());
  1057. }
  1058.  
  1059. /**
  1060. * @see {@link EncounterService#getAllEncounterTypes(null)}
  1061. */
  1062. @Test
  1063. @Verifies(value = "should include retired types with true includeRetired parameter", method = "getAllEncounterTypes(null)")
  1064. public void getAllEncounterTypes_shouldIncludeRetiredTypesWithTrueIncludeRetiredParameter() throws Exception {
  1065. EncounterService encounterService = Context.getEncounterService();
  1066. boolean foundRetired = false;
  1067. List<EncounterType> types = encounterService.getAllEncounterTypes(true);
  1068.  
  1069. // there should be five types in the database
  1070. assertEquals(5, types.size());
  1071.  
  1072. for (EncounterType type : types) {
  1073. if (type.isRetired())
  1074. foundRetired = true;
  1075. }
  1076. assertTrue("Retired types should be returned as well", foundRetired);
  1077. }
  1078.  
  1079. /**
  1080. * @see {@link EncounterService#findEncounterTypes(String)}
  1081. */
  1082. @Test
  1083. @Verifies(value = "should return types by partial name match", method = "findEncounterTypes(String)")
  1084. public void findEncounterTypes_shouldReturnTypesByPartialNameMatch() throws Exception {
  1085. EncounterService encounterService = Context.getEncounterService();
  1086. List<EncounterType> types = encounterService.findEncounterTypes("Test Enc");
  1087. assertEquals(3, types.size());
  1088. }
  1089.  
  1090. /**
  1091. * @see {@link EncounterService#findEncounterTypes(String)}
  1092. */
  1093. @Test
  1094. @Verifies(value = "should return types by partial case insensitive match", method = "findEncounterTypes(String)")
  1095. public void findEncounterTypes_shouldReturnTypesByPartialCaseInsensitiveMatch() throws Exception {
  1096. EncounterService encounterService = Context.getEncounterService();
  1097. List<EncounterType> types = encounterService.findEncounterTypes("Test ENC");
  1098. assertEquals(3, types.size());
  1099. }
  1100.  
  1101. /**
  1102. * @see {@link EncounterService#findEncounterTypes(String)}
  1103. */
  1104. @Test
  1105. @Verifies(value = "should include retired types in the results", method = "findEncounterTypes(String)")
  1106. public void findEncounterTypes_shouldIncludeRetiredTypesInTheResults() throws Exception {
  1107. EncounterService encounterService = Context.getEncounterService();
  1108. List<EncounterType> types = encounterService.findEncounterTypes("Test Enc");
  1109. assertEquals(3, types.size());
  1110.  
  1111. // make sure at least one of the types was retired
  1112. boolean foundRetired = false;
  1113. for (EncounterType type : types) {
  1114. if (type.isRetired())
  1115. foundRetired = true;
  1116. }
  1117. assertTrue("Retired types should be returned as well", foundRetired);
  1118. }
  1119.  
  1120. /**
  1121. * No types should be returned when using a substring other than the starting substring
  1122. *
  1123. * @see {@link EncounterService#findEncounterTypes(String)}
  1124. */
  1125. @Test
  1126. @Verifies(value = "should not partial match name on internal substrings", method = "findEncounterTypes(String)")
  1127. public void findEncounterTypes_shouldNotPartialMatchNameOnInternalSubstrings() throws Exception {
  1128. EncounterService encounterService = Context.getEncounterService();
  1129. List<EncounterType> types = encounterService.findEncounterTypes("Test Enc Type");
  1130. assertEquals(3, types.size());
  1131.  
  1132. types = encounterService.findEncounterTypes("Enc Type");
  1133. assertEquals(0, types.size());
  1134. }
  1135.  
  1136. /**
  1137. * @see {@link EncounterService#findEncounterTypes(String)}
  1138. */
  1139. @Test
  1140. @Verifies(value = "should return types ordered on name and nonretired first", method = "findEncounterTypes(String)")
  1141. public void findEncounterTypes_shouldReturnTypesOrderedOnNameAndNonretiredFirst() throws Exception {
  1142. EncounterService encounterService = Context.getEncounterService();
  1143. List<EncounterType> types = encounterService.findEncounterTypes("Test Enc");
  1144.  
  1145. // make sure the order is id 2, 3, 1
  1146. assertEquals(2, types.get(0).getEncounterTypeId().intValue());
  1147. assertEquals(3, types.get(1).getEncounterTypeId().intValue());
  1148. assertEquals(1, types.get(2).getEncounterTypeId().intValue());
  1149.  
  1150. // this test expects that id #2 and id #3 have the same name and that
  1151. // id #3 is retired
  1152. assertEquals(types.get(0).getName(), types.get(1).getName());
  1153. assertTrue(types.get(1).isRetired());
  1154. }
  1155.  
  1156. /**
  1157. * @see {@link EncounterService#retireEncounterType(EncounterType,String)}
  1158. */
  1159. @Test
  1160. @Verifies(value = "should retire type and set attributes", method = "retireEncounterType(EncounterType,String)")
  1161. public void retireEncounterType_shouldRetireTypeAndSetAttributes() throws Exception {
  1162. EncounterService encounterService = Context.getEncounterService();
  1163. EncounterType type = encounterService.getEncounterType(1);
  1164. assertFalse(type.isRetired());
  1165. assertNull(type.getRetiredBy());
  1166. assertNull(type.getRetireReason());
  1167. assertNull(type.getDateRetired());
  1168.  
  1169. EncounterType retiredEncType = encounterService.retireEncounterType(type, "Just Testing");
  1170.  
  1171. // make sure its still the same object
  1172. assertEquals(retiredEncType, type);
  1173.  
  1174. // make sure that all the values were filled in
  1175. assertTrue(retiredEncType.isRetired());
  1176. assertNotNull(retiredEncType.getDateRetired());
  1177. assertEquals(Context.getAuthenticatedUser(), retiredEncType.getRetiredBy());
  1178. assertEquals("Just Testing", retiredEncType.getRetireReason());
  1179. }
  1180.  
  1181. /**
  1182. * @see {@link EncounterService#retireEncounterType(EncounterType,String)}
  1183. */
  1184. @Test(expected = IllegalArgumentException.class)
  1185. @Verifies(value = "should throw error if given null reason parameter", method = "retireEncounterType(EncounterType,String)")
  1186. public void retireEncounterType_shouldThrowErrorIfGivenNullReasonParameter() throws Exception {
  1187. EncounterService encounterService = Context.getEncounterService();
  1188. EncounterType type = encounterService.getEncounterType(1);
  1189. encounterService.retireEncounterType(type, null);
  1190. }
  1191.  
  1192. /**
  1193. * @see {@link EncounterService#unretireEncounterType(EncounterType)}
  1194. */
  1195. @Test
  1196. @Verifies(value = "should unretire type and unmark attributes", method = "unretireEncounterType(EncounterType)")
  1197. public void unretireEncounterType_shouldUnretireTypeAndUnmarkAttributes() throws Exception {
  1198. EncounterService encounterService = Context.getEncounterService();
  1199. EncounterType type = encounterService.getEncounterType(3);
  1200. // TestUtil.printOutTableContents(getConnection(), "encounter_type",
  1201. // "encounter");
  1202. assertTrue(type.isRetired());
  1203. assertNotNull(type.getRetiredBy());
  1204. assertNotNull(type.getRetireReason());
  1205. assertNotNull(type.getDateRetired());
  1206.  
  1207. EncounterType unretiredEncType = encounterService.unretireEncounterType(type);
  1208.  
  1209. // make sure its still the same object
  1210. assertEquals(unretiredEncType, type);
  1211.  
  1212. // make sure that all the values were unfilled in
  1213. assertFalse(unretiredEncType.isRetired());
  1214. assertNull(unretiredEncType.getDateRetired());
  1215. assertNull(unretiredEncType.getRetiredBy());
  1216. assertNull(unretiredEncType.getRetireReason());
  1217. }
  1218.  
  1219. /**
  1220. * @see {@link EncounterService#saveEncounterType(EncounterType)}
  1221. */
  1222. @Test
  1223. @Verifies(value = "should update an existing encounter type name", method = "saveEncounterType(EncounterType)")
  1224. public void saveEncounterType_shouldUpdateAnExistingEncounterTypeName() throws Exception {
  1225. EncounterService encounterService = Context.getEncounterService();
  1226.  
  1227. EncounterType encounterTypeToChange = encounterService.getEncounterType(1);
  1228.  
  1229. // change the name of the type
  1230. encounterTypeToChange.setName("another test");
  1231.  
  1232. // save the type to the database
  1233. encounterService.saveEncounterType(encounterTypeToChange);
  1234.  
  1235. // make sure the encounter type id didn't change
  1236. assertEquals(1, encounterTypeToChange.getEncounterTypeId().intValue());
  1237.  
  1238. // refetch the encounter type from the database
  1239. EncounterType fetchedEncounterType = encounterService.getEncounterType(1);
  1240. assertTrue(fetchedEncounterType.getName().equals("another test"));
  1241. }
  1242.  
  1243. /**
  1244. * @see {@link EncounterService#purgeEncounterType(EncounterType)}
  1245. */
  1246. @Test
  1247. @Verifies(value = "should purge type", method = "purgeEncounterType(EncounterType)")
  1248. public void purgeEncounterType_shouldPurgeType() throws Exception {
  1249. EncounterService encounterService = Context.getEncounterService();
  1250.  
  1251. EncounterType encounterTypeToPurge = encounterService.getEncounterType(4);
  1252. assertNotNull(encounterTypeToPurge);
  1253.  
  1254. // check deletion
  1255. encounterService.purgeEncounterType(encounterTypeToPurge);
  1256. assertNull(encounterService.getEncounterType(4));
  1257. }
  1258.  
  1259. /**
  1260. * @see {@link EncounterService#getEncounter(Integer)}
  1261. */
  1262. @Test(expected = IllegalArgumentException.class)
  1263. @Verifies(value = "should throw error if given null parameter", method = "getEncounter(Integer)")
  1264. public void getEncounter_shouldThrowErrorIfGivenNullParameter() throws Exception {
  1265. Context.getEncounterService().getEncounter(null);
  1266. }
  1267.  
  1268. /**
  1269. * @see {@link EncounterService#getEncounterType(Integer)}
  1270. */
  1271. @Test(expected = IllegalArgumentException.class)
  1272. @Verifies(value = "should throw error if given null parameter", method = "getEncounterType(Integer)")
  1273. public void getEncounterType_shouldThrowErrorIfGivenNullParameter() throws Exception {
  1274. Context.getEncounterService().getEncounterType((Integer) null);
  1275. }
  1276.  
  1277. /**
  1278. * @see {@link EncounterService#saveEncounter(Encounter)}
  1279. */
  1280. @Test
  1281. @Verifies(value = "should cascade patient to orders in the encounter", method = "saveEncounter(Encounter)")
  1282. public void saveEncounter_shouldCascadePatientToOrdersInTheEncounter() throws Exception {
  1283. Encounter enc = Context.getEncounterService().getEncounter(15);
  1284. Order existing = enc.getOrders().iterator().next();
  1285.  
  1286. // for some reason the xml for the existing encounter has already given
  1287. // this order a different patient than the encounter that it's contained
  1288. // in, but let's verify that:
  1289. Assert.assertNotSame(enc.getPatient().getId(), existing.getPatient().getId());
  1290.  
  1291. Context.getEncounterService().saveEncounter(enc);
  1292. Assert.assertEquals(enc.getPatient().getId(), existing.getPatient().getId());
  1293. }
  1294.  
  1295. /**
  1296. * @see {@link EncounterService#getEncounterByUuid(String)}
  1297. */
  1298. @Test
  1299. @Verifies(value = "should find object given valid uuid", method = "getEncounterByUuid(String)")
  1300. public void getEncounterByUuid_shouldFindObjectGivenValidUuid() throws Exception {
  1301. String uuid = "6519d653-393b-4118-9c83-a3715b82d4ac";
  1302. Encounter encounter = Context.getEncounterService().getEncounterByUuid(uuid);
  1303. Assert.assertEquals(3, (int) encounter.getEncounterId());
  1304. }
  1305.  
  1306. /**
  1307. * @see {@link EncounterService#getEncounterByUuid(String)}
  1308. */
  1309. @Test
  1310. @Verifies(value = "should return null if no object found with given uuid", method = "getEncounterByUuid(String)")
  1311. public void getEncounterByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() throws Exception {
  1312. Assert.assertNull(Context.getEncounterService().getEncounterByUuid("some invalid uuid"));
  1313. }
  1314.  
  1315. /**
  1316. * @see {@link EncounterService#getEncounterTypeByUuid(String)}
  1317. */
  1318. @Test
  1319. @Verifies(value = "should find object given valid uuid", method = "getEncounterTypeByUuid(String)")
  1320. public void getEncounterTypeByUuid_shouldFindObjectGivenValidUuid() throws Exception {
  1321. String uuid = "02c533ab-b74b-4ee4-b6e5-ffb6d09a0ac8";
  1322. EncounterType encounterType = Context.getEncounterService().getEncounterTypeByUuid(uuid);
  1323. Assert.assertEquals(6, (int) encounterType.getEncounterTypeId());
  1324. }
  1325.  
  1326. /**
  1327. * @see {@link EncounterService#getEncounterTypeByUuid(String)}
  1328. */
  1329. @Test
  1330. @Verifies(value = "should return null if no object found with given uuid", method = "getEncounterTypeByUuid(String)")
  1331. public void getEncounterTypeByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() throws Exception {
  1332. Assert.assertNull(Context.getEncounterService().getEncounterTypeByUuid("some invalid uuid"));
  1333. }
  1334.  
  1335. /**
  1336. * @see {@link EncounterService#getEncountersByPatient(String,boolean)}
  1337. * @see {@link EncounterService#getEncountersByPatient(String)}
  1338. */
  1339. @Test
  1340. @Verifies(value = "should get all unvoided encounters for the given patient identifier", method = "getEncountersByPatient(String,boolean)")
  1341. public void getEncountersByPatient_shouldGetAllUnvoidedEncountersForTheGivenPatientIdentifier() throws Exception {
  1342. EncounterService encounterService = Context.getEncounterService();
  1343.  
  1344. List<Encounter> encounters = encounterService.getEncountersByPatient("12345", false);
  1345. assertEquals(2, encounters.size());
  1346. }
  1347.  
  1348. /**
  1349. * @see {@link EncounterService#getEncountersByPatient(String,boolean)}
  1350. */
  1351. @Test
  1352. @Verifies(value = "should get all unvoided encounters for the given patient name", method = "getEncountersByPatient(String,boolean)")
  1353. public void getEncountersByPatient_shouldGetAllUnvoidedEncountersForTheGivenPatientName() throws Exception {
  1354. EncounterService encounterService = Context.getEncounterService();
  1355.  
  1356. List<Encounter> encounters = encounterService.getEncountersByPatient("John", false);
  1357. assertEquals(2, encounters.size());
  1358. }
  1359.  
  1360. /**
  1361. * @see {@link EncounterService#getEncountersByPatient(String,boolean)}
  1362. */
  1363. @Test
  1364. @Verifies(value = "should include voided encounters in the returned list if includedVoided is true", method = "getEncountersByPatient(String,boolean)")
  1365. public void getEncountersByPatient_shouldIncludeVoidedEncountersInTheReturnedListIfIncludedVoidedIsTrue()
  1366. throws Exception {
  1367. EncounterService encounterService = Context.getEncounterService();
  1368.  
  1369. List<Encounter> encounters = encounterService.getEncountersByPatient("12345", true);
  1370. assertEquals(4, encounters.size());
  1371. }
  1372.  
  1373. /**
  1374. * @see {@link EncounterService#getEncountersByPatient(String,boolean)}
  1375. */
  1376. @Test(expected = IllegalArgumentException.class)
  1377. @Verifies(value = "should throw error if given null parameter", method = "getEncountersByPatient(String,boolean)")
  1378. public void getEncountersByPatient_shouldThrowErrorIfGivenNullParameter() throws Exception {
  1379. Context.getEncounterService().getEncountersByPatient(null, false);
  1380. }
  1381.  
  1382. /**
  1383. * Tests that all encounters for all patients in a cohort are returned
  1384. *
  1385. * @see {@link EncounterService#getAllEncounters(Cohort)}
  1386. */
  1387. @Test
  1388. @Verifies(value = "should get all encounters for a cohort of patients", method = "getAllEncounters(Cohort)")
  1389. public void getAllEncounters_shouldGetAllEncountersForACohortOfPatients() throws Exception {
  1390. Cohort cohort = new Cohort();
  1391. cohort.addMember(7);
  1392. Map<Integer, List<Encounter>> allEncounters = Context.getEncounterService().getAllEncounters(cohort);
  1393. Assert.assertEquals(1, allEncounters.size());
  1394. Assert.assertEquals(3, allEncounters.get(7).size());
  1395. }
  1396.  
  1397. /**
  1398. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  1399. */
  1400. @Test
  1401. @Verifies(value = "should get encounters by visit", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QVisitType;>,Collection<QVisit;>,Collection<QUser;>,null)")
  1402. public void getEncounters_shouldGetEncountersByVisit() throws Exception {
  1403. List<Visit> visits = new ArrayList<Visit>();
  1404. visits.add(new Visit(1));
  1405. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, null, null, null, null,
  1406. null, visits, true);
  1407. assertEquals(2, encounters.size());
  1408. }
  1409.  
  1410. /**
  1411. * @see {@link EncounterService#getEncounters(Patient, Location, Date, Date, java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection, boolean)}
  1412. */
  1413. @Test
  1414. @Verifies(value = "should get encounters by visit type", method = "getEncounters(Patient,Location,Date,Date,Collection<QForm;>,Collection<QEncounterType;>,Collection<QVisitType;>,Collection<QVisit;>,Collection<QUser;>,null)")
  1415. public void getEncounters_shouldGetEncountersByVisitType() throws Exception {
  1416. List<VisitType> visitTypes = new Vector<VisitType>();
  1417. visitTypes.add(new VisitType(2));
  1418. List<Encounter> encounters = Context.getEncounterService().getEncounters(null, null, null, null, null, null, null,
  1419. visitTypes, null, true);
  1420. assertEquals(2, encounters.size());
  1421. }
  1422.  
  1423. /**
  1424. * @see EncounterService#getEncountersByVisit(Visit, boolean)
  1425. * @verifies get active encounters by visit
  1426. */
  1427. @Test
  1428. public void getEncountersByVisit_shouldGetActiveEncountersByVisit() throws Exception {
  1429. List<Encounter> encounters = Context.getEncounterService().getEncountersByVisit(new Visit(1), false);
  1430. assertEquals(1, encounters.size());
  1431. }
  1432.  
  1433. /**
  1434. * @see EncounterService#getEncountersByVisit(Visit, boolean)
  1435. * @verifies include voided encounters when includeVoided is true
  1436. */
  1437. @Test
  1438. public void getEncountersByVisit_shouldIncludeVoidedEncountersWhenIncludeVoidedIsTrue() throws Exception {
  1439. List<Encounter> encounters = Context.getEncounterService().getEncountersByVisit(new Visit(1), true);
  1440. assertEquals(2, encounters.size());
  1441. }
  1442.  
  1443. /**
  1444. * @see {@link EncounterService#getCountOfEncounters(String,null)}
  1445. */
  1446. @Test
  1447. @Verifies(value = "should get the correct count of unique encounters", method = "getCountOfEncounters(String,null)")
  1448. public void getCountOfEncounters_shouldGetTheCorrectCountOfUniqueEncounters() throws Exception {
  1449. executeDataSet(UNIQUE_ENC_WITH_PAGING_XML);
  1450. Assert.assertEquals(4, Context.getEncounterService().getCountOfEncounters("qwerty", true).intValue());
  1451. }
  1452.  
  1453. /**
  1454. * TODO see ticket https://tickets.openmrs.org/browse/TRUNK-1956 to fix this test
  1455. *
  1456. * @see {@link EncounterService#getEncounters(String,Integer,Integer,null,null)}
  1457. */
  1458. @Test
  1459. @Ignore
  1460. @Verifies(value = "should get all the unique encounters that match the specified parameter values", method = "getEncounters(String,Integer,Integer,null,null)")
  1461. public void getEncounters_shouldGetAllTheUniqueEncountersThatMatchTheSpecifiedParameterValues() throws Exception {
  1462. executeDataSet(UNIQUE_ENC_WITH_PAGING_XML);
  1463. List<Encounter> encs = Context.getEncounterService().getEncounters("qwerty", 0, 4, true);
  1464. Assert.assertEquals(4, encs.size());
  1465. }
  1466.  
  1467. /**
  1468. * TODO see ticket https://tickets.openmrs.org/browse/TRUNK-1956 to fix this test
  1469. *
  1470. * @see {@link EncounterService#getEncounters(String,Integer,Integer,null,null)}
  1471. */
  1472. @Test
  1473. @Ignore
  1474. @Verifies(value = "should not return voided encounters if includeVoided is set to true", method = "getEncounters(String,Integer,Integer,null,null)")
  1475. public void getEncounters_shouldNotReturnVoidedEncountersIfIncludeVoidedIsSetToTrue() throws Exception {
  1476. executeDataSet(UNIQUE_ENC_WITH_PAGING_XML);
  1477. List<Encounter> encs = Context.getEncounterService().getEncounters("qwerty", 0, 3, false);
  1478. Assert.assertEquals(3, encs.size());
  1479. }
  1480.  
  1481. /**
  1482. * @see org.openmrs.api.EncounterService#saveEncounterRole(org.openmrs.EncounterRole)
  1483. */
  1484. @Test
  1485. @Verifies(value = "should save encounter role with basic details", method = "saveEncounterRole(EncounterRole)")
  1486. public void saveEncounterRole_shouldSaveEncounterRoleWithBasicDetails() throws Exception {
  1487. EncounterRole encounterRole = new EncounterRole();
  1488. encounterRole.setName("Attending physician");
  1489. encounterRole.setDescription("The person in charge");
  1490. EncounterService encounterService = Context.getEncounterService();
  1491. encounterService.saveEncounterRole(encounterRole);
  1492.  
  1493. assertNotNull("The saved encounter role should have an encounter role id now", encounterRole.getEncounterRoleId());
  1494. EncounterRole newSavedEncounterRole = encounterService.getEncounterRole(encounterRole.getEncounterRoleId());
  1495. assertNotNull("We should get back an encounter role", newSavedEncounterRole);
  1496. assertEquals(encounterRole, newSavedEncounterRole);
  1497. assertTrue("The created encounter role needs to equal the pojo encounter role", encounterRole
  1498. .equals(newSavedEncounterRole));
  1499.  
  1500. }
  1501.  
  1502. /**
  1503. * Make sure that purging an encounter removes the row from the database
  1504. *
  1505. * @see {@link EncounterService#purgeEncounterRole(org.openmrs.EncounterRole)}
  1506. */
  1507. @Test
  1508. @Verifies(value = "should purge Encounter Role", method = "purgeEncounterRole(EncounterRole)")
  1509. public void purgeEncounterRole_shouldPurgeEncounterRole() throws Exception {
  1510. EncounterService encounterService = Context.getEncounterService();
  1511. EncounterRole encounterRole = encounterService.getEncounterRole(1);
  1512. encounterService.purgeEncounterRole(encounterRole);
  1513. EncounterRole fetchedEncounterRole = encounterService.getEncounterRole(encounterRole.getEncounterRoleId());
  1514. assertNull("We shouldn't find the encounter after deletion", fetchedEncounterRole);
  1515. }
  1516.  
  1517. /**
  1518. * @see {@link EncounterService#getAllEncounterRoles(boolean)}
  1519. */
  1520. @Test
  1521. @Verifies(value = "get all encounter roles based on include retired flag", method = "getAllEncounterRoles(boolean)")
  1522. public void getAllEncounterRoles_shouldGetAllEncounterRolesBasedOnIncludeRetiredFlag() throws Exception {
  1523. EncounterService encounterService = Context.getEncounterService();
  1524. List<EncounterRole> encounterRoles = encounterService.getAllEncounterRoles(true);
  1525. assertEquals("get all encounter roles including retired", 3, encounterRoles.size());
  1526. encounterRoles = encounterService.getAllEncounterRoles(false);
  1527. assertEquals("get all encounter roles excluding retired", 2, encounterRoles.size());
  1528. }
  1529.  
  1530. /**
  1531. * @see {@link EncounterService#getEncounterRoleByUuid(String)}
  1532. */
  1533. @Test
  1534. @Verifies(value = "find encounter role based on uuid", method = "getEncounterRoleByUuid(String)")
  1535. public void getEncounterRoleByUuid_shouldFindEncounterRoleBasedOnUuid() throws Exception {
  1536. EncounterService encounterService = Context.getEncounterService();
  1537. EncounterRole encounterRole = encounterService.getEncounterRoleByUuid("430bbb70-6a9c-4e1e-badb-9d1054b1b5e9");
  1538. assertNotNull("valid uuid should be returned", encounterRole);
  1539. encounterRole = encounterService.getEncounterRoleByUuid("invaid uuid");
  1540. assertNull("returns null for invalid uuid", encounterRole);
  1541. }
  1542.  
  1543. /**
  1544. * @see {@link EncounterService#retireEncounterRole(org.openmrs.EncounterRole, String)}
  1545. */
  1546. @Test
  1547. @Verifies(value = "should retire type and set attributes", method = "retireEncounterRole(EncounterRole,String)")
  1548. public void retireEncounterRole_shouldRetireTypeAndSetAttributes() throws Exception {
  1549. EncounterService encounterService = Context.getEncounterService();
  1550. EncounterRole encounterRole = encounterService.getEncounterRole(1);
  1551. assertFalse(encounterRole.isRetired());
  1552. assertNull(encounterRole.getRetiredBy());
  1553. assertNull(encounterRole.getRetireReason());
  1554. assertNull(encounterRole.getDateRetired());
  1555. EncounterRole retiredEncounterRole = encounterService.retireEncounterRole(encounterRole, "Just Testing");
  1556.  
  1557. assertEquals(retiredEncounterRole, encounterRole);
  1558. assertTrue(retiredEncounterRole.isRetired());
  1559. assertNotNull(retiredEncounterRole.getDateRetired());
  1560. assertEquals(Context.getAuthenticatedUser(), retiredEncounterRole.getRetiredBy());
  1561. assertEquals("Just Testing", retiredEncounterRole.getRetireReason());
  1562. }
  1563.  
  1564. /**
  1565. * @see {@link EncounterService#retireEncounterRole(org.openmrs.EncounterRole, String)}
  1566. */
  1567. @Test(expected = IllegalArgumentException.class)
  1568. @Verifies(value = "should throw error if given null reason parameter", method = "retireEncounterRole(EncounterRole,String)")
  1569. public void retireEncounterRole_shouldThrowErrorIfGivenNullReasonParameter() throws Exception {
  1570. EncounterService encounterService = Context.getEncounterService();
  1571. EncounterRole encounterRole = encounterService.getEncounterRole(1);
  1572. encounterService.retireEncounterRole(encounterRole, null);
  1573. }
  1574.  
  1575. /**
  1576. * @see {@link EncounterService#unretireEncounterRole(org.openmrs.EncounterRole)}
  1577. */
  1578. @Test
  1579. @Verifies(value = "should unretire type and unmark attributes", method = "unretireEncounterRole(EncounterRole)")
  1580. public void unretireEncounterRole_shouldUnretireTypeAndUnmarkAttributes() throws Exception {
  1581. EncounterService encounterService = Context.getEncounterService();
  1582. EncounterRole encounterRole = encounterService.getEncounterRole(2);
  1583. assertTrue(encounterRole.isRetired());
  1584. assertNotNull(encounterRole.getRetiredBy());
  1585. assertNotNull(encounterRole.getRetireReason());
  1586. assertNotNull(encounterRole.getDateRetired());
  1587. EncounterRole unretiredEncounterRole = encounterService.unretireEncounterRole(encounterRole);
  1588.  
  1589. assertEquals(unretiredEncounterRole, encounterRole);
  1590. assertFalse(unretiredEncounterRole.isRetired());
  1591. assertNull(unretiredEncounterRole.getDateRetired());
  1592. assertNull(unretiredEncounterRole.getRetiredBy());
  1593. assertNull(unretiredEncounterRole.getRetireReason());
  1594. }
  1595.  
  1596. /**
  1597. * @see EncounterService#saveEncounter(Encounter)
  1598. * @verifies cascade delete encounter providers
  1599. */
  1600. @Test
  1601. public void saveEncounter_shouldCascadeDeleteEncounterProviders() throws Exception {
  1602. //given
  1603. Encounter encounter = new Encounter();
  1604. encounter.setLocation(new Location(1));
  1605. encounter.setEncounterType(new EncounterType(1));
  1606. encounter.setEncounterDatetime(new Date());
  1607. encounter.setPatient(new Patient(3));
  1608.  
  1609. EncounterRole role = new EncounterRole();
  1610. role.setName("role");
  1611. role = Context.getEncounterService().saveEncounterRole(role);
  1612.  
  1613. Provider provider = new Provider();
  1614. provider.setName("provider");
  1615. provider.setIdentifier("id1");
  1616. provider = Context.getProviderService().saveProvider(provider);
  1617.  
  1618. Provider provider2 = new Provider();
  1619. provider2.setName("provider2");
  1620. provider2.setIdentifier("id2");
  1621. provider2 = Context.getProviderService().saveProvider(provider2);
  1622.  
  1623. encounter.addProvider(role, provider);
  1624. encounter.addProvider(role, provider2);
  1625.  
  1626. EncounterService es = Context.getEncounterService();
  1627. es.saveEncounter(encounter);
  1628. Context.flushSession();
  1629. Context.clearSession();
  1630.  
  1631. //when
  1632. encounter = Context.getEncounterService().getEncounter(encounter.getEncounterId());
  1633. encounter.setProvider(role, provider);
  1634. es.saveEncounter(encounter);
  1635. Context.flushSession();
  1636. Context.clearSession();
  1637.  
  1638. //then
  1639. encounter = Context.getEncounterService().getEncounter(encounter.getEncounterId());
  1640. Assert.assertEquals(1, encounter.getProvidersByRole(role).size());
  1641. Assert.assertTrue("Role", encounter.getProvidersByRole(role).contains(provider));
  1642. }
  1643.  
  1644. /**
  1645. * @see EncounterService#saveEncounter(Encounter)
  1646. * @verifies cascade save encounter providers
  1647. */
  1648. @Test
  1649. public void saveEncounter_shouldCascadeSaveEncounterProviders() throws Exception {
  1650. //given
  1651. Encounter encounter = new Encounter();
  1652. encounter.setLocation(new Location(1));
  1653. encounter.setEncounterType(new EncounterType(1));
  1654. encounter.setEncounterDatetime(new Date());
  1655. encounter.setPatient(new Patient(3));
  1656.  
  1657. EncounterRole role = new EncounterRole();
  1658. role.setName("role");
  1659. role = Context.getEncounterService().saveEncounterRole(role);
  1660.  
  1661. EncounterRole role2 = new EncounterRole();
  1662. role2.setName("role2");
  1663. role2 = Context.getEncounterService().saveEncounterRole(role2);
  1664.  
  1665. Provider provider = new Provider();
  1666. provider.setName("provider");
  1667. provider.setIdentifier("id1");
  1668. provider = Context.getProviderService().saveProvider(provider);
  1669.  
  1670. Provider provider2 = new Provider();
  1671. provider2.setName("provider2");
  1672. provider2.setIdentifier("id2");
  1673. provider2 = Context.getProviderService().saveProvider(provider2);
  1674.  
  1675. encounter.addProvider(role, provider);
  1676. encounter.addProvider(role, provider2);
  1677. encounter.addProvider(role2, provider2);
  1678.  
  1679. //when
  1680. EncounterService es = Context.getEncounterService();
  1681. es.saveEncounter(encounter);
  1682. Context.flushSession();
  1683. Context.clearSession();
  1684.  
  1685. //then
  1686. encounter = Context.getEncounterService().getEncounter(encounter.getEncounterId());
  1687. Assert.assertEquals(2, encounter.getProvidersByRole(role).size());
  1688. Assert.assertTrue("Role", encounter.getProvidersByRole(role).containsAll(Arrays.asList(provider, provider2)));
  1689. Assert.assertEquals(1, encounter.getProvidersByRole(role2).size());
  1690. Assert.assertTrue("Role2", encounter.getProvidersByRole(role2).contains(provider2));
  1691. }
  1692.  
  1693. /**
  1694. * @see EncounterService#getEncounters(String,Integer,Integer,boolean)
  1695. * @verifies return empty list for empty query
  1696. */
  1697. @Test
  1698. public void getEncounters_shouldReturnEmptyListForEmptyQuery() throws Exception {
  1699. //given
  1700.  
  1701. //when
  1702. List<Encounter> encounters = Context.getEncounterService().getEncounters("", null, null, true);
  1703.  
  1704. //then
  1705. Assert.assertTrue(encounters.isEmpty());
  1706. }
  1707.  
  1708. /**
  1709. * @see EncounterService#getVisitAssignmentHandlers()
  1710. * @verifies return the no assignment handler
  1711. */
  1712. @Test
  1713. public void getVisitAssignmentHandlers_shouldReturnTheNoAssignmentHandler() throws Exception {
  1714.  
  1715. List<EncounterVisitHandler> handlers = Context.getEncounterService().getEncounterVisitHandlers();
  1716.  
  1717. boolean found = false;
  1718. for (EncounterVisitHandler handler : handlers) {
  1719. if (handler instanceof NoVisitAssignmentHandler)
  1720. found = true;
  1721. }
  1722.  
  1723. Assert.assertTrue("The basic 'no assignment' handler was not found", found);
  1724. }
  1725.  
  1726. /**
  1727. * @see EncounterService#getVisitAssignmentHandlers()
  1728. * @verifies return the existing visit only assignment handler
  1729. */
  1730. @Test
  1731. public void getVisitAssignmentHandlers_shouldReturnTheExistingVisitOnlyAssignmentHandler() throws Exception {
  1732.  
  1733. List<EncounterVisitHandler> handlers = Context.getEncounterService().getEncounterVisitHandlers();
  1734.  
  1735. boolean found = false;
  1736. for (EncounterVisitHandler handler : handlers) {
  1737. if (handler instanceof ExistingVisitAssignmentHandler) {
  1738. found = true;
  1739. break;
  1740. }
  1741. }
  1742.  
  1743. Assert.assertTrue("The 'existing visit only assignment' handler was not found", found);
  1744. }
  1745.  
  1746. /**
  1747. * @see EncounterService#getVisitAssignmentHandlers()
  1748. * @verifies return the existing or new visit assignment handler
  1749. */
  1750. @Test
  1751. public void getVisitAssignmentHandlers_shouldReturnTheExistingOrNewVisitAssignmentHandler() throws Exception {
  1752.  
  1753. List<EncounterVisitHandler> handlers = Context.getEncounterService().getEncounterVisitHandlers();
  1754.  
  1755. boolean found = false;
  1756. for (EncounterVisitHandler handler : handlers) {
  1757. if (handler instanceof ExistingOrNewVisitAssignmentHandler) {
  1758. found = true;
  1759. break;
  1760. }
  1761. }
  1762.  
  1763. Assert.assertTrue("The 'existing or new visit only assignment' handler was not found", found);
  1764. }
  1765.  
  1766. /**
  1767. * @see {@link EncounterService#saveEncounter(Encounter)}
  1768. */
  1769. @Test
  1770. @Verifies(value = "should not assign encounter to visit if no handler is registered", method = "saveEncounter(Encounter)")
  1771. public void saveEncounter_shouldNotAssignEncounterToVisitIfNoHandlerIsRegistered() throws Exception {
  1772. Encounter encounter = buildEncounter();
  1773.  
  1774. //We should have no visit
  1775. assertNull(encounter.getVisit());
  1776.  
  1777. Context.getEncounterService().saveEncounter(encounter);
  1778.  
  1779. //We should have no visit
  1780. assertNull(encounter.getVisit());
  1781. }
  1782.  
  1783. /**
  1784. * @see {@link EncounterService#saveEncounter(Encounter)}
  1785. */
  1786. @Test
  1787. @Verifies(value = "should not assign encounter to visit if the no assign handler is registered", method = "saveEncounter(Encounter)")
  1788. public void saveEncounter_shouldNotAssignEncounterToVisitIfTheNoAssignHandlerIsRegistered() throws Exception {
  1789. Encounter encounter = buildEncounter();
  1790.  
  1791. //We should have no visit
  1792. assertNull(encounter.getVisit());
  1793.  
  1794. GlobalProperty gp = Context.getAdministrationService().getGlobalPropertyObject(
  1795. OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER);
  1796. gp.setPropertyValue("org.openmrs.api.handler.NoVisitAssignmentHandler");
  1797. Context.getAdministrationService().saveGlobalProperty(gp);
  1798.  
  1799. Context.getEncounterService().saveEncounter(encounter);
  1800.  
  1801. //We should have no visit.
  1802. assertNull(encounter.getVisit());
  1803. }
  1804.  
  1805. /**
  1806. * @see {@link EncounterService#saveEncounter(Encounter)}
  1807. */
  1808. @Test
  1809. @Verifies(value = "should assign encounter to visit if the assign to existing handler is registered", method = "saveEncounter(Encounter)")
  1810. public void saveEncounter_shouldAssignEncounterToVisitIfTheAssignToExistingHandlerIsRegistered() throws Exception {
  1811. Encounter encounter = buildEncounter();
  1812.  
  1813. //We should have no visit
  1814. assertNull(encounter.getVisit());
  1815.  
  1816. GlobalProperty gp = Context.getAdministrationService().getGlobalPropertyObject(
  1817. OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER);
  1818. gp.setPropertyValue("org.openmrs.api.handler.ExistingVisitAssignmentHandler");
  1819. Context.getAdministrationService().saveGlobalProperty(gp);
  1820.  
  1821. Context.getEncounterService().saveEncounter(encounter);
  1822.  
  1823. //We should have a visit.
  1824. assertNotNull(encounter.getVisit());
  1825. assertNotNull(encounter.getVisit().getVisitId());
  1826. }
  1827.  
  1828. /**
  1829. * @see {@link EncounterService#saveEncounter(Encounter)}
  1830. */
  1831. @Test
  1832. @Verifies(value = "should assign encounter to visit if the assign to existing or new handler is registered", method = "saveEncounter(Encounter)")
  1833. public void saveEncounter_shouldAssignEncounterToVisitIfTheAssignToExistingOrNewHandlerIsRegistered() throws Exception {
  1834. Encounter encounter = new Encounter();
  1835. encounter.setLocation(new Location(2));
  1836. encounter.setEncounterType(new EncounterType(1));
  1837. encounter.setEncounterDatetime(new Date());
  1838. encounter.setPatient(new Patient(2));
  1839. encounter.setCreator(new User(4));
  1840.  
  1841. //We should have no visit
  1842. assertNull(encounter.getVisit());
  1843.  
  1844. GlobalProperty gp = Context.getAdministrationService().getGlobalPropertyObject(
  1845. OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER);
  1846. gp.setPropertyValue("org.openmrs.api.handler.ExistingOrNewVisitAssignmentHandler");
  1847. Context.getAdministrationService().saveGlobalProperty(gp);
  1848.  
  1849. Calendar calendar = Calendar.getInstance();
  1850. calendar.setTime(encounter.getEncounterDatetime());
  1851. calendar.set(Calendar.YEAR, 1900);
  1852. encounter.setEncounterDatetime(calendar.getTime());
  1853.  
  1854. Context.getEncounterService().saveEncounter(encounter);
  1855.  
  1856. //We should have a visit.
  1857. assertNotNull(encounter.getVisit());
  1858.  
  1859. //The visit should be persisted.
  1860. assertNotNull(encounter.getVisit().getVisitId());
  1861. }
  1862.  
  1863. /**
  1864. * @see {@link EncounterService#getEncountersNotAssignedToAnyVisit(Patient)}
  1865. */
  1866. @Test
  1867. @Verifies(value = "should return the unvoided encounters not assigned to any visit", method = "getEncountersNotAssignedToAnyVisit(Patient)")
  1868. public void getEncountersNotAssignedToAnyVisit_shouldReturnTheUnvoidedEncountersNotAssignedToAnyVisit() throws Exception {
  1869. executeDataSet(UNIQUE_ENC_WITH_PAGING_XML);
  1870. List<Encounter> encs = Context.getEncounterService().getEncountersNotAssignedToAnyVisit(
  1871. Context.getPatientService().getPatient(10));
  1872. Assert.assertEquals(2, encs.size());
  1873. Assert.assertNull(encs.get(0).getVisit());
  1874. Assert.assertNull(encs.get(1).getVisit());
  1875. }
  1876.  
  1877. /**
  1878. * @see {@link EncounterService#saveEncounter(Encounter)}
  1879. */
  1880. @Test
  1881. @Ignore("TRUNK-50")
  1882. @Verifies(value = "should void and create new obs when saving encounter", method = "saveEncounter(Encounter)")
  1883. public void saveEncounter_shouldVoidAndCreateNewObsWhenSavingEncounter() throws Exception {
  1884. // create an encounter
  1885. Encounter encounter = new Encounter();
  1886. encounter.setLocation(new Location(1));
  1887. encounter.setEncounterType(new EncounterType(1));
  1888. encounter.setEncounterDatetime(new Date());
  1889. encounter.setPatient(new Patient(3));
  1890.  
  1891. // Now add an obs to it
  1892. Obs obs = new Obs();
  1893. obs.setConcept(new Concept(1));
  1894. obs.setValueNumeric(50d);
  1895. encounter.addObs(obs);
  1896.  
  1897. // save the encounter
  1898. EncounterService es = Context.getEncounterService();
  1899. es.saveEncounter(encounter);
  1900.  
  1901. // get the id of this obs
  1902. int oldObsId = obs.getObsId();
  1903.  
  1904. // now change the obs value
  1905. obs.setValueNumeric(100d);
  1906.  
  1907. // resave the encounters
  1908. es.saveEncounter(encounter);
  1909.  
  1910. // get the new obs id
  1911. int newObsId = encounter.getAllObs().iterator().next().getId();
  1912.  
  1913. Assert.assertTrue(oldObsId != newObsId);
  1914. Assert.assertEquals(2, encounter.getAllObs(true).size());
  1915. Assert.assertEquals(1, encounter.getAllObs().size());
  1916. }
  1917.  
  1918. /**
  1919. * @see {@link EncounterService#voidEncounter(Encounter, String)}
  1920. */
  1921. @Test
  1922. @Verifies(value = "should not void providers", method = "voidEncounter(Encounter, String)")
  1923. public void voidEncounter_shouldNotVoidProviders() throws Exception {
  1924. EncounterService encounterService = Context.getEncounterService();
  1925.  
  1926. Encounter encounter = new Encounter();
  1927. encounter.setLocation(new Location(1));
  1928. encounter.setEncounterType(new EncounterType(1));
  1929. encounter.setEncounterDatetime(new Date());
  1930. encounter.setPatient(new Patient(3));
  1931.  
  1932. EncounterRole role = new EncounterRole();
  1933. role.setName("role");
  1934. role = encounterService.saveEncounterRole(role);
  1935.  
  1936. Provider provider = new Provider();
  1937. provider.setName("provider");
  1938. provider.setIdentifier("id1");
  1939. provider = Context.getProviderService().saveProvider(provider);
  1940.  
  1941. encounter.addProvider(role, provider);
  1942. encounterService.saveEncounter(encounter);
  1943.  
  1944. assertEquals(1, encounter.getProvidersByRoles().size());
  1945.  
  1946. encounterService.voidEncounter(encounter, "reason");
  1947.  
  1948. encounter = encounterService.getEncounter(encounter.getEncounterId());
  1949. assertEquals(1, encounter.getProvidersByRoles().size());
  1950. }
  1951.  
  1952. /**
  1953. * @see {@link EncounterService#filterEncountersByViewPermissions(List, User)}
  1954. */
  1955. @Test
  1956. @Verifies(value = "should filter encounters if user is not allowed to see some encounters", method = "filterEncountersByViewPermissions(List, User)")
  1957. public void filterEncountersByViewPermissions_shouldFilterEncountersIfUserIsNotAllowedToSeeSomeEncounters()
  1958. throws Exception {
  1959. EncounterService encounterService = Context.getEncounterService();
  1960.  
  1961. int expectedSize = encounterService.getEncountersByPatientId(3).size();
  1962.  
  1963. Encounter encounter = new Encounter();
  1964. encounter.setLocation(new Location(1));
  1965. encounter.setEncounterDatetime(new Date());
  1966. encounter.setPatient(Context.getPatientService().getPatient(3));
  1967. EncounterType encounterType = new EncounterType(1);
  1968. encounterType.setViewPrivilege(Context.getUserService().getPrivilege("Some Privilege For View Encounter Types"));
  1969. encounter.setEncounterType(encounterType);
  1970.  
  1971. EncounterRole role = new EncounterRole();
  1972. role.setName("role");
  1973. role = encounterService.saveEncounterRole(role);
  1974.  
  1975. Provider provider = new Provider();
  1976. provider.setName("provider");
  1977. provider.setIdentifier("id1");
  1978. provider = Context.getProviderService().saveProvider(provider);
  1979.  
  1980. encounter.addProvider(role, provider);
  1981. encounterService.saveEncounter(encounter);
  1982.  
  1983. List<Encounter> patientEncounters = encounterService.getEncountersByPatientId(3);
  1984. assertEquals(expectedSize + 1, patientEncounters.size());
  1985.  
  1986. if (Context.isAuthenticated()) {
  1987. Context.logout();
  1988. }
  1989. Context.authenticate("test_user", "test");
  1990. Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS);
  1991.  
  1992. patientEncounters = encounterService.getEncountersByPatientId(3);
  1993. int actualSize = patientEncounters.size();
  1994.  
  1995. Context.removeProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS);
  1996. Context.logout();
  1997.  
  1998. assertEquals(actualSize, expectedSize);
  1999. assertTrue(!patientEncounters.contains(encounter));
  2000. }
  2001.  
  2002. /**
  2003. * @see {@link EncounterService#filterEncountersByViewPermissions(List, User)}
  2004. */
  2005. @Test
  2006. @Verifies(value = "should not filter all encounters when the encounter type's view privilege column is null", method = "filterEncountersByViewPermissions(List, User)")
  2007. public void filterEncountersByViewPermissions_shouldNotFilterAllEncountersWhenTheEncounterTypesViewPrivilegeColumnIsNull()
  2008. throws Exception {
  2009. EncounterService encounterService = Context.getEncounterService();
  2010.  
  2011. int beforeSize = encounterService.getEncountersByPatientId(3).size();
  2012.  
  2013. Encounter encounter = new Encounter();
  2014. encounter.setLocation(new Location(1));
  2015. encounter.setEncounterDatetime(new Date());
  2016. encounter.setPatient(Context.getPatientService().getPatient(3));
  2017. EncounterType encounterType = new EncounterType(1);
  2018. // viewPrivilige on encounter type intentionally left null
  2019. encounter.setEncounterType(encounterType);
  2020.  
  2021. EncounterRole role = new EncounterRole();
  2022. role.setName("role");
  2023. role = encounterService.saveEncounterRole(role);
  2024.  
  2025. Provider provider = new Provider();
  2026. provider.setName("provider");
  2027. provider.setIdentifier("id1");
  2028. provider = Context.getProviderService().saveProvider(provider);
  2029.  
  2030. encounter.addProvider(role, provider);
  2031. encounterService.saveEncounter(encounter);
  2032.  
  2033. List<Encounter> patientEncounters = encounterService.getEncountersByPatientId(3);
  2034. assertNotNull(patientEncounters);
  2035. assertEquals(beforeSize + 1, patientEncounters.size());
  2036. }
  2037.  
  2038. /**
  2039. * @see {@link EncounterService#canViewAllEncounterTypes(User)}
  2040. */
  2041. @Test
  2042. @Verifies(value = "should return true if user is granted to view all encounters", method = "canViewAllEncounterTypes(User)")
  2043. public void canViewAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToViewEncounters() throws Exception {
  2044. EncounterService encounterService = Context.getEncounterService();
  2045.  
  2046. EncounterType encounterType = new EncounterType("testing", "desc");
  2047. Privilege viewPrivilege = Context.getUserService().getPrivilege("Some Privilege For View Encounter Types");
  2048. encounterType.setViewPrivilege(viewPrivilege);
  2049.  
  2050. encounterService.saveEncounterType(encounterType);
  2051.  
  2052. User user = Context.getUserService().getUserByUsername("test_user");
  2053. assertNotNull(user);
  2054.  
  2055. assertFalse(encounterService.canViewAllEncounterTypes(user));
  2056.  
  2057. Role role = Context.getUserService().getRole("Provider");
  2058. role.addPrivilege(viewPrivilege);
  2059. user.addRole(role);
  2060.  
  2061. assertTrue(encounterService.canViewAllEncounterTypes(user));
  2062. }
  2063.  
  2064. /**
  2065. * @see {@link EncounterService#canViewAllEncounterTypes(User)}
  2066. */
  2067. @Test
  2068. @Verifies(value = "should return true when the encounter type's view privilege column is null", method = "canViewAllEncounterTypes(User)")
  2069. public void canViewAllEncounterTypes_shouldReturnTrueWhenTheEncounterTypesViewPrivilegeColumnIsNull() throws Exception {
  2070. EncounterService encounterService = Context.getEncounterService();
  2071.  
  2072. // set viewPrivilege on each encounter type to null
  2073. for (EncounterType encounterType : encounterService.getAllEncounterTypes()) {
  2074. encounterType.setViewPrivilege(null);
  2075. encounterService.saveEncounterType(encounterType);
  2076. }
  2077.  
  2078. User user = Context.getUserService().getUserByUsername("test_user");
  2079. assertNotNull(user);
  2080.  
  2081. assertTrue(encounterService.canViewAllEncounterTypes(user));
  2082. }
  2083.  
  2084. /**
  2085. * @see {@link EncounterService#canEditAllEncounterTypes(User)}
  2086. */
  2087. @Test
  2088. @Verifies(value = "should return true if user is granted to edit all encounters", method = "canEditAllEncounterTypes(User)")
  2089. public void canEditAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToEditEncounters() throws Exception {
  2090. EncounterService encounterService = Context.getEncounterService();
  2091.  
  2092. EncounterType encounterType = new EncounterType("testing", "desc");
  2093. Privilege editPrivilege = Context.getUserService().getPrivilege("Some Privilege For Edit Encounter Types");
  2094. encounterType.setEditPrivilege(editPrivilege);
  2095.  
  2096. encounterService.saveEncounterType(encounterType);
  2097.  
  2098. User user = Context.getUserService().getUserByUsername("test_user");
  2099. assertNotNull(user);
  2100.  
  2101. assertFalse(encounterService.canEditAllEncounterTypes(user));
  2102.  
  2103. Role role = Context.getUserService().getRole("Provider");
  2104. role.addPrivilege(editPrivilege);
  2105. user.addRole(role);
  2106.  
  2107. assertTrue(encounterService.canEditAllEncounterTypes(user));
  2108. }
  2109.  
  2110. /**
  2111. * @see {@link EncounterService#canEditAllEncounterTypes(User)}
  2112. */
  2113. @Test
  2114. @Verifies(value = "should return true when the encounter type's edit privilege column is null", method = "canViewAllEncounterTypes(User)")
  2115. public void canViewAllEncounterTypes_shouldReturnTrueWhenTheEncounterTypesEditPrivilegeColumnIsNull() throws Exception {
  2116. EncounterService encounterService = Context.getEncounterService();
  2117.  
  2118. // set editPrivilege on each encounter type to null
  2119. for (EncounterType encounterType : encounterService.getAllEncounterTypes()) {
  2120. encounterType.setEditPrivilege(null);
  2121. encounterService.saveEncounterType(encounterType);
  2122. }
  2123.  
  2124. User user = Context.getUserService().getUserByUsername("test_user");
  2125. assertNotNull(user);
  2126.  
  2127. assertTrue(encounterService.canEditAllEncounterTypes(user));
  2128. }
  2129.  
  2130. /**
  2131. * @see {@link EncounterService#canEditEncounter(Encounter, User)}
  2132. */
  2133. @Test
  2134. @Verifies(value = "should return true if user can edit encounter", method = "canEditEncounter(Encounter, User)")
  2135. public void canEditEncounter_shouldReturnTrueIfUserCanEditEncounter() throws Exception {
  2136. // get encounter that has type with edit privilege set
  2137. Encounter encounter = getEncounterWithEditPrivilege();
  2138.  
  2139. User user = Context.getUserService().getUserByUsername("test_user");
  2140. assertNotNull(user);
  2141.  
  2142. // add required privilege to role in which this user is
  2143. Role role = Context.getUserService().getRole("Provider");
  2144. role.addPrivilege(encounter.getEncounterType().getEditPrivilege());
  2145. user.addRole(role);
  2146.  
  2147. assertTrue(Context.getEncounterService().canEditEncounter(encounter, user));
  2148. }
  2149.  
  2150. /**
  2151. * @see {@link EncounterService#canEditEncounter(Encounter, User)}
  2152. */
  2153. @Test
  2154. @Verifies(value = "should return false if user can not edit encounter", method = "canEditEncounter(Encounter, User)")
  2155. public void canEditEncounter_shouldReturnFalseIfUserCanNotEditEncounter() throws Exception {
  2156. // get encounter that has type with edit privilege set
  2157. Encounter encounter = getEncounterWithEditPrivilege();
  2158.  
  2159. User user = Context.getUserService().getUserByUsername("test_user");
  2160. assertNotNull(user);
  2161.  
  2162. // left user as is - i.e. without required privilege
  2163.  
  2164. assertFalse(Context.getEncounterService().canEditEncounter(encounter, user));
  2165. }
  2166.  
  2167. /**
  2168. * @see {@link EncounterService#canEditEncounter(Encounter, User)}
  2169. */
  2170. @Test(expected = IllegalArgumentException.class)
  2171. @Verifies(value = "should fail if encounter is null", method = "canEditEncounter(Encounter, User)")
  2172. public void canEditEncounter_shouldFailfIfEncounterIsNull() throws Exception {
  2173. // invoke method using null encounter
  2174. Context.getEncounterService().canEditEncounter(null, null);
  2175. }
  2176.  
  2177. /**
  2178. * @see {@link EncounterService#canViewEncounter(Encounter, User)}
  2179. */
  2180. @Test
  2181. @Verifies(value = "should return true if user can view encounter", method = "canViewEncounter(Encounter, User)")
  2182. public void canViewEncounter_shouldReturnTrueIfUserCanViewEncounter() throws Exception {
  2183. // get encounter that has type with view privilege set
  2184. Encounter encounter = getEncounterWithViewPrivilege();
  2185.  
  2186. User user = Context.getUserService().getUserByUsername("test_user");
  2187. assertNotNull(user);
  2188.  
  2189. // add required privilege to role in which this user is
  2190. Role role = Context.getUserService().getRole("Provider");
  2191. role.addPrivilege(encounter.getEncounterType().getViewPrivilege());
  2192. user.addRole(role);
  2193.  
  2194. assertTrue(Context.getEncounterService().canViewEncounter(encounter, user));
  2195. }
  2196.  
  2197. /**
  2198. * @see {@link EncounterService#canViewEncounter(Encounter, User)}
  2199. */
  2200. @Test(expected = IllegalArgumentException.class)
  2201. @Verifies(value = "should fail if encounter is null", method = "canViewEncounter(Encounter, User)")
  2202. public void canViewEncounter_shouldFailfIfEncounterIsNull() throws Exception {
  2203. // invoke method using null encounter
  2204. Context.getEncounterService().canViewEncounter(null, null);
  2205. }
  2206.  
  2207. /**
  2208. * @see {@link EncounterService#canViewEncounter(Encounter, User)}
  2209. */
  2210. @Test
  2211. @Verifies(value = "should return false if user can not view encounter", method = "canViewEncounter(Encounter, User)")
  2212. public void canViewEncounter_shouldReturnFalseIfUserCanNotViewEncounter() throws Exception {
  2213. // get encounter that has type with view privilege set
  2214. Encounter encounter = getEncounterWithViewPrivilege();
  2215.  
  2216. User user = Context.getUserService().getUserByUsername("test_user");
  2217. assertNotNull(user);
  2218.  
  2219. // left user as is - i.e. without required privilege
  2220.  
  2221. assertFalse(Context.getEncounterService().canViewEncounter(encounter, user));
  2222. }
  2223.  
  2224. /**
  2225. * @see {@link EncounterService#getEncounter(Integer)}
  2226. */
  2227. @Test(expected = APIException.class)
  2228. @Verifies(value = "should fail if user is not allowed to view encounter by given id", method = "getEncounter(Integer)")
  2229. public void getEncounter_shouldFailIfUserIsNotAllowedToViewEncounterByGivenId() throws Exception {
  2230. // get encounter that has type with view privilege set
  2231. Encounter encounter = getEncounterWithViewPrivilege();
  2232.  
  2233. User user = Context.getUserService().getUserByUsername("test_user");
  2234. assertNotNull(user);
  2235.  
  2236. // left this user as is - i.e. without required privilege
  2237. // and authenticate under it's account
  2238. Context.becomeUser(user.getSystemId());
  2239.  
  2240. // have to add privilege in order to be able to call getEncounter(Integer) method
  2241. Context.addProxyPrivilege(PrivilegeConstants.VIEW_ENCOUNTERS);
  2242.  
  2243. assertNull(Context.getEncounterService().getEncounter(encounter.getId()));
  2244. }
  2245.  
  2246. /**
  2247. * @see {@link EncounterService#getEncounter(Integer)}
  2248. */
  2249. @Test
  2250. @Verifies(value = "should return encounter if user is allowed to view it", method = "getEncounter(Integer)")
  2251. public void getEncounter_shouldReturnEncounterIfUserIsAllowedToViewIt() throws Exception {
  2252. // get encounter that has type with view privilege set
  2253. Encounter encounter = getEncounterWithViewPrivilege();
  2254.  
  2255. User user = Context.getUserService().getUserByUsername("test_user");
  2256. assertNotNull(user);
  2257.  
  2258. // add required privilege to role in which this user is
  2259. Role role = Context.getUserService().getRole("Provider");
  2260. role.addPrivilege(encounter.getEncounterType().getViewPrivilege());
  2261. user.addRole(role);
  2262.  
  2263. // and authenticate under it's account
  2264. Context.becomeUser(user.getSystemId());
  2265.  
  2266. // have to add privilege in order to be able to call getEncounter(Integer) method
  2267. Context.addProxyPrivilege(PrivilegeConstants.VIEW_ENCOUNTERS);
  2268.  
  2269. assertNotNull(Context.getEncounterService().getEncounter(encounter.getId()));
  2270. }
  2271.  
  2272. /**
  2273. * @see {@link EncounterService#saveEncounter(Encounter)}
  2274. */
  2275. @Test(expected = APIException.class)
  2276. @Verifies(value = "should fail if user is not supposed to edit encounters of type of given encounter", method = "saveEncounter(Encounter)")
  2277. public void saveEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() throws Exception {
  2278. // get encounter that has type with edit privilege set
  2279. Encounter encounter = getEncounterWithEditPrivilege();
  2280.  
  2281. User user = Context.getUserService().getUserByUsername("test_user");
  2282. assertNotNull(user);
  2283.  
  2284. // left this user as is - i.e. without required privilege
  2285. // and authenticate under it's account
  2286. Context.becomeUser(user.getSystemId());
  2287.  
  2288. // have to add privilege in order to be able to call saveEncounter(Encounter) method
  2289. Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
  2290.  
  2291. Context.getEncounterService().saveEncounter(encounter);
  2292. }
  2293.  
  2294. /**
  2295. * @see {@link EncounterService#voidEncounter(Encounter, String)}
  2296. */
  2297. @Test(expected = APIException.class)
  2298. @Verifies(value = "should fail if user is not supposed to edit encounters of type of given encounter", method = "voidEncounter(Encounter, String)")
  2299. public void voidEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() throws Exception {
  2300. // get encounter that has type with edit privilege set
  2301. Encounter encounter = getEncounterWithEditPrivilege();
  2302.  
  2303. User user = Context.getUserService().getUserByUsername("test_user");
  2304. assertNotNull(user);
  2305.  
  2306. // left this user as is - i.e. without required privilege
  2307. // and authenticate under it's account
  2308. Context.becomeUser(user.getSystemId());
  2309.  
  2310. // have to add privilege in order to be able to call voidEncounter(Encounter,String) method
  2311. Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
  2312.  
  2313. Context.getEncounterService().voidEncounter(encounter, "test");
  2314. }
  2315.  
  2316. /**
  2317. * @see {@link EncounterService#unvoidEncounter(Encounter)}
  2318. */
  2319. @Test(expected = APIException.class)
  2320. @Verifies(value = "should fail if user is not supposed to edit encounters of type of given encounter", method = "voidEncounter(Encounter)")
  2321. public void unvoidEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() throws Exception {
  2322. // get encounter that has type with edit privilege set
  2323. Encounter encounter = getEncounterWithEditPrivilege();
  2324.  
  2325. User user = Context.getUserService().getUserByUsername("test_user");
  2326. assertNotNull(user);
  2327.  
  2328. // left this user as is - i.e. without required privilege
  2329. // and authenticate under it's account
  2330. Context.becomeUser(user.getSystemId());
  2331.  
  2332. // have to add privilege in order to be able to call unvoidEncounter(Encounter) method
  2333. Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
  2334.  
  2335. Context.getEncounterService().unvoidEncounter(encounter);
  2336. }
  2337.  
  2338. /**
  2339. * @see {@link EncounterService#purgeEncounter(Encounter)}
  2340. */
  2341. @Test(expected = APIException.class)
  2342. @Verifies(value = "should fail if user is not supposed to edit encounters of type of given encounter", method = "purgeEncounter(Encounter)")
  2343. public void purgeEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() throws Exception {
  2344. // get encounter that has type with edit privilege set
  2345. Encounter encounter = getEncounterWithEditPrivilege();
  2346.  
  2347. User user = Context.getUserService().getUserByUsername("test_user");
  2348. assertNotNull(user);
  2349.  
  2350. // left this user as is - i.e. without required privilege
  2351. // and authenticate under it's account
  2352. Context.becomeUser(user.getSystemId());
  2353.  
  2354. // have to add privilege in order to be able to call purgeEncounter(Encounter) method
  2355. Context.addProxyPrivilege(PrivilegeConstants.PURGE_ENCOUNTERS);
  2356.  
  2357. Context.getEncounterService().purgeEncounter(encounter);
  2358. }
  2359.  
  2360. /**
  2361. * @see {@link EncounterService#purgeEncounter(Encounter,Boolean)}
  2362. */
  2363. @Test(expected = APIException.class)
  2364. @Verifies(value = "should fail if user is not supposed to edit encounters of type of given encounter", method = "purgeEncounter(Encounter,Boolean)")
  2365. public void purgeEncounterCascade_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter()
  2366. throws Exception {
  2367. // get encounter that has type with edit privilege set
  2368. Encounter encounter = getEncounterWithEditPrivilege();
  2369.  
  2370. User user = Context.getUserService().getUserByUsername("test_user");
  2371. assertNotNull(user);
  2372.  
  2373. // left this user as is - i.e. without required privilege
  2374. // and authenticate under it's account
  2375. Context.becomeUser(user.getSystemId());
  2376.  
  2377. // have to add privilege in order to be able to call purgeEncounter(Encounter,Boolean) method
  2378. Context.addProxyPrivilege(PrivilegeConstants.PURGE_ENCOUNTERS);
  2379.  
  2380. Context.getEncounterService().purgeEncounter(encounter, Boolean.TRUE);
  2381. }
  2382.  
  2383. @Test(expected = APIException.class)
  2384. public void getActiveEncounterVisitHandler_shouldThrowIfBeanWithGivenTypeAndNameNotFound() {
  2385.  
  2386. String incorrectBeanName = OpenmrsConstants.REGISTERED_COMPONENT_NAME_PREFIX + "invalidName";
  2387.  
  2388. GlobalProperty visitHandlerProperty = new GlobalProperty(OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER,
  2389. incorrectBeanName);
  2390.  
  2391. Context.getAdministrationService().saveGlobalProperty(visitHandlerProperty);
  2392.  
  2393. Context.getEncounterService().getActiveEncounterVisitHandler();
  2394. }
  2395.  
  2396. @Test
  2397. public void getActiveEncounterVisitHandler_shouldReturnBeanHaveBeenRegisteredWithGivenName() {
  2398.  
  2399. String correctBeanName = OpenmrsConstants.REGISTERED_COMPONENT_NAME_PREFIX + "existingOrNewVisitAssignmentHandler";
  2400.  
  2401. GlobalProperty visitHandlerProperty = new GlobalProperty(OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER,
  2402. correctBeanName);
  2403.  
  2404. Context.getAdministrationService().saveGlobalProperty(visitHandlerProperty);
  2405.  
  2406. EncounterVisitHandler activeEncounterVisitHandler = Context.getEncounterService().getActiveEncounterVisitHandler();
  2407.  
  2408. Assert.assertNotNull(activeEncounterVisitHandler);
  2409. }
  2410.  
  2411. /**
  2412. * Gets encounter and adds edit privilege to it
  2413. *
  2414. * @return encounter with type having non null edit privilege
  2415. */
  2416. private Encounter getEncounterWithEditPrivilege() {
  2417. // create service to be used for encounter manipulations
  2418. EncounterService encounterService = Context.getEncounterService();
  2419.  
  2420. Encounter encounter = encounterService.getEncounter(1);
  2421. EncounterType encounterType = encounter.getEncounterType();
  2422. // make sure that encounter type is not null
  2423. assertNotNull(encounterType);
  2424. // set view privilege on this encounter type
  2425. Privilege editPrivilege = Context.getUserService().getPrivilege("Some Privilege For Edit Encounter Types");
  2426. encounterType.setEditPrivilege(editPrivilege);
  2427. encounter.setEncounterType(encounterType);
  2428. // update encounter
  2429. encounter = encounterService.saveEncounter(encounter);
  2430. // make sure that encounter type updated successfully
  2431. assertNotNull(encounter);
  2432.  
  2433. return encounter;
  2434. }
  2435.  
  2436. /**
  2437. * Gets encounter and adds view privilege to it
  2438. *
  2439. * @return encounter with type having non null view privilege
  2440. */
  2441. private Encounter getEncounterWithViewPrivilege() {
  2442. // create service to be used for encounter manipulations
  2443. EncounterService encounterService = Context.getEncounterService();
  2444.  
  2445. Encounter encounter = encounterService.getEncounter(1);
  2446. EncounterType encounterType = encounter.getEncounterType();
  2447. // make sure that encounter type is not null
  2448. assertNotNull(encounterType);
  2449. // set view privilege on this encounter type
  2450. Privilege viewPrivilege = Context.getUserService().getPrivilege("Some Privilege For View Encounter Types");
  2451. encounterType.setViewPrivilege(viewPrivilege);
  2452. encounter.setEncounterType(encounterType);
  2453. // update encounter
  2454. encounter = encounterService.saveEncounter(encounter);
  2455. // make sure that encounter was updated successfully
  2456. assertNotNull(encounter);
  2457.  
  2458. return encounter;
  2459. }
  2460.  
  2461. /**
  2462. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2463. *
  2464. */
  2465. @Test
  2466. @Verifies(value = "should fetch encounters by patient id", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2467. public void getEncounters_shouldFetchEncountersByPatientId() throws Exception {
  2468. Assert.assertEquals(2, Context.getEncounterService().getEncounters(null, 3, null, null, false).size());
  2469. Assert.assertEquals(4, Context.getEncounterService().getEncounters(null, 3, null, null, true).size());
  2470. }
  2471.  
  2472. /**
  2473. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2474. */
  2475. @Test
  2476. @Verifies(value = "should match on the location name", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2477. public void getEncounters_shouldMatchOnTheLocationName() throws Exception {
  2478. Assert.assertEquals(2, Context.getEncounterService().getEncounters("Test Location", 3, null, null, false).size());
  2479. Assert.assertEquals(4, Context.getEncounterService().getEncounters("Test Location", 3, null, null, true).size());
  2480. }
  2481.  
  2482. /**
  2483. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2484. */
  2485. @Test
  2486. @Verifies(value = "should match on the provider name", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2487. public void getEncounters_shouldMatchOnTheProviderName() throws Exception {
  2488. Assert.assertEquals(1, Context.getEncounterService().getEncounters("phys", 3, null, null, false).size());
  2489. }
  2490.  
  2491. /**
  2492. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2493. */
  2494. @Test
  2495. @Verifies(value = "should should match on provider identifier", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2496. public void getEncounters_shouldShouldMatchOnProviderIdentifier() throws Exception {
  2497. Assert.assertEquals(1, Context.getEncounterService().getEncounters("2", 3, null, null, false).size());
  2498. Assert.assertEquals(2, Context.getEncounterService().getEncounters("2", 3, null, null, true).size());
  2499. }
  2500.  
  2501. /**
  2502. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2503. */
  2504. @Test
  2505. @Verifies(value = "should match on the provider person name", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2506. public void getEncounters_shouldMatchOnTheProviderPersonName() throws Exception {
  2507. //Should match on Super User and John3 Doe
  2508. Assert.assertEquals(1, Context.getEncounterService().getEncounters("er jo", 3, null, null, false).size());
  2509. Assert.assertEquals(2, Context.getEncounterService().getEncounters("er jo", 3, null, null, true).size());
  2510. Assert.assertEquals(0, Context.getEncounterService().getEncounters("none", 3, null, null, true).size());
  2511. }
  2512.  
  2513. /**
  2514. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2515. */
  2516. @Test
  2517. @Verifies(value = "should include voided encounters if includeVoided is set to true", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2518. public void getEncounters_shouldIncludeVoidedEncountersIfIncludeVoidedIsSetToTrue() throws Exception {
  2519. Assert.assertEquals(2, Context.getEncounterService().getEncounters("2", 3, null, null, true).size());
  2520. }
  2521.  
  2522. /**
  2523. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2524. *
  2525. */
  2526. @Test
  2527. @Verifies(value = "should match on the encounter type name", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2528. public void getEncounters_shouldMatchOnTheEncounterTypeName() throws Exception {
  2529. Assert.assertEquals(2, Context.getEncounterService().getEncounters("Type B", 3, null, null, false).size());
  2530. }
  2531.  
  2532. /**
  2533. * @see {@link EncounterService#getEncounters(String,Integer,Integer,Integer,null)}
  2534. *
  2535. */
  2536. @Test
  2537. @Verifies(value = "should match on the form name", method = "getEncounters(String,Integer,Integer,Integer,null)")
  2538. public void getEncounters_shouldMatchOnTheFormName() throws Exception {
  2539. Assert.assertEquals(2, Context.getEncounterService().getEncounters("Basic", 3, null, null, false).size());
  2540. }
  2541.  
  2542. /**
  2543. * @see {@link EncounterService#saveEncounterType(EncounterType)}}
  2544. * @see {@link EncounterService#checkIfEncounterTypesAreLocked()}}
  2545. */
  2546. @Test(expected = EncounterTypeLockedException.class)
  2547. @Verifies(value = "throw error when trying to save encounter type when encounter types are locked", method = "saveEncounterType(EncounterType)")
  2548. public void saveEncounterType_throwErrorWhenTryingToSaveEncounterTypeWhenEncounterTypesAreLocked() throws Exception {
  2549. // dataset to lock encounter types
  2550. executeDataSet("../../../../../../../web/src/test/resources/org/openmrs/web/encounter/include/EncounterTypeFormControllerTest.xml");
  2551.  
  2552. EncounterService encounterService = Context.getEncounterService();
  2553. EncounterType encounterType = Context.getEncounterService().getEncounterType(1);
  2554.  
  2555. Assert.assertNotNull(encounterType);
  2556.  
  2557. encounterService.saveEncounterType(encounterType);
  2558. }
  2559. }
Advertisement
Add Comment
Please, Sign In to add comment