1 package ca.uhn.hl7v2.hoh.llp;
2
3 import static org.junit.Assert.*;
4
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.Executors;
7
8 import ca.uhn.hl7v2.parser.XMLParser;
9 import org.junit.AfterClass;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12
13 import ca.uhn.hl7v2.DefaultHapiContext;
14 import ca.uhn.hl7v2.app.Connection;
15 import ca.uhn.hl7v2.app.ConnectionHub;
16 import ca.uhn.hl7v2.app.Initiator;
17 import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback;
18 import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback;
19 import ca.uhn.hl7v2.hoh.encoder.EncodingStyle;
20 import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
21 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
22 import ca.uhn.hl7v2.model.Message;
23 import ca.uhn.hl7v2.model.v25.message.ADT_A05;
24 import ca.uhn.hl7v2.parser.DefaultXMLParser;
25 import ca.uhn.hl7v2.parser.PipeParser;
26
27 public class LlpClientTest {
28
29 private static Hl7OverHttpLowerLayerProtocol ourLlp;
30 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LlpClientTest.class);
31 private static int ourPort;
32 private static SingleCredentialServerCallback ourServerCallback;
33 private static ServerSocketThreadForTesting ourServerSocketThread;
34 private static ExecutorService ourExecutor;
35
36
37
38 @Test
39 public void testSendMessageSimpl() throws Exception {
40
41 String message =
42 "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" +
43 "EVN||200803051509\r" +
44 "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
45 ADT_A05 msg = new ADT_A05();
46 msg.parse(message);
47
48 Connection conn;
49 try {
50 DefaultHapiContext context = new DefaultHapiContext();
51 context.setExecutorService(ourExecutor);
52 ConnectionHub connectionHub = context.getConnectionHub();
53 PipeParser pipeParser = PipeParser.getInstanceWithNoValidation();
54 conn = connectionHub.attach("localhost", ourPort, pipeParser, ourLlp, false);
55 } catch (Exception e) {
56 throw new Exception(e.getMessage() + " - " + RandomServerPortProvider.list().toString(), e);
57 }
58 Initiator initiator = conn.getInitiator();
59 Message response = initiator.sendAndReceive(msg);
60
61 ourLog.info("Received response");
62
63 assertEquals(message, ourServerSocketThread.getMessage());
64 assertEquals(ourServerSocketThread.getReply().encode(), response.encode());
65
66 }
67
68 @Test
69 public void testSendMessageSimpleXmlViaParser() throws Exception {
70
71 String messageStr =
72 "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" +
73 "EVN||200803051509\r" +
74 "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
75 Message msg = PipeParser.getInstanceWithNoValidation().parse(messageStr);
76 msg.setParser(DefaultXMLParser.getInstanceWithNoValidation());
77 messageStr = msg.encode();
78
79 Connection conn;
80 try {
81 DefaultHapiContext context = new DefaultHapiContext();
82 context.setExecutorService(ourExecutor);
83 ConnectionHub connectionHub = context.getConnectionHub();
84 XMLParser parser = DefaultXMLParser.getInstanceWithNoValidation();
85 conn = connectionHub.attach("localhost", ourPort, parser, ourLlp, false);
86 } catch (Exception e) {
87 throw new Exception(e.getMessage() + " - " + RandomServerPortProvider.list().toString(), e);
88 }
89 Initiator initiator = conn.getInitiator();
90 Message response = initiator.sendAndReceive(msg);
91
92 ourLog.info("Received response");
93
94 assertEquals(messageStr, ourServerSocketThread.getMessage());
95 assertEquals(ourServerSocketThread.getReply().encode(), response.encode());
96
97 assertEquals(EncodingStyle.XML.getContentType(), ourServerSocketThread.getContentType());
98 assertEquals(EncodingStyle.XML, ourServerSocketThread.getEncoding());
99 }
100
101 @AfterClass
102 public static void afterClass() throws InterruptedException {
103 ourLog.info("Marking done as true");
104 ourServerSocketThread.done();
105 ourExecutor.shutdown();
106 }
107
108 @BeforeClass
109 public static void beforeClass() throws InterruptedException {
110 ourPort = RandomServerPortProvider.findFreePort();
111
112 ourLlp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
113 ourLlp.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
114 ourServerCallback = new SingleCredentialServerCallback("hello", "hapiworld");
115
116 ourServerSocketThread = new ServerSocketThreadForTesting(ourPort, ourServerCallback);
117 ourServerSocketThread.start();
118 ourServerSocketThread.getLatch().await();
119
120 ourExecutor = Executors.newCachedThreadPool();
121
122 Thread.sleep(1000);
123 }
124
125
126 @Test
127 public void testSendMessageSimpleWithClientSigner() throws Exception {
128
129
130
131
132 String message =
133 "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" +
134 "EVN||200803051509\r" +
135 "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
136 ADT_A05 msg = new ADT_A05();
137 msg.parse(message);
138
139 Connection conn;
140 try {
141 DefaultHapiContext context = new DefaultHapiContext();
142 context.setExecutorService(ourExecutor);
143 ConnectionHub connectionHub = context.getConnectionHub();
144 PipeParser pipeParser = PipeParser.getInstanceWithNoValidation();
145 conn = connectionHub.attach("localhost", ourPort, pipeParser, ourLlp, false);
146 } catch (Exception e) {
147 throw new Exception(e.getMessage() + " - " + RandomServerPortProvider.list().toString(), e);
148 }
149 Initiator initiator = conn.getInitiator();
150 Message response = initiator.sendAndReceive(msg);
151
152 ourLog.info("Received response");
153
154 assertEquals(message, ourServerSocketThread.getMessage());
155 assertEquals(ourServerSocketThread.getReply().encode(), response.encode());
156
157 }
158
159 }