View Javadoc
1   /*
2    * Copyright 2017 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Martin Krasser
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(); // read second end byte
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              // Some systems send end bytes in a separate frame
87              Thread.sleep(10);
88              out.write(msg.getBytes());
89              out.flush();
90              // Some systems send end bytes in a separate frame
91              // Thread.sleep(10);
92              out.write(28);
93              out.write(13);
94              out.flush();
95              // Thread.sleep(10);
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 }