Search in sources :

Example 21 with TaskDataProvider

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

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

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

the class TaskServiceImpl method validateTasksAfterTaskDataProviderUpdate.

@MotechListener(subjects = DATA_PROVIDER_UPDATE_SUBJECT)
@Transactional
public void validateTasksAfterTaskDataProviderUpdate(MotechEvent event) {
    String providerName = event.getParameters().get(DATA_PROVIDER_NAME).toString();
    TaskDataProvider provider = providerService.getProvider(providerName);
    LOGGER.debug("Handling a task data provider update: {}", providerName);
    for (Task task : getAllTasks()) {
        SortedSet<DataSource> dataSources = task.getTaskConfig().getDataSources(provider.getName());
        if (isNotEmpty(dataSources)) {
            Set<TaskError> errors = new HashSet<>();
            for (DataSource dataSource : dataSources) {
                errors.addAll(validateProvider(provider, dataSource, task, new HashMap<Long, TaskDataProvider>()));
            }
            errors.addAll(validateActions(task));
            handleValidationErrors(task, errors, TASK_DATA_PROVIDER_VALIDATION_ERRORS);
        }
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) HashSet(java.util.HashSet) MotechListener(org.motechproject.event.listener.annotations.MotechListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with TaskDataProvider

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

the class TaskValidator method validateDataProviderObjects.

private Set<TaskError> validateDataProviderObjects(KeyInformation key, Map<Long, TaskDataProvider> providers) {
    Set<TaskError> errors = new HashSet<>();
    boolean isObjectValid = false;
    if (key.fromAdditionalData()) {
        for (Map.Entry<Long, TaskDataProvider> entry : providers.entrySet()) {
            for (TaskDataProviderObject dataProviderObject : entry.getValue().getObjects()) {
                if (dataProviderObject.getType().equals(key.getObjectType())) {
                    isObjectValid = true;
                    break;
                }
            }
        }
        if (!isObjectValid) {
            errors.add(new TaskError("task.validation.error.providerObjectFieldNotExist", key.getKey(), key.getObjectType()));
        }
    }
    return errors;
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 25 with TaskDataProvider

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

the class TaskDataProviderServiceImpl method bind.

@Override
@Transactional
public void bind(Object service, Map properties) {
    LOGGER.info("Data Service for task data providers registered, starting to register queued providers");
    dataProviderDataService = (DataProviderDataService) service;
    // add providers from queue
    LOGGER.debug("Adding the following task data providers: {}", providersToAdd);
    synchronized (additionLock) {
        for (TaskDataProvider provider : providersToAdd) {
            LOGGER.info("Registering a task data provider with name: {}", provider.getName());
            addProviderImpl(provider);
        }
        providersToAdd.clear();
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)28 Test (org.junit.Test)20 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)14 LookupFieldsParameter (org.motechproject.tasks.domain.mds.task.LookupFieldsParameter)9 ArrayList (java.util.ArrayList)8 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)8 Task (org.motechproject.tasks.domain.mds.task.Task)8 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)7 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)7 Type (java.lang.reflect.Type)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Channel (org.motechproject.tasks.domain.mds.channel.Channel)6 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)6 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)6 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)6 FieldParameter (org.motechproject.tasks.domain.mds.task.FieldParameter)5 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)5 LinkedHashMap (java.util.LinkedHashMap)3 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)3