简体   繁体   中英

How to write the unit tests for the apache camel routes

Can someone please help in writing the unit tests for this class.

==============================================================================================

@Component("edi820AdapterRouteBuilder") public class EDI820AdapterRouteBuilder extends BaseRouteBuilder {

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

}

============================================================================== Please let me know if any additional information is needed. Thanks for the help in advance.

It is not possible to write entire test cases mentioning those classes. So I'm writing snippet code for understanding purposes.

Sample Example for Understanding:

Below is a snippet of code for the route builder class:

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

Follow the test cases of the above class:

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();
   }
}

# -> Mention body like String, JSONObject, XML. If body is not required then simply pass the null value.
## -> Mention Header like Content-type:application/json to need to pass in Map<String, Object) format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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