Search in sources :

Example 6 with ActionEventBuilder

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

the class TaskActionExecutorTest method shouldRaiseEventIfActionHasSubject.

@Test
public void shouldRaiseEventIfActionHasSubject() throws ActionNotFoundException, TaskHandlerException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "actionSubject");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setActionParameters(new TreeSet<>()).build();
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    task.setId(11L);
    Map<String, Object> metadata = new HashMap<>();
    metadata.put(EventDataKeys.TASK_ID, 11L);
    metadata.put(EventDataKeys.TASK_RETRY, null);
    metadata.put(EventDataKeys.TASK_ACTIVITY_ID, TASK_ACTIVITY_ID);
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), metadata, activityService), TASK_ACTIVITY_ID);
    MotechEvent raisedEvent = new MotechEvent("actionSubject", new HashMap<>(), TasksEventCallbackService.TASKS_EVENT_CALLBACK_NAME, metadata);
    verify(eventRelay).sendEventMessage(raisedEvent);
}
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) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) TreeSet(java.util.TreeSet) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 7 with ActionEventBuilder

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

the class TaskActionExecutorTest method shouldNotRaiseEventIfActionHasSubjectAndService_IfServiceIsAvailable.

@Test
public void shouldNotRaiseEventIfActionHasSubjectAndService_IfServiceIsAvailable() throws ActionNotFoundException, TaskHandlerException {
    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();
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    ServiceReference serviceReference = mock(ServiceReference.class);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(serviceReference);
    when(bundleContext.getService(serviceReference)).thenReturn(new 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);
    verify(eventRelay, never()).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) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 8 with ActionEventBuilder

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

the class TaskServiceImplTest method shouldValidateTasksOfDependentModulesAfterChannelUpdateForInvalidTriggers.

@Test
public void shouldValidateTasksOfDependentModulesAfterChannelUpdateForInvalidTriggers() {
    Task task = new Task("name", trigger, new ArrayList<>(asList(action)), new TaskConfig(), true, false);
    task.setId(6l);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    when(tasksDataService.findById(6l)).thenReturn(task);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SENDING", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("schedule").setSubject("SCHEDULE").setDescription("").setActionParameters(null).build()));
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    Set<TaskError> triggerValidationErrors = new HashSet<>();
    triggerValidationErrors.add(new TaskError("task.validation.error.triggerNotExist", trigger.getDisplayName()));
    when(triggerEventService.validateTrigger(eq(trigger))).thenReturn(triggerValidationErrors);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(trigger));
    Task actualTask = verifyUpdateAndCaptureTask(times(2));
    assertFalse(actualTask.isEnabled());
    List<Object> errors = new ArrayList<Object>(actualTask.getValidationErrors());
    assertEquals(1, errors.size());
    assertThat(errors, hasItem(hasProperty("message", equalTo("task.validation.error.triggerNotExist"))));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) QueryExecution(org.motechproject.mds.query.QueryExecution) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with ActionEventBuilder

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

the class TaskServiceImplTest method shouldSaveTaskWithEmptyActionInputFields.

@Test
public void shouldSaveTaskWithEmptyActionInputFields() {
    Task task = new Task("name", trigger, asList(action), null, false, false);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build()));
    TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", asList(new LookupFieldsParameter("id", asList("id"))), null)));
    provider.setId(1234L);
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    when(providerService.getProviderById(1234L)).thenReturn(provider);
    taskService.save(task);
    verify(triggerHandler).registerHandlerFor(task.getTrigger().getEffectiveListenerSubject());
    // When task has not set number of retries, it should not register handler for retries
    verifyNoMoreInteractions(triggerHandler);
    verifyCreateAndCaptureTask();
}
Also used : EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) Test(org.junit.Test)

Example 10 with ActionEventBuilder

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

the class TaskServiceImplTest method shouldNotSaveTaskIfNotMatchExtraValidationConditions.

@Test(expected = ValidationException.class)
public void shouldNotSaveTaskIfNotMatchExtraValidationConditions() {
    Map<String, String> map = new HashMap<>();
    map.put("phone", "12345");
    TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
    action.setValues(map);
    Task task = new Task("name", trigger, asList(action), config, true, false);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SENDING", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build()));
    TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", asList(new LookupFieldsParameter("id", asList("id"))), null)));
    provider.setId(1234L);
    Set<TaskError> errors = new HashSet<>();
    errors.add(new TaskError("task.validation.error.triggerNotExist", trigger.getDisplayName()));
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    when(providerService.getProviderById(1234L)).thenReturn(provider);
    when(triggerEventService.validateTrigger(trigger)).thenReturn(errors);
    taskService.save(task);
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) HashSet(java.util.HashSet) Test(org.junit.Test)

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