正在测试Camel REST DSL使用者模板

hgqdbh6s  于 2022-11-07  发布在  Apache
关注(0)|答案(3)|浏览(200)

下面的代码:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;

public class OrderNumberRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").bindingMode(RestBindingMode.json)          
            .dataFormatProperty("prettyPrint", "true")
            .contextPath("suppliera/rest").port(8080);

        rest("/ordernumber").description("ordernumber rest service")
            .consumes("application/json").produces("application/json")

            .get("/{id}").description("get ordernumber").outType(ServiceResponse.class)
            .to("bean:orderNumberService?method=getOrderNumber(${header.id})");
    }
}

如何使用JUnit来测试这段代码?CamelTestSupport能处理吗?
我想创建一个测试,如下所示:

@Produce(------myendpoint----) 
protected ProducerTemplate testProducer; 

public void mytest(){
testProducer.requestBody("foo");
}

我怎么能嘲笑它呢?我在-----myendpoint----中输入了什么来引用路由?

5w9g7ksd

5w9g7ksd1#

作为一种可能的解决方案,您可以为REST路由设置URI,并在junit测试中使用该URI。为此,您需要通过调用 route 方法将RestDefinition切换到RouteDefinition,然后您可以调用 from 方法并设置uri参数。使用直接端点的示例:

rest("/ordernumber").description("ordernumber rest service")
    .consumes("application/json").produces("application/json")
    .get("/{id}").description("get ordernumber").outType(ServiceResponse.class)
    .route().from("direct:myendpoint")
    .to("bean:orderNumberService?method=getOrderNumber(${header.id})");

在junit类中,可以键入:

@Produce(uri = "direct:myendpoint")
protected ProducerTemplate testProducer;

希望这对你有帮助。

3lxsmp7m

3lxsmp7m2#

您看过本页上的示例吗?https://camel.apache.org/testing.html
既然你有Spring,那就试试这个页面上的Spring测试例子,它们会比使用CamelTestSupport更适合你的情况。

rhfm7lfc

rhfm7lfc3#

您可以设置要在测试中使用的自定义RestConsumerFactory。

public class TestRestConsumerFactory implements RestConsumerFactory {

    @Override
    public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {

        // just use a direct endpoint for testing purpose
        String id;
        if (uriTemplate != null) {
            id = DefaultUuidGenerator.generateSanitizedId(basePath + uriTemplate);
        } else {
            id = DefaultUuidGenerator.generateSanitizedId(basePath);
        }
        // remove leading dash as we add that ourselves
        if (id.startsWith("-")) {
            id = id.substring(1);
        }

        DirectEndpoint direct = camelContext.getEndpoint("direct:" + verb + "-" + id, DirectEndpoint.class);
        return direct.createConsumer(processor);
    }
}

代码灵感源自Camel test
如果您使用的是spring,则可以在测试配置中创建bean

@Bean
public RestConsumerFactory testRestConsumerFactory(){
   return new TestRestConsumerFactory();
}

否则,请将其显式设置为Camel rest配置。Camel将找到它并将其用作Rest组件。请参阅Camel文档:Camel REST DSL documentation

If no component has been explicit configured, then Camel will lookup if there is a Camel component that integrates with the Rest DSL, or if a org.apache.camel.spi.RestConsumerFactory is registered in the registry. If either one is found, then that is being used.

此自定义RestConsumerFactory将其余端点替换为直接端点,例如
GET /say/hello -〉direct:获取-说-你好
这样您就可以在测试代码中使用它们

testProducer.requestBody("direct:get-say-hello", "body");

奖金

如果您已参数化端点,则上面实现的RestConsumerFactory将使用大括号替换它,例如
GET /说/{单词} -〉直接:获取-说-{单词}
您仍然可以设置适当的头来测试此类端点

Map<String, Object> headers = ImmutableMap.of("word", "hello");
producerTemplate.requestBodyAndHeaders("direct:get-say-{word}", null, headers);

相关问题