测试休息端点 - Camel 3 + Spring Boot

Test rest endpoint - Camel 3 + Spring Boot

我不知道如何使用 Apache Camel 3 测试端点 rest。你能帮我吗?

这是我的代码。将 xml 解组为 pojo,然后将 pojo 解组为 json 并将其发送到外部服务 "my.applications.url"。我需要模拟外部响应。我该怎么做?

from("direct:my-application")
                .id("my-application")
                .log("app: ${body}")
                .log("country: ${headers.country}")
                .unmarshal(jaxbDataFormat).process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                ApplicationInput bodyIn = (ApplicationInput) exchange
                        .getIn().getBody();

                exchange.getIn().setBody(bodyIn);
            }
        }).setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.POST).marshal().json(JsonLibrary.Jackson)
                .toD("{{my.applications.url}}?throwExceptionOnFailure=false") //<--- I need to mock in in test
                .choice()
                .when((header("CamelHttpResponseCode").isEqualTo("200")))
                        .unmarshal().json(JsonLibrary.Jackson, NCCLResponse.class)
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {

                                Message in = exchange.getIn();
                                int responseCode = in.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                                myResponse response = (myResponse) exchange.getIn().getBody();
                                //create response
                                myApplicationOutput output = createResponseOk(responseCode, response);

                                exchange.getIn().setBody(output);
                            }

                        })
                .otherwise()
                .unmarshal().json(JsonLibrary.Jackson, myResponse.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {

                        Message in = exchange.getIn();
                        int responseCode = in.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                        myResponse response = (myResponse) exchange.getIn().getBody();
                        //create response
                        ErrorResponse output = createResponseError(responseCode, response);
                        exchange.getIn().setBody(output);
                    }
                })
                .end();```

如果你想在 Camel 路由测试中模拟这个调用,你可以使用 AdviceWith

1) 添加一个identifier/marker到你想模拟的路由步骤

.toD("{{my.applications.url}}?throwExceptionOnFailure=false").id("RequestToMock")

2) 然后使用 AdviceWith 用其他东西替换标记的步骤

context.getRouteDefinition("yourRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            weaveById("RequestToMock") // <-- same identifier
                    .replace()
                    .setBody(simple("resource:classpath:TestResponse.json"));
        }
    });

3) 告诉 Camel 你的测试使用 AdviceWith(取决于你的测试类型)

@UseAdviceWith // for Spring Boot tests

@Override
public boolean isUseAdviceWith() { // for CamelTestSupport
    return true;
}