Spring Framework Apache Camel asynchronous processing unit testing

In the previous post entitled Apache Camel for asynchronous processing demonstrates the power of Apache camel. In this post, I will provide a sample code that unit tests the Apache camel.

package com.myapp.camel;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.myapp.config.MyappConfig;
import com.myapp.model.FeedGenerationRequest;
import com.myapp.model.JobType;
import com.myapp.service.MyappService;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(
locations =
{
"com.myapp.camel.JobHandlingRouteBuilderTest$ContextConfig"
},
loader = JavaConfigContextLoader.class)
public class JobHandlingRouteBuilderTest extends AbstractJUnit4SpringContextTests
{

@Produce(uri = DIRECT_START)
private ProducerTemplate template;

private static final String DIRECT_START = "direct:start";

@Mock
FeedGenerationRequest request;

private static MyappService MyappService = mock(MyappService.class);

@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
}

@Test
@Ignore
public void testConfigureJobHandlingRoute() throws Exception
{
template.sendBodyAndHeader(request,
JobHandlingRouteBuilder.JOB_TYPE_HEADER, JobType.FEED_GENERATION);
Thread.sleep(1000);
verify(MyappService, times(1)).produceGroupLevelCashFeed(any(FeedGenerationRequest.class));
verify(MyappService, times(1)).produceGroupLevelPositionFeed(any(FeedGenerationRequest.class));
}

@Test
@Ignore
public void testConfigureJobHandlingRouteOnFailure() throws Exception
{
doThrow(new Exception()).when(MyappService).produceGroupLevelCashFeed(any(FeedGenerationRequest.class));
template.sendBodyAndHeader(request,
JobHandlingRouteBuilder.JOB_TYPE_HEADER, JobType.FEED_GENERATION);
Thread.sleep(1000);
verify(MyappService, times(1)).markControllableJobFailed(any(FeedGenerationRequest.class),
any(String.class));
}

@Configuration
@Import(
{
MyappConfig.class
})
public static class ContextConfig extends CamelConfiguration
{
@Bean
public MyappService myappService()
{
return MyappService;
}

private MyappConfig MyappConfig;

@Autowired
public void setMyappConfig(MyappConfig MyappConfig)
{
this.MyappConfig = MyappConfig;
}

@Bean
public RouteBuilder route()
{
return new RouteBuilder()
{
@Override
public void configure()
{
from(DIRECT_START)
.to(JobHandlingRouteBuilder.JOB_QUEUE);
}
};
}

@Override
public List<routebuilder> routes()
{
List<routebuilder> routes = new ArrayList<routebuilder>();
MyappConfig.setCamelContext(mock(CamelContext.class));
routes.add(MyappConfig.jobHandlingRouteBuilder());
routes.add(route());
return routes;
}

}

}



The MyappConfig class will be as shown below.


package com.myapp.config;

import com.myapp.camel.JobHandlingRouteBuilder;
import com.myapp.service.MyappForecastService;

import javax.annotation.Resource;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MyappConfig implements CamelContextAware
{

private CamelContext context;

@Resource(name = "myappService")
private MyappService myappService;

@Bean
public ProducerTemplate template()
{
if (context != null)
{
return context.createProducerTemplate();
}
//For unit tests
else
{
return null;
}
}

@Bean
public RouteBuilder jobHandlingRouteBuilder()
{
return new JobHandlingRouteBuilder(cashForecastService);
}

@Override
public void setCamelContext(CamelContext camelContext)
{
context = camelContext;
}

@Override
public CamelContext getCamelContext()
{
return context;
}

}



-->