Advertisement
Guest User

Untitled

a guest
Aug 29th, 2012
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 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.hl7query.web.controller;
  15.  
  16. import java.util.Date;
  17.  
  18. import javax.servlet.http.HttpServletRequest;
  19.  
  20. import org.apache.commons.io.IOUtils;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.openmrs.Encounter;
  26. import org.openmrs.Patient;
  27. import org.openmrs.api.context.Context;
  28. import org.openmrs.module.hl7query.HL7Template;
  29. import org.openmrs.module.hl7query.api.HL7QueryService;
  30. import org.openmrs.test.BaseModuleContextSensitiveTest;
  31. import org.openmrs.test.Verifies;
  32. import org.springframework.mock.web.MockHttpServletRequest;
  33.  
  34. /**
  35. * Contains tests for the {@link HL7QueryController}
  36. */
  37. public class HL7QueryControllerTest extends BaseModuleContextSensitiveTest {
  38.  
  39. private static final String ENCOUNTER_1_UUID = "6519d653-393b-4118-9c83-a3715b82d4ac";
  40.  
  41. private static final String MODULE_TEST_DATA_XML = "moduleTestData.xml";
  42.  
  43. private HL7QueryService service;
  44.  
  45. @Before
  46. public void before() throws Exception {
  47. service = Context.getService(HL7QueryService.class);
  48. executeDataSet(MODULE_TEST_DATA_XML);
  49.  
  50. //Set the templates text for all the templates by loading them form the test template files
  51. HL7Template oruro1Template = service.getHL7TemplateByName("Generic ORUR01");
  52. oruro1Template.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  53. "templates/complete_orur01.xml")));
  54.  
  55. HL7Template MSHTemplate = service.getHL7TemplateByName("MSH");
  56. MSHTemplate.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  57. "templates/MSH.xml")));
  58.  
  59. HL7Template patientTemplate = service.getHL7TemplateByName("Generic Patient");
  60. patientTemplate.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  61. "templates/patient_orur01.xml")));
  62.  
  63. HL7Template PIDTemplate = service.getHL7TemplateByName("Generic PID");
  64. PIDTemplate.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  65. "templates/PID.xml")));
  66.  
  67. HL7Template patientNameTemplate = service.getHL7TemplateByName("Default Patient Name");
  68. patientNameTemplate.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  69. "templates/DefaultPatientNameTemplate.xml")));
  70.  
  71. HL7Template patientIdTemplate = service.getHL7TemplateByName("Default Patient Identifier");
  72. patientIdTemplate.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  73. "templates/DefaultPatientIdentifier.xml")));
  74.  
  75. //Why is this template located in a different places from the others
  76. HL7Template PV1Template = service.getHL7TemplateByName("Generic PV1");
  77. PV1Template.setTemplate(IOUtils.toString(HL7QueryControllerTest.class.getClassLoader().getResourceAsStream(
  78. "org/openmrs/module/hl7query/api/templates/PV1.xml")));
  79.  
  80. //TODO Set the template text for the template in https://tickets.openmrs.org/browse/HLQRY-38
  81. service.saveHL7Template(oruro1Template);
  82. service.saveHL7Template(MSHTemplate);
  83. service.saveHL7Template(patientTemplate);
  84. service.saveHL7Template(PIDTemplate);
  85. service.saveHL7Template(patientNameTemplate);
  86. service.saveHL7Template(patientIdTemplate);
  87. service.saveHL7Template(PV1Template);
  88. }
  89.  
  90. /**
  91. * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)}
  92. */
  93. @Test
  94. @Verifies(value = "should return the expected hl7 output as xml if the xml header exists", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)")
  95. public void getEncounters_shouldReturnTheExpectedHl7OutputAsXmlIfTheXmlHeaderExists() throws Exception {
  96. //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedORUR01Output.xml'
  97. //When https://tickets.openmrs.org/browse/HLQRY-38 is completed
  98. String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
  99. "expectedORUR01XmlOutput.xml"));
  100. expectedOutput = StringUtils.deleteWhitespace(expectedOutput);
  101.  
  102. MockHttpServletRequest request = new MockHttpServletRequest();
  103. request.addHeader("Accept", "text/xml");
  104. String hl7Output = new HL7QueryController().getEncounters(null, null, ENCOUNTER_1_UUID, null, null, request)
  105. .toString();
  106. hl7Output = StringUtils.deleteWhitespace(hl7Output);
  107. //Ignore timestamp by removing it
  108. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), "");
  109. //Ignore the uuid of the message
  110. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), "");
  111.  
  112. Assert.assertEquals(expectedOutput, hl7Output);
  113. }
  114.  
  115. /**
  116. * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)}
  117. */
  118. @Test
  119. @Verifies(value = "should return the expected hl7 in the format that matches the accept header value", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)")
  120. public void getEncounters_shouldReturnTheExpectedHl7InTheFormatThatMatchesTheAcceptHeaderValue() throws Exception {
  121. MockHttpServletRequest request = new MockHttpServletRequest();
  122. String hl7Output = new HL7QueryController().getEncounters(null, null, ENCOUNTER_1_UUID, null, null, request)
  123. .toString();
  124. hl7Output = StringUtils.deleteWhitespace(hl7Output);
  125.  
  126. //TODO Find a better way to test this
  127. //Check it contains series of pipes
  128. Assert.assertTrue(hl7Output.contains("||||||"));
  129. Assert.assertFalse(hl7Output.contains("<ORU_R01 xmlns=\"urn:hl7-org:v2xml\">"));
  130. Assert.assertFalse(hl7Output.contains("</ORU_R01>"));
  131. }
  132.  
  133. /**
  134. * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)}
  135. */
  136. @Test
  137. @Verifies(value = "should return the patient encounters given the patient identifier and id type", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)")
  138. public void getEncounters_shouldReturnThePatientEncountersGivenThePatientIdentifierAndIdType() throws Exception {
  139. //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedOutput_encountersByPatient.xml'
  140. //When https://tickets.openmrs.org/browse/HLQRY-38 is completed
  141. String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
  142. "expectedOutput_encountersByPatient.xml"));
  143. expectedOutput = StringUtils.deleteWhitespace(expectedOutput);
  144.  
  145. final String identifier = "6TS-4";
  146. final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968";
  147. final int expectedEncounterCount = 3;
  148. Patient patient = Context.getPatientService().getPatient(7);
  149. //sanity checks
  150. Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier());
  151. Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid());
  152. Assert.assertEquals(expectedEncounterCount, Context.getEncounterService().getEncountersByPatient(patient).size());
  153.  
  154. MockHttpServletRequest request = new MockHttpServletRequest();
  155. request.addHeader("Accept", "text/xml");
  156. String hl7Output = new HL7QueryController().getEncounters(identifier, identifierTypeUuid, null, null, null, request)
  157. .toString();
  158.  
  159. hl7Output = StringUtils.deleteWhitespace(hl7Output);
  160. //Ignore timestamp by removing it
  161. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), "");
  162. //Ignore the uuid of the message
  163. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), "");
  164.  
  165. Assert.assertEquals(expectedOutput, hl7Output);
  166. }
  167.  
  168. /**
  169. * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)}
  170. */
  171. @Test
  172. @Verifies(value = "should return the patient encounters matching specified start and end encounter dates", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)")
  173. public void getEncounters_shouldReturnThePatientEncountersMatchingSpecifiedStartAndEndEncounterDates() throws Exception {
  174. //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedOutput_encountersByStartAndEndDate.xml'
  175. //When https://tickets.openmrs.org/browse/HLQRY-38 is completed
  176. String expectedOutput = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
  177. "expectedOutput_encountersByStartAndEndDate.xml"));
  178. expectedOutput = StringUtils.deleteWhitespace(expectedOutput);
  179.  
  180. final String identifier = "6TS-4";
  181. final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968";
  182. Patient patient = Context.getPatientService().getPatient(7);
  183. //sanity checks
  184. Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier());
  185. Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid());
  186.  
  187. MockHttpServletRequest request = new MockHttpServletRequest();
  188. request.addHeader("Accept", "text/xml");
  189. Encounter expectedEncounter = Context.getEncounterService().getEncounter(4);
  190. String hl7Output = new HL7QueryController().getEncounters(identifier, identifierTypeUuid, null,
  191. expectedEncounter.getEncounterDatetime(), expectedEncounter.getEncounterDatetime(), request).toString();
  192.  
  193. hl7Output = StringUtils.deleteWhitespace(hl7Output);
  194. //Ignore timestamp by removing it
  195. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), "");
  196. //Ignore the uuid of the message
  197. hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), "");
  198.  
  199. Assert.assertEquals(expectedOutput, hl7Output);
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement