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();
}
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));
}
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);
}
}
}
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;
}
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();
}
}
Aggregations