001package ca.uhn.hl7v2.hoh.encoder; 002 003import static org.junit.Assert.*; 004 005import java.io.IOException; 006import java.io.InputStream; 007import java.io.UnsupportedEncodingException; 008import java.nio.charset.Charset; 009import java.security.KeyStore; 010import java.security.KeyStoreException; 011import java.security.NoSuchAlgorithmException; 012import java.security.cert.CertificateException; 013import java.util.Map.Entry; 014 015import org.junit.BeforeClass; 016import org.junit.Test; 017 018import ca.uhn.hl7v2.hoh.api.EncodeException; 019import ca.uhn.hl7v2.hoh.sign.BouncyCastleCmsMessageSigner; 020 021public class Hl7OverHttpEncoderTest { 022 023 private static BouncyCastleCmsMessageSigner mySigner; 024 025 // TODO: add a test to make sure that the date header matches the message timestamp 026 027 @Test 028 public void testContentLengthCalculatedCorrectly() throws EncodeException, UnsupportedEncodingException { 029 030 // Contains a multi-byte sequence 031 String message = "MSH|^~\\&|||||200803051508||ACK^A31|33|P|2.5\r" 032 + "MSA|AR|Iā„HAPI\r"; 033 034 Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); 035 enc.setCharset(Charset.forName("UTF-8")); 036 enc.setMessage(message); 037 enc.encode(); 038 039 assertEquals("" + message.getBytes("UTF-8").length, enc.getHeaders().get("Content-Length")); 040 } 041 042 @Test 043 public void testContentLengthCalculation() throws EncodeException { 044 045 String message = "MSH|^~\\&|||||200803051508||ACK^A31|33|P|2.5\r" 046 + "MSA|AR|2\r"; 047 048 Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); 049 enc.setCharset(Charset.forName("ISO-8859-1")); 050 enc.setUsername("hello"); 051 enc.setPassword("world"); 052 enc.setSigner(mySigner); 053 enc.setMessage(message); 054 enc.encode(); 055 056 for (Entry<String, String> next : enc.getHeaders().entrySet()) { 057 System.out.println(next.getKey() + ": " + next.getValue()); 058 } 059 060 } 061 062 063 @Test 064 public void testEncode() throws EncodeException { 065 066 String message = "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" 067 + "EVN||200803051509\r" 068 + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; 069 070 Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); 071 enc.setCharset(Charset.forName("ISO-8859-1")); 072 enc.setUsername("hello"); 073 enc.setPassword("world"); 074 enc.setSigner(mySigner); 075 enc.setMessage(message); 076 enc.encode(); 077 078 for (Entry<String, String> next : enc.getHeaders().entrySet()) { 079 System.out.println(next.getKey() + ": " + next.getValue()); 080 } 081 082 } 083 084 @BeforeClass 085 public static void beforeClass() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { 086 KeyStore keyStore = KeyStore.getInstance("JKS"); 087 InputStream ksStream = BouncyCastleCmsMessageSigner.class.getResourceAsStream("/keystore.jks"); 088 keyStore.load(ksStream, "changeit".toCharArray()); 089 090 mySigner = new BouncyCastleCmsMessageSigner(); 091 mySigner.setKeyStore(keyStore); 092 mySigner.setKeyAlias("testcert"); 093 mySigner.setAliasPassword("changeit"); 094 } 095 096}