如何编写apache camel路由的单元测试

watbbzwu  于 2023-02-08  发布在  Apache
关注(0)|答案(1)|浏览(176)

有人能帮忙写这个类的单元测试吗?

@Component ("edi820AdapterRouteBuilder")公共类EDI820AdapterRouteBuilder扩展了基本路由构建器{

private static final Logger LOGGER = LoggerFactory.getLogger(EDI820AdapterRouteBuilder.class);
private static final String DIRECT_Q_RECEIVER = "direct:queueReceiver";
private static final String DIRECT_PROCESS_820 = "direct:process820";
private static final String DIRECT_TO_HIX = "direct:toHIX";
private static final String DIRECT_TO_CMS = "direct:toCMS";
private static final String MDC_TRANSACTIONID = "transactionId";
private static final String REQUEST_ID = "RequestID";
private static final String DIRECT_PUT_MDC = "direct:putMDC";
private static final String DIRECT_REMOVE_MDC = "direct:removeMDC";

@Override
public void configure() throws Exception {
    super.configure();
    LOGGER.debug("configure called.");
    String queueName = appConfig.getInboundQueueName();
    LOGGER.debug("inboundQueueName: {}", queueName);
    String toHIXendpoint = appConfig.getEndpointWithOptions("toHIXendpoint");
    LOGGER.debug("toHIXendpoint: {}", toHIXendpoint);
    String toCMSendpoint = appConfig.getEndpointWithOptions("toCMSendpoint");
    LOGGER.debug("toCMSendpoint: {}", toCMSendpoint);
    String routeDelay = appConfig.getRouteDelay();
    LOGGER.debug("routeDelay: {}",routeDelay);
    
    from("timer://runOnce?repeatCount=1&delay="+routeDelay)
    .to("bean:edi820AdapterRouteBuilder?method=addRoute")
    .end();
        
    from(DIRECT_Q_RECEIVER)
        .to(PERSIST_EDI820_XDATA)
        .to(EDI820_REQUEST_TRANSFORMER)
        .to(DIRECT_PROCESS_820) 
        .log(LoggingLevel.INFO, LOGGER,"Executed "+DIRECT_Q_RECEIVER)
        .end();
    
    from(DIRECT_PROCESS_820)
        .choice()
            .when(header(TRANSACTION_SOURCE_STR).isEqualTo(HIX_SOURCE_SYSTEM))
                .log(LoggingLevel.INFO, LOGGER,"Calling route for: "+HIX_SOURCE_SYSTEM)
                .to(DIRECT_TO_HIX)
            .when(header(TRANSACTION_SOURCE_STR).isEqualTo(CMS_SOURCE_SYSTEM))
                .log(LoggingLevel.INFO, LOGGER,"Calling route for: "+CMS_SOURCE_SYSTEM)
                .to(DIRECT_TO_CMS)
            .otherwise()
                .log(LoggingLevel.INFO, LOGGER,"Invalid "+TRANSACTION_SOURCE_STR+" ${header["+TRANSACTION_SOURCE_STR+"]}")
            .end();

    from(DIRECT_TO_HIX).routeId("edi820adapter-to-hix-producer-route")
        .log(LoggingLevel.INFO, LOGGER,"Executing edi820adapter-to-hix-producer-route")
        .marshal().json(JsonLibrary.Jackson)//Convert body to json string
        .to(toHIXendpoint)
        .log(LoggingLevel.DEBUG, LOGGER, "json body sent to edi820-hix: ${body}")
        .log(LoggingLevel.INFO, LOGGER,"Executed edi820adapter-to-hix-producer-route") 
        .end();

    from(DIRECT_TO_CMS).routeId("edi820adapter-to-cms-producer-route")
        .log(LoggingLevel.INFO, LOGGER,"Executing edi820adapter-to-cms-producer-route")
        .marshal().json(JsonLibrary.Jackson)//Convert body to json string
        .to(toCMSendpoint) 
        .log(LoggingLevel.DEBUG, LOGGER, "json body sent to edi820-cms: ${body}")
        .log(LoggingLevel.INFO, LOGGER,"Executed edi820adapter-to-cms-producer-route") 
        .end();

    from(DIRECT_PUT_MDC).process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            if (exchange.getIn().getHeader(REQUEST_ID) != null) {
                MDC.put(MDC_TRANSACTIONID, (String) exchange.getIn().getHeader(REQUEST_ID));
            }
        }
    }).end();

    from(DIRECT_REMOVE_MDC).process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            MDC.remove(MDC_TRANSACTIONID);
        }
    }).end();

}

public void addRoute(Exchange exchange) {
    try {
        CamelContext context = exchange.getContext();
        ModelCamelContext modelContext = context.adapt(ModelCamelContext.class);
        modelContext.addRouteDefinition(buildRouteDefinition());
    } catch (Exception e) {
        LOGGER.error("Exception in addRoute: {}", e.getMessage());
        LOGGER.error(ExceptionUtils.getFullStackTrace(e));
    }
}

private RouteDefinition buildRouteDefinition() {
    String queueName = appConfig.getInboundQueueName();     
    RouteDefinition routeDefinition = new RouteDefinition();
    routeDefinition
    .from("jms:queue:" + queueName).routeId("edi820-adapter-jms-consumer-route")
    .to(DIRECT_PUT_MDC)
    .log(LoggingLevel.INFO, LOGGER,"Executing edi820-adapter-jms-consumer-route")
    .log(LoggingLevel.DEBUG, LOGGER, "Received Message from queue: "+queueName)
    .to(DIRECT_Q_RECEIVER)
    .log(LoggingLevel.INFO, LOGGER,"Executed edi820-adapter-jms-consumer-route")
    .to(DIRECT_REMOVE_MDC)
    .end();
    return routeDefinition;
}

}
=================================================================如果需要任何额外的信息,请告诉我,提前感谢您的帮助。

yjghlzjz

yjghlzjz1#

不可能编写包含这些类的完整测试用例,所以我编写了一些片段代码以便于理解。
理解示例:
以下是路线生成器类的代码片段:

public class CheckRouteBuilder extends RouteBuilder{
   @Override
   public void configure() throws Exception {
       from("timer://runOnce?repeatCount=1&delay="+routeDelay)
          .to("bean:edi820AdapterRouteBuilder?method=addRoute")
          .end();
    }
}

遵循上述类别的测试用例:

public class CheckRouteBuilderTest extends CamelTestSupport {
    @Test
    public void testConfigure() throws Exception {
        context.addRoutes(new CheckRouteBuilder ());
        RouteDefinition route = context.getRouteDefinitions().get(0);
        AdviceWith.adviceWith(route, context, new AdviceWithRouteBuilder() {

            @Override
            public void configure() throws Exception {
                replaceFromWith("direct:TestEndpoint"); // timer://runOnce?repeatCount=1&delay="+routeDelay replace with direct:TestEndpoint because during test timer component not support that's why need to replace with direct component.
                weaveAddLast().to("mock:endPoint");
            }
        });
        MockEndpoint mockEndpoint = getMockEndpoint("mock:endPoint");
        mockEndpoint.expectedMessageCount(1);
        template.sendBodyAndHeaders("direct:TestEndpoint", "#", "##");
        mockEndpoint.assertIsSatisfied();
   }
}

#-〉提及主体,如String、JSONObject、XML。如果主体不是必需的,则只需传递null值。
##-〉需要以Map〈String,Object)格式传递类似内容类型:application/json的标题。

相关问题