Search in sources :

Example 1 with ActionEventBuilder

use of org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder 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 ActionEventBuilder

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

the class TaskActionExecutorTest method shouldRaiseEventWhenActionHasSubjectAndService_IfServiceIsNotAvailable.

@Test
public void shouldRaiseEventWhenActionHasSubjectAndService_IfServiceIsNotAvailable() throws TaskHandlerException, ActionNotFoundException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<ActionParameter>()).build();
    actionEvent.setActionParameters(new TreeSet<>());
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(null);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    verify(eventRelay).sendEventMessage(any(MotechEvent.class));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 3 with ActionEventBuilder

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

the class TaskActionExecutorTest method shouldInvokeServiceIfActionHasService.

@Test
public void shouldInvokeServiceIfActionHasService() throws ActionNotFoundException, TaskHandlerException {
    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();
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    ServiceReference serviceReference = mock(ServiceReference.class);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(serviceReference);
    TestService testService = new TestService();
    when(bundleContext.getService(serviceReference)).thenReturn(testService);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    assertTrue(testService.serviceMethodInvoked());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 4 with ActionEventBuilder

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

the class TaskActionExecutorTest method shouldAddActivityNotificationIfServiceIsNotAvailable.

@Test
public void shouldAddActivityNotificationIfServiceIsNotAvailable() throws TaskHandlerException, ActionNotFoundException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<>()).build();
    actionEvent.setActionParameters(new TreeSet<>());
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(null);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    verify(activityService).addWarning(task, "task.warning.serviceUnavailable", "serviceInterface");
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 5 with ActionEventBuilder

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

the class TaskActionExecutorTest method prepareActionEventWithService.

private ActionEvent prepareActionEventWithService() {
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").build();
    SortedSet<ActionParameter> parameters = new TreeSet<>();
    ActionParameter parameter = new ActionParameter();
    parameter.setDisplayName("test");
    parameter.setKey("testKey");
    parameter.setType(TEXTAREA);
    parameter.setOrder(0);
    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) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter)

Aggregations

ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)26 Test (org.junit.Test)23 Task (org.motechproject.tasks.domain.mds.task.Task)19 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)18 Channel (org.motechproject.tasks.domain.mds.channel.Channel)14 HashMap (java.util.HashMap)12 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)12 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)12 TreeSet (java.util.TreeSet)11 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)9 ArrayList (java.util.ArrayList)8 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)8 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)8 ObjectTest (org.motechproject.tasks.domain.ObjectTest)7 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)7 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)7 TaskContext (org.motechproject.tasks.service.util.TaskContext)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)6 MotechEvent (org.motechproject.event.MotechEvent)5