Search in sources :

Example 11 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig 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 12 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig 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)

Example 13 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskServiceImplTest method shouldFailValidationIfTriggerChannelIsNotRegistered.

@Test
public void shouldFailValidationIfTriggerChannelIsNotRegistered() {
    TaskTriggerInformation trigger = new TaskTriggerInformation("triggerDisplay", "triggerChannel", "triggerModule", "1.0", "subject", "triggerListenerSubject");
    TaskActionInformation action = new TaskActionInformation("actionDisplay", "actionChannel", "actionModule", "1.0", "subject");
    Task fooTask = new TaskBuilder().withName("foo").withTrigger(trigger).withTaskConfig(new TaskConfig()).addAction(action).build();
    fooTask.setEnabled(true);
    when(channelService.getChannel("foo-module")).thenReturn(null);
    when(channelService.getChannel("actionModule")).thenReturn(null);
    Set<TaskError> errors = new HashSet<>();
    errors.add(new TaskError("task.validation.error.triggerChannelNotRegistered"));
    when(triggerEventService.validateTrigger(trigger)).thenReturn(errors);
    expectedException.expect(ValidationException.class);
    expectedException.expect(new TypeSafeMatcher<ValidationException>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matchesSafely(ValidationException actualException) {
            final TaskErrorDto triggerChannelError = new TaskErrorDto("task.validation.error.triggerChannelNotRegistered");
            final TaskErrorDto actionChannelError = new TaskErrorDto("task.validation.error.actionChannelNotRegistered");
            Set<TaskErrorDto> taskErrors = actualException.getTaskErrors();
            return taskErrors.contains(triggerChannelError) && taskErrors.contains(actionChannelError);
        }
    });
    taskService.save(fooTask);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) ValidationException(org.motechproject.tasks.exception.ValidationException) Description(org.hamcrest.Description) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) Set(java.util.Set) HashSet(java.util.HashSet) TaskErrorDto(org.motechproject.tasks.dto.TaskErrorDto) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskConfigTest method shouldRemoveAllSteps.

@Test
public void shouldRemoveAllSteps() throws Exception {
    TaskConfig config = new TaskConfig().add(new FilterSet(), new FilterSet(), new FilterSet()).add(new DataSource(), new DataSource(), new DataSource());
    assertNotNull(config.getSteps());
    assertFalse(config.getSteps().isEmpty());
    config.removeAll();
    assertNotNull(config.getSteps());
    assertTrue(config.getSteps().isEmpty());
}
Also used : FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 15 with TaskConfig

use of org.motechproject.tasks.domain.mds.task.TaskConfig in project motech by motech.

the class TaskConfigTest method shouldAddAllStepsAndSetNewOrderNumber.

@Test
public void shouldAddAllStepsAndSetNewOrderNumber() throws Exception {
    List<TaskConfigStep> steps = new ArrayList<>();
    steps.add(new FilterSet());
    steps.add(new DataSource());
    steps.add(new DataSource());
    steps.add(new FilterSet());
    Random random = new Random();
    for (TaskConfigStep step : steps) {
        step.setOrder(random.nextInt());
    }
    TaskConfig config = new TaskConfig().addAll(null).addAll(new TreeSet<TaskConfigStep>()).addAll(new TreeSet<>(steps));
    List<Integer> orders = new ArrayList<>(steps.size());
    for (TaskConfigStep step : config.getSteps()) {
        orders.add(step.getOrder());
    }
    assertThat(orders, hasItems(0, 1, 2, 3));
}
Also used : TaskConfigStep(org.motechproject.tasks.domain.mds.task.TaskConfigStep) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) Random(java.util.Random) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Aggregations

TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)25 Test (org.junit.Test)23 Task (org.motechproject.tasks.domain.mds.task.Task)19 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)17 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)12 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)12 ArrayList (java.util.ArrayList)11 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)10 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)10 Channel (org.motechproject.tasks.domain.mds.channel.Channel)9 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)9 HashMap (java.util.HashMap)8 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)8 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)7 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)7 HashSet (java.util.HashSet)6 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)6 Matchers.anyString (org.mockito.Matchers.anyString)5 QueryExecution (org.motechproject.mds.query.QueryExecution)5 LinkedHashMap (java.util.LinkedHashMap)4