View Javadoc

1   package ca.uhn.hl7v2.hoh.relay.sender;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.IOException;
6   import java.util.concurrent.TimeUnit;
7   
8   import org.junit.After;
9   import org.junit.Assert;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import ca.uhn.hl7v2.DefaultHapiContext;
14  import ca.uhn.hl7v2.HL7Exception;
15  import ca.uhn.hl7v2.app.Connection;
16  import ca.uhn.hl7v2.app.ConnectionHub;
17  import ca.uhn.hl7v2.hoh.api.DecodeException;
18  import ca.uhn.hl7v2.hoh.api.EncodeException;
19  import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback;
20  import ca.uhn.hl7v2.hoh.llp.ServerSocketThreadForTesting;
21  import ca.uhn.hl7v2.hoh.relay.Launcher;
22  import ca.uhn.hl7v2.hoh.sockets.CustomCertificateTlsSocketFactory;
23  import ca.uhn.hl7v2.hoh.sockets.CustomCertificateTlsSocketFactoryTest;
24  import ca.uhn.hl7v2.hoh.util.Holder;
25  import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
26  import ca.uhn.hl7v2.llp.LLPException;
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_A01;
30  
31  public class HttpSenderTest {
32  
33  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HttpSenderTest.class);
34  	private int myOutPort;
35  	private ServerSocketThreadForTesting myServerSocketThread;
36  	private SingleCredentialServerCallback ourServerCallback;
37  	private int myInPort;
38  	private int myInPort2;
39  
40  	@After
41  	public void after() throws InterruptedException {
42  		ourLog.info("Marking done as true");
43  		myServerSocketThread.done();
44  	}
45  
46  	@Before
47  	public void before() throws InterruptedException {
48  		// System.setProperty("javax.net.debug", "ssl");
49  
50  		myOutPort = RandomServerPortProvider.findFreePort();
51  		myInPort = RandomServerPortProvider.findFreePort();
52  		myInPort2 = RandomServerPortProvider.findFreePort();
53  		System.setProperty("relay.port.out", Integer.toString(myOutPort));
54  		System.setProperty("relay.port.in", Integer.toString(myInPort));
55  		System.setProperty("relay.port.in.2", Integer.toString(myInPort2));
56  
57  		ourServerCallback = new SingleCredentialServerCallback("hello", "hapiworld");
58  
59  		myServerSocketThread = new ServerSocketThreadForTesting(myOutPort, ourServerCallback);
60  	}
61  
62  	@SuppressWarnings("resource")
63  	@Test
64  	public void testSenderWithTls() throws HL7Exception, IOException, LLPException, InterruptedException, DecodeException, EncodeException {
65  
66  		CustomCertificateTlsSocketFactory serverSocketFactory = CustomCertificateTlsSocketFactoryTest.createTrustedServerSocketFactory();
67  		myServerSocketThread.setServerSockewtFactory(serverSocketFactory);
68  		myServerSocketThread.start();
69  		myServerSocketThread.getLatch().await();
70  
71  		ADT_A01 adt = new ADT_A01();
72  		adt.initQuickstart("ADT", "A01", "T");
73  
74  		Launcher l = new Launcher("src/test/resources/relay/MllpToHttpTlsMutualAuth.xml");
75  
76  		ConnectionHub hub = new DefaultHapiContext().getConnectionHub();
77  		try {
78  
79  			Connection c = hub.attach("localhost", myInPort, false);
80  			c.getInitiator().setTimeout(10000000, TimeUnit.MILLISECONDS);
81  			Message response = c.getInitiator().sendAndReceive(adt);
82  
83  			ourLog.info("Response was:\n{}", response.encode().replace('\r', '\n'));
84  
85  		} finally {
86  			l.shutdown();
87  		}
88  	}
89  
90  	
91  
92  	@SuppressWarnings("resource")
93  	@Test
94  	public void testErrorMessageReferencesRelay() throws HL7Exception, IOException, LLPException, InterruptedException, DecodeException, EncodeException {
95  
96  		ADT_A01 adt = new ADT_A01();
97  		adt.initQuickstart("ADT", "A01", "T");
98  
99  		Launcher l = new Launcher("src/test/resources/relay/MllpToHttp.xml");
100 
101 		ConnectionHub hub = new DefaultHapiContext().getConnectionHub();
102 		try {
103 
104 			Connection c = hub.attach("localhost", myInPort, false);
105 			c.getInitiator().setTimeout(10000000, TimeUnit.MILLISECONDS);
106 			Message response = c.getInitiator().sendAndReceive(adt);
107 
108 			ourLog.info("Response was:\n{}", response.encode().replace('\r', '\n'));
109 
110 			Assert.assertTrue(response.encode().contains("HAPI HL7 over HTTP Relay"));
111 			
112 		} finally {
113 			l.shutdown();
114 		}
115 	}
116 	
117 	
118 	@Test
119 	public void testSetUrl() throws Exception {
120 
121 		RelayHttpSender s = new RelayHttpSender();
122 		s.setUrlString("http://localhost:8888/path");
123 
124 		assertEquals("localhost", s.getHost());
125 		assertEquals(8888, s.getPort());
126 		assertEquals("/path", s.getUriPath());
127 
128 		s.afterPropertiesSet();
129 
130 	}
131 
132 	@Test
133 	public void setMultipleConcurrentSenders() throws Throwable {
134 		Launcher l = new Launcher("src/test/resources/relay/MllpToHttpMultipleListeners.xml");
135 		try {
136 
137 			myServerSocketThread.setResponseDelays(500L, 0L);
138 			myServerSocketThread.start();
139 			myServerSocketThread.getLatch().await();
140 
141 			final ADT_A01 msg1 = new ADT_A01();
142 			msg1.initQuickstart("ADT", "A01", "T");
143 
144 			final ADT_A01 msg2 = new ADT_A01();
145 			msg2.initQuickstart("ADT", "A01", "T");
146 
147 			final Holder<ACK> resp1Holder = new Holder<ACK>();
148 			final Holder<ACK> resp2Holder = new Holder<ACK>();
149 			final Holder<Throwable> failHolder = new Holder<Throwable>();
150 
151 			Thread t1 = new Thread() {
152 				@Override
153 				public void run() {
154 					try {
155 						Connection client1 = new DefaultHapiContext().newClient("localhost", myInPort, false);
156 						resp1Holder.myValue = (ACK) client1.getInitiator().sendAndReceive(msg1);
157 					} catch (Throwable e) {
158 						failHolder.myValue = e;
159 					}
160 				}
161 			};
162 			t1.start();
163 
164 			Thread.sleep(100);
165 			
166 			Thread t2 = new Thread() {
167 				@Override
168 				public void run() {
169 					try {
170 						Connection client2 = new DefaultHapiContext().newClient("localhost", myInPort, false);
171 						resp2Holder.myValue = (ACK) client2.getInitiator().sendAndReceive(msg2);
172 					} catch (Throwable e) {
173 						failHolder.myValue = e;
174 					}
175 				}
176 			};
177 			t2.start();
178 
179 			t1.join();
180 			t2.join();
181 			
182 			if (failHolder.myValue!=null) {
183 				throw failHolder.myValue;
184 			}
185 			
186 			ACK resp1 = resp1Holder.myValue;
187 			ACK resp2 = resp2Holder.myValue;
188 			assertEquals(msg1.getMSH().getMsh10_MessageControlID().getValue(), resp1.getMSA().getMsa2_MessageControlID().getValue());
189 			assertEquals(msg2.getMSH().getMsh10_MessageControlID().getValue(), resp2.getMSA().getMsa2_MessageControlID().getValue());
190 
191 		} finally {
192 			l.shutdown();
193 		}
194 	}
195 
196 }