Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testIntercept;
- import org.apache.camel.*;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.component.mock.MockComponent;
- import org.apache.camel.component.mock.MockEndpoint;
- import org.apache.camel.component.seda.SedaComponent;
- import org.apache.camel.component.seda.SedaEndpoint;
- import org.apache.camel.processor.aggregate.AggregationStrategy;
- import org.apache.camel.test.junit4.CamelTestSupport;
- import org.junit.Ignore;
- import org.junit.Test;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.concurrent.LinkedBlockingQueue;
- public class TestRemoveInvalidRecipient3 extends CamelTestSupport {
- String directEndPointURI = "direct:myDirect";
- String finalEndpointURI = "mock:finalEndpoint";
- RecipientBean recipientBean = new RecipientBean();
- @Override
- public boolean isUseRouteBuilder() {
- return false;
- }
- @Test
- public void testIntegrationDynamicRecipient() throws Exception {
- context.addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- from(directEndPointURI)
- .doTry()
- .recipientList(method(recipientBean, "getRecipientList"))
- .parallelProcessing()
- .ignoreInvalidEndpoints()
- .aggregationStrategy(new AggregationStrategy() {
- @Override
- public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
- System.out.println("EEEEEEEEEEEEEEEEEEEEEE");
- if (oldExchange == null) {
- newExchange.getIn().setHeader("count", 1);
- return newExchange;
- } else {
- int count = oldExchange.getIn().getHeader("count", Integer.class);
- oldExchange.getIn().setHeader("count", count + 1);
- return oldExchange;
- }
- }
- })
- .end()
- .doCatch(Exception.class)
- .process(new Processor() {
- @Override
- public void process(Exchange exchange) throws Exception {
- int a1 = exchange.getIn().getHeader("count", Integer.class);
- System.out.println("WE GOT : " + a1);
- }
- })
- .end().to(finalEndpointURI);
- }
- });
- context.start();
- MockEndpoint mockEndpoint = new MockEndpoint("mock:abc", new MockComponent());
- Endpoint sedaEndpoint = new SedaEndpoint("seda:mySeda", new SedaComponent(), new LinkedBlockingQueue<Exchange>());
- sedaEndpoint.setCamelContext(context);
- recipientBean.addRecipient(mockEndpoint);
- recipientBean.addRecipient(sedaEndpoint);
- // Do a simple go through the route
- getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
- template.sendBody(directEndPointURI, "MY_MESSAGE");
- getMockEndpoint(finalEndpointURI).assertIsSatisfied();
- // Make sure we have the 2 recipients.
- assertEquals(2, recipientBean.getRecipientList().length);
- // Add the bad endpoint
- Endpoint fake = new MyFakeEndpoint();
- recipientBean.addRecipient(fake);
- // Run the second test
- getMockEndpoint(finalEndpointURI).reset();
- getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
- template.sendBody(directEndPointURI, "MY_MESSAGE2");
- getMockEndpoint(finalEndpointURI).assertIsSatisfied();
- // The fake endpoint should have been deleted by the time finalEndpointURI is satisfied.
- assertEquals(2, recipientBean.getRecipientList().length);
- }
- public class RecipientBean {
- private ArrayList<Endpoint> recipientList = new ArrayList<Endpoint>();
- public void addRecipient(Endpoint endpoint) {
- recipientList.add(endpoint);
- }
- public void removeRecipient(Endpoint endpoint) {
- recipientList.remove(endpoint);
- }
- public void removeRecipient(String uri) {
- Endpoint toDeleteEndpoint = null;
- for (Endpoint endpoint : recipientList) {
- if (endpoint.getEndpointUri().equals(uri))
- toDeleteEndpoint = endpoint;
- }
- if (toDeleteEndpoint != null)
- recipientList.remove(toDeleteEndpoint);
- }
- public Endpoint[] getRecipientList() {
- return recipientList.toArray(new Endpoint[0]);
- }
- }
- public class MyFakeEndpoint implements Endpoint {
- @Override
- public String getEndpointUri() {
- return "myEvilURI";
- }
- @Override
- public EndpointConfiguration getEndpointConfiguration() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public String getEndpointKey() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public Exchange createExchange() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public Exchange createExchange(ExchangePattern pattern) {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public Exchange createExchange(Exchange exchange) {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public CamelContext getCamelContext() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public Producer createProducer() throws Exception {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public Consumer createConsumer(Processor processor) throws Exception {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public PollingConsumer createPollingConsumer() throws Exception {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public void configureProperties(Map<String, Object> options) {
- //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public void setCamelContext(CamelContext context) {
- //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public boolean isLenientProperties() {
- return false; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public boolean isSingleton() {
- return false; //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public void start() throws Exception {
- //To change body of implemented methods use File | Settings | File Templates.
- }
- @Override
- public void stop() throws Exception {
- //To change body of implemented methods use File | Settings | File Templates.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement