1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.hl7.transport;
17
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.test.context.ContextConfiguration;
23 import org.springframework.test.context.TestExecutionListeners;
24 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25 import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
26
27 import java.io.BufferedInputStream;
28 import java.io.BufferedOutputStream;
29 import java.io.DataInputStream;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.net.Socket;
33 import java.util.Scanner;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36
37 import static org.junit.Assert.assertTrue;
38
39
40
41
42 @RunWith(SpringJUnit4ClassRunner.class)
43 @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
44 @ContextConfiguration(locations = { "/config/context-transport2.xml" })
45 public class TransportTest2 {
46
47 private static final Logger LOG = LoggerFactory.getLogger(TransportTest2.class);
48
49 @Test
50 public void testMessage01() throws Exception {
51 String message = inputMessage("message/msg-01.hl7");
52
53 Socket socket = new Socket("localhost", 8888);
54 BufferedOutputStream out = new BufferedOutputStream(new DataOutputStream(socket.getOutputStream()));
55 final BufferedInputStream in = new BufferedInputStream(new DataInputStream(socket.getInputStream()));
56
57 int messageCount = 100;
58 CountDownLatch latch = new CountDownLatch(messageCount);
59
60 Thread t = new Thread(() -> {
61 int response;
62 StringBuilder s = new StringBuilder();
63 try {
64 while ((response = in.read()) >= 0) {
65 if (response == 28) {
66 response = in.read();
67 if (response == 13) {
68 LOG.debug("Received response");
69 LOG.debug(s.toString().replace('\r', '\n'));
70 s.setLength(0);
71 latch.countDown();
72 }
73 } else {
74 s.append((char) response);
75 }
76 }
77 } catch (IOException ignored) {
78 }
79 });
80 t.start();
81
82 for (int i = 0; i < messageCount; i++) {
83 String msg = message.replace("123456", String.valueOf(i));
84 out.write(11);
85 out.flush();
86
87 Thread.sleep(10);
88 out.write(msg.getBytes());
89 out.flush();
90
91
92 out.write(28);
93 out.write(13);
94 out.flush();
95
96 }
97
98 boolean success = latch.await(20, TimeUnit.SECONDS);
99
100 out.close();
101 in.close();
102 socket.close();
103
104 assertTrue(success);
105 }
106
107 private static String inputMessage(String resource) {
108 return new Scanner(TransportTest2.class.getResourceAsStream("/" + resource)).useDelimiter("\\A").next();
109 }
110
111 }