Search in sources :

Example 1 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskTriggerHandlerTest method shouldNotTriggerErrorWhenFilterDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected.

@Test
public void shouldNotTriggerErrorWhenFilterDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected() throws Exception {
    Map<String, DataProvider> providers = new HashMap<>();
    DataProvider provider = mock(DataProvider.class);
    Map<String, String> lookup = new HashMap<>();
    List<String> filtersManipulations = new ArrayList<>();
    lookup.put("patientId", "123");
    when(provider.lookup("Patient", null, 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);
    Task task = new Task();
    task.setName("task");
    task.setId(44l);
    task.setTrigger(new TaskTriggerInformation("Trigger", "channel", "module", "0.1", "trigger", "listener"));
    task.setHasRegisteredChannel(true);
    task.setActions(Collections.<TaskActionInformation>emptyList());
    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")), false));
    taskConfig.add(new FilterSet(asList(new Filter("Patient ID", "ad.12345.Patient#1.patientId", INTEGER, false, EXIST.getValue(), "", filtersManipulations))));
    List<Task> tasks = asList(task);
    when(taskService.findActiveTasksForTriggerSubject("trigger")).thenReturn(tasks);
    setTaskActivities();
    task.setFailuresInRow(taskActivities.size());
    Map<String, Object> param = new HashMap<>(4);
    param.put("patientId", "123");
    handler.handle(new MotechEvent("trigger", param));
    verify(postExecutionHandler, never()).handleError(anyMap(), anyMap(), eq(task), any(TaskHandlerException.class), eq(TASK_ACTIVITY_ID));
    verify(taskActivityService).addWarning(eq(task), eq("task.warning.notFoundObjectForType"), eq("Patient"));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) HashMap(java.util.HashMap) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) DataProvider(org.motechproject.commons.api.DataProvider) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) Filter(org.motechproject.tasks.domain.mds.task.Filter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 2 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig 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 TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskTriggerHandlerTest method shouldTriggerErrorWhenFilterDoesNotFindDataSourceWithFailIfDataNotFoundSelected.

@Test
public void shouldTriggerErrorWhenFilterDoesNotFindDataSourceWithFailIfDataNotFoundSelected() throws Exception {
    Map<String, DataProvider> providers = new HashMap<>();
    DataProvider provider = mock(DataProvider.class);
    Map<String, String> lookup = new HashMap<>();
    List<String> filtersManipulations = new ArrayList<>();
    lookup.put("patientId", "123");
    when(provider.lookup("Patient", null, 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);
    Task task = new Task();
    task.setName("task");
    task.setId(77l);
    task.setTrigger(new TaskTriggerInformation("Trigger", "channel", "module", "0.1", "trigger", "listener"));
    task.setHasRegisteredChannel(true);
    task.setActions(Collections.<TaskActionInformation>emptyList());
    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));
    taskConfig.add(new FilterSet(asList(new Filter("Patient ID", "ad.12345.Patient#1.patientId", INTEGER, false, EXIST.getValue(), "", filtersManipulations))));
    List<Task> tasks = asList(task);
    when(taskService.findActiveTasksForTriggerSubject("trigger")).thenReturn(tasks);
    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) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) HashMap(java.util.HashMap) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) DataProvider(org.motechproject.commons.api.DataProvider) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) Filter(org.motechproject.tasks.domain.mds.task.Filter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 4 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskBuilderTest method shouldReturnEmptyTaskObject.

@Test
public void shouldReturnEmptyTaskObject() throws Exception {
    TaskBuilder builder = new TaskBuilder();
    Task task = builder.withName(TASK_NAME).withDescription(TASK_DESCRIPTION).isEnabled(isEnabled).withTrigger(new TaskTriggerInformation()).addAction(new TaskActionInformation()).withTaskConfig(new TaskConfig()).addFilterSet(new FilterSet()).addDataSource(new DataSource()).clear().build();
    assertNotNull(task);
    assertTrue(task.getName().isEmpty());
    assertTrue(task.getDescription().isEmpty());
    assertEquals(false, task.isEnabled());
    assertNull(task.getTrigger());
    assertNotNull(task.getActions());
    assertTrue(task.getActions().isEmpty());
    assertNotNull(task.getTaskConfig());
    assertTrue(task.getTaskConfig().getSteps().isEmpty());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 5 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskDeserializer method deserialize.

@Override
public Task deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    jsonNode = jsonParser.readValueAsTree();
    task = new Task();
    ObjectCodec codec = jsonParser.getCodec();
    if (codec instanceof ObjectMapper) {
        mapper = (ObjectMapper) codec;
    } else {
        mapper = new ObjectMapper();
    }
    TypeFactory typeFactory = mapper.getTypeFactory();
    JavaType stringType = typeFactory.constructType(String.class);
    JavaType longType = typeFactory.constructType(Long.class);
    JavaType intType = typeFactory.constructType(Integer.class);
    JavaType dateTime = typeFactory.constructType(DateTime.class);
    JavaType booleanType = typeFactory.constructType(boolean.class);
    JavaType timeType = typeFactory.constructType(Time.class);
    setProperty("id", longType);
    setProperty("owner", stringType);
    setProperty("creator", stringType);
    setProperty("creationDate", dateTime);
    setProperty("modificationDate", dateTime);
    setProperty("modifiedBy", stringType);
    setProperty("description", stringType);
    setProperty("name", stringType);
    setProperty("enabled", stringType);
    setProperty("hasRegisteredChannel", stringType);
    setProperty("taskConfig", typeFactory.constructType(TaskConfig.class));
    setProperty("trigger", typeFactory.constructType(TaskTriggerInformation.class));
    setProperty("numberOfRetries", intType);
    setProperty("retryIntervalInMilliseconds", intType);
    setProperty("retryTaskOnFailure", stringType);
    setProperty("useTimeWindow", booleanType);
    setProperty("startTime", timeType);
    setProperty("endTime", timeType);
    setProperty("validationErrors", typeFactory.constructCollectionType(Set.class, TaskError.class));
    setProperty("actions", typeFactory.constructCollectionType(List.class, TaskActionInformation.class));
    setProperty("days", typeFactory.constructCollectionType(List.class, String.class));
    return task;
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) JavaType(org.codehaus.jackson.type.JavaType) Set(java.util.Set) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) List(java.util.List) ObjectCodec(org.codehaus.jackson.ObjectCodec) TypeFactory(org.codehaus.jackson.map.type.TypeFactory) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)25 Test (org.junit.Test)23 Task (org.motechproject.tasks.domain.mds.task.Task)19 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)17 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)12 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)12 ArrayList (java.util.ArrayList)11 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)10 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)10 Channel (org.motechproject.tasks.domain.mds.channel.Channel)9 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)9 HashMap (java.util.HashMap)8 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)8 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)7 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)7 HashSet (java.util.HashSet)6 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)6 Matchers.anyString (org.mockito.Matchers.anyString)5 QueryExecution (org.motechproject.mds.query.QueryExecution)5 LinkedHashMap (java.util.LinkedHashMap)4