001package ca.uhn.hl7v2.hoh.llp;
002
003import static org.junit.Assert.*;
004
005import java.util.concurrent.ExecutorService;
006import java.util.concurrent.Executors;
007
008import ca.uhn.hl7v2.parser.XMLParser;
009import org.junit.AfterClass;
010import org.junit.BeforeClass;
011import org.junit.Test;
012
013import ca.uhn.hl7v2.DefaultHapiContext;
014import ca.uhn.hl7v2.app.Connection;
015import ca.uhn.hl7v2.app.ConnectionHub;
016import ca.uhn.hl7v2.app.Initiator;
017import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback;
018import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback;
019import ca.uhn.hl7v2.hoh.encoder.EncodingStyle;
020import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
021import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
022import ca.uhn.hl7v2.model.Message;
023import ca.uhn.hl7v2.model.v25.message.ADT_A05;
024import ca.uhn.hl7v2.parser.DefaultXMLParser;
025import ca.uhn.hl7v2.parser.PipeParser;
026
027public class LlpClientTest {
028
029        private static Hl7OverHttpLowerLayerProtocol ourLlp;
030        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LlpClientTest.class);
031        private static int ourPort;
032        private static SingleCredentialServerCallback ourServerCallback;
033        private static ServerSocketThreadForTesting ourServerSocketThread;
034        private static ExecutorService ourExecutor;
035
036        // TODO: LLP should respect the "close" header in a response
037        
038        @Test
039        public void testSendMessageSimpl() throws Exception {
040
041                String message = // -
042                "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // -
043                                "EVN||200803051509\r" + // -
044                                "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -
045                ADT_A05 msg = new ADT_A05();
046                msg.parse(message);
047
048                Connection conn;
049                try {
050                        DefaultHapiContext context = new DefaultHapiContext();
051                        context.setExecutorService(ourExecutor);
052                        ConnectionHub connectionHub = context.getConnectionHub();
053                        PipeParser pipeParser = PipeParser.getInstanceWithNoValidation();
054                        conn = connectionHub.attach("localhost", ourPort, pipeParser, ourLlp, false);
055                } catch (Exception e) {
056                        throw new Exception(e.getMessage() + " - " + RandomServerPortProvider.list().toString(), e);
057                }
058                Initiator initiator = conn.getInitiator();
059                Message response = initiator.sendAndReceive(msg);
060
061                ourLog.info("Received response");
062
063                assertEquals(message, ourServerSocketThread.getMessage());
064                assertEquals(ourServerSocketThread.getReply().encode(), response.encode());
065
066        }
067
068        @Test
069        public void testSendMessageSimpleXmlViaParser() throws Exception {
070
071                String messageStr = // -
072                "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // -
073                                "EVN||200803051509\r" + // -
074                                "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -
075                Message msg = PipeParser.getInstanceWithNoValidation().parse(messageStr);
076                msg.setParser(DefaultXMLParser.getInstanceWithNoValidation());
077                messageStr = msg.encode();
078
079                Connection conn;
080                try {
081                        DefaultHapiContext context = new DefaultHapiContext();
082                        context.setExecutorService(ourExecutor);
083                        ConnectionHub connectionHub = context.getConnectionHub();
084                        XMLParser parser = DefaultXMLParser.getInstanceWithNoValidation();
085                        conn = connectionHub.attach("localhost", ourPort, parser, ourLlp, false);
086                } catch (Exception e) {
087                        throw new Exception(e.getMessage() + " - " + RandomServerPortProvider.list().toString(), e);
088                }
089                Initiator initiator = conn.getInitiator();
090                Message response = initiator.sendAndReceive(msg);
091
092                ourLog.info("Received response");
093
094                assertEquals(messageStr, ourServerSocketThread.getMessage());
095                assertEquals(ourServerSocketThread.getReply().encode(), response.encode());
096
097                assertEquals(EncodingStyle.XML.getContentType(), ourServerSocketThread.getContentType());
098                assertEquals(EncodingStyle.XML, ourServerSocketThread.getEncoding());
099        }
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//              ourLlp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
129//              ourLlp.setSigner(BouncyCastleCmsMessageSignerTest.createSigner());
130//              ourLlp.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
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}