Search in sources :

Example 31 with TaskActionInformation

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

the class TaskActionExecutorTest method shouldThrowExceptionIfActionHasNeitherEventNorService.

@Test(expected = TaskHandlerException.class)
public void shouldThrowExceptionIfActionHasNeitherEventNorService() throws TaskHandlerException, ActionNotFoundException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<>()).build();
    actionEvent.setActionParameters(new TreeSet<>());
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 32 with TaskActionInformation

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

the class TaskActionExecutorTest method prepareTaskActionInformation.

private TaskActionInformation prepareTaskActionInformation() {
    TaskActionInformation actionInformation = new TaskActionInformation();
    actionInformation.setDisplayName("action");
    actionInformation.setChannelName("channel");
    actionInformation.setModuleName("module");
    actionInformation.setModuleVersion("0.1");
    actionInformation.setSubject("actionSubject");
    return actionInformation;
}
Also used : TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation)

Example 33 with TaskActionInformation

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

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

the class TaskServiceImpl method validateActions.

private Set<TaskError> validateActions(Task task) {
    LOGGER.debug("Validating all actions in task: {} with ID: {}", task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    for (TaskActionInformation action : task.getActions()) {
        Channel channel = channelService.getChannel(action.getModuleName());
        errors.addAll(validateAction(task, channel, action));
    }
    logResultOfValidation("actions", task.getName(), errors);
    return errors;
}
Also used : Channel(org.motechproject.tasks.domain.mds.channel.Channel) HandlerPredicates.tasksWithRegisteredChannel(org.motechproject.tasks.service.util.HandlerPredicates.tasksWithRegisteredChannel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) HashSet(java.util.HashSet)

Example 35 with TaskActionInformation

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

the class TaskServiceImpl method checkChannelAvailableInTask.

private void checkChannelAvailableInTask(Task task) {
    if (null != task) {
        List<String> symbolic = WebBundleUtil.getSymbolicNames(bundleContext);
        TaskTriggerInformation trigger = task.getTrigger();
        List<TaskActionInformation> actions = task.getActions();
        task.setHasRegisteredChannel(null != trigger && symbolic.contains(trigger.getModuleName()));
        if (CollectionUtils.isNotEmpty(actions)) {
            for (TaskActionInformation action : actions) {
                if (!symbolic.contains(action.getModuleName())) {
                    task.setHasRegisteredChannel(false);
                }
            }
        }
    }
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation)

Aggregations

TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)44 Task (org.motechproject.tasks.domain.mds.task.Task)34 Test (org.junit.Test)32 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)24 HashMap (java.util.HashMap)18 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)14 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)12 KeyInformation (org.motechproject.tasks.domain.KeyInformation)10 TreeSet (java.util.TreeSet)9 ObjectTest (org.motechproject.tasks.domain.ObjectTest)9 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)9 TaskContext (org.motechproject.tasks.service.util.TaskContext)8 MotechEvent (org.motechproject.event.MotechEvent)7 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)6 ArrayList (java.util.ArrayList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)5 HashSet (java.util.HashSet)3 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)3 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)3