View Javadoc

1   package ca.uhn.hl7v2.hoh.hapi.server;
2   
3   import java.io.IOException;
4   import java.nio.charset.Charset;
5   import java.util.Map;
6   import java.util.concurrent.TimeUnit;
7   
8   import junit.framework.Assert;
9   
10  import org.junit.After;
11  import org.junit.AfterClass;
12  import org.junit.Before;
13  import org.junit.BeforeClass;
14  import org.junit.Test;
15  import org.mortbay.jetty.Server;
16  import org.mortbay.jetty.servlet.Context;
17  import org.mortbay.jetty.servlet.ServletHolder;
18  
19  import ca.uhn.hl7v2.AcknowledgmentCode;
20  import ca.uhn.hl7v2.DefaultHapiContext;
21  import ca.uhn.hl7v2.HL7Exception;
22  import ca.uhn.hl7v2.app.Connection;
23  import ca.uhn.hl7v2.app.ConnectionHub;
24  import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol;
25  import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
26  import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
27  import ca.uhn.hl7v2.model.Message;
28  import ca.uhn.hl7v2.model.v25.message.ACK;
29  import ca.uhn.hl7v2.model.v25.message.ADT_A05;
30  import ca.uhn.hl7v2.parser.DefaultXMLParser;
31  import ca.uhn.hl7v2.parser.PipeParser;
32  import ca.uhn.hl7v2.protocol.ReceivingApplication;
33  import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
34  import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
35  
36  public class HohServletTest {
37  
38  	private static DefaultHapiContext ourHapiContext;
39  
40  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohServletTest.class);
41  	private static ConnectionHub ourConnectionHub;
42  	private String myMessage;
43  	private Message myResponse;
44  	private int myPort;
45  	private Server myServer;
46  
47  	@After
48  	public void after() throws Exception {
49  		myServer.stop();
50  	}
51  
52  	// TODO: Add test to make sure that client encoding and charset get respected in response
53  	
54  
55  	@Before
56  	public void before() {
57  		myMessage = null;
58  		myResponse = null;
59  	}
60  
61  	private void startServer(HohServlet theServlet) throws Exception {
62  		myPort = RandomServerPortProvider.findFreePort();
63  		myServer = new Server(myPort);
64  		Context context = new Context(myServer, "/", Context.SESSIONS);
65  		context.addServlet(new ServletHolder(theServlet), "/*");
66  
67  		myServer.start();
68  
69  		while (myServer.isStarting()) {
70  			ourLog.info("Waiting for server to start...");
71  			Thread.sleep(100);
72  		}
73  
74  	}
75  
76  	@Test
77  	public void testServlet() throws Exception {
78  		ReceivingApplication<Message> app = new MyReceivingApp();
79  		HohServlet servlet = new HohServlet();
80  		servlet.setApplication(app);
81  		startServer(servlet);
82  
83  		ADT_A05 msg = new ADT_A05();
84  		msg.initQuickstart("ADT", "A05", "T");
85  		msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs
86  																										// utf-8
87  		Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
88  		llp.setPreferredCharset(Charset.forName("UTF-8"));
89  		Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false);
90  		Message response;
91  		try {
92  			conn.getInitiator().setTimeout(20000, TimeUnit.SECONDS);
93  			response = conn.getInitiator().sendAndReceive(msg);
94  		} catch (Exception e) {
95  			throw e;
96  		}
97  		String responseStr = response.encode();
98  		ourLog.info("Got response: {}", responseStr);
99  
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 }