use of org.motechproject.tasks.domain.mds.task.TaskDataProviderObject in project motech by motech.
the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForValidTaskDataProviders.
@Test
public void shouldValidateTasksAfterChannelUpdateForValidTaskDataProviders() {
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);
Set<TaskError> existingErrors = new HashSet<>();
existingErrors.add(new TaskError("task.validation.error.providerObjectLookupNotExist"));
task.addValidationErrors(existingErrors);
TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", null, null)));
provider.setId(1234L);
LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
hashMap.put("displayName", "id");
ArrayList<String> list = new ArrayList<>();
list.add("id");
hashMap.put("fields", list);
provider.getObjects().get(0).setLookupFields(asList((Object) hashMap));
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.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 = verifyCreateAndCaptureTask();
assertTrue(task.isEnabled());
assertTrue(actualTask.getValidationErrors().isEmpty());
}
use of org.motechproject.tasks.domain.mds.task.TaskDataProviderObject 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"))));
}
use of org.motechproject.tasks.domain.mds.task.TaskDataProviderObject 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.TaskDataProviderObject 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;
}
Aggregations