Search in sources :

Example 16 with TriggerEvent

use of org.motechproject.tasks.domain.mds.channel.TriggerEvent 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);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) Filter(org.motechproject.tasks.domain.mds.task.Filter) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 17 with TriggerEvent

use of org.motechproject.tasks.domain.mds.channel.TriggerEvent in project motech by motech.

the class TriggerEventServiceImplTest method shouldGetTrigger.

@Test
public void shouldGetTrigger() throws Exception {
    TriggerEvent expectedTrigger = prepareTrigger();
    TaskTriggerInformation triggerInformation = prepareTaskTriggerInformation(expectedTrigger);
    when(triggerEventsDataService.byChannelModuleNameAndSubject(MODULE_NAME, expectedTrigger.getSubject())).thenReturn(null);
    when(dynamicChannelLoader.getTrigger(eq(triggerInformation))).thenReturn(expectedTrigger);
    TriggerEvent trigger = triggerEventService.getTrigger(triggerInformation);
    verify(triggerEventsDataService, times(1)).byChannelModuleNameAndSubject(MODULE_NAME, expectedTrigger.getSubject());
    verify(dynamicChannelLoader, times(1)).getTrigger(triggerInformation);
    assertEquals(expectedTrigger, trigger);
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Test(org.junit.Test)

Example 18 with TriggerEvent

use of org.motechproject.tasks.domain.mds.channel.TriggerEvent in project motech by motech.

the class TriggerEventServiceImplTest method shouldReturnTrueIfTriggerExists.

@Test
public void shouldReturnTrueIfTriggerExists() throws Exception {
    TriggerEvent expectedTrigger = prepareTrigger();
    TaskTriggerInformation triggerInformation = prepareTaskTriggerInformation(expectedTrigger);
    when(triggerEventsDataService.byChannelModuleNameAndSubject(MODULE_NAME, expectedTrigger.getSubject())).thenReturn(null);
    when(dynamicChannelLoader.getTrigger(eq(triggerInformation))).thenReturn(expectedTrigger);
    boolean exists = triggerEventService.triggerExists(triggerInformation);
    verify(triggerEventsDataService, times(1)).byChannelModuleNameAndSubject(MODULE_NAME, expectedTrigger.getSubject());
    verify(dynamicChannelLoader, times(1)).getTrigger(triggerInformation);
    assertTrue(exists);
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Test(org.junit.Test)

Example 19 with TriggerEvent

use of org.motechproject.tasks.domain.mds.channel.TriggerEvent in project motech by motech.

the class TriggerEventServiceImplTest method shouldGetStaticTriggers.

@Test
public void shouldGetStaticTriggers() throws Exception {
    int page = 1;
    int pageSize = 10;
    List<TriggerEvent> expectedTriggers = prepareTriggers();
    QueryParams params = new QueryParams(page, pageSize);
    when(triggerEventsDataService.byChannelModuleName(MODULE_NAME, params)).thenReturn(expectedTriggers);
    List<TriggerEvent> triggers = triggerEventService.getStaticTriggers(MODULE_NAME, page, pageSize);
    verify(triggerEventsDataService, times(1)).byChannelModuleName(eq(MODULE_NAME), eq(params));
    assertEquals(expectedTriggers, triggers);
}
Also used : TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 20 with TriggerEvent

use of org.motechproject.tasks.domain.mds.channel.TriggerEvent in project motech by motech.

the class SchedulerChannelProvider method getTrigger.

@Override
public TriggerEvent getTrigger(TaskTriggerInformation info) {
    try (Connection conn = DBConnectionManager.getInstance().getConnection(sqlProperties.getProperty(DATA_SOURCE));
        Statement stmt = conn.createStatement()) {
        ResultSet rs = stmt.executeQuery(buildGetTriggerQuery(info.getSubject()));
        if (rs.next()) {
            try (InputStream is = new ByteArrayInputStream(rs.getBytes(JOB_DATA));
                ObjectInputStream ois = new ObjectInputStream(is)) {
                JobDataMap dataMap = (JobDataMap) ois.readObject();
                List<EventParameter> parameters = new ArrayList<>();
                parameters.add(new EventParameter("scheduler.jobId", MotechSchedulerService.JOB_ID_KEY));
                return new TriggerEvent("Job: " + rs.getString(JOB_NAME), rs.getString(JOB_NAME), rs.getString(JOB_DESCRIPTION), parameters, dataMap.getString(SchedulerConstants.EVENT_TYPE_KEY_NAME));
            }
        }
        return null;
    } catch (SQLException | IOException | ClassNotFoundException e) {
        throw new TriggerRetrievalException("Couldn't retrieve triggers for Scheduler channel", e);
    }
}
Also used : TriggerRetrievalException(org.motechproject.tasks.exception.TriggerRetrievalException) JobDataMap(org.quartz.JobDataMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)34 Test (org.junit.Test)24 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)22 ArrayList (java.util.ArrayList)17 Task (org.motechproject.tasks.domain.mds.task.Task)16 Channel (org.motechproject.tasks.domain.mds.channel.Channel)13 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)12 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)12 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)9 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)9 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)9 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)8 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)6 MotechEvent (org.motechproject.event.MotechEvent)5 LookupFieldsParameter (org.motechproject.tasks.domain.mds.task.LookupFieldsParameter)5 Matchers.anyString (org.mockito.Matchers.anyString)4