Search in sources :

Example 21 with ActionEventBuilder

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

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForInvalidActions.

@Test
public void shouldValidateTasksAfterChannelUpdateForInvalidActions() {
    Task task = new Task("name", trigger, new ArrayList<>(asList(action)), new TaskConfig(), true, false);
    task.setId(124l);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    when(tasksDataService.findById(124l)).thenReturn(task);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SENDING", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("schedule").setSubject("SCHEDULE").setDescription("").setActionParameters(null).build()));
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(action));
    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.actionNotExist"))));
}
Also used : EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) QueryExecution(org.motechproject.mds.query.QueryExecution) Test(org.junit.Test)

Example 22 with ActionEventBuilder

use of org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder 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();
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) Test(org.junit.Test)

Example 23 with ActionEventBuilder

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

the class TaskAnnotationBeanPostProcessorTest method shouldAddActionWithParams.

@Test
public void shouldAddActionWithParams() throws Exception {
    Channel channel = new Channel(CHANNEL_NAME, MODULE_NAME, MODULE_VERSION);
    channel.addActionTaskEvent(new ActionEventBuilder().setDisplayName(ACTION_DISPLAY_NAME).setSubject(ACTION_DISPLAY_NAME).setDescription("").setActionParameters(null).build());
    channel.addActionTaskEvent(new ActionEventBuilder().setDisplayName(ACTION_DISPLAY_NAME).setDescription("").setServiceInterface(TestAction.class.getName()).setServiceMethod("action").setActionParameters(null).build());
    when(channelService.getChannel(MODULE_NAME)).thenReturn(channel);
    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    processor.postProcessAfterInitialization(new TestActionWithParam(), null);
    verify(channelService).addOrUpdate(captor.capture());
    Channel actualChannel = captor.getValue();
    assertNotNull(actualChannel);
    assertEquals(CHANNEL_NAME, actualChannel.getDisplayName());
    assertEquals(MODULE_NAME, actualChannel.getModuleName());
    assertEquals(MODULE_VERSION, actualChannel.getModuleVersion());
    assertNotNull(actualChannel.getActionTaskEvents());
    assertEquals(channel.getActionTaskEvents().size(), actualChannel.getActionTaskEvents().size());
    ActionEvent actualActionEvent = (ActionEvent) find(actualChannel.getActionTaskEvents(), new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            return object instanceof ActionEvent && ((ActionEvent) object).hasService() && ((ActionEvent) object).getActionParameters().size() > 1 && ((ActionEvent) object).getPostActionParameters().size() > 0;
        }
    });
    assertNotNull(actualActionEvent);
    assertEquals(ACTION_DISPLAY_NAME, actualActionEvent.getDisplayName());
    assertEquals(TestAction.class.getName(), actualActionEvent.getServiceInterface());
    assertEquals(METHOD_NAME, actualActionEvent.getServiceMethod());
    assertEquals(getExpectedActionParameters(), actualActionEvent.getActionParameters());
    assertEquals(getExpectedPostActionParameters(), actualActionEvent.getPostActionParameters());
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskChannel(org.motechproject.tasks.annotations.TaskChannel) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) Predicate(org.apache.commons.collections.Predicate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with ActionEventBuilder

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

the class ChannelServiceImplTest method shouldRegisterChannelFromChannelRequest.

@Test
public void shouldRegisterChannelFromChannelRequest() {
    List<ActionEventRequest> actionEventRequests = asList(new TestActionEventRequestBuilder().setDisplayName("actionName").setSubject("subject.foo").setDescription("action description").setServiceInterface("some.interface").setServiceMethod("method").setActionParameters(new TreeSet<ActionParameterRequest>()).setPostActionParameters(new TreeSet<ActionParameterRequest>()).createActionEventRequest());
    List<TriggerEventRequest> triggerEventsRequest = asList(new TriggerEventRequest("displayName", "subject.foo", "description", asList(new EventParameterRequest("displayName", "eventKey"))));
    ChannelRequest channelRequest = new ChannelRequest(BUNDLE_SYMBOLIC_NAME, BUNDLE_SYMBOLIC_NAME, VERSION, "", triggerEventsRequest, actionEventRequests);
    channelService.registerChannel(channelRequest);
    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    verify(channelsDataService).create(captor.capture());
    Channel channelToBeCreated = captor.getValue();
    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getDisplayName());
    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getModuleName());
    assertEquals(VERSION, channelToBeCreated.getModuleVersion());
    assertEquals(1, channelToBeCreated.getTriggerTaskEvents().size());
    TriggerEvent expectedTrigger = new TriggerEvent("displayName", "subject.foo", "description", asList(new EventParameter("displayName", "eventKey")), "");
    TriggerEvent actualTrigger = channelToBeCreated.getTriggerTaskEvents().get(0);
    assertEquals(expectedTrigger, actualTrigger);
    assertEquals(1, channelToBeCreated.getActionTaskEvents().size());
    ActionEvent expectedAction = new ActionEventBuilder().setDisplayName("actionName").setSubject("subject.foo").setDescription("action description").setServiceInterface("some.interface").setServiceMethod("method").setActionParameters(new TreeSet<>()).setPostActionParameters(new TreeSet<>()).build();
    ActionEvent actualAction = channelToBeCreated.getActionTaskEvents().get(0);
    assertEquals(expectedAction, actualAction);
}
Also used : ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) EventParameterRequest(org.motechproject.tasks.contract.EventParameterRequest) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) TestActionEventRequestBuilder(org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TriggerEventRequest(org.motechproject.tasks.contract.TriggerEventRequest) TreeSet(java.util.TreeSet) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) Test(org.junit.Test)

Example 25 with ActionEventBuilder

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

the class MethodHandlerTest method getActionEvent.

private ActionEvent getActionEvent(MethodCallManner callManner) {
    ActionEvent action = new ActionEventBuilder().setServiceMethodCallManner(callManner).build();
    action.addParameter(new ActionParameterBuilder().setDisplayName("String").setKey("string").build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Integer").setKey("integer").setType(ParameterType.INTEGER).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Long").setKey("long").setType(ParameterType.LONG).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Double").setKey("double").setType(ParameterType.DOUBLE).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Boolean").setKey("boolean").setType(ParameterType.BOOLEAN).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Date").setKey("date").setType(ParameterType.DATE).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("Map").setKey("map").setType(ParameterType.MAP).build(), true);
    action.addParameter(new ActionParameterBuilder().setDisplayName("List").setKey("list").setType(ParameterType.LIST).build(), true);
    return action;
}
Also used : ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)

Aggregations

ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)26 Test (org.junit.Test)23 Task (org.motechproject.tasks.domain.mds.task.Task)19 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)18 Channel (org.motechproject.tasks.domain.mds.channel.Channel)14 HashMap (java.util.HashMap)12 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)12 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)12 TreeSet (java.util.TreeSet)11 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)9 ArrayList (java.util.ArrayList)8 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)8 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)8 ObjectTest (org.motechproject.tasks.domain.ObjectTest)7 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)7 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)7 TaskContext (org.motechproject.tasks.service.util.TaskContext)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)6 MotechEvent (org.motechproject.event.MotechEvent)5