use of org.apache.camel.example.transformer.demo.OrderResponse in project camel by apache.
the class CamelClient method main.
public static void main(final String[] args) throws Exception {
File csvLogFile = new File(CSV_PATH);
if (csvLogFile.exists()) {
LOG.info("---> Removing log file '{}'...", csvLogFile.getAbsolutePath());
csvLogFile.delete();
}
// START SNIPPET: e1
LOG.info("---> Starting 'order' camel route...");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
context.start();
CamelContext camelContext = context.getBean("order", CamelContext.class);
ProducerTemplate producer = camelContext.createProducerTemplate();
// END SNIPPET: e1
Thread.sleep(1000);
Order order = new Order().setOrderId("Order-Java-0001").setItemId("MILK").setQuantity(3);
LOG.info("---> Sending '{}' to 'direct:java'", order);
OrderResponse response = producer.requestBody("direct:java", order, OrderResponse.class);
Thread.sleep(1000);
LOG.info("---> Received '{}'", response);
LOG.info("---> CSV log now contains:{}", getCsvLog());
Thread.sleep(1000);
String orderXml = "<order orderId=\"Order-XML-0001\" itemId=\"MIKAN\" quantity=\"365\"/>";
LOG.info("---> Sending '{}' to 'direct:xml'", orderXml);
Exchange answerXml = producer.send("direct:xml", ex -> {
((DataTypeAware) ex.getIn()).setBody(orderXml, new DataType("xml:XMLOrder"));
});
Thread.sleep(1000);
LOG.info("---> Received '{}'", answerXml.getOut().getBody(String.class));
LOG.info("---> CSV log now contains:{}", getCsvLog());
Thread.sleep(1000);
String orderJson = "{\"orderId\":\"Order-JSON-0001\", \"itemId\":\"MIZUYO-KAN\", \"quantity\":\"16350\"}";
LOG.info("---> Sending '{}' to 'direct:json'", orderJson);
Exchange answerJson = producer.send("direct:json", ex -> {
((DataTypeAware) ex.getIn()).setBody(orderJson, new DataType("json"));
});
Thread.sleep(1000);
LOG.info("---> Received '{}'", answerJson.getOut().getBody(String.class));
LOG.info("---> CSV log now contains:{}", getCsvLog());
Thread.sleep(1000);
context.stop();
}
use of org.apache.camel.example.transformer.demo.OrderResponse in project camel by apache.
the class OrderRouteSpringTest method testJava.
@Test
public void testJava() throws Exception {
mockCsv.whenAnyExchangeReceived(new Processor() {
public void process(Exchange exchange) {
Object mockBody = exchange.getIn().getBody();
assertEquals(Order.class, mockBody.getClass());
Order mockOrder = (Order) mockBody;
assertEquals("Order-Java-0001", mockOrder.getOrderId());
assertEquals("MILK", mockOrder.getItemId());
assertEquals(3, mockOrder.getQuantity());
}
});
mockCsv.setExpectedMessageCount(1);
Order order = new Order().setOrderId("Order-Java-0001").setItemId("MILK").setQuantity(3);
Object answer = javaProducer.requestBody(order);
assertEquals(OrderResponse.class, answer.getClass());
OrderResponse or = (OrderResponse) answer;
assertEquals("Order-Java-0001", or.getOrderId());
assertEquals(true, or.isAccepted());
assertEquals("Order accepted:[item='MILK' quantity='3']", or.getDescription());
mockCsv.assertIsSatisfied();
}
use of org.apache.camel.example.transformer.demo.OrderResponse in project camel by apache.
the class OrderRouteSpringTest method testJSON.
@Test
public void testJSON() throws Exception {
mockCsv.whenAnyExchangeReceived(new Processor() {
public void process(Exchange exchange) {
Object mockBody = exchange.getIn().getBody();
assertEquals(Order.class, mockBody.getClass());
Order mockOrder = (Order) mockBody;
assertEquals("Order-JSON-0001", mockOrder.getOrderId());
assertEquals("MIZUYO-KAN", mockOrder.getItemId());
assertEquals(16350, mockOrder.getQuantity());
}
});
mockCsv.setExpectedMessageCount(1);
String order = "{\"orderId\":\"Order-JSON-0001\", \"itemId\":\"MIZUYO-KAN\", \"quantity\":\"16350\"}";
OrderResponse expected = new OrderResponse().setAccepted(true).setOrderId("Order-JSON-0001").setDescription("Order accepted:[item='MIZUYO-KAN' quantity='16350']");
ObjectMapper jsonMapper = new ObjectMapper();
String expectedJson = jsonMapper.writeValueAsString(expected);
Exchange answer = jsonProducer.send("direct:json", ex -> {
((DataTypeAware) ex.getIn()).setBody(order, new DataType("json"));
});
assertEquals(expectedJson, answer.getOut().getBody(String.class));
mockCsv.assertIsSatisfied();
}
Aggregations