TaskActionExecutor { @Transactional public void execute(Task task, TaskActionInformation actionInformation, Integer actionIndex, TaskContext taskContext, long activityId) throws TaskHandlerException { LOGGER.info("Executing task action: {} from task: {}", actionInformation.getName(), task.getName()); KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext); ActionEvent action = getActionEvent(actionInformation); Map<String, Object> parameters = createParameters(actionInformation, action, keyEvaluator); addTriggerParameters(task, action, parameters, taskContext.getTriggerParameters()); LOGGER.debug("Parameters created: {} for task action: {}", parameters.toString(), action.getName()); if (action.hasService() && bundleContext != null) { if (callActionServiceMethod(action, actionIndex, parameters, taskContext)) { LOGGER.info("Action: {} from task: {} was executed through an OSGi service call", actionInformation.getName(), task.getName()); postExecutionHandler.handleActionExecuted(taskContext.getTriggerParameters(), taskContext.getMetadata(), activityId); return; } LOGGER.info("There is no service: {}", action.getServiceInterface()); activityService.addWarning(task, "task.warning.serviceUnavailable", action.getServiceInterface()); } if (!action.hasSubject()) { throw new TaskHandlerException(ACTION, "task.error.cantExecuteAction"); } else { eventRelay.sendEventMessage(new MotechEvent(action.getSubject(), parameters, TasksEventCallbackService.TASKS_EVENT_CALLBACK_NAME, taskContext.getMetadata())); LOGGER.info("Event: {} was sent", action.getSubject()); } }  @Transactional void execute(Task task, TaskActionInformation actionInformation, Integer actionIndex, TaskContext taskContext, long activityId); void setBundleContext(BundleContext bundleContext); @Autowired void setEventRelay(EventRelay eventRelay); @Autowired void setTaskService(TaskService taskService); @Autowired void setActivityService(TaskActivityService activityService); @Autowired void setTasksPostExecutionHandler(TasksPostExecutionHandler postExecutionHandler);  }
@Test(expected = TaskHandlerException.class) public void shouldThrowExceptionIfBundleContextIsNotAvailable() throws TaskHandlerException, ActionNotFoundException { TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod"); ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action") .setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod") .setActionParameters(new TreeSet<>()).build(); actionEvent.setActionParameters(new TreeSet<>()); when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent); Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build(); taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID); }
@Test(expected = TaskHandlerException.class) public void shouldThrowExceptionIfActionHasNeitherEventNorService() throws TaskHandlerException, ActionNotFoundException { TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod"); ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action") .setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod") .setActionParameters(new TreeSet<>()).build(); actionEvent.setActionParameters(new TreeSet<>()); when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent); Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build(); taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID); }