BAEL-5237 - Apache Camel Conditional Routing (#12675)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito * BAEL-4205 - A Guide to Events in OkHTTP * BAEL-5408 - Update Camel version in spring-boot-camel module * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5237 - Apache Camel Conditional Routing Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
8a79e04d39
commit
cd9f1cce2e
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ConditionalBeanRouter extends RouteBuilder {
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:start-conditional-bean")
|
||||
.routeId("conditional-bean-route")
|
||||
.choice()
|
||||
.when(method(FruitBean.class, "isApple"))
|
||||
.setHeader("favourite", simple("Apples"))
|
||||
.to("mock:result")
|
||||
.otherwise()
|
||||
.setHeader("favourite", header("fruit"))
|
||||
.to("mock:result")
|
||||
.end();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ConditionalBodyRouter extends RouteBuilder {
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:start-conditional")
|
||||
.routeId("conditional-body-route")
|
||||
.choice()
|
||||
.when(body().contains("Baeldung"))
|
||||
.setBody(simple("Goodbye, Baeldung!"))
|
||||
.to("mock:result-body")
|
||||
.otherwise()
|
||||
.to("mock:result-body")
|
||||
.end();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ConditionalHeaderRouter extends RouteBuilder {
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:start-conditional-header")
|
||||
.routeId("conditional-header-route")
|
||||
.choice()
|
||||
.when(header("fruit").isEqualTo("Apple"))
|
||||
.setHeader("favourite", simple("Apples"))
|
||||
.to("mock:result")
|
||||
.otherwise()
|
||||
.setHeader("favourite", header("fruit"))
|
||||
.to("mock:result")
|
||||
.end();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConditionalRoutingSpringApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConditionalRoutingSpringApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
|
||||
public class FruitBean {
|
||||
|
||||
private FruitBean() {
|
||||
}
|
||||
|
||||
public static boolean isApple(Exchange exchange) {
|
||||
return "Apple".equals(exchange.getIn()
|
||||
.getHeader("fruit"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ConditionalBeanRouterUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@EndpointInject("mock:result")
|
||||
private MockEndpoint mock;
|
||||
|
||||
@Test
|
||||
void whenSendBodyWithFruit_thenFavouriteHeaderReceivedSuccessfully() throws InterruptedException {
|
||||
mock.expectedHeaderReceived("favourite", "Apples");
|
||||
|
||||
template.sendBodyAndHeader("direct:start-conditional-bean", null, "fruit", "Apple");
|
||||
|
||||
mock.assertIsSatisfied();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ConditionalBodyRouterUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@EndpointInject("mock:result-body")
|
||||
private MockEndpoint mock;
|
||||
|
||||
@Test
|
||||
void whenSendBodyWithBaeldung_thenGoodbyeMessageReceivedSuccessfully() throws InterruptedException {
|
||||
mock.expectedBodiesReceived("Goodbye, Baeldung!");
|
||||
|
||||
template.sendBody("direct:start-conditional", "Hello Baeldung Readers!");
|
||||
|
||||
mock.assertIsSatisfied();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.camel.conditional;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ConditionalHeaderRouterUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@EndpointInject("mock:result")
|
||||
private MockEndpoint mock;
|
||||
|
||||
@Test
|
||||
void whenSendBodyWithFruit_thenFavouriteHeaderReceivedSuccessfully() throws InterruptedException {
|
||||
mock.expectedHeaderReceived("favourite", "Banana");
|
||||
|
||||
template.sendBodyAndHeader("direct:start-conditional-header", null, "fruit", "Banana");
|
||||
|
||||
mock.assertIsSatisfied();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue