Search in sources :

Example 1 with ActionParameter

use of org.motechproject.tasks.domain.mds.channel.ActionParameter 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 2 with ActionParameter

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

the class MethodHandler method initForNamedParametersCall.

private void initForNamedParametersCall(Map<String, Object> parameters, SortedSet<ActionParameter> actionParameters) {
    if (!actionParameters.isEmpty()) {
        classes = new Class[parameters.size()];
        objects = new Object[parameters.size()];
        for (ActionParameter actionParameter : actionParameters) {
            Object obj = parameters.get(actionParameter.getKey());
            Integer idx = actionParameter.getOrder();
            objects[idx] = obj;
            if (obj instanceof Map) {
                classes[idx] = Map.class;
            } else if (obj instanceof List) {
                classes[idx] = List.class;
            } else if (obj != null) {
                classes[idx] = obj.getClass();
            } else if (actionParameter.getType() != null) {
                classes[idx] = actionParameter.getType().getUnderlyingClass();
            } else {
                classes[idx] = Object.class;
            }
        }
    } else {
        classes = new Class[0];
        objects = new Object[0];
    }
}
Also used : List(java.util.List) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) Map(java.util.Map)

Example 3 with ActionParameter

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

the class TaskAnnotationBeanPostProcessor method getActionParams.

private SortedSet<ActionParameter> getActionParams(Method method) {
    SortedSet<ActionParameter> parameters = new TreeSet<>();
    int order = 0;
    for (Annotation[] annotations : method.getParameterAnnotations()) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof TaskActionParam) {
                LOGGER.debug("The @TaskActionParam annotation was found in parameters from method: {}", method.getName());
                TaskActionParam param = (TaskActionParam) annotation;
                LOGGER.debug("Task action parameter: {} added", param.displayName());
                parameters.add(new ActionParameterBuilder().setDisplayName(param.displayName()).setKey(param.key()).setType(param.type()).setOrder(order).setRequired(param.required()).build());
                ++order;
            }
        }
    }
    return parameters.isEmpty() ? null : parameters;
}
Also used : ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) TreeSet(java.util.TreeSet) TaskActionParam(org.motechproject.tasks.annotations.TaskActionParam) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) Annotation(java.lang.annotation.Annotation)

Example 4 with ActionParameter

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

the class TaskAnnotationBeanPostProcessor method getPostActionParams.

private SortedSet<ActionParameter> getPostActionParams(Method method) {
    SortedSet<ActionParameter> parameters = new TreeSet<>();
    int order = 0;
    for (Annotation[] annotations : method.getParameterAnnotations()) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof TaskPostActionParam) {
                LOGGER.debug("The @TaskPostActionParam annotation was found in parameters from method: {}", method.getName());
                TaskPostActionParam param = (TaskPostActionParam) annotation;
                LOGGER.debug("Task action parameter: {} added", param.displayName());
                parameters.add(new ActionParameterBuilder().setDisplayName(param.displayName()).setKey(param.key()).setType(param.type()).setOrder(order).setRequired(param.required()).build());
                ++order;
            }
        }
    }
    return parameters.isEmpty() ? null : parameters;
}
Also used : ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) TreeSet(java.util.TreeSet) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) TaskPostActionParam(org.motechproject.tasks.annotations.TaskPostActionParam) Annotation(java.lang.annotation.Annotation)

Example 5 with ActionParameter

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

the class TaskActionExecutorTest method prepareActionEvent.

private ActionEvent prepareActionEvent() {
    ActionEvent actionEvent = new ActionEvent();
    actionEvent.setDisplayName("Action");
    actionEvent.setSubject("actionSubject");
    actionEvent.setDescription("");
    SortedSet<ActionParameter> parameters = new TreeSet<>();
    ActionParameter parameter = new ActionParameter();
    parameter.setDisplayName("Map");
    parameter.setKey("map");
    parameter.setType(MAP);
    parameter.setOrder(1);
    parameters.add(parameter);
    actionEvent.setActionParameters(parameters);
    actionEvent.setPostActionParameters(parameters);
    return actionEvent;
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter)

Aggregations

ActionParameter (org.motechproject.tasks.domain.mds.channel.ActionParameter)12 TreeSet (java.util.TreeSet)8 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)6 HashMap (java.util.HashMap)4 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)4 ArrayList (java.util.ArrayList)3 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)3 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)3 Annotation (java.lang.annotation.Annotation)2 List (java.util.List)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 DataProvider (org.motechproject.commons.api.DataProvider)2 MotechEvent (org.motechproject.event.MotechEvent)2 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)2 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)2 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)2 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)2 Task (org.motechproject.tasks.domain.mds.task.Task)2 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)2