001package ca.uhn.hl7v2.hoh.hapi.server; 002 003import java.io.IOException; 004import java.nio.charset.Charset; 005import java.util.Map; 006import java.util.concurrent.TimeUnit; 007 008import junit.framework.Assert; 009 010import org.junit.After; 011import org.junit.AfterClass; 012import org.junit.Before; 013import org.junit.BeforeClass; 014import org.junit.Test; 015import org.mortbay.jetty.Server; 016import org.mortbay.jetty.servlet.Context; 017import org.mortbay.jetty.servlet.ServletHolder; 018 019import ca.uhn.hl7v2.AcknowledgmentCode; 020import ca.uhn.hl7v2.DefaultHapiContext; 021import ca.uhn.hl7v2.HL7Exception; 022import ca.uhn.hl7v2.app.Connection; 023import ca.uhn.hl7v2.app.ConnectionHub; 024import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol; 025import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider; 026import ca.uhn.hl7v2.hoh.util.ServerRoleEnum; 027import ca.uhn.hl7v2.model.Message; 028import ca.uhn.hl7v2.model.v25.message.ACK; 029import ca.uhn.hl7v2.model.v25.message.ADT_A05; 030import ca.uhn.hl7v2.parser.DefaultXMLParser; 031import ca.uhn.hl7v2.parser.PipeParser; 032import ca.uhn.hl7v2.protocol.ReceivingApplication; 033import ca.uhn.hl7v2.protocol.ReceivingApplicationException; 034import ca.uhn.hl7v2.validation.impl.ValidationContextFactory; 035 036public class HohServletTest { 037 038 private static DefaultHapiContext ourHapiContext; 039 040 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohServletTest.class); 041 private static ConnectionHub ourConnectionHub; 042 private String myMessage; 043 private Message myResponse; 044 private int myPort; 045 private Server myServer; 046 047 @After 048 public void after() throws Exception { 049 myServer.stop(); 050 } 051 052 // TODO: Add test to make sure that client encoding and charset get respected in response 053 054 055 @Before 056 public void before() { 057 myMessage = null; 058 myResponse = null; 059 } 060 061 private void startServer(HohServlet theServlet) throws Exception { 062 myPort = RandomServerPortProvider.findFreePort(); 063 myServer = new Server(myPort); 064 Context context = new Context(myServer, "/", Context.SESSIONS); 065 context.addServlet(new ServletHolder(theServlet), "/*"); 066 067 myServer.start(); 068 069 while (myServer.isStarting()) { 070 ourLog.info("Waiting for server to start..."); 071 Thread.sleep(100); 072 } 073 074 } 075 076 @Test 077 public void testServlet() throws Exception { 078 ReceivingApplication<Message> app = new MyReceivingApp(); 079 HohServlet servlet = new HohServlet(); 080 servlet.setApplication(app); 081 startServer(servlet); 082 083 ADT_A05 msg = new ADT_A05(); 084 msg.initQuickstart("ADT", "A05", "T"); 085 msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs 086 // utf-8 087 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); 088 llp.setPreferredCharset(Charset.forName("UTF-8")); 089 Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); 090 Message response; 091 try { 092 conn.getInitiator().setTimeout(20000, TimeUnit.SECONDS); 093 response = conn.getInitiator().sendAndReceive(msg); 094 } catch (Exception e) { 095 throw e; 096 } 097 String responseStr = response.encode(); 098 ourLog.info("Got response: {}", responseStr); 099 100 Assert.assertEquals(myResponse.encode(), responseStr); 101 String expected = msg.encode(); 102 Assert.assertEquals(expected, myMessage); 103 104 } 105 106 @Test 107 public void testServletAE() throws Exception { 108 ReceivingApplication<Message> app = new MyReceivingApp(); 109 HohServlet servlet = new HohServlet(); 110 servlet.setApplication(app); 111 startServer(servlet); 112 113 ADT_A05 msg = new ADT_A05(); 114 msg.initQuickstart("ADT", "A05", "T"); 115 msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs 116 // utf-8 117 myResponse = msg.generateACK(AcknowledgmentCode.AE, new HL7Exception("dsfasfs")); 118 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); 119 llp.setPreferredCharset(Charset.forName("UTF-8")); 120 Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); 121 Message response = conn.getInitiator().sendAndReceive(msg); 122 123 String responseStr = response.encode(); 124 ourLog.info("Got response: {}", responseStr); 125 126 Assert.assertEquals(myResponse.encode(), responseStr); 127 Assert.assertEquals(msg.encode(), myMessage); 128 129 } 130 131 @Test 132 public void testServletAR() throws Exception { 133 ReceivingApplication<Message> app = new MyReceivingApp(); 134 HohServlet servlet = new HohServlet(); 135 servlet.setApplication(app); 136 startServer(servlet); 137 138 ADT_A05 msg = new ADT_A05(); 139 msg.initQuickstart("ADT", "A05", "T"); 140 msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs 141 // utf-8 142 myResponse = msg.generateACK(AcknowledgmentCode.AR, new HL7Exception("dsfasfs")); 143 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); 144 llp.setPreferredCharset(Charset.forName("UTF-8")); 145 Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); 146 Message response = conn.getInitiator().sendAndReceive(msg); 147 148 String responseStr = response.encode(); 149 ourLog.info("Got response: {}", responseStr); 150 151 Assert.assertEquals(myResponse.encode(), responseStr); 152 Assert.assertEquals(msg.encode(), myMessage); 153 154 } 155 156 @Test 157 public void testServletXml() throws Exception { 158 ReceivingApplication<Message> app = new MyReceivingApp(); 159 HohServlet servlet = new HohServlet(); 160 servlet.setApplication(app); 161 startServer(servlet); 162 163 ADT_A05 msg = new ADT_A05(); 164 msg.initQuickstart("ADT", "A05", "T"); 165 msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs 166 // utf-8 167 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); 168 llp.setPreferredCharset(Charset.forName("UTF-8")); 169 Connection conn = ourConnectionHub.attach("localhost", myPort, DefaultXMLParser.getInstanceWithNoValidation(), llp, false); 170 Message response = conn.getInitiator().sendAndReceive(msg); 171 172 String responseStr = response.encode(); 173 ourLog.info("Got response: {}", responseStr); 174 175 Assert.assertEquals(new DefaultXMLParser().encode(myResponse), responseStr); 176 Assert.assertEquals(new DefaultXMLParser().encode(msg), myMessage); 177 178 } 179 180 @AfterClass 181 public static void afterClass() throws InterruptedException { 182 // Thread.sleep(1000000); 183 ourHapiContext.getExecutorService().shutdown(); 184 } 185 186 @BeforeClass 187 public static void beforeClass() { 188 System.setProperty("DEBUG", "true"); 189 190 ourHapiContext = new DefaultHapiContext(); 191 ourHapiContext.setValidationContext(ValidationContextFactory.noValidation()); 192 ourConnectionHub = ourHapiContext.getConnectionHub(); 193 } 194 195 public class MyReceivingApp implements ReceivingApplication<Message> { 196 197 public boolean canProcess(Message theMessage) { 198 return true; 199 } 200 201 public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception { 202 myMessage = theMessage.encode(); 203 204 ACK response; 205 try { 206 response = (ACK) theMessage.generateACK(); 207 } catch (IOException e) { 208 throw new ReceivingApplicationException(e); 209 } 210 211 response.getMSA().getMsa3_TextMessage().setValue("I♥HAPI"); // needs utf-8 212 213 if (myResponse == null) { 214 myResponse = response; 215 } 216 217 myResponse.setParser(PipeParser.getInstanceWithNoValidation()); 218 219 return myResponse; 220 } 221 222 } 223 224}