Search in sources :

Example 21 with DataSource

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

the class TaskFilterExecutorTest method testcheckFilters.

@Test
public void testcheckFilters() throws TaskHandlerException {
    DateTime dateTime = DateTime.now().minusDays(2);
    DataSource dataSource = new DataSource("ProviderName", null, 0L, "", "", "", null, false);
    TaskConfig taskConfig = mock(TaskConfig.class);
    when(taskConfig.getDataSource(anyString(), anyLong(), anyString())).thenReturn(dataSource);
    Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
    TaskContext taskContext = new TaskContext(task, null, null, activityService);
    TaskFilterExecutor taskFilterExecutor = new TaskFilterExecutor();
    assertTrue(taskFilterExecutor.checkFilters(null, null, taskContext));
    assertTrue(taskFilterExecutor.checkFilters(new ArrayList<>(), null, taskContext));
    List<Filter> filters = new ArrayList<>();
    List<String> filtersManipulations = new ArrayList<>();
    filters.add(new Filter("EventName", "trigger.eventName", UNICODE, true, CONTAINS.getValue(), "ven", filtersManipulations));
    filters.add(new Filter("EventName", "trigger.eventName", TEXTAREA, true, EXIST.getValue(), "", filtersManipulations));
    filters.add(new Filter("EventName", "trigger.eventName", UNICODE, true, EQUALS.getValue(), "event name", filtersManipulations));
    filters.add(new Filter("EventName", "trigger.eventName", UNICODE, true, EQUALS_IGNORE_CASE.getValue(), "EvEnT nAmE", filtersManipulations));
    filters.add(new Filter("EventName", "trigger.eventName", UNICODE, true, STARTSWITH.getValue(), "ev", filtersManipulations));
    filters.add(new Filter("EventName", "trigger.eventName", UNICODE, true, ENDSWITH.getValue(), "me", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, true, GT.getValue(), "19", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, false, GT.getValue(), "1234567891", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, true, LT.getValue(), "1234567891", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, false, LT.getValue(), "123", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, true, EQ_NUMBER.getValue(), "123456789", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, false, EQ_NUMBER.getValue(), "789", filtersManipulations));
    filters.add(new Filter("ExternalID", "trigger.externalId", INTEGER, true, EXIST.getValue(), "", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, CONTAINS.getValue(), "am", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, EXIST.getValue(), "", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, EQUALS.getValue(), "name", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, EQUALS_IGNORE_CASE.getValue(), "nAmE", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, STARTSWITH.getValue(), "na", filtersManipulations));
    filters.add(new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, ENDSWITH.getValue(), "me", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, true, GT.getValue(), "30", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, true, LT.getValue(), "50", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, true, EQ_NUMBER.getValue(), "46", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, true, EXIST.getValue(), "", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, false, GT.getValue(), "100", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Dead", "ad.2.Person#1.dead", BOOLEAN, false, EXIST.getValue(), "", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Dead", "ad.2.Person#1.dead", BOOLEAN, false, AND.getValue(), "false", filtersManipulations));
    filters.add(new Filter("MRS.Person#1.Dead", "ad.2.Person#1.dead", BOOLEAN, true, OR.getValue(), "true", filtersManipulations));
    taskContext = new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService);
    assertFalse(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.OR, taskContext));
    Map<String, Object> triggerParameters = new HashMap<>();
    triggerParameters.put("eventName", "etName");
    triggerParameters.put("externalId", "12345");
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("Eman"), false);
    taskContext.addDataSourceObject("1", new Person(150, true), false);
    assertFalse(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.OR, taskContext));
    triggerParameters.put("eventName", "event name");
    triggerParameters.put("externalId", "123456789");
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, false), false);
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    Filter equals = new Filter("Test date", "trigger.test_date", DATE, true, EQUALS.getValue(), dateTime.toString(), filtersManipulations);
    Filter after = new Filter("Test date", "trigger.test_date", DATE, false, AFTER.getValue(), DateUtil.now().toString(), filtersManipulations);
    Filter afterNow = new Filter("Test date", "trigger.test_date", DATE, false, AFTER_NOW.getValue(), "", filtersManipulations);
    Filter before = new Filter("Test date", "trigger.test_date", DATE, true, BEFORE.getValue(), DateUtil.now().toString(), filtersManipulations);
    Filter beforeNow = new Filter("Test date", "trigger.test_date", DATE, true, BEFORE_NOW.getValue(), "", filtersManipulations);
    Filter lessDays = new Filter("Test date", "trigger.test_date", DATE, true, LESS_DAYS_FROM_NOW.getValue(), "3", filtersManipulations);
    Filter moreDays = new Filter("Test date", "trigger.test_date", DATE, true, MORE_DAYS_FROM_NOW.getValue(), "0", filtersManipulations);
    filters.add(equals);
    filters.add(after);
    filters.add(afterNow);
    filters.add(before);
    filters.add(beforeNow);
    filters.add(new Filter("Test date", "trigger.test_date", DATE, true, EXIST.getValue(), "", filtersManipulations));
    filters.add(lessDays);
    filters.add(moreDays);
    triggerParameters.put("test_date", dateTime.toString());
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, false), false);
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    dateTime = dateTime.plusDays(4);
    triggerParameters.put("test_date", dateTime.toString());
    filters.remove(equals);
    filters.remove(after);
    filters.remove(afterNow);
    filters.remove(before);
    filters.remove(beforeNow);
    filters.remove(lessDays);
    filters.remove(moreDays);
    filters.add(new Filter("Test date", "trigger.test_date", DATE, true, LESS_MONTHS_FROM_NOW.getValue(), "5", filtersManipulations));
    filters.add(new Filter("Test date", "trigger.test_date", DATE, true, MORE_MONTHS_FROM_NOW.getValue(), "1", filtersManipulations));
    dateTime = DateTime.now().minusMonths(3);
    triggerParameters.put("test_date", dateTime.toString());
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, false), false);
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    dateTime = dateTime.plusMonths(6);
    triggerParameters.put("test_date", dateTime.toString());
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, false), false);
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    equals.setExpression(dateTime.toString());
    after.setNegationOperator(!after.isNegationOperator());
    afterNow.setNegationOperator(!afterNow.isNegationOperator());
    before.setNegationOperator(!before.isNegationOperator());
    beforeNow.setNegationOperator(!beforeNow.isNegationOperator());
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, false), false);
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    Filter triggerFilter = new Filter("Trigger.Event Name", "trigger.eventName", UNICODE, true, "abc", "", filtersManipulations);
    filters.add(triggerFilter);
    Filter additionalDataFilter = new Filter("CMS Lite.StreamContent#0.Name", "ad.1.StreamContent#0.name", UNICODE, true, "abc", "", filtersManipulations);
    filters.add(additionalDataFilter);
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, true), false);
    assertFalse(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.OR, taskContext));
    filters.remove(triggerFilter);
    filters.add(new Filter("Trigger.External Id", "trigger.externalId", INTEGER, true, "abc", "", filtersManipulations));
    filters.remove(additionalDataFilter);
    filters.add(new Filter("MRS.Person#1.Age", "ad.2.Person#1.age", INTEGER, true, "abc", "", filtersManipulations));
    taskContext = new TaskContext(task, triggerParameters, new HashMap<>(), activityService);
    taskContext.addDataSourceObject("0", new StreamContent("name"), false);
    taskContext.addDataSourceObject("1", new Person(46, true), false);
    assertFalse(taskFilterExecutor.checkFilters(filters, LogicalOperator.AND, taskContext));
    assertTrue(taskFilterExecutor.checkFilters(filters, LogicalOperator.OR, taskContext));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DateTime(org.joda.time.DateTime) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Filter(org.motechproject.tasks.domain.mds.task.Filter) Test(org.junit.Test)

