@Override
    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        byte[] body = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, inputStream);
        String charsetName = HL7Charset.getCharsetName(body, guessCharsetName(body, exchange));
        String bodyAsString = new String(body, charsetName);
        Message message = parser.parse(bodyAsString);

        // add MSH fields as message out headers
        Terser terser = new Terser(message);
        for (Map.Entry<String, String> entry : HEADER_MAP.entrySet()) {
            exchange.getOut().setHeader(entry.getKey(), terser.get(entry.getValue()));
        }
        exchange.getOut().setHeader(HL7_CONTEXT, hapiContext);
        exchange.getOut().setHeader(Exchange.CHARSET_NAME, charsetName);
        return message;
    }
@Test
    public void testUnmarshal() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:unmarshal");
        mock.expectedMessageCount(1);
        mock.message(0).body().isInstanceOf(Message.class);

        mock.expectedHeaderReceived(HL7Constants.HL7_SENDING_APPLICATION, "MYSENDER");
        mock.expectedHeaderReceived(HL7Constants.HL7_SENDING_FACILITY, "MYSENDERAPP");
        mock.expectedHeaderReceived(HL7Constants.HL7_RECEIVING_APPLICATION, "MYCLIENT");
        mock.expectedHeaderReceived(HL7Constants.HL7_RECEIVING_FACILITY, "MYCLIENTAPP");
        mock.expectedHeaderReceived(HL7Constants.HL7_TIMESTAMP, "200612211200");
        mock.expectedHeaderReceived(HL7Constants.HL7_SECURITY, null);
        mock.expectedHeaderReceived(HL7Constants.HL7_MESSAGE_TYPE, "QRY");
        mock.expectedHeaderReceived(HL7Constants.HL7_TRIGGER_EVENT, "A19");
        mock.expectedHeaderReceived(HL7Constants.HL7_MESSAGE_CONTROL, "1234");
        mock.expectedHeaderReceived(HL7Constants.HL7_PROCESSING_ID, "P");
        mock.expectedHeaderReceived(HL7Constants.HL7_VERSION_ID, "2.4");
        mock.expectedHeaderReceived(HL7Constants.HL7_CONTEXT, hl7.getHapiContext());
        mock.expectedHeaderReceived(HL7Constants.HL7_CHARSET, null);

        mock.expectedHeaderReceived(Exchange.CHARSET_NAME, "UTF-8");

        String body = createHL7AsString();
        template.sendBody("direct:unmarshal", body);

        MockEndpoint.assertIsSatisfied(context);

        Message msg = mock.getExchanges().get(0).getIn().getBody(Message.class);
        assertEquals("2.4", msg.getVersion());
        QRD qrd = (QRD) msg.get("QRD");
        assertEquals("0101701234", qrd.getWhoSubjectFilter(0).getIDNumber().getValue());
    }