I am fairly new to Camel & just managed to implement a use case as below with 2 routes which is using file & http components. Looking for some leads on writing junits for the same. Have tried some sample test case below based on the inputs that i found on the net. Not sure if that suffices. Appreciate your help!
Implementation:
@Override public void configure() throws Exception { // Global Exception Handling block onException(FileWatcherException.class).process(new Processor() { public void process(Exchange exchange) throws Exception { System.out.println("Exception handled"); } }).to("file:C:/error?recursive=true").handled(true); // Actively listen to the input folder for an incoming file from("file:C:/input?noop=true&recursive=true&delete=true") .process(new Processor() { public void process(Exchange exchange) throws Exception { String fileName = exchange.getIn().getHeader("CamelFileName").toString(); exchange.getIn().setHeader("fileName", fileName); } }) // Call the Get endpoint with fileName as input parameter .setHeader(Exchange.HTTP_METHOD, simple("GET")) .toD("http://localhost:8090/fileWatcher?fileName=${header.fileName}") .choice() // if the API returns true, move the file to the outbox folder .when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(constant(200))) .to("file:C:/outbox?noop=true&recursive=true") .endChoice() // If the API's response code is other than 200, move the file to error folder .otherwise() .log("Moving the file to error folder") .to("file:C:/error?recursive=true") .end(); // Listen to the outbox folder for file arrival after it gets moved in the above step from("file:C:/outbox?noop=true&recursive=true") // Request Body for POST call is set in FileDetailsProcessor class .process(new FileDetailsProcessor()) .marshal(jsonDataFormat) .setHeader(Exchange.HTTP_METHOD, simple("POST")) .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) // Call the Rest endpoint with fileName & filePath as RequestBody .to("http://localhost:8090/fileWatcher") .process(new MyProcessor()) .end(); }
Junit
@Test public void checkFileWatcherFunctionality() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // mocking all endpoints. **QUESTION** - Is this required? mockEndpointsAndSkip("http://localhost:8090:fileWatcher?fileName=loan.csv"); mockEndpointsAndSkip("file:C:/processing"); mockEndpointsAndSkip("file:C:/error"); mockEndpointsAndSkip("http://localhost:8090:fileWatcher"); } }); context.start(); // **QUESTION** - This is a GET call. Expecting only the HTTP status code from it. How to check that? getMockEndpoint("mock:http://localhost:8090:fileWatcher?fileName=abc.txt").expectedBodyReceived(); // **QUESTION** - This is a POST call. How to send request body along? Expecting only the HTTP status code from it. How to check that? getMockEndpoint("mock:http://localhost:8090:fileWatcher").expectedBodyReceived(); // **QUESTION** - Is this the right way to check? getMockEndpoint("mock:file:C:/processing").expectedFileExists("loan.csv");; template.sendBodyAndHeader("file:C:/inbound", "", Exchange.FILE_NAME, "loan.csv"); // QUESTION - What can be asserted now?
}
Also - How to write test cases for negative flow (exception scenario)? Looking for suggestions.
I have managed to draft the test case. Is this the right approach or can there be a better way? This might be more of an integration test i suppose.
The issue i see now is that the test case doesn't report at the end (success or failure), instead it keeps waiting for file arrival in the input folder. What am i missing?