Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Camel REST DSL Consumer Template

I have the following code:

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

How can I use JUnit to test this code? Can CamelTestSupport handle it?

I want to create a test like:

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

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

how can I mock that? what I put in -----myendpoint---- to reference that route?

like image 434
maxcpereira Avatar asked Jun 26 '15 19:06

maxcpereira


People also ask

How do you do the camel route unit test?

A good way to perform unit testing on a Camel application is to start the application, send messages to the application, and verify that the messages are routed as expected. This is illustrated in figure 6.1. You send a message to the application, which transforms the message to another format and returns the output.

What is rest DSL?

The Rest DSL is a facade that builds Rest endpoints as consumers for Camel routes. The actual REST transport is leveraged by using Camel REST components such as Netty HTTP, Servlet, and others that has native REST integration.

What is DSL in camel?

Camel uses a Java Domain Specific Language or DSL for creating Enterprise Integration Patterns or Routes in a variety of domain-specific languages (DSL) as listed below: Java DSL - A Java based DSL using the fluent builder style.


1 Answers

As a possible solution, you can set the URI for your REST route and use that URI in your junit test. In order to do so, you need to switch RestDefinition to RouteDefinition by calling route method, and then you can call the from method and set the uri argument. Example using direct endpoint:

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

In your junit class, you can then type:

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

Hope this helps.

like image 194
user3687442 Avatar answered Sep 30 '22 02:09

user3687442