@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")); }
@Override @Transactional public void handle(MotechEvent event) { LOGGER.info("Handling the motech event with subject: {}", event.getSubject()); Map<String, Object> eventParams = event.getParameters(); eventParams.putAll(event.getMetadata()); TasksEventParser parser = null; if (eventParams != null) { parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY)); } String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams); Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams); List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject); for (Task task : tasks) { checkAndHandleTask(task, parameters); } }
TaskTriggerHandler implements TriggerHandler { @Override @Transactional public void handle(MotechEvent event) { LOGGER.info("Handling the motech event with subject: {}", event.getSubject()); Map<String, Object> eventParams = event.getParameters(); eventParams.putAll(event.getMetadata()); TasksEventParser parser = null; if (eventParams != null) { parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY)); } String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams); Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams); List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject); for (Task task : tasks) { checkAndHandleTask(task, parameters); } } }
TaskTriggerHandler implements TriggerHandler { @Override @Transactional public void handle(MotechEvent event) { LOGGER.info("Handling the motech event with subject: {}", event.getSubject()); Map<String, Object> eventParams = event.getParameters(); eventParams.putAll(event.getMetadata()); TasksEventParser parser = null; if (eventParams != null) { parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY)); } String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams); Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams); List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject); for (Task task : tasks) { checkAndHandleTask(task, parameters); } }  }
TaskTriggerHandler implements TriggerHandler { @Override @Transactional public void handle(MotechEvent event) { LOGGER.info("Handling the motech event with subject: {}", event.getSubject()); Map<String, Object> eventParams = event.getParameters(); eventParams.putAll(event.getMetadata()); TasksEventParser parser = null; if (eventParams != null) { parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY)); } String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams); Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams); List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject); for (Task task : tasks) { checkAndHandleTask(task, parameters); } }  @PostConstruct void init(); @PreDestroy void preDestroy(); @Override void registerHandlerFor(String subject); @Override void registerHandlerFor(String subject, boolean isRetryHandler); @Override @Transactional void handle(MotechEvent event); @Override @Transactional void handleRetry(MotechEvent event); @Override @Transactional void retryTask(Long activityId); @Override void addDataProvider(DataProvider provider); @Override void removeDataProvider(String taskDataProviderId); @Autowired void setBundleContext(BundleContext bundleContext); }
TaskTriggerHandler implements TriggerHandler { @Override @Transactional public void handle(MotechEvent event) { LOGGER.info("Handling the motech event with subject: {}", event.getSubject()); Map<String, Object> eventParams = event.getParameters(); eventParams.putAll(event.getMetadata()); TasksEventParser parser = null; if (eventParams != null) { parser = taskService.findCustomParser((String) eventParams.get(TasksEventParser.CUSTOM_PARSER_EVENT_KEY)); } String triggerSubject = parser == null ? event.getSubject() : parser.parseEventSubject(event.getSubject(), eventParams); Map<String, Object> parameters = parser == null ? eventParams : parser.parseEventParameters(event.getSubject(), eventParams); List<Task> tasks = taskService.findActiveTasksForTriggerSubject(triggerSubject); for (Task task : tasks) { checkAndHandleTask(task, parameters); } }  @PostConstruct void init(); @PreDestroy void preDestroy(); @Override void registerHandlerFor(String subject); @Override void registerHandlerFor(String subject, boolean isRetryHandler); @Override @Transactional void handle(MotechEvent event); @Override @Transactional void handleRetry(MotechEvent event); @Override @Transactional void retryTask(Long activityId); @Override void addDataProvider(DataProvider provider); @Override void removeDataProvider(String taskDataProviderId); @Autowired void setBundleContext(BundleContext bundleContext);  }