Search in sources :

Example 11 with Lookup

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

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForInvalidTaskDataProviders.

@Test
public void shouldValidateTasksAfterChannelUpdateForInvalidTaskDataProviders() {
    TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
    Task task = new Task("name", trigger, asList(action), config, true, false);
    task.setId(12345l);
    List<LookupFieldsParameter> lookupFields = new ArrayList<>();
    lookupFields.add(new LookupFieldsParameter("property", asList("property")));
    TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", lookupFields, null)));
    provider.setId(1234L);
    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()));
    when(tasksDataService.findById(12345l)).thenReturn(task);
    when(tasksDataService.retrieveAll()).thenReturn(asList(task));
    when(providerService.getProvider(provider.getName())).thenReturn(provider);
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    taskService.validateTasksAfterTaskDataProviderUpdate(getProviderUpdateEvent(provider.getName()));
    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.providerObjectLookupNotExist"))));
}
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) ArrayList(java.util.ArrayList) 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) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Test(org.junit.Test)

Example 12 with Lookup

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

the class TaskServiceImplTest method shouldConvertTaskToJSON.

@Test
public void shouldConvertTaskToJSON() throws Exception {
    Map<String, String> values = new HashMap<>();
    values.put("id", "1");
    action.setValues(values);
    long taskId = 12345L;
    Task expected = new TaskBuilder().withName("test").withTrigger(trigger).addAction(action).addDataSource(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true)).isEnabled(true).build();
    ObjectMapper mapper = new ObjectMapper();
    when(tasksDataService.findById(taskId)).thenReturn(expected);
    String json = taskService.exportTask(taskId);
    JsonNode node = mapper.readTree(json);
    notContainsIgnoreFields(node);
    // should preserve action values
    assertNotNull((node).findValue("id"));
    assertEquals(expected, mapper.readValue(node, Task.class));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 13 with Lookup

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

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

the class TaskServiceImplTest method shouldNotValidateTasksAfterChannelUpdateIfDataSourceDoesNotExistForGivenProvider.

@Test
public void shouldNotValidateTasksAfterChannelUpdateIfDataSourceDoesNotExistForGivenProvider() {
    TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
    Task task = new Task("name", trigger, asList(action), config, true, false);
    TaskDataProvider dataProvider = new TaskDataProvider("abc", null);
    dataProvider.setId(5678L);
    when(tasksDataService.retrieveAll()).thenReturn(asList(task));
    when(providerService.getProvider(dataProvider.getName())).thenReturn(dataProvider);
    taskService.validateTasksAfterTaskDataProviderUpdate(getProviderUpdateEvent("abc"));
    verify(tasksDataService, never()).create(any(Task.class));
    verify(tasksDataService, never()).update(any(Task.class));
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Task(org.motechproject.tasks.domain.mds.task.Task) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 15 with Lookup

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

the class TaskInitializer method getDataSourceObject.

private Object getDataSourceObject(DataSource dataSource, Map<String, DataProvider> providers) throws TaskHandlerException {
    if (providers == null || providers.isEmpty()) {
        throw new TaskHandlerException(DATA_SOURCE, "task.error.notFoundDataProvider", dataSource.getType());
    }
    DataProvider provider = providers.get(dataSource.getProviderName());
    if (provider == null) {
        throw new TaskHandlerException(DATA_SOURCE, "task.error.notFoundDataProvider", dataSource.getType());
    }
    KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext);
    Map<String, String> lookupFields = new HashMap<>();
    for (Lookup lookup : dataSource.getLookup()) {
        lookupFields.put(lookup.getField(), keyEvaluator.evaluateTemplateString(lookup.getValue()));
    }
    return provider.lookup(dataSource.getType(), dataSource.getName(), lookupFields);
}
Also used : DataProvider(org.motechproject.commons.api.DataProvider) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) HashMap(java.util.HashMap) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) KeyEvaluator(org.motechproject.tasks.service.util.KeyEvaluator)

Aggregations

Lookup (org.motechproject.tasks.domain.mds.task.Lookup)16 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)13 Test (org.junit.Test)11 Task (org.motechproject.tasks.domain.mds.task.Task)11 HashMap (java.util.HashMap)9 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)9 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)9 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)9 ArrayList (java.util.ArrayList)7 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)6 DataProvider (org.motechproject.commons.api.DataProvider)5 Channel (org.motechproject.tasks.domain.mds.channel.Channel)5 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)5 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)5 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)5 HashSet (java.util.HashSet)4 LinkedHashMap (java.util.LinkedHashMap)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MotechEvent (org.motechproject.event.MotechEvent)4