Repository: camel Updated Branches: refs/heads/master 2d80be3bf -> 13bd2a7f1
CAMEL-8723 : Add desired message type to ProducerTemplate.sendBody methods Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/13bd2a7f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/13bd2a7f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/13bd2a7f Branch: refs/heads/master Commit: 13bd2a7f122000cf00a2d61a00654fe1f8020fdd Parents: 2d80be3 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue May 3 15:25:01 2016 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue May 3 15:25:24 2016 +0200 ---------------------------------------------------------------------- .../camel/builder/FluentProducerTemplate.java | 37 +++++++----- .../builder/FluentProducerTemplateTest.java | 61 ++++++++++++++------ 2 files changed, 65 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/13bd2a7f/camel-core/src/main/java/org/apache/camel/builder/FluentProducerTemplate.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/FluentProducerTemplate.java b/camel-core/src/main/java/org/apache/camel/builder/FluentProducerTemplate.java index bb8ae98..98bec58 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/FluentProducerTemplate.java +++ b/camel-core/src/main/java/org/apache/camel/builder/FluentProducerTemplate.java @@ -59,7 +59,7 @@ public class FluentProducerTemplate { * * @param key the key of the header * @param value the value of the header - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate withHeader(String key, Object value) { if (headers == null) { @@ -74,7 +74,7 @@ public class FluentProducerTemplate { /** * Remove the headers. * - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate clearHeaders() { if (headers != null) { @@ -88,7 +88,7 @@ public class FluentProducerTemplate { * Set the message body * * @param body the body - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate withBody(Object body) { this.body = body; @@ -97,9 +97,24 @@ public class FluentProducerTemplate { } /** + * Set the message body after converting it to the given type + * + * @param body the body + * @param type the type which the body should be converted to + * @return this FluentProducerTemplate instance + */ + public FluentProducerTemplate withBodyAs(Object body, Class<?> type) { + this.body = type != null + ? context.getTypeConverter().convertTo(type, body) + : body; + + return this; + } + + /** * Remove the body. * - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate clearBody() { this.body = null; @@ -197,7 +212,7 @@ public class FluentProducerTemplate { * Set the message body * * @param endpointUri the endpoint URI to send to - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate to(String endpointUri) { return to(context.getEndpoint(endpointUri)); @@ -207,7 +222,7 @@ public class FluentProducerTemplate { * Set the message body * * @param endpoint the endpoint to send to - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public FluentProducerTemplate to(Endpoint endpoint) { this.endpoint = endpoint; @@ -290,15 +305,9 @@ public class FluentProducerTemplate { * @throws CamelExecutionException */ public Exchange send() throws CamelExecutionException { - Exchange result = exchangeSupplier != null + return exchangeSupplier != null ? template().send(endpoint, exchangeSupplier.get()) : template().send(endpoint, processorSupplier.get()); - - // TODO: validate - // must invoke extract result body in case of exception to be rethrown - //ExchangeHelper.extractResultBody(result, null); - - return result; } /** @@ -320,7 +329,7 @@ public class FluentProducerTemplate { * Create the FluentProducerTemplate by setting the camel context * * @param context the camel context - * @return ProducerTemplate builder + * @return this FluentProducerTemplate instance */ public static FluentProducerTemplate on(CamelContext context) { return new FluentProducerTemplate(context); http://git-wip-us.apache.org/repos/asf/camel/blob/13bd2a7f/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java b/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java index c95a8ad..29d8dc0 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java @@ -16,6 +16,7 @@ */ package org.apache.camel.builder; +import org.apache.camel.CamelExecutionException; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; @@ -57,6 +58,37 @@ public class FluentProducerTemplateTest extends ContextTestSupport { assertEquals("Bye Bye World", result); } + public void testInOutWithBodyConversion() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived(11); + + Object result = FluentProducerTemplate.on(context) + .withBodyAs("10", Integer.class) + .to("direct:sum") + .request(); + + assertMockEndpointsSatisfied(); + + assertEquals(11, result); + } + + public void testInOutWithBodyConversionFault() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(0); + + try { + FluentProducerTemplate.on(context) + .withBodyAs("10", Double.class) + .to("direct:sum") + .request(); + } catch (CamelExecutionException e) { + assertTrue(e.getCause() instanceof IllegalArgumentException); + assertEquals("Expected body of type Integer", e.getCause().getMessage()); + } + + assertMockEndpointsSatisfied(); + } + public void testFault() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(0); @@ -71,7 +103,6 @@ public class FluentProducerTemplateTest extends ContextTestSupport { assertEquals("Faulty World", result); } - // TODO: to review public void testExceptionUsingBody() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(0); @@ -85,25 +116,9 @@ public class FluentProducerTemplateTest extends ContextTestSupport { assertTrue(out.getException() instanceof IllegalArgumentException); assertEquals("Forced exception by unit test", out.getException().getMessage()); - /* - try { - Exchange out = FluentProducerTemplate.on(context) - .withBody("Hello World") - .to("direct:exception") - .send(); - - assertTrue(out.isFailed()); - fail("Should have thrown RuntimeCamelException"); - } catch (RuntimeCamelException e) { - assertTrue(e.getCause() instanceof IllegalArgumentException); - assertEquals("Forced exception by unit test", e.getCause().getMessage()); - } - */ - assertMockEndpointsSatisfied(); } - // TODO: to review public void testExceptionUsingProcessor() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(0); @@ -264,11 +279,19 @@ public class FluentProducerTemplateTest extends ContextTestSupport { from("direct:in") .process(exchange -> exchange.getIn().setBody("Bye World")) .to("mock:result"); - + from("direct:sum") + .process(exchange -> { + Object body = exchange.getIn().getBody(); + if (body instanceof Integer) { + exchange.getIn().setBody((Integer) body + 1); + } else { + throw new IllegalArgumentException("Expected body of type Integer"); + } + }) + .to("mock:result"); from("direct:out") .process(exchange -> exchange.getOut().setBody("Bye Bye World")) .to("mock:result"); - from("direct:fault") .process(exchange -> { exchange.getOut().setFault(true);