use of org.motechproject.event.MotechEvent in project motech by motech.
the class TaskTriggerHandlerTest method shouldNotScheduleTaskRetriesWhenTaskIsDeleted.
@Test
public void shouldNotScheduleTaskRetriesWhenTaskIsDeleted() throws Exception {
setTriggerEvent();
setActionEvent();
task.setNumberOfRetries(5);
task.setRetryIntervalInMilliseconds(5000);
actionEvent.setServiceInterface("TestService");
actionEvent.setServiceMethod("abc");
when(taskService.getTask(5L)).thenReturn(null);
MotechEvent event = createEvent();
event.getParameters().put(EventDataKeys.TASK_ID, 5L);
event.getParameters().put(EventDataKeys.TASK_RETRY, true);
event.getParameters().put(EventDataKeys.JOB_SUBJECT, task.getTrigger().getEffectiveListenerRetrySubject());
handler.setBundleContext(bundleContext);
handler.handleRetry(event);
verify(retryHandler, never()).handleTaskRetries(task, event.getParameters());
}
use of org.motechproject.event.MotechEvent 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"));
}
use of org.motechproject.event.MotechEvent 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));
}
use of org.motechproject.event.MotechEvent 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));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TaskTriggerHandlerTest method shouldExecuteTwoActions.
@Test
public void shouldExecuteTwoActions() throws Exception {
setTriggerEvent();
setActionEvent();
setSecondAction();
when(taskService.findActiveTasksForTriggerSubject(TRIGGER_SUBJECT)).thenReturn(tasks);
when(taskService.getActionEventFor(any(TaskActionInformation.class))).thenReturn(actionEvent);
ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class);
handler.handle(createEvent());
verify(taskService).findActiveTasksForTriggerSubject(TRIGGER_SUBJECT);
verify(taskService).getActionEventFor(task.getActions().get(0));
verify(taskService).getActionEventFor(task.getActions().get(1));
verify(eventRelay, times(2)).sendEventMessage(captor.capture());
List<MotechEvent> events = captor.getAllValues();
assertEquals(asList(ACTION_SUBJECT, ACTION_SUBJECT), extract(events, on(MotechEvent.class).getSubject()));
MotechEvent motechEventAction1 = events.get(0);
assertEquals(ACTION_SUBJECT, motechEventAction1.getSubject());
assertNotNull(motechEventAction1.getParameters());
assertEquals(2, motechEventAction1.getParameters().size());
assertEquals(task.getActions().get(0).getValues().get("phone"), motechEventAction1.getParameters().get("phone").toString());
assertEquals("Hello 123456789, You have an appointment on 2012-11-20", motechEventAction1.getParameters().get("message"));
MotechEvent motechEventAction2 = events.get(1);
assertEquals(ACTION_SUBJECT, motechEventAction2.getSubject());
assertNotNull(motechEventAction2.getParameters());
assertEquals(2, motechEventAction2.getParameters().size());
assertEquals(task.getActions().get(0).getValues().get("phone"), motechEventAction2.getParameters().get("phone").toString());
assertEquals("Hello, world! I'm second action", motechEventAction2.getParameters().get("message"));
}
Aggregations