CamelMessageConverter implements MessageConverter<Exchange, Exchange, CamelEndpointConfiguration> { @Override public Message convertInbound(Exchange exchange, CamelEndpointConfiguration endpointConfiguration, TestContext context) { if (exchange == null) { return null; } org.apache.camel.Message camelMessage; if (exchange.hasOut()) { camelMessage = exchange.getOut(); } else { camelMessage = exchange.getIn(); } Message message = new DefaultMessage(camelMessage.getBody(), camelMessage.getHeaders()) .setHeader(CitrusCamelMessageHeaders.EXCHANGE_ID, exchange.getExchangeId()) .setHeader(CitrusCamelMessageHeaders.ROUTE_ID, exchange.getFromRouteId()) .setHeader(CitrusCamelMessageHeaders.EXCHANGE_PATTERN, exchange.getPattern().name()) .setHeader(CitrusCamelMessageHeaders.EXCHANGE_FAILED, exchange.isFailed()); for (Map.Entry<String, Object> property : exchange.getProperties().entrySet()) { message.setHeader(property.getKey(), property.getValue()); } if (exchange.getException() != null) { message.setHeader(CitrusCamelMessageHeaders.EXCHANGE_EXCEPTION, exchange.getException().getClass().getName()); message.setHeader(CitrusCamelMessageHeaders.EXCHANGE_EXCEPTION_MESSAGE, exchange.getException().getMessage()); } return message; }  @Override Exchange convertOutbound(Message message, CamelEndpointConfiguration endpointConfiguration, TestContext context); @Override void convertOutbound(Exchange exchange, Message message, CamelEndpointConfiguration endpointConfiguration, TestContext context); @Override Message convertInbound(Exchange exchange, CamelEndpointConfiguration endpointConfiguration, TestContext context);  }
@Test public void testConvertInbound() { DefaultExchange exchange = new DefaultExchange(camelContext); exchange.setExchangeId(UUID.randomUUID().toString()); exchange.setFromRouteId("helloRoute"); exchange.getIn().setBody("Hello from Citrus!"); exchange.getIn().setHeader("operation", "sayHello"); Message result = messageConverter.convertInbound(exchange, endpointConfiguration, context); Assert.assertEquals(result.getPayload(), "Hello from Citrus!"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_ID), exchange.getExchangeId()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_PATTERN), ExchangePattern.InOnly.name()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.ROUTE_ID), "helloRoute"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_FAILED), false); Assert.assertEquals(result.getHeader("operation"), "sayHello"); }
@Test public void testConvertInboundWithProperties() { DefaultExchange exchange = new DefaultExchange(camelContext); exchange.setExchangeId(UUID.randomUUID().toString()); exchange.setFromRouteId("helloRoute"); exchange.getIn().setBody("Hello from Citrus!"); exchange.getIn().setHeader("operation", "sayHello"); exchange.setProperty("SpecialProperty", "foo"); exchange.setProperty("VerySpecialProperty", "bar"); Message result = messageConverter.convertInbound(exchange, endpointConfiguration, context); Assert.assertEquals(result.getPayload(), "Hello from Citrus!"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_ID), exchange.getExchangeId()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_PATTERN), ExchangePattern.InOnly.name()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.ROUTE_ID), "helloRoute"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_FAILED), false); Assert.assertEquals(result.getHeader("operation"), "sayHello"); Assert.assertEquals(result.getHeader("SpecialProperty"), "foo"); Assert.assertEquals(result.getHeader("VerySpecialProperty"), "bar"); }
@Test public void testConvertInboundWithException() { DefaultExchange exchange = new DefaultExchange(camelContext); exchange.setExchangeId(UUID.randomUUID().toString()); exchange.setFromRouteId("helloRoute"); exchange.getIn().setBody("Hello from Citrus!"); exchange.getIn().setHeader("operation", "sayHello"); exchange.setException(new CitrusRuntimeException("Something went wrong")); Message result = messageConverter.convertInbound(exchange, endpointConfiguration, context); Assert.assertEquals(result.getPayload(), "Hello from Citrus!"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_ID), exchange.getExchangeId()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_PATTERN), ExchangePattern.InOnly.name()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.ROUTE_ID), "helloRoute"); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_FAILED), true); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_EXCEPTION), CitrusRuntimeException.class.getName()); Assert.assertEquals(result.getHeader(CitrusCamelMessageHeaders.EXCHANGE_EXCEPTION_MESSAGE), "Something went wrong"); Assert.assertEquals(result.getHeader("operation"), "sayHello"); }