Advertisement
Guest User

Untitled

a guest
Nov 28th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.82 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.module.webservices.rest19ext.web.v1_0.controller;
  15.  
  16. import java.text.DateFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import java.util.List;
  20.  
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23.  
  24. import org.apache.commons.beanutils.PropertyUtils;
  25. import org.codehaus.jackson.map.ObjectMapper;
  26. import org.junit.Assert;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.openmrs.Patient;
  30. import org.openmrs.Visit;
  31. import org.openmrs.api.VisitService;
  32. import org.openmrs.api.context.Context;
  33. import org.openmrs.module.webservices.rest.SimpleObject;
  34. import org.openmrs.module.webservices.rest.test.Util;
  35. import org.openmrs.module.webservices.rest.web.RestConstants;
  36. import org.openmrs.module.webservices.rest19ext.test.Rest19ExtTestConstants;
  37. import org.openmrs.test.Verifies;
  38. import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
  39. import org.springframework.mock.web.MockHttpServletRequest;
  40. import org.springframework.mock.web.MockHttpServletResponse;
  41.  
  42. public class VisitControllerTest extends BaseModuleWebContextSensitiveTest {
  43.  
  44. private VisitService service;
  45.  
  46. private VisitController controller;
  47.  
  48. private MockHttpServletRequest request;
  49.  
  50. private HttpServletResponse response;
  51.  
  52. private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  53.  
  54. @Before
  55. public void before() {
  56. this.service = Context.getVisitService();
  57. this.controller = new VisitController();
  58. this.request = new MockHttpServletRequest();
  59. this.response = new MockHttpServletResponse();
  60. }
  61.  
  62. @Test
  63. public void shouldGetAVisitByUuid() throws Exception {
  64. Object result = controller.retrieve(Rest19ExtTestConstants.VISIT_UUID, request);
  65. Assert.assertNotNull(result);
  66. Assert.assertEquals(Rest19ExtTestConstants.VISIT_UUID, PropertyUtils.getProperty(result, "uuid"));
  67. Assert.assertNotNull(PropertyUtils.getProperty(result, "visitType"));
  68. Assert.assertNotNull(PropertyUtils.getProperty(result, "patient"));
  69. Assert.assertNotNull(PropertyUtils.getProperty(result, "location"));
  70. Assert.assertNotNull(PropertyUtils.getProperty(result, "indication"));
  71. Assert.assertNotNull(PropertyUtils.getProperty(result, "startDatetime"));
  72. Assert.assertNotNull(PropertyUtils.getProperty(result, "attributes"));
  73. Assert.assertNull(PropertyUtils.getProperty(result, "stopDatetime"));
  74. Assert.assertNull(PropertyUtils.getProperty(result, "auditinfo"));
  75. Util.log("VISIT:", result);
  76. }
  77.  
  78. @Test
  79. public void shouldCreateAVisit() throws Exception {
  80. int originalCount = service.getAllVisits().size();
  81. String json = "{ \"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\", \"visitType\":\""
  82. + Rest19ExtTestConstants.VISIT_TYPE_UUID + "\", \"location\":\"" + Rest19ExtTestConstants.LOCATION_UUID
  83. + "\", \"startDatetime\":\"" + DATE_FORMAT.format(new Date()) + "\"}";
  84. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  85. Object newVisit = controller.create(post, request, response);
  86. Assert.assertNotNull(PropertyUtils.getProperty(newVisit, "uuid"));
  87. Assert.assertEquals(originalCount + 1, service.getAllVisits().size());
  88. }
  89.  
  90. @Test
  91. public void shouldCreateAVisitWithAttributes() throws Exception {
  92. int originalCount = service.getAllVisits().size();
  93. String json = "{ \"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\", \"visitType\":\""
  94. + Rest19ExtTestConstants.VISIT_TYPE_UUID + "\", \"location\":\"" + Rest19ExtTestConstants.LOCATION_UUID
  95. + "\", \"startDatetime\":\"" + DATE_FORMAT.format(new Date()) + "\","
  96. + "\"attributes\":[{\"attributeType\":\"" + Rest19ExtTestConstants.VISIT_ATTRIBUTE_TYPE_UUID
  97. + "\",\"value\":\"some text\"}]}";
  98. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  99. Object newVisit = controller.create(post, request, response);
  100. Assert.assertNotNull(PropertyUtils.getProperty(newVisit, "uuid"));
  101. Assert.assertEquals(originalCount + 1, service.getAllVisits().size());
  102. }
  103.  
  104. @Test
  105. public void shouldCreateAVisitWithEncounters() throws Exception {
  106. int originalCount = service.getAllVisits().size();
  107. final String patientUuid = "5946f880-b197-400b-9caa-a3c661d23041";
  108. Patient patient = Context.getPatientService().getPatientByUuid(patientUuid);
  109. Assert.assertEquals(0, service.getVisitsByPatient(patient).size());
  110. String json = "{ \"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\", \"visitType\":\""
  111. + Rest19ExtTestConstants.VISIT_TYPE_UUID
  112. + "\", \"location\":\""
  113. + Rest19ExtTestConstants.LOCATION_UUID
  114. + "\", \"startDatetime\":\""
  115. + DATE_FORMAT.format(new Date())
  116. + "\", \"encounters\": [\"6519d653-393b-4118-9c83-a3715b82d4ac\", \"eec646cb-c847-45a7-98bc-91c8c4f70add\"] }";
  117. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  118. Object newVisit = controller.create(post, request, response);
  119. Assert.assertNotNull(PropertyUtils.getProperty(newVisit, "uuid"));
  120. Assert.assertEquals(originalCount + 1, service.getAllVisits().size());
  121. Assert.assertEquals(2, service.getVisitsByPatient(patient).get(0).getEncounters().size());
  122. }
  123.  
  124. @Test
  125. public void shouldEditAVisit() throws Exception {
  126. final String newVisitTypeUuid = Rest19ExtTestConstants.VISIT_TYPE_UUID;
  127. final String newLocationUuid = "9356400c-a5a2-4532-8f2b-2361b3446eb8";
  128. final String newIndicationConceptUuid = "c607c80f-1ea9-4da3-bb88-6276ce8868dd";
  129. final Date newStartDatetime = new Date();
  130. final Date newStopDatetime = new Date();
  131. Visit visit = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  132. Assert.assertNotNull(visit);
  133. //sanity checks
  134. Assert.assertFalse(newVisitTypeUuid.equalsIgnoreCase(visit.getVisitType().getUuid()));
  135. Assert.assertFalse(newLocationUuid.equalsIgnoreCase(visit.getLocation().getUuid()));
  136. Assert.assertFalse(newIndicationConceptUuid.equalsIgnoreCase(visit.getIndication().getUuid()));
  137. Assert.assertFalse(newStartDatetime.equals(visit.getStartDatetime()));
  138. Assert.assertFalse(newStopDatetime.equals(visit.getStopDatetime()));
  139.  
  140. String json = "{ \"visitType\":\"" + newVisitTypeUuid + "\", \"location\":\"" + newLocationUuid
  141. + "\", \"indication\":\"" + newIndicationConceptUuid + "\", \"startDatetime\":\""
  142. + DATE_FORMAT.format(newStartDatetime) + "\", \"stopDatetime\":\"" + DATE_FORMAT.format(newStopDatetime)
  143. + "\" }";
  144.  
  145. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  146. controller.update(Rest19ExtTestConstants.VISIT_UUID, post, request, response);
  147.  
  148. Visit updated = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  149. Assert.assertNotNull(updated);
  150. Assert.assertEquals(newVisitTypeUuid, updated.getVisitType().getUuid());
  151. Assert.assertEquals(newLocationUuid, updated.getLocation().getUuid());
  152. Assert.assertEquals(newIndicationConceptUuid, updated.getIndication().getUuid());
  153. Assert.assertEquals(newStartDatetime, updated.getStartDatetime());
  154. Assert.assertEquals(newStopDatetime, updated.getStopDatetime());
  155. }
  156.  
  157. @Test
  158. public void shouldAddEncountersToAnExistingVisitOnEdit() throws Exception {
  159. Visit visit = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  160. Assert.assertEquals(0, visit.getEncounters().size());
  161.  
  162. String json = "{\"encounters\": [\"6519d653-393b-4118-9c83-a3715b82d4ac\", \"eec646cb-c847-45a7-98bc-91c8c4f70add\"] }";
  163. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  164. controller.update(Rest19ExtTestConstants.VISIT_UUID, post, request, response);
  165. Visit updated = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  166. Assert.assertEquals(2, updated.getEncounters().size());
  167. }
  168.  
  169. @Test
  170. public void shouldRemoveAnEncounterFromAnExistingVisitOnEdit() throws Exception {
  171. final String encounterId = "6519d653-393b-4118-9c83-a3715b82d4ac";
  172. Visit visit = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  173. //add an encounter to be removed for testing purposes
  174. visit.getEncounters().add(Context.getEncounterService().getEncounterByUuid(encounterId));
  175. service.saveVisit(visit);
  176. Assert.assertEquals(1, visit.getEncounters().size());
  177.  
  178. String json = "{\"encounters\": [] }";
  179. SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
  180. controller.update(Rest19ExtTestConstants.VISIT_UUID, post, request, response);
  181. Visit updated = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  182. Assert.assertEquals(0, updated.getEncounters().size());
  183. }
  184.  
  185. @Test
  186. public void shouldVoidAVisit() throws Exception {
  187. Visit visit = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  188. Assert.assertFalse(visit.isVoided());
  189.  
  190. controller.delete(Rest19ExtTestConstants.VISIT_UUID, "test reason", request, response);
  191. visit = service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID);
  192. Assert.assertTrue(visit.isVoided());
  193. Assert.assertEquals("test reason", visit.getVoidReason());
  194. }
  195.  
  196. @Test
  197. public void shouldPurgeAVisit() throws Exception {
  198. Assert.assertNotNull(service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID));
  199. int originalCount = service.getAllVisits().size();
  200. controller.purge(Rest19ExtTestConstants.VISIT_UUID, request, response);
  201. Assert.assertNull(service.getVisitByUuid(Rest19ExtTestConstants.VISIT_UUID));
  202. Assert.assertEquals(originalCount - 1, service.getAllVisits().size());
  203. }
  204.  
  205. @Test
  206. public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception {
  207. request.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
  208. Object result = controller.retrieve(Rest19ExtTestConstants.VISIT_UUID, request);
  209. Assert.assertNotNull(result);
  210. Assert.assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
  211. }
  212.  
  213. /**
  214. * @see {@link VisitController#searchByPatient(String,HttpServletRequest,HttpServletResponse)}
  215. */
  216. @SuppressWarnings("unchecked")
  217. @Test
  218. @Verifies(value = "should get visits for the patient", method = "searchByPatient(String,HttpServletRequest,HttpServletResponse)")
  219. public void searchByPatient_shouldGetUnretiredVisitsForThePatient() throws Exception {
  220. Assert.assertEquals(3, ((List<Object>) controller.searchByPatient("da7f524f-27ce-4bb2-86d6-6d1d05312bd5", request,
  221. response).get("results")).size());
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement