I am new to multithreading and have the below sample code. How do we write unit test cases with max code coverage?
public class jobExecutorImpl<T> implements jobExecutor<T>
{
private AcquisitionProcessor processor;
private ExecutorService jobExecutor;
private AcquisitionProcessExecutor executorService;
@Override
public AcquisitionResponse<T> processContent(Collection<T> itemsToProcess) throws Exception {
AcquisitionResponse<T> acquisitionResponse = new AcquisitionResponse<>();
if(CollectionUtils.isNotEmpty(itemsToProcess)){
// List<Callable<T>> tasks = new ArrayList<Callable<T>>();
// CountDownLatch latch=new CountDownLatch(itemsToProcess.size());
LOGGER.info("Items to Process: "+itemsToProcess.size());
for (T item : itemsToProcess) {
jobExecutor.execute(new Runnable() {
@Override
public void run() {
AcquisitionStatus status= null;
try {
status = executorService.process(item);
} catch (Exception e) {
e.printStackTrace();
//TODO catch filenotfound and throw
}
if(status!=null) {
acquisitionResponse.addStatus(item, status);
}
}
});
// tasks.add(new threadExecutor(item,acquisitionResponse,latch));
}
// jobExecutor.invokeAll(tasks);
}
processor.archiveFile();
return acquisitionResponse;
}
// actually code processed in each thread
public class ProcessExecutor<T>
{
private AcquisitionPreProcessor preProcessor;
private AcquisitionProcessor processor;
private Converter requestConverter;
private Converter responseConverter;
private MediaManagementService mediaManagementService;
private ContentComparator contentComparator;
private MediaContentDAO mediaContentDao;
public AcquisitionStatus process(T item) throws Exception
{
LOGGER.debug("Processing for Id: " + item.toString()+"Thread: "+Thread.currentThread().getName());
Object response = null;
try {
response = processor.processInternal(requestConverter.convert(item));
List<MediaContent> targetList = ((SupplierContent) responseConverter.convert(response)).getMediaContents();
if(CollectionUtils.isNotEmpty(targetList)){
List<MediaContent> sourceList = mediaContentDao.getAllParentMediaContentsByLobTypeAndValue(targetList.get(0).getContentLobValue(), targetList.get(0).getContentLOBType());
List<MediaContent> listOfComparedMediaContents = contentComparator.compare(sourceList,targetList);
int insertCount = 0,deleteCount = 0;
for(MediaContent mediaContent:listOfComparedMediaContents){
if(mediaContent.getActionType().equals(ActionType.DELETE)){
LOGGER.info("Processing delete");
mediaManagementService.deleteContent(mediaContent);
deleteCount = deleteCount + getNumOfMediaContents(mediaContent);
}
if(mediaContent.getActionType().equals(ActionType.INSERT)){
LOGGER.info("Processing insert");
mediaManagementService.insertContent(mediaContent);
insertCount = insertCount + getNumOfMediaContents(mediaContent);
}
}
if(deleteCount + insertCount > 0){
return new AcquisitionStatus(StatusType.Processed, insertCount, deleteCount);
}
}
}catch(FileNotFoundException e)
{
throw e;
}
catch(Exception e) {
handleException(item, e);
return new AcquisitionStatus(StatusType.Error, e);
}
return null;
}