Search in sources :

Example 26 with TriggerEvent

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

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForValidTriggers.

@Test
public void shouldValidateTasksAfterChannelUpdateForValidTriggers() {
    Task task = new Task("name", trigger, asList(action), new TaskConfig(), true, false);
    Set<TaskError> existingErrors = new HashSet<>();
    existingErrors.add(new TaskError("task.validation.error.triggerNotExist"));
    task.addValidationErrors(existingErrors);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    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("schedule").setSubject("SCHEDULE").setDescription("").setActionParameters(null).build()));
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    when(triggerEventService.triggerExists(task.getTrigger())).thenReturn(true);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(trigger));
    Task actualTask = verifyCreateAndCaptureTask();
    assertTrue(actualTask.isEnabled());
    assertTrue(actualTask.getValidationErrors().isEmpty());
}
Also used : EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) 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) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) QueryExecution(org.motechproject.mds.query.QueryExecution) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with TriggerEvent

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

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForInvalidActions.

@Test
public void shouldValidateTasksAfterChannelUpdateForInvalidActions() {
    Task task = new Task("name", trigger, new ArrayList<>(asList(action)), new TaskConfig(), true, false);
    task.setId(124l);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    when(tasksDataService.findById(124l)).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);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(action));
    Task actualTask = verifyUpdateAndCaptureTask();
    assertFalse(actualTask.isEnabled());
    List<Object> errors = new ArrayList<>(actualTask.getValidationErrors());
    assertEquals(1, errors.size());
    assertThat(errors, hasItem(hasProperty("message", equalTo("task.validation.error.actionNotExist"))));
}
Also used : EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) QueryExecution(org.motechproject.mds.query.QueryExecution) Test(org.junit.Test)

Example 28 with TriggerEvent

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

the class TaskServiceImplTest method shouldSaveTask.

@Test
public void shouldSaveTask() {
    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);
    task.setNumberOfRetries(5);
    task.setRetryTaskOnFailure(true);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build();
    actionEvent.addParameter(new ActionParameterBuilder().setDisplayName("Phone").setKey("phone").build(), true);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(actionEvent));
    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.getProvider("TestProvider")).thenReturn(provider);
    when(triggerEventService.triggerExists(task.getTrigger())).thenReturn(true);
    taskService.save(task);
    verify(triggerHandler).registerHandlerFor(task.getTrigger().getEffectiveListenerSubject());
    // Because task has set number of retries to 5, it should register retries handler for this task
    verify(triggerHandler).registerHandlerFor(task.getTrigger().getEffectiveListenerRetrySubject(), true);
    verifyCreateAndCaptureTask();
}
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) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) 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) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) Test(org.junit.Test)

Example 29 with TriggerEvent

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

the class TaskServiceImplTest method shouldNotFindTasksForGivenTrigger.

@Test
public void shouldNotFindTasksForGivenTrigger() {
    TriggerEvent triggerEvent = new TriggerEvent();
    triggerEvent.setSubject(trigger.getSubject());
    List<Task> tasks = taskService.findActiveTasksForTrigger(triggerEvent);
    assertNotNull(tasks);
    assertTrue(tasks.isEmpty());
    triggerEvent.setSubject(null);
    tasks = taskService.findActiveTasksForTrigger(triggerEvent);
    assertNotNull(tasks);
    assertTrue(tasks.isEmpty());
    tasks = taskService.findActiveTasksForTriggerSubject(null);
    assertNotNull(tasks);
    assertTrue(tasks.isEmpty());
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Test(org.junit.Test)

Example 30 with TriggerEvent

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

the class TaskServiceImplTest method shouldFindTasksForGivenTrigger.

@Test
public void shouldFindTasksForGivenTrigger() {
    Task t = new Task("name", trigger, asList(action));
    TriggerEvent triggerEvent = new TriggerEvent();
    triggerEvent.setSubject(trigger.getSubject());
    when(tasksDataService.executeQuery(Matchers.<QueryExecution<Object>>any())).thenReturn(asList(t));
    List<Task> tasks = taskService.findActiveTasksForTrigger(triggerEvent);
    assertEquals(asList(t), tasks);
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Test(org.junit.Test)

Aggregations

TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)34 Test (org.junit.Test)24 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)22 ArrayList (java.util.ArrayList)17 Task (org.motechproject.tasks.domain.mds.task.Task)16 Channel (org.motechproject.tasks.domain.mds.channel.Channel)13 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)12 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)12 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)9 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)9 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)9 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)8 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)6 MotechEvent (org.motechproject.event.MotechEvent)5 LookupFieldsParameter (org.motechproject.tasks.domain.mds.task.LookupFieldsParameter)5 Matchers.anyString (org.mockito.Matchers.anyString)4