use of org.motechproject.tasks.domain.mds.channel.EventParameter in project motech by motech.
the class SchedulerChannelProvider method getTriggers.
@Override
public List<TriggerEvent> getTriggers(int page, int pageSize) {
try (Connection conn = DBConnectionManager.getInstance().getConnection(sqlProperties.getProperty(DATA_SOURCE));
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(buildGetTriggersQuery(page, pageSize));
List<TriggerEvent> triggers = new ArrayList<>();
while (rs.next()) {
try (InputStream is = new ByteArrayInputStream(rs.getBytes(JOB_DATA));
ObjectInputStream ois = new ObjectInputStream(is)) {
JobDataMap dataMap = (JobDataMap) ois.readObject();
List<EventParameter> parameters = new ArrayList<>();
parameters.add(new EventParameter("scheduler.jobId", MotechSchedulerService.JOB_ID_KEY));
triggers.add(new TriggerEvent("Job: " + rs.getString(JOB_NAME), rs.getString(JOB_NAME), rs.getString(JOB_DESCRIPTION), parameters, dataMap.getString(SchedulerConstants.EVENT_TYPE_KEY_NAME)));
}
}
return triggers;
} catch (SQLException | IOException | ClassNotFoundException e) {
throw new TriggerRetrievalException("Couldn't retrieve triggers for Scheduler channel", e);
}
}
use of org.motechproject.tasks.domain.mds.channel.EventParameter in project motech by motech.
the class ChannelServiceImplTest method shouldSendEventWhenChannelWasUpdated.
@Test
public void shouldSendEventWhenChannelWasUpdated() {
Channel channel = new Channel("displayName", BUNDLE_SYMBOLIC_NAME, VERSION);
EventParameter eventParameter = new EventParameter("displayName", "eventKey");
TriggerEvent triggerEvent = new TriggerEvent("displayName", "subject", null, Arrays.asList(eventParameter), "");
channel.getTriggerTaskEvents().add(triggerEvent);
when(channelsDataService.findByModuleName(channel.getModuleName())).thenReturn(channel);
when(bundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
when(bundle.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC_NAME);
ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class);
Channel updatedChannel = new Channel("displayName2", BUNDLE_SYMBOLIC_NAME, VERSION);
updatedChannel.getTriggerTaskEvents().add(triggerEvent);
channelService.addOrUpdate(updatedChannel);
verify(channelsDataService).update(channel);
verify(eventRelay).sendEventMessage(captor.capture());
MotechEvent event = captor.getValue();
assertEquals(CHANNEL_UPDATE_SUBJECT, event.getSubject());
assertEquals(BUNDLE_SYMBOLIC_NAME, event.getParameters().get(CHANNEL_MODULE_NAME));
}
use of org.motechproject.tasks.domain.mds.channel.EventParameter 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.tasks.domain.mds.channel.EventParameter 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.tasks.domain.mds.channel.EventParameter 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));
}
Aggregations