001package ca.uhn.hl7v2.hoh.hapi.client; 002 003import static org.junit.Assert.*; 004 005import org.junit.After; 006import org.junit.Before; 007import org.junit.Test; 008 009import ca.uhn.hl7v2.hoh.api.IReceivable; 010import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback; 011import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback; 012import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable; 013import ca.uhn.hl7v2.hoh.llp.ServerSocketThreadForTesting; 014import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider; 015import ca.uhn.hl7v2.model.Message; 016import ca.uhn.hl7v2.model.v25.message.ADT_A05; 017import ca.uhn.hl7v2.parser.PipeParser; 018 019public class HapiClientTest { 020 021 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HapiClientTest.class); 022 private static int ourPort; 023 private static SingleCredentialServerCallback ourServerCallback; 024 private static ServerSocketThreadForTesting ourServerSocketThread; 025 026 @Test 027 public void testSendMessageSimple() throws Exception { 028 setUpTest(); 029 030 String message = // - 031 "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - 032 "EVN||200803051509\r" + // - 033 "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - 034 ADT_A05 msg = new ADT_A05(); 035 msg.parse(message); 036 037 HohClientSimple client = new HohClientSimple("localhost", ourPort, "/someuri", PipeParser.getInstanceWithNoValidation()); 038 client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld")); 039 client.setAutoClose(true); 040 041 IReceivable<Message> response = client.sendAndReceiveMessage(new MessageSendable(msg)); 042 ourLog.info("Received response"); 043 044 assertEquals(message, ourServerSocketThread.getMessage()); 045 assertEquals(ourServerSocketThread.getReply().encode(), response.getMessage().encode()); 046 047 } 048 049 050 051 @After 052 public void after() throws InterruptedException { 053 ourLog.info("Marking done as true"); 054 ourServerSocketThread.done(); 055 } 056 057 @Before 058 public void before() { 059 //nothing 060 } 061 062 private void setUpTest() throws InterruptedException { 063 ourPort = RandomServerPortProvider.findFreePort(); 064 ourLog.info("Port is: {}", ourPort); 065 066 ourServerCallback = new SingleCredentialServerCallback("hello", "hapiworld"); 067 068 ourServerSocketThread = new ServerSocketThreadForTesting(ourPort, ourServerCallback); 069 ourServerSocketThread.start(); 070 ourServerSocketThread.getLatch().await(); 071 072 } 073 074}