use of org.motechproject.tasks.domain.mds.task.Filter in project motech by motech.
the class TaskServiceImplTest method shouldImportTask.
@Test
public void shouldImportTask() throws Exception {
ObjectMapper mapper = new ObjectMapper();
action.getValues().put("phone", "{{ad.providerName.Test#1.id}}");
List<String> filtersManipulations = new ArrayList<>();
Task given = new TaskBuilder().withName("test").withTrigger(trigger).addAction(action).addDataSource(new DataSource("providerName", 1234L, 1L, "Test", "id", asList(new Lookup("id", "trigger.value")), true)).addFilterSet(new FilterSet(asList(new Filter("displayName", "ad.providerName.Test#1.id", UNICODE, true, OperatorType.EXIST.getValue(), "", filtersManipulations)))).isEnabled(true).build();
TaskDataProvider provider = new TaskDataProvider();
provider.setName("providerName");
provider.setId(56789L);
provider.setObjects(asList(new TaskDataProviderObject("display", "Test", asList(new LookupFieldsParameter("id", asList("id"))), asList(new FieldParameter("display", "id", UNICODE)))));
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));
when(providerService.getProviders()).thenReturn(asList(provider));
when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
when(triggerEventService.triggerExists(given.getTrigger())).thenReturn(true);
when(providerService.getProvider("providerName")).thenReturn(provider);
String json = mapper.writeValueAsString(given);
taskService.importTask(json);
action.getValues().put("phone", "{{ad.providerName.Test#1.id}}");
Task expected = new TaskBuilder().withName("test").withTrigger(trigger).addAction(action).addDataSource(new DataSource("providerName", 56789L, 1L, "Test", "id", asList(new Lookup("id", "trigger.value")), true)).addFilterSet(new FilterSet(asList(new Filter("displayName", "ad.providerName.Test#1.id", UNICODE, true, OperatorType.EXIST.getValue(), "", filtersManipulations)))).isEnabled(true).build();
Task actual = verifyCreateAndCaptureTask();
assertEquals(expected, actual);
verify(taskMigrationManager).migrateTask(actual);
}
use of org.motechproject.tasks.domain.mds.task.Filter in project motech by motech.
the class TaskFilterExecutorTest method shouldThrowExceptionIfDataSourceObjectIsNotFound.
@Test(expected = TaskHandlerException.class)
public void shouldThrowExceptionIfDataSourceObjectIsNotFound() throws TaskHandlerException {
List<Filter> filters = new ArrayList<>();
List<String> filtersManipulations = new ArrayList<>();
filters.add(new Filter("MRS.Person#2.Age", "ad.2.Person#2.age", INTEGER, false, EXIST.getValue(), "", filtersManipulations));
Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
TaskContext taskContext = new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService);
new TaskFilterExecutor().checkFilters(filters, LogicalOperator.AND, taskContext);
}
use of org.motechproject.tasks.domain.mds.task.Filter in project motech by motech.
the class ProviderNameTaskMigrator method migrateFilters.
private void migrateFilters(Task task) {
for (FilterSet filterSet : task.getTaskConfig().getFilters()) {
for (Filter filter : filterSet.getFilters()) {
// replace data source references in filter expressions
String oldVal = filter.getExpression();
String newVal = replaceTaskValue(oldVal, EXPR_PATTERN, task);
filter.setExpression(newVal);
// also make sure the key is correct
oldVal = filter.getKey();
newVal = replaceTaskValue(oldVal, FILTER_KEY_PATTERN, task);
filter.setKey(newVal);
}
}
}
use of org.motechproject.tasks.domain.mds.task.Filter 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));
}
use of org.motechproject.tasks.domain.mds.task.Filter in project motech by motech.
the class TaskFilterExecutor method checkFilters.
/**
* Checks whether task with the given context matches the given filters.
*
* @param filters the filters, null returns true
* @param logicalOperator the logical operator
* @param taskContext the task context, not null
* @return true if the task matches the filters
* @throws TaskHandlerException if there were problems while handling task
*/
public boolean checkFilters(List<Filter> filters, LogicalOperator logicalOperator, TaskContext taskContext) throws TaskHandlerException {
LOGGER.debug("Checking if task: {} matches the filters", taskContext.getTask().getName());
Map<String, Object> parameters = taskContext.getTriggerParameters();
if (isEmpty(filters) || parameters == null) {
return true;
}
boolean filterCheck = false;
for (Filter filter : filters) {
KeyInformation key = parse(filter.getKey());
Object value;
try {
KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext);
value = keyEvaluator.getManipulatedValue(key);
} catch (TaskHandlerException e) {
if (TaskFailureCause.DATA_SOURCE.equals(e.getFailureCause())) {
// data source lookups disable the task
throw e;
}
// trigger parameter lookups don't disable the task
value = null;
LOGGER.error("Unable to retrieve value for filter", e);
} catch (RuntimeException e) {
value = null;
LOGGER.error("Unable to retrieve value for filter", e);
}
filterCheck = value != null && checkValue(filter, value);
if (!filter.isNegationOperator()) {
filterCheck = !filterCheck;
}
LOGGER.debug("Result of checking filter: {} for task: {} is: {}", filter.getDisplayName(), taskContext.getTask().getName(), filterCheck);
if (isFilterConditionFulfilled(filterCheck, logicalOperator)) {
LOGGER.debug("Filters condition is fulfilled, because logicalOperator is: {} and filters checking has already: {} value", logicalOperator, filterCheck);
break;
}
}
LOGGER.info("Result of checking filters for task: {} is: {}", taskContext.getTask().getName(), filterCheck);
return filterCheck;
}
Aggregations