繁体   English   中英

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

[英]Test rest endpoint - Camel 3 + Spring Boot

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

那是我的代码。 将 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) 向要模拟的路线步骤添加标识符/标记

.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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM