Created
October 3, 2018 14:53
-
-
Save BorisDaich/36379e105651492666f0ae3f7ef6b0a0 to your computer and use it in GitHub Desktop.
camel sample Junit test - how to replace a pollEnrich call to a 3rd party service in an existing Route with a constant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package biz.daich.learning.camel; | |
import org.apache.camel.builder.AdviceWithRouteBuilder; | |
import org.apache.camel.builder.RouteBuilder; | |
import org.apache.camel.component.mock.MockEndpoint; | |
import org.apache.camel.model.RouteDefinition; | |
import org.apache.camel.test.junit4.CamelTestSupport; | |
import org.junit.Test; | |
import lombok.extern.slf4j.Slf4j; | |
@Slf4j | |
public class Test_Eli_s3 extends CamelTestSupport { | |
@Override | |
protected RouteBuilder createRouteBuilder() throws Exception { | |
return new TestRoute(); | |
} | |
private static class TestRoute extends RouteBuilder { | |
public void configure() throws Exception { | |
from("direct:a") | |
.routeId("a1") | |
.pollEnrich("http://www.aaaaa.com").id("enrichID") | |
.convertBodyTo(String.class) | |
.process(e -> { | |
log.info("e={}", e.getIn().getBody(String.class)); | |
}) | |
.to("http://www.somedomainrrrr.rrr").id("httpServiceID") | |
.to("log:AAAAAAAAAAAA?showBody=true&showHeaders=true").id("last"); | |
} | |
} | |
@Override | |
public boolean isUseAdviceWith() { | |
return true; | |
} | |
@Test | |
public void test_1() throws Exception { | |
// get reference to the right route | |
RouteDefinition route = context.getRouteDefinition("a1"); | |
assertNotNull(route); | |
// now intercept and replace the endpoints and nodes that are not relevant | |
// notice the class is AdviceWithRouteBuilder !!! | |
route.adviceWith(context, new AdviceWithRouteBuilder() { | |
public void configure() throws Exception { | |
weaveById("enrichID").replace().transform().constant("crap"); // replace the enrich node with the constant "crap" thats it!!!! | |
weaveById("httpServiceID").replace().transform().body(x -> x); /// replace the HTTP service call with just NO OP transform | |
weaveById("last").after().to("mock:end"); // add to the existing route a MOCK node at the end to do asserts that | |
// replaceFromWith("http://www.cnn.com"); | |
} | |
}); | |
MockEndpoint mockEnd = getMockEndpoint("mock:end"); | |
// now set the expectations:: | |
// there was a message | |
mockEnd.expectedMessageCount(1); | |
/// another way to check that the right contents of the body of the messages arrived | |
mockEnd.expectedBodiesReceived(new Object[] { | |
"crap" | |
}); | |
// run the context | |
context.start(); | |
// send a message down the route | |
template.sendBody("direct:a", "123"); | |
// check that all expectations on the mocks are as expected | |
assertMockEndpointsSatisfied(); | |
/// this is the way to see what the mock Endpoint has AFTER running things through the routes | |
assertEquals("crap", mockEnd.getReceivedExchanges().get(0).getIn().getBody(String.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment