Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel test: how to get access to the headers set in the route

I have a Camel Unit Test and I want to get access to the header values that are set on the Exchange on the first point in the route.

Route example :

<route id="VCM001_incoming">
    <from uri="file:{{InLocation}}"/>
    <convertBodyTo type="java.lang.String"/>
    <setHeader headerName="FileNameWithoutExtension">
        <simple>${file:onlyname.noext}</simple>
    </setHeader>
    <to uri="direct:splitFile"/>
</route>

Java code where it's used :

public List<String> createList(Exchange exchange) {
    String fileName = (String) exchange.getIn().getHeader("FileNameWithoutExtension");

So all good to this point.

Now in my test I want to find out what header value is "FileNameWithoutExtension".

@Produce(uri = "file:{{InLocation}}")
private ProducerTemplate inputEndpoint;

@EndpointInject(uri = "mock:output1")
private MockEndpoint outputEndpointRPR;

@Test
public void testCamelRoute() throws Exception {
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:{{OutLocation}}").to(outputEndpoint);
        }

    inputEndpoint.sendBody("test-message");

        Object[] expectedBodies = new Object[]{"Success: filename=xxx"};
        // At this point I need the header 'FileNameWithoutExtension' to setup the correct 'expectedBodies'

    outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies);

        assertMockEndpointsSatisfied();

    }

}
like image 354
jeff porter Avatar asked Jan 10 '23 05:01

jeff porter


2 Answers

Know this is very late, for camel 2.15.2 you can use the following

outputEndpoint.expectedHeaderReceived("header", "value");
like image 152
mornejvr Avatar answered May 09 '23 13:05

mornejvr


You could get it quite easily using:

outputEndpoint.getExchanges().get(0).getIn().getHeader("FileNameWithoutExtension");
like image 34
abbasdgr8 Avatar answered May 09 '23 13:05

abbasdgr8