View Javadoc

1   package ca.uhn.hl7v2.hoh.encoder;
2   
3   import static org.junit.Assert.*;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.UnsupportedEncodingException;
8   import java.nio.charset.Charset;
9   import java.security.KeyStore;
10  import java.security.KeyStoreException;
11  import java.security.NoSuchAlgorithmException;
12  import java.security.cert.CertificateException;
13  import java.util.Map.Entry;
14  
15  import org.junit.BeforeClass;
16  import org.junit.Test;
17  
18  import ca.uhn.hl7v2.hoh.api.EncodeException;
19  import ca.uhn.hl7v2.hoh.sign.BouncyCastleCmsMessageSigner;
20  
21  public class Hl7OverHttpEncoderTest {
22  
23  	private static BouncyCastleCmsMessageSigner mySigner;
24  	
25  	// TODO: add a test to make sure that the date header matches the message timestamp
26  	
27  	@Test
28  	public void testContentLengthCalculatedCorrectly() throws EncodeException, UnsupportedEncodingException {
29  		
30  		// Contains a multi-byte sequence
31  		String message = "MSH|^~\\&|||||200803051508||ACK^A31|33|P|2.5\r"
32  				+ "MSA|AR|Iā™„HAPI\r";
33  
34  		Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder();
35  		enc.setCharset(Charset.forName("UTF-8"));
36  		enc.setMessage(message);
37  		enc.encode();
38  		
39  		assertEquals("" + message.getBytes("UTF-8").length, enc.getHeaders().get("Content-Length"));
40  	}
41  
42  	@Test
43  	public void testContentLengthCalculation() throws EncodeException {
44  		
45  		String message = "MSH|^~\\&|||||200803051508||ACK^A31|33|P|2.5\r"
46  				+ "MSA|AR|2\r";
47  
48  		Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder();
49  		enc.setCharset(Charset.forName("ISO-8859-1"));
50  		enc.setUsername("hello");
51  		enc.setPassword("world");
52  		enc.setSigner(mySigner);
53  		enc.setMessage(message);
54  		enc.encode();
55  		
56  		for (Entry<String, String> next : enc.getHeaders().entrySet()) {
57  			System.out.println(next.getKey() + ": " + next.getValue());
58  		}
59  
60  	}
61  
62  	
63  	@Test
64  	public void testEncode() throws EncodeException {
65  		
66  		String message = "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r"
67  				+ "EVN||200803051509\r"
68  				+ "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
69  
70  		Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder();
71  		enc.setCharset(Charset.forName("ISO-8859-1"));
72  		enc.setUsername("hello");
73  		enc.setPassword("world");
74  		enc.setSigner(mySigner);
75  		enc.setMessage(message);
76  		enc.encode();
77  		
78  		for (Entry<String, String> next : enc.getHeaders().entrySet()) {
79  			System.out.println(next.getKey() + ": " + next.getValue());
80  		}
81  
82  	}
83  
84  	@BeforeClass
85  	public static void beforeClass() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
86  		KeyStore keyStore = KeyStore.getInstance("JKS");
87  		InputStream ksStream = BouncyCastleCmsMessageSigner.class.getResourceAsStream("/keystore.jks");
88  		keyStore.load(ksStream, "changeit".toCharArray());
89  
90  		mySigner = new BouncyCastleCmsMessageSigner();
91  		mySigner.setKeyStore(keyStore);
92  		mySigner.setKeyAlias("testcert");
93  		mySigner.setAliasPassword("changeit");
94  	}
95  	
96  }