Example 22 with DataSource

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

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

the class TaskValidator method validateFilter.

private Set<TaskError> validateFilter(Integer setOrder, int index, Filter filter, TaskConfig config) {
    Set<TaskError> errors = new HashSet<>();
    String field = String.format("taskConfig.filterSet[%d].filters[%d]", setOrder, index);
    KeyInformation key = parse(filter.getKey());
    DataSource dataSource = config.getDataSource(key.getDataProviderName(), key.getObjectId(), key.getObjectType());
    checkNullValue(errors, TASK, field, filter);
    if (isEmpty(errors)) {
        String objectName = "task." + field;
        if (key.fromAdditionalData() && dataSource == null) {
            errors.add(new TaskError("task.validation.error.DataSourceNotExist", key.getObjectType()));
        }
        checkBlankValue(errors, objectName, "key", filter.getKey());
        checkBlankValue(errors, objectName, "displayName", filter.getDisplayName());
        checkBlankValue(errors, objectName, "operator", filter.getOperator());
        checkNullValue(errors, objectName, "type", filter.getType());
        if (OperatorType.needExpression(filter.getOperator())) {
            checkBlankValue(errors, objectName, "expression", filter.getExpression());
        }
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) KeyInformation(org.motechproject.tasks.domain.KeyInformation) DataSource(org.motechproject.tasks.domain.mds.task.DataSource)

Example 24 with DataSource

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

the class TaskBuilderTest method shouldReturnBuiltTaskObject.

@Test
public void shouldReturnBuiltTaskObject() throws Exception {
    TaskBuilder builder = new TaskBuilder();
    Task task = builder.withName(TASK_NAME).withDescription(TASK_DESCRIPTION).isEnabled(isEnabled).withTrigger(new TaskTriggerInformation()).addAction(new TaskActionInformation()).withTaskConfig(new TaskConfig()).addFilterSet(new FilterSet()).addDataSource(new DataSource()).build();
    assertNotNull(task);
    assertEquals(TASK_NAME, task.getName());
    assertEquals(TASK_DESCRIPTION, task.getDescription());
    assertEquals(isEnabled, task.isEnabled());
    assertNotNull(task.getTrigger());
    assertNotNull(task.getActions());
    assertFalse(task.getActions().isEmpty());
    assertNotNull(task.getTaskConfig());
    assertFalse(task.getTaskConfig().getDataSources().isEmpty());
    assertFalse(task.getTaskConfig().getFilters().isEmpty());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) Test(org.junit.Test)

Example 25 with DataSource

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

the class TaskTriggerHandlerTest method shouldNotTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected.

@Test
public void shouldNotTriggerErrorWhenActionDoesNotFindDataSourceWithFailIfDataNotFoundNotSelected() throws Exception {
    Map<String, DataProvider> providers = new HashMap<>();
    DataProvider provider = mock(DataProvider.class);
    Map<String, String> lookup = new HashMap<>();
    lookup.put("patientId", "123");
    when(provider.lookup("Patient", null, lookup)).thenReturn(null);
    providers.put(TASK_DATA_PROVIDER_NAME, provider);
    handler.setDataProviders(providers);
    TriggerEvent trigger = new TriggerEvent();
    trigger.setSubject("trigger");
    List<EventParameter> triggerEventParameters = new ArrayList<>();
    triggerEventParameters.add(new EventParameter("patientId", "123"));
    trigger.setEventParameters(triggerEventParameters);
    ActionEvent action = new ActionEventBuilder().build();
    action.setSubject("action");
    SortedSet<ActionParameter> actionEventParameters = new TreeSet<>();
    actionEventParameters.add(new ActionParameterBuilder().setDisplayName("Patient ID").setKey("patientId").setType(UNICODE).setOrder(0).build());
    action.setActionParameters(actionEventParameters);
    Task task = new Task();
    task.setName("task");
    task.setTrigger(new TaskTriggerInformation("Trigger", "channel", "module", "0.1", "trigger", "listener"));
    Map<String, String> actionValues = new HashMap<>();
    actionValues.put("patientId", "{{ad.12345.Patient#1.patientId}}");
    task.addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "action", actionValues));
    task.setId(7l);
    task.setHasRegisteredChannel(true);
    TaskConfig taskConfig = new TaskConfig();
    task.setTaskConfig(taskConfig);
    taskConfig.add(new DataSource(TASK_DATA_PROVIDER_NAME, 3L, 1L, "Patient", "provider", "specifiedName", asList(new Lookup("patientId", "trigger.patientId")), false));
    List<Task> tasks = asList(task);
    when(taskService.findActiveTasksForTriggerSubject("trigger")).thenReturn(tasks);
    when(taskService.getActionEventFor(task.getActions().get(0))).thenReturn(action);
    setTaskActivities();
    task.setFailuresInRow(taskActivities.size());
    Map<String, Object> param = new HashMap<>(4);
    param.put("patientId", "123");
    handler.handle(new MotechEvent("trigger", param));
    verify(postExecutionHandler, never()).handleError(anyMap(), anyMap(), eq(task), any(TaskHandlerException.class), eq(TASK_ACTIVITY_ID));
    verify(taskActivityService).addWarning(eq(task), eq("task.warning.notFoundObjectForType"), eq("Patient"));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(org.motechproject.commons.api.DataProvider) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) TreeSet(java.util.TreeSet) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) MotechEvent(org.motechproject.event.MotechEvent) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) Test(org.junit.Test)

Aggregations

DataSource (org.motechproject.tasks.domain.mds.task.DataSource)25 Test (org.junit.Test)19 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)17 Task (org.motechproject.tasks.domain.mds.task.Task)15 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)13 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)9 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)9 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)8 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)7 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)6 HashSet (java.util.HashSet)5 Matchers.anyString (org.mockito.Matchers.anyString)5 Channel (org.motechproject.tasks.domain.mds.channel.Channel)5 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)5 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)5 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)5 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)5