Search in sources :

Example 1 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent in project motech by motech.

the class MethodHandlerTest method shouldConstructMethodHandlerForNamedParametersMethodCallByDefault.

@Test
public void shouldConstructMethodHandlerForNamedParametersMethodCallByDefault() throws TaskHandlerException {
    ActionEvent action = getActionEvent(MethodCallManner.NAMED_PARAMETERS);
    action.setServiceMethodCallManner(null);
    Map<String, Object> parameters = getParameters();
    Class[] expectedClassArray = { String.class, Integer.class, Long.class, Double.class, Boolean.class, DateTime.class, Map.class, List.class };
    Object[] expectedObjectArray = { STRING_VALUE, INTEGER_VALUE, LONG_VALUE, DOUBLE_VALUE, BOOLEAN_VALUE, DATETIME_VALUE, MAP_VALUE, LIST_VALUE };
    MethodHandler methodHandler = new MethodHandler(action, parameters);
    assertArrayEquals(expectedClassArray, methodHandler.getClasses());
    assertArrayEquals(expectedObjectArray, methodHandler.getObjects());
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Test(org.junit.Test)

Example 2 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent in project motech by motech.

the class TaskTriggerHandlerTest method shouldTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundSelected.

@Test
public void shouldTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundSelected() throws Exception {
    Map<String, DataProvider> providers = new HashMap<>();
    DataProvider provider = mock(DataProvider.class);
    Map<String, String> lookup = new HashMap<>();
    lookup.put("patientId", "123");
    when(provider.lookup("Patient", "", lookup)).thenReturn(null);
    providers.put(TASK_DATA_PROVIDER_NAME, provider);
    handler.setDataProviders(providers);
    TriggerEvent trigger = new TriggerEvent();
    trigger.setSubject("trigger");
    List<EventParameter> triggerEventParameters = new ArrayList<>();
    triggerEventParameters.add(new EventParameter("patientId", "123"));
    trigger.setEventParameters(triggerEventParameters);
    ActionEvent action = new ActionEventBuilder().build();
    action.setSubject("action");
    SortedSet<ActionParameter> actionEventParameters = new TreeSet<>();
    actionEventParameters.add(new ActionParameterBuilder().setDisplayName("Patient ID").setKey("patientId").setType(UNICODE).setOrder(0).build());
    action.setActionParameters(actionEventParameters);
    Task task = new Task();
    task.setName("task");
    task.setTrigger(new TaskTriggerInformation("Trigger", "channel", "module", "0.1", "trigger", "listener"));
    Map<String, String> actionValues = new HashMap<>();
    actionValues.put("patientId", "{{ad.providerId.Patient#1.patientId}}");
    task.addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "action", actionValues));
    task.setId(44l);
    task.setHasRegisteredChannel(true);
    TaskConfig taskConfig = new TaskConfig();
    task.setTaskConfig(taskConfig);
    taskConfig.add(new DataSource(TASK_DATA_PROVIDER_NAME, 4L, 1L, "Patient", "provider", "specifiedName", asList(new Lookup("patientId", "trigger.patientId")), true));
    List<Task> tasks = asList(task);
    when(taskService.findActiveTasksForTriggerSubject("trigger")).thenReturn(tasks);
    when(taskService.getActionEventFor(task.getActions().get(0))).thenReturn(action);
    setTaskActivities();
    task.setFailuresInRow(taskActivities.size());
    Map<String, Object> param = new HashMap<>(4);
    param.put("patientId", "123");
    handler.handle(new MotechEvent("trigger", param));
    verify(postExecutionHandler).handleError(anyMap(), anyMap(), eq(task), any(TaskHandlerException.class), eq(TASK_ACTIVITY_ID));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(org.motechproject.commons.api.DataProvider) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) TreeSet(java.util.TreeSet) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) MotechEvent(org.motechproject.event.MotechEvent) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) Test(org.junit.Test)

Example 3 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent in project motech by motech.

the class TaskServiceImpl method validateAction.

private Set<TaskError> validateAction(Task task, Channel channel, TaskActionInformation action) {
    LOGGER.debug("Validating task action: {} from task: {} with ID: {}", action.getName(), task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    if (channel == null) {
        errors.add(new TaskError("task.validation.error.actionChannelNotRegistered"));
        return errors;
    }
    if (channel.getModuleName().equalsIgnoreCase(action.getModuleName())) {
        errors.addAll(taskValidator.validateAction(action, channel));
        TriggerEvent trigger = triggerEventService.getTrigger(task.getTrigger());
        Map<Long, TaskDataProvider> providers = getProviders(task);
        ActionEvent actionEvent = channel.getAction(action);
        if (actionEvent != null) {
            errors.addAll(taskValidator.validateActionFields(action, actionEvent, trigger, providers));
        }
    }
    logResultOfValidation("task action", task.getName(), errors);
    return errors;
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet)

Example 4 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent in project motech by motech.

the class TaskServiceImpl method getActionEventFor.

@Override
@Transactional
public ActionEvent getActionEventFor(TaskActionInformation taskActionInformation) throws ActionNotFoundException {
    Channel channel = channelService.getChannel(taskActionInformation.getModuleName());
    ActionEvent event = null;
    for (ActionEvent action : channel.getActionTaskEvents()) {
        if (action.accept(taskActionInformation)) {
            event = action;
            break;
        }
    }
    if (event == null) {
        throw new ActionNotFoundException(format("Cant find action on the basic of information: %s", taskActionInformation));
    }
    return event;
}
Also used : ActionNotFoundException(org.motechproject.tasks.exception.ActionNotFoundException) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) HandlerPredicates.tasksWithRegisteredChannel(org.motechproject.tasks.service.util.HandlerPredicates.tasksWithRegisteredChannel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent in project motech by motech.

the class AbstractTaskBundleIT method findActionEventBySubject.

protected ActionEvent findActionEventBySubject(List<ActionEvent> actionEvents, String subject) {
    TaskEvent taskEvent = findTaskEventBySubject(actionEvents, subject);
    assertTrue(taskEvent instanceof ActionEvent);
    return (ActionEvent) taskEvent;
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TaskEvent(org.motechproject.tasks.domain.mds.channel.TaskEvent)

Aggregations

ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)32 Test (org.junit.Test)21 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)18 Task (org.motechproject.tasks.domain.mds.task.Task)13 TreeSet (java.util.TreeSet)12 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)12 HashMap (java.util.HashMap)10 ObjectTest (org.motechproject.tasks.domain.ObjectTest)9 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)9 Channel (org.motechproject.tasks.domain.mds.channel.Channel)8 TaskContext (org.motechproject.tasks.service.util.TaskContext)8 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)7 MotechEvent (org.motechproject.event.MotechEvent)6 ArrayList (java.util.ArrayList)5 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)5 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)5 ActionParameter (org.motechproject.tasks.domain.mds.channel.ActionParameter)4 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)4 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)4 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)3