2012-01-19 14:38:44 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-05-23 11:37:56 -04:00
|
|
|
package akka.camel
|
|
|
|
|
|
|
|
|
|
import org.apache.camel.{ Exchange, Processor }
|
|
|
|
|
import org.apache.camel.builder.RouteBuilder
|
|
|
|
|
import org.apache.camel.component.mock.MockEndpoint
|
2012-05-11 09:46:49 +02:00
|
|
|
import akka.dispatch.Await
|
2012-05-07 14:18:06 +02:00
|
|
|
import akka.camel.TestSupport.SharedCamelSystem
|
|
|
|
|
import akka.actor.SupervisorStrategy.Stop
|
|
|
|
|
import org.scalatest.{ GivenWhenThen, BeforeAndAfterEach, BeforeAndAfterAll, WordSpec }
|
2012-01-19 14:38:44 +00:00
|
|
|
import akka.actor._
|
|
|
|
|
import akka.pattern._
|
|
|
|
|
import akka.util.duration._
|
2012-05-07 14:18:06 +02:00
|
|
|
import akka.util.Timeout
|
|
|
|
|
import akka.testkit.TestLatch
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
/**
|
|
|
|
|
* Tests the features of the Camel Producer.
|
|
|
|
|
*/
|
2012-05-07 14:18:06 +02:00
|
|
|
class ProducerFeatureTest extends WordSpec with BeforeAndAfterAll with BeforeAndAfterEach with SharedCamelSystem with GivenWhenThen with MustMatchers {
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
import ProducerFeatureTest._
|
|
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
val camelContext = camel.context
|
|
|
|
|
// to make testing equality of messages easier, otherwise the breadcrumb shows up in the result.
|
|
|
|
|
camelContext.setUseBreadcrumb(false)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-05-07 14:18:06 +02:00
|
|
|
val timeoutDuration = 1 second
|
|
|
|
|
implicit val timeout = Timeout(timeoutDuration)
|
2012-01-19 14:38:44 +00:00
|
|
|
override protected def beforeAll { camelContext.addRoutes(new TestRoute(system)) }
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
override protected def afterEach { mockEndpoint.reset() }
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"A Producer on a sync Camel route" must {
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce a message and receive normal response" in {
|
2012-01-19 14:38:44 +00:00
|
|
|
given("a registered two-way producer")
|
|
|
|
|
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-2", true)))
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("test", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-07 14:18:06 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration)
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a normal response must have been returned by the producer")
|
2012-03-01 17:32:10 +01:00
|
|
|
val expected = CamelMessage("received TEST", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-07 14:18:06 +02:00
|
|
|
Await.result(future, timeoutDuration) match {
|
2012-03-01 17:32:10 +01:00
|
|
|
case result: CamelMessage ⇒ assert(result === expected)
|
|
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce a message and receive failure response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer")
|
2012-05-07 14:18:06 +02:00
|
|
|
val latch = TestLatch()
|
|
|
|
|
var deadActor: Option[ActorRef] = None
|
|
|
|
|
val supervisor = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case p: Props ⇒ {
|
|
|
|
|
val producer = context.actorOf(p)
|
|
|
|
|
context.watch(producer)
|
|
|
|
|
sender ! producer
|
|
|
|
|
}
|
|
|
|
|
case Terminated(actorRef) ⇒ {
|
|
|
|
|
deadActor = Some(actorRef)
|
|
|
|
|
latch.countDown()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
|
|
|
|
|
case _: AkkaCamelException ⇒ Stop
|
|
|
|
|
}
|
|
|
|
|
}))
|
2012-05-11 09:46:49 +02:00
|
|
|
val producer = Await.result[ActorRef](supervisor.ask(Props(new TestProducer("direct:producer-test-2"))).mapTo[ActorRef], timeoutDuration)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message causing an exception is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("fail", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-11 09:46:49 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration).failed
|
|
|
|
|
Await.ready(future, timeoutDuration).value match {
|
|
|
|
|
case Some(Right(e: AkkaCamelException)) ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been returned by the producer")
|
2012-05-11 09:46:49 +02:00
|
|
|
e.getMessage must be("failure")
|
|
|
|
|
e.headers must be(Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2012-05-07 14:18:06 +02:00
|
|
|
then("an AkkaCamelException must have been thrown, which can be used for supervision")
|
|
|
|
|
// check that the supervisor stopped the producer and received a Terminated
|
|
|
|
|
Await.ready(latch, timeoutDuration)
|
|
|
|
|
deadActor must be(Some(producer))
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce a message oneway" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered one-way producer")
|
2012-01-19 14:38:44 +00:00
|
|
|
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-1", true) with Oneway))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedBodiesReceived("TEST")
|
2012-03-01 17:32:10 +01:00
|
|
|
producer ! CamelMessage("test", Map())
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("the test message must have been sent to mock:mock")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produces message twoway without sender reference" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer")
|
2012-01-19 14:38:44 +00:00
|
|
|
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-1")))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedBodiesReceived("test")
|
2012-03-01 17:32:10 +01:00
|
|
|
producer ! CamelMessage("test", Map())
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("there must be only a warning that there's no sender reference")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"A Producer on an async Camel route" must {
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message to direct:producer-test-3 and receive normal response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer")
|
2012-01-19 14:38:44 +00:00
|
|
|
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-3")))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("test", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-07 14:18:06 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-05-07 14:18:06 +02:00
|
|
|
Await.result(future, timeoutDuration) match {
|
2012-03-01 17:32:10 +01:00
|
|
|
case result: CamelMessage ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a normal response must have been returned by the producer")
|
2012-03-01 17:32:10 +01:00
|
|
|
val expected = CamelMessage("received test", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-11 09:46:49 +02:00
|
|
|
result must be(expected)
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message to direct:producer-test-3 and receive failure response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer")
|
2012-01-19 14:38:44 +00:00
|
|
|
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-3")))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message causing an exception is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("fail", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-11 09:46:49 +02:00
|
|
|
|
|
|
|
|
val future = producer.ask(message)(timeoutDuration).failed
|
|
|
|
|
Await.ready(future, timeoutDuration).value match {
|
|
|
|
|
case Some(Right(e: AkkaCamelException)) ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been returned by the producer")
|
2012-05-11 09:46:49 +02:00
|
|
|
e.getMessage must be("failure")
|
|
|
|
|
e.headers must be(Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward normal response of direct:producer-test-2 to a replying target actor and receive response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ReplyingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("test", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-07 14:18:06 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-05-07 14:18:06 +02:00
|
|
|
Await.result(future, timeoutDuration) match {
|
2012-03-01 17:32:10 +01:00
|
|
|
case result: CamelMessage ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a normal response must have been returned by the forward target")
|
2012-03-01 17:32:10 +01:00
|
|
|
val expected = CamelMessage("received test", Map(CamelMessage.MessageExchangeId -> "123", "test" -> "result"))
|
2012-05-11 09:46:49 +02:00
|
|
|
result must be(expected)
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward failure response of direct:producer-test-2 to a replying target actor and receive response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ReplyingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message causing an exception is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("fail", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-11 09:46:49 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration).failed
|
|
|
|
|
Await.ready(future, timeoutDuration).value match {
|
|
|
|
|
case Some(Right(e: AkkaCamelException)) ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been returned by the forward target")
|
2012-05-11 09:46:49 +02:00
|
|
|
e.getMessage must be("failure")
|
|
|
|
|
e.headers must be(Map(CamelMessage.MessageExchangeId -> "123", "test" -> "failure"))
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward normal response to a producing target actor and produce response to direct:forward-test-1" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered one-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ProducingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedBodiesReceived("received test")
|
2012-03-01 17:32:10 +01:00
|
|
|
producer.tell(CamelMessage("test", Map()), producer)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a normal response must have been produced by the forward target")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward failure response to a producing target actor and produce response to direct:forward-test-1" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered one-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
val target = system.actorOf(Props[ProducingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message causing an exception is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedMessageCount(1)
|
2012-05-11 09:46:49 +02:00
|
|
|
mockEndpoint.message(0).body().isInstanceOf(classOf[akka.actor.Status.Failure])
|
2012-03-01 17:32:10 +01:00
|
|
|
producer.tell(CamelMessage("fail", Map()), producer)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been produced by the forward target")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward normal response from direct:producer-test-3 to a replying target actor and receive response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ReplyingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2011-06-28 16:53:11 +02:00
|
|
|
when("a test message is sent to the producer with ?")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("test", Map(CamelMessage.MessageExchangeId -> "123"))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-05-07 14:18:06 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration)
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
then("a normal response must have been returned by the forward target")
|
2012-05-07 14:18:06 +02:00
|
|
|
Await.result(future, timeoutDuration) match {
|
2012-03-01 17:32:10 +01:00
|
|
|
case message: CamelMessage ⇒
|
|
|
|
|
val expected = CamelMessage("received test", Map(CamelMessage.MessageExchangeId -> "123", "test" -> "result"))
|
2012-05-11 09:46:49 +02:00
|
|
|
message must be(expected)
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward failure response from direct:producer-test-3 to a replying target actor and receive response" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered two-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ReplyingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-03-18 10:46:08 +01:00
|
|
|
when("a test message causing an exception is sent to the producer with ask")
|
2012-03-01 17:32:10 +01:00
|
|
|
val message = CamelMessage("fail", Map(CamelMessage.MessageExchangeId -> "123"))
|
2012-05-11 09:46:49 +02:00
|
|
|
val future = producer.ask(message)(timeoutDuration).failed
|
|
|
|
|
Await.ready(future, timeoutDuration).value match {
|
|
|
|
|
case Some(Right(e: AkkaCamelException)) ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been returned by the forward target")
|
2012-05-11 09:46:49 +02:00
|
|
|
e.getMessage must be("failure")
|
|
|
|
|
e.headers must be(Map(CamelMessage.MessageExchangeId -> "123", "test" -> "failure"))
|
2012-01-19 14:38:44 +00:00
|
|
|
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward normal response from direct:producer-test-3 to a producing target actor and produce response to direct:forward-test-1" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered one-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ProducingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedBodiesReceived("received test")
|
2012-03-01 17:32:10 +01:00
|
|
|
producer.tell(CamelMessage("test", Map()), producer)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a normal response must have been produced by the forward target")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
"produce message, forward failure response from direct:producer-test-3 to a producing target actor and produce response to direct:forward-test-1" in {
|
2011-05-23 11:37:56 -04:00
|
|
|
given("a registered one-way producer configured with a forward target")
|
2012-01-19 14:38:44 +00:00
|
|
|
val target = system.actorOf(Props[ProducingForwardTarget])
|
|
|
|
|
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
2011-05-23 11:37:56 -04:00
|
|
|
|
|
|
|
|
when("a test message causing an exception is sent to the producer with !")
|
|
|
|
|
mockEndpoint.expectedMessageCount(1)
|
2012-05-11 09:46:49 +02:00
|
|
|
mockEndpoint.message(0).body().isInstanceOf(classOf[akka.actor.Status.Failure])
|
2012-03-01 17:32:10 +01:00
|
|
|
producer.tell(CamelMessage("fail", Map()), producer)
|
2011-05-23 11:37:56 -04:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
then("a failure response must have been produced by the forward target")
|
|
|
|
|
mockEndpoint.assertIsSatisfied()
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
private def mockEndpoint = camel.context.getEndpoint("mock:mock", classOf[MockEndpoint])
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object ProducerFeatureTest {
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2011-05-23 11:37:56 -04:00
|
|
|
class TestProducer(uri: String, upper: Boolean = false) extends Actor with Producer {
|
|
|
|
|
def endpointUri = uri
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2012-04-06 11:13:59 +01:00
|
|
|
override protected def transformOutgoingMessage(msg: Any) = msg match {
|
2012-03-01 17:32:10 +01:00
|
|
|
case msg: CamelMessage ⇒ if (upper) msg.mapBody {
|
2012-01-19 14:38:44 +00:00
|
|
|
body: String ⇒ body.toUpperCase
|
|
|
|
|
}
|
|
|
|
|
else msg
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestForwarder(uri: String, target: ActorRef) extends Actor with Producer {
|
|
|
|
|
def endpointUri = uri
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2012-05-11 09:46:49 +02:00
|
|
|
override def headersToCopy = Set(CamelMessage.MessageExchangeId, "test")
|
|
|
|
|
|
|
|
|
|
override def routeResponse(msg: Any): Unit = target forward msg
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestResponder extends Actor {
|
|
|
|
|
protected def receive = {
|
2012-03-01 17:32:10 +01:00
|
|
|
case msg: CamelMessage ⇒ msg.body match {
|
2012-05-11 09:46:49 +02:00
|
|
|
case "fail" ⇒ context.sender ! akka.actor.Status.Failure(new AkkaCamelException(new Exception("failure"), msg.headers))
|
2012-03-01 17:32:10 +01:00
|
|
|
case _ ⇒
|
2012-01-19 14:38:44 +00:00
|
|
|
context.sender ! (msg.mapBody {
|
|
|
|
|
body: String ⇒ "received %s" format body
|
|
|
|
|
})
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ReplyingForwardTarget extends Actor {
|
|
|
|
|
protected def receive = {
|
2012-03-01 17:32:10 +01:00
|
|
|
case msg: CamelMessage ⇒
|
|
|
|
|
context.sender ! (msg.addHeader("test" -> "result"))
|
2012-05-11 09:46:49 +02:00
|
|
|
case msg: akka.actor.Status.Failure ⇒
|
|
|
|
|
msg.cause match {
|
|
|
|
|
case e: AkkaCamelException ⇒ context.sender ! Status.Failure(new AkkaCamelException(e, e.headers + ("test" -> "failure")))
|
|
|
|
|
}
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ProducingForwardTarget extends Actor with Producer with Oneway {
|
|
|
|
|
def endpointUri = "direct:forward-test-1"
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
class TestRoute(system: ActorSystem) extends RouteBuilder {
|
|
|
|
|
val responder = system.actorOf(Props[TestResponder], name = "TestResponder")
|
|
|
|
|
|
2011-05-23 11:37:56 -04:00
|
|
|
def configure {
|
|
|
|
|
from("direct:forward-test-1").to("mock:mock")
|
|
|
|
|
// for one-way messaging tests
|
|
|
|
|
from("direct:producer-test-1").to("mock:mock")
|
|
|
|
|
// for two-way messaging tests (async)
|
2012-01-19 14:38:44 +00:00
|
|
|
from("direct:producer-test-3").to(responder)
|
2011-05-23 11:37:56 -04:00
|
|
|
// for two-way messaging tests (sync)
|
|
|
|
|
from("direct:producer-test-2").process(new Processor() {
|
|
|
|
|
def process(exchange: Exchange) = {
|
|
|
|
|
exchange.getIn.getBody match {
|
|
|
|
|
case "fail" ⇒ throw new Exception("failure")
|
|
|
|
|
case body ⇒ exchange.getOut.setBody("received %s" format body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2011-05-23 11:37:56 -04:00
|
|
|
}
|