Search in sources :

Example 1 with TaskDataProvider

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

the class DataProviderManagerTest method shouldRegisterProviderAndAddToHandler.

@Test
public void shouldRegisterProviderAndAddToHandler() throws IOException {
    DataProviderManager mgr = new DataProviderManager(taskTriggerHandler, taskDataProviderService);
    TaskDataProvider provider = new TaskDataProvider();
    provider.setId(TASK_DATA_PROVIDER_ID);
    String json = String.format("{ name: '%s', objects: []", TASK_DATA_PROVIDER_NAME);
    when(dataProvider.toJSON()).thenReturn(json);
    mgr.bind(dataProvider, null);
    verify(taskDataProviderService).registerProvider(json);
    verify(taskTriggerHandler).addDataProvider(dataProvider);
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 2 with TaskDataProvider

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

the class TasksBundleIT method testDataProviderService.

private void testDataProviderService() throws InterruptedException {
    Resource resource = ServiceRetriever.getWebAppContext(bundleContext).getResource("classpath:task-data-provider.json");
    try (InputStream in = resource.getInputStream()) {
        taskDataProviderService.registerProvider(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    TaskDataProvider fromFile;
    int tries = 0;
    do {
        fromFile = taskDataProviderService.getProvider("mrs-name");
        ++tries;
        Thread.sleep(500);
    } while (fromFile == null && tries < TRIES_COUNT);
    assertNotNull(fromFile);
    TaskDataProvider fromDB = dataProviderDataService.findByName("mrs-name");
    assertNotNull(fromDB);
    assertEquals(fromDB, fromFile);
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 3 with TaskDataProvider

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

the class TaskServiceImpl method validateAction.

private Set<TaskError> validateAction(Task task, Channel channel, TaskActionInformation action) {
    LOGGER.debug("Validating task action: {} from task: {} with ID: {}", action.getName(), task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    if (channel == null) {
        errors.add(new TaskError("task.validation.error.actionChannelNotRegistered"));
        return errors;
    }
    if (channel.getModuleName().equalsIgnoreCase(action.getModuleName())) {
        errors.addAll(taskValidator.validateAction(action, channel));
        TriggerEvent trigger = triggerEventService.getTrigger(task.getTrigger());
        Map<Long, TaskDataProvider> providers = getProviders(task);
        ActionEvent actionEvent = channel.getAction(action);
        if (actionEvent != null) {
            errors.addAll(taskValidator.validateActionFields(action, actionEvent, trigger, providers));
        }
    }
    logResultOfValidation("task action", task.getName(), errors);
    return errors;
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet)

Example 4 with TaskDataProvider

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

the class TaskServiceImpl method validateDataSources.

private Set<TaskError> validateDataSources(Task task) {
    LOGGER.debug("Validating task data sources in task: {} with ID: {}", task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    Map<Long, TaskDataProvider> availableDataProviders = new HashMap<>();
    for (DataSource dataSource : task.getTaskConfig().getDataSources()) {
        TaskDataProvider provider = providerService.getProvider(dataSource.getProviderName());
        errors.addAll(validateProvider(provider, dataSource, task, availableDataProviders));
        if (provider != null) {
            availableDataProviders.put(provider.getId(), provider);
        }
    }
    logResultOfValidation("task data sources", task.getName(), errors);
    return errors;
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) DataSource(org.motechproject.tasks.domain.mds.task.DataSource)

Example 5 with TaskDataProvider

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

the class TaskDataProviderServiceImpl method addProviderImpl.

private void addProviderImpl(final TaskDataProvider provider) {
    // becomes available
    if (dataProviderDataService != null) {
        final TaskDataProvider existing = dataProviderDataService.findByName(provider.getName());
        // Only update data provider when there's actual change
        if (existing != null && !existing.equals(provider)) {
            LOGGER.debug("Updating a task data provider with name: {}", provider.getName());
            existing.setObjects(provider.getObjects());
            dataProviderDataService.update(existing);
            Map<String, Object> parameters = new HashMap<>();
            parameters.put(DATA_PROVIDER_NAME, provider.getName());
            eventRelay.sendEventMessage(new MotechEvent(DATA_PROVIDER_UPDATE_SUBJECT, parameters));
        } else if (existing == null) {
            LOGGER.debug("Creating a task data provider with name: {}", provider.getName());
            dataProviderDataService.create(provider);
        }
    } else {
        LOGGER.debug("DataProviderDataService is not available, storing a task data provider with name: {} for later addition", provider.getName());
        // store for later addition
        providersToAdd.add(provider);
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent)

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