Author: davsclaus Date: Tue Jun 28 14:24:46 2011 New Revision: 1140636 URL: http://svn.apache.org/viewvc?rev=1140636&view=rev Log: CAMEL-4145: pollEnrich should set empty message body if could not poll from resource endpoint. CAMEL-4158: The Exchange.TO_ENDPOINT is decored in enrich and pollEnrich with the uri of the resource endpoint.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherNoResourceTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherNoResourceTest.java - copied, changed from r1140565, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Enricher.java camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Enricher.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Enricher.java?rev=1140636&r1=1140635&r2=1140636&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Enricher.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Enricher.java Tue Jun 28 14:24:46 2011 @@ -132,6 +132,13 @@ public class Enricher extends ServiceSup } } + // set header with the uri of the endpoint enriched so we can use that for tracing etc + if (exchange.hasOut()) { + exchange.getOut().setHeader(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri()); + } else { + exchange.getIn().setHeader(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri()); + } + callback.done(false); } }); @@ -160,6 +167,13 @@ public class Enricher extends ServiceSup } } + // set header with the uri of the endpoint enriched so we can use that for tracing etc + if (exchange.hasOut()) { + exchange.getOut().setHeader(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri()); + } else { + exchange.getIn().setHeader(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri()); + } + callback.done(true); return true; } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java?rev=1140636&r1=1140635&r2=1140636&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java Tue Jun 28 14:24:46 2011 @@ -150,6 +150,13 @@ public class PollEnricher extends Servic } } } + + // set header with the uri of the endpoint enriched so we can use that for tracing etc + if (exchange.hasOut()) { + exchange.getOut().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri()); + } else { + exchange.getIn().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri()); + } } /** @@ -192,6 +199,12 @@ public class PollEnricher extends Servic public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { if (newExchange != null) { copyResultsPreservePattern(oldExchange, newExchange); + } else { + // if no newExchange then there was no message from the external resource + // and therefore we should set an empty body to indicate this fact + // but keep headers/attachments as we want to propagate those + oldExchange.getIn().setBody(null); + oldExchange.setOut(null); } return oldExchange; } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherNoResourceTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherNoResourceTest.java?rev=1140636&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherNoResourceTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherNoResourceTest.java Tue Jun 28 14:24:46 2011 @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.processor.enricher; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +public class EnricherNoResourceTest extends ContextTestSupport { + + public void testNoResourceA() throws Exception { + // there should be no message body + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").message(0).body().isNull(); + + template.sendBody("direct:a", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:a") + .enrich("direct:foo") + .to("mock:result"); + + // set an empty message + from("direct:foo") + .setBody(constant(null)); + } + }; + } + +} \ No newline at end of file Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherTest.java?rev=1140636&r1=1140635&r2=1140636&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherTest.java Tue Jun 28 14:24:46 2011 @@ -46,6 +46,7 @@ public class EnricherTest extends Contex public void testEnrichInOnly() throws InterruptedException { mock.expectedBodiesReceived("test:blah"); + mock.expectedHeaderReceived(Exchange.TO_ENDPOINT, "direct://enricher-constant-resource"); template.sendBody("direct:enricher-test-1", "test"); mock.assertIsSatisfied(); } @@ -61,6 +62,7 @@ public class EnricherTest extends Contex assertEquals("test", exchange.getIn().getBody()); assertTrue(exchange.getOut() != null && exchange.getOut().isFault()); assertEquals("failed", exchange.getOut().getBody()); + assertEquals("direct://enricher-fault-resource", exchange.getOut().getHeader(Exchange.TO_ENDPOINT)); assertNull(exchange.getException()); } Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherNoResourceTest.java (from r1140565, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherNoResourceTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherNoResourceTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java&r1=1140565&r2=1140636&rev=1140636&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherNoResourceTest.java Tue Jun 28 14:24:46 2011 @@ -18,134 +18,70 @@ package org.apache.camel.processor.enric import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; -import org.apache.camel.ExchangePattern; -import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -public class PollEnricherTest extends ContextTestSupport { +public class PollEnricherNoResourceTest extends ContextTestSupport { - private static SampleAggregator aggregationStrategy = new SampleAggregator(); + public void testNoResourceA() throws Exception { + // there should be no message body + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").message(0).body().isNull(); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://foo"); - protected MockEndpoint mock; + template.sendBody("direct:a", "Hello World"); - @Override - protected void setUp() throws Exception { - super.setUp(); - mock = getMockEndpoint("mock:mock"); + assertMockEndpointsSatisfied(); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } + public void testNoResourceB() throws Exception { + // there should be no message body + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").message(0).body().isNull(); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://bar"); - // ------------------------------------------------------------- - // InOnly routes - // ------------------------------------------------------------- + template.sendBody("direct:b", "Hello World"); - public void testPollEnrichInOnly() throws InterruptedException { - template.sendBody("seda:foo1", "blah"); + assertMockEndpointsSatisfied(); + } - Thread.sleep(250); + public void testResourceA() throws Exception { + template.sendBody("seda:foo", "Bye World"); - mock.expectedBodiesReceived("test:blah"); - template.sendBody("direct:enricher-test-1", "test"); - mock.assertIsSatisfied(); - } + Thread.sleep(250); - public void testPollEnrichInOnlyWaitWithTimeout() throws InterruptedException { - // this first try there is no data so we timeout - mock.expectedBodiesReceived("test:blah"); - template.sendBody("direct:enricher-test-2", "test"); - // not expected data so we are not happy - mock.assertIsNotSatisfied(); - - // now send it and try again - mock.reset(); - template.sendBody("seda:foo2", "blah"); - template.sendBody("direct:enricher-test-2", "test"); - mock.assertIsSatisfied(); - } + // there should be a message body + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://foo"); - public void testPollEnrichInOnlyWaitNoTimeout() throws InterruptedException { - // use another thread to send it a bit later - Thread t = new Thread(new Runnable() { - public void run() { - try { - Thread.sleep(250); - } catch (InterruptedException e) { - // ignore - } - template.sendBody("seda:foo3", "blah"); - } - }); + template.sendBody("direct:a", "Hello World"); - long start = System.currentTimeMillis(); - mock.expectedBodiesReceived("test:blah"); - t.start(); - template.sendBody("direct:enricher-test-3", "test"); - // should take approx 1 sec to complete as the other thread is sending a bit later and we wait - mock.assertIsSatisfied(); - long delta = System.currentTimeMillis() - start; - assertTrue("Should take approx 0.25 sec: was " + delta, delta > 150); + assertMockEndpointsSatisfied(); } - // ------------------------------------------------------------- - // InOut routes - // ------------------------------------------------------------- - - public void testPollEnrichInOut() throws InterruptedException { - template.sendBody("seda:foo4", "blah"); + public void testResourceB() throws Exception { + template.sendBody("seda:bar", "Bye World"); Thread.sleep(250); - String result = (String) template.sendBody("direct:enricher-test-4", ExchangePattern.InOut, "test"); - assertEquals("test:blah", result); - } - - public void testPollEnrichInOutPlusHeader() throws InterruptedException { - template.sendBody("seda:foo4", "blah"); + // there should be a message body + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://bar"); - Thread.sleep(250); + template.sendBody("direct:b", "Hello World"); - Exchange exchange = template.request("direct:enricher-test-4", new Processor() { - public void process(Exchange exchange) { - exchange.getIn().setHeader("foo", "bar"); - exchange.getIn().setBody("test"); - } - }); - assertEquals("bar", exchange.getIn().getHeader("foo")); - assertEquals("test:blah", exchange.getIn().getBody()); - assertFalse(exchange.hasOut() && exchange.getOut().isFault()); - assertNull(exchange.getException()); + assertMockEndpointsSatisfied(); } protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { - // ------------------------------------------------------------- - // InOnly routes - // ------------------------------------------------------------- - - from("direct:enricher-test-1") - .pollEnrich("seda:foo1", aggregationStrategy) - .to("mock:mock"); - - from("direct:enricher-test-2") - .pollEnrich("seda:foo2", 1000, aggregationStrategy) - .to("mock:mock"); - - from("direct:enricher-test-3") - .pollEnrich("seda:foo3", -1, aggregationStrategy) - .to("mock:mock"); - - // ------------------------------------------------------------- - // InOut routes - // ------------------------------------------------------------- - - from("direct:enricher-test-4") - .pollEnrich("seda:foo4", aggregationStrategy); + from("direct:a") + .pollEnrich("seda:foo", 1000) + .to("mock:result"); + + from("direct:b") + .pollEnrich("seda:bar") + .to("mock:result"); } }; } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java?rev=1140636&r1=1140635&r2=1140636&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnricherTest.java Tue Jun 28 14:24:46 2011 @@ -50,6 +50,7 @@ public class PollEnricherTest extends Co Thread.sleep(250); mock.expectedBodiesReceived("test:blah"); + mock.expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://foo1"); template.sendBody("direct:enricher-test-1", "test"); mock.assertIsSatisfied(); } @@ -57,6 +58,7 @@ public class PollEnricherTest extends Co public void testPollEnrichInOnlyWaitWithTimeout() throws InterruptedException { // this first try there is no data so we timeout mock.expectedBodiesReceived("test:blah"); + mock.expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://foo2"); template.sendBody("direct:enricher-test-2", "test"); // not expected data so we are not happy mock.assertIsNotSatisfied(); @@ -83,6 +85,7 @@ public class PollEnricherTest extends Co long start = System.currentTimeMillis(); mock.expectedBodiesReceived("test:blah"); + mock.expectedHeaderReceived(Exchange.TO_ENDPOINT, "seda://foo3"); t.start(); template.sendBody("direct:enricher-test-3", "test"); // should take approx 1 sec to complete as the other thread is sending a bit later and we wait @@ -118,6 +121,7 @@ public class PollEnricherTest extends Co assertEquals("bar", exchange.getIn().getHeader("foo")); assertEquals("test:blah", exchange.getIn().getBody()); assertFalse(exchange.hasOut() && exchange.getOut().isFault()); + assertEquals("seda://foo4", exchange.getOut().getHeader(Exchange.TO_ENDPOINT)); assertNull(exchange.getException()); }