1 package ca.uhn.hl7v2.hoh.hapi.client;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import ca.uhn.hl7v2.hoh.api.IReceivable;
10 import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback;
11 import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback;
12 import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;
13 import ca.uhn.hl7v2.hoh.llp.ServerSocketThreadForTesting;
14 import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
15 import ca.uhn.hl7v2.model.Message;
16 import ca.uhn.hl7v2.model.v25.message.ADT_A05;
17 import ca.uhn.hl7v2.parser.PipeParser;
18
19 public class HapiClientTest {
20
21 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HapiClientTest.class);
22 private static int ourPort;
23 private static SingleCredentialServerCallback ourServerCallback;
24 private static ServerSocketThreadForTesting ourServerSocketThread;
25
26 @Test
27 public void testSendMessageSimple() throws Exception {
28 setUpTest();
29
30 String message =
31 "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" +
32 "EVN||200803051509\r" +
33 "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
34 ADT_A05 msg = new ADT_A05();
35 msg.parse(message);
36
37 HohClientSimple client = new HohClientSimple("localhost", ourPort, "/someuri", PipeParser.getInstanceWithNoValidation());
38 client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
39 client.setAutoClose(true);
40
41 IReceivable<Message> response = client.sendAndReceiveMessage(new MessageSendable(msg));
42 ourLog.info("Received response");
43
44 assertEquals(message, ourServerSocketThread.getMessage());
45 assertEquals(ourServerSocketThread.getReply().encode(), response.getMessage().encode());
46
47 }
48
49
50
51 @After
52 public void after() throws InterruptedException {
53 ourLog.info("Marking done as true");
54 ourServerSocketThread.done();
55 }
56
57 @Before
58 public void before() {
59
60 }
61
62 private void setUpTest() throws InterruptedException {
63 ourPort = RandomServerPortProvider.findFreePort();
64 ourLog.info("Port is: {}", ourPort);
65
66 ourServerCallback = new SingleCredentialServerCallback("hello", "hapiworld");
67
68 ourServerSocketThread = new ServerSocketThreadForTesting(ourPort, ourServerCallback);
69 ourServerSocketThread.start();
70 ourServerSocketThread.getLatch().await();
71
72 }
73
74 }