如何通过单元测试设置Camel exchangeProperty

isr3a4wc  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(182)

我有一堆单元测试,这些测试使用如下代码运行 Camel 路径

// setup code here...
    String route = "direct:someroute";
    try (CamelContext context = new DefaultCamelContext()) {
        Object response = runCamelRoute(context, route, ci);
        checkResponse(route, response);
    }

但是这个路由要求在到达这里之前设置一个exchange属性-我该如何设置它呢?CamelContext有很多方法,但是我似乎找不到类似这样的方法:

CamelRoute cr = context.getRoute(route);
cr.getExchange().setProperty("propertyName", "propetyValue");

下面是我用于单元测试的camel run方法,以及一些用于设置Oracle连接等的额外代码。

protected Object runCamelRoute(CamelContext context, String route, Object message) throws Exception {
    context.addRoutes(new MyRouteBuilder() {
    });

    setupRegistry(context);
    context.start();

    FluentProducerTemplate template = context.createFluentProducerTemplate();
    template.withBody(message)
            .withHeader("hello", "goodbye") 
            .withProcessor(e -> e.setProperty("propertyName", "propertyValue")) // fail use header instead
            .to(route);

    try {
        Future<Object> future = template.asyncRequest();
        return future.get();
    } 
    catch(Exception ex) {
        System.out.println(route + " " + ex.getClass().getCanonicleName() + " " + ex.getMessage());
        throw ex;
    } 
    finally {
        template.stop();
        context.stop();
    }
}

private void setupRegistry(CamelContext context) {
    DataSource ds = DataSourceHelper.createConnectionPoolDev();
    context.getRegistry().bind("dataSource", ds);
    context.getRegistry().bind("Transformer", new Transformer());
}

public static OracleDataSource createConnectionPoolDev() {
    try {
        OracleDataSource ds = new OracleDataSource();
        ds.setConnectionCacheName("oraCache");
        ds.setURL("jdbc:oracle:thin:@//cluster:1521/server.domain.ca");
        ds.setUser("user");
        ds.setPassword("pass");
        return ds;
    } 
    catch (Exception ex) {
        logger.error("Failed to create connection to the database " + ex.getMessage());
    }
    return null;
}
lhcgjxsq

lhcgjxsq1#

这样子的事情可能吗?

context.createFluentProducerTemplate()          
            .withBody(...)
            .withHeader(..., ...)
            .withProcessor( e -> e.setProperty(propertyName, propertyValue) )
            .to("direct:someroute")         
            .send();

相关问题