Search in sources :

Example 26 with ActionEvent

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

the class TaskAnnotationBeanPostProcessorTest method assertChannel.

private void assertChannel(Channel actualChannel) {
    assertNotNull(actualChannel);
    assertEquals(CHANNEL_NAME, actualChannel.getDisplayName());
    assertEquals(MODULE_NAME, actualChannel.getModuleName());
    assertEquals(MODULE_VERSION, actualChannel.getModuleVersion());
    assertNotNull(actualChannel.getActionTaskEvents());
    assertEquals(1, actualChannel.getActionTaskEvents().size());
    ActionEvent actualActionEvent = actualChannel.getActionTaskEvents().get(0);
    assertEquals(ACTION_DISPLAY_NAME, actualActionEvent.getDisplayName());
    assertEquals(TestAction.class.getName(), actualActionEvent.getServiceInterface());
    assertEquals(METHOD_NAME, actualActionEvent.getServiceMethod());
    assertEquals(0, actualActionEvent.getActionParameters().size());
    assertEquals(0, actualActionEvent.getPostActionParameters().size());
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent)

Example 27 with ActionEvent

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

the class TaskActionExecutor method execute.

/**
 * Executes the action for the given task.
 *
 * @param task  the task for which its action should be executed, not null
 * @param actionInformation  the information about the action, not null
 * @param actionIndex the order of the task action
 * @param taskContext  the context of the current task execution, not null
 * @param activityId the ID of the activity associated with this execution
 * @throws TaskHandlerException when the task couldn't be executed
 */
@Transactional
public void execute(Task task, TaskActionInformation actionInformation, Integer actionIndex, TaskContext taskContext, long activityId) throws TaskHandlerException {
    LOGGER.info("Executing task action: {} from task: {}", actionInformation.getName(), task.getName());
    KeyEvaluator keyEvaluator = new KeyEvaluator(taskContext);
    ActionEvent action = getActionEvent(actionInformation);
    Map<String, Object> parameters = createParameters(actionInformation, action, keyEvaluator);
    addTriggerParameters(task, action, parameters, taskContext.getTriggerParameters());
    LOGGER.debug("Parameters created: {} for task action: {}", parameters.toString(), action.getName());
    if (action.hasService() && bundleContext != null) {
        if (callActionServiceMethod(action, actionIndex, parameters, taskContext)) {
            LOGGER.info("Action: {} from task: {} was executed through an OSGi service call", actionInformation.getName(), task.getName());
            postExecutionHandler.handleActionExecuted(taskContext.getTriggerParameters(), taskContext.getMetadata(), activityId);
            return;
        }
        LOGGER.info("There is no service: {}", action.getServiceInterface());
        activityService.addWarning(task, "task.warning.serviceUnavailable", action.getServiceInterface());
    }
    if (!action.hasSubject()) {
        throw new TaskHandlerException(ACTION, "task.error.cantExecuteAction");
    } else {
        eventRelay.sendEventMessage(new MotechEvent(action.getSubject(), parameters, TasksEventCallbackService.TASKS_EVENT_CALLBACK_NAME, taskContext.getMetadata()));
        LOGGER.info("Event: {} was sent", action.getSubject());
    }
}
Also used : TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) MotechEvent(org.motechproject.event.MotechEvent) KeyEvaluator(org.motechproject.tasks.service.util.KeyEvaluator) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent 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 29 with ActionEvent

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

the class MethodHandlerTest method shouldConstructMethodHandlerForMapMethodCall.

@Test
public void shouldConstructMethodHandlerForMapMethodCall() throws TaskHandlerException {
    ActionEvent action = getActionEvent(MethodCallManner.MAP);
    Map<String, Object> parameters = getParameters();
    MethodHandler methodHandler = new MethodHandler(action, parameters);
    Class[] expectedClassArray = { Map.class };
    Object[] expectedObjectArray = { parameters };
    assertArrayEquals(expectedClassArray, methodHandler.getClasses());
    assertArrayEquals(expectedObjectArray, methodHandler.getObjects());
}
Also used : ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Test(org.junit.Test)

Example 30 with ActionEvent

use of org.motechproject.tasks.domain.mds.channel.ActionEvent 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

ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)32 Test (org.junit.Test)21 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)18 Task (org.motechproject.tasks.domain.mds.task.Task)13 TreeSet (java.util.TreeSet)12 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)12 HashMap (java.util.HashMap)10 ObjectTest (org.motechproject.tasks.domain.ObjectTest)9 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)9 Channel (org.motechproject.tasks.domain.mds.channel.Channel)8 TaskContext (org.motechproject.tasks.service.util.TaskContext)8 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)7 MotechEvent (org.motechproject.event.MotechEvent)6 ArrayList (java.util.ArrayList)5 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)5 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)5 ActionParameter (org.motechproject.tasks.domain.mds.channel.ActionParameter)4 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)4 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)4 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)3