CamelTransformer extends BaseTransformer <Message, Message> { @Override public Message transform(Message message) { try { Exchange exchange = _endpoint.createExchange(); UnitOfWork uow = exchange.getContext().getUnitOfWorkFactory().createUnitOfWork(exchange); RouteContext rc = new DefaultRouteContext(exchange.getContext()); uow.pushRouteContext(rc); exchange.setUnitOfWork(uow); uow.start(); exchange.getIn().setBody(message.getContent()); copyProperties(message.getContext(), exchange); Producer producer = _endpoint.createProducer(); producer.process(exchange); if (exchange.isFailed()) { if (exchange.getException() != null) { throw TransformMessages.MESSAGES.failedToTransformViaCamelEndpoint(_endpoint.getEndpointUri(), exchange.getException()); } else { throw TransformMessages.MESSAGES.failedToTransformViaCamelEndpoint(_endpoint.getEndpointUri(), exchange.getIn().getBody(String.class)); } } if (QNameUtil.isJavaMessageType(getTo())) { message.setContent(exchange.getIn().getBody(QNameUtil.toJavaMessageType(getTo()))); } else { message.setContent(exchange.getIn().getBody()); } return message; } catch (Exception e) { throw TransformMessages.MESSAGES.failedToTransformViaCamelEndpoint(_endpoint.getEndpointUri(), e); } } protected  CamelTransformer(final QName from, final QName to, Endpoint endpoint); @Override Message transform(Message message);  }
@Test public void testEndpoint() throws Exception { SwitchYardCamelContextImpl camelContext = new SwitchYardCamelContextImpl(); ServiceDomain domain = new MockDomain(); domain.setProperty(CamelContextConfigurator.CAMEL_CONTEXT_CONFIG_XML, PATH_CAMEL_CONTEXT_XML); camelContext.setServiceDomain(domain); camelContext.start(); Transformer<?,?> transformer = getTransformer("sw-config.xml", domain); Assert.assertEquals(CamelTransformer.class, transformer.getClass()); CamelTransformer camel = CamelTransformer.class.cast(transformer); Message message = new DefaultMessage(); Message output = camel.transform(message.setContent("input")); camelContext.stop(); Assert.assertEquals("input-transformed", output.getContent(String.class)); }
@Test public void testNoEndpoint() throws Exception { SwitchYardCamelContextImpl camelContext = new SwitchYardCamelContextImpl(); ServiceDomain domain = new MockDomain(); domain.setProperty(CamelContextConfigurator.CAMEL_CONTEXT_CONFIG_XML, PATH_CAMEL_CONTEXT_XML); camelContext.setServiceDomain(domain); camelContext.start(); Transformer<?,?> transformer = getTransformer("sw-config-no-endpoint.xml", domain); Assert.assertEquals(CamelTransformer.class, transformer.getClass()); CamelTransformer camel = CamelTransformer.class.cast(transformer); try { Message message = new DefaultMessage(); camel.transform(message.setContent("input")); } catch (SwitchYardException e) { _logger.info("Caught expected Exception", e); return; } finally { camelContext.stop(); } Assert.fail("Camel transform succeeded without target endpoint"); }