@Test
  public void shouldNotRemoveAnyActivitiesWhenTaskHasNotActivities() {
    when(taskActivitiesDataService.byTask(TASK_ID)).thenReturn(new ArrayList<TaskActivity>());

    activityService.deleteActivitiesForTask(TASK_ID);

    verify(taskActivitiesDataService, never()).delete(any(TaskActivity.class));
  }
  @Test
  public void shouldDeleteAllTaskActivitiesForGivenTask() {
    when(taskActivitiesDataService.byTask(TASK_ID)).thenReturn(activities);

    activityService.deleteActivitiesForTask(TASK_ID);

    verify(taskActivitiesDataService, times(activities.size())).delete(any(TaskActivity.class));
  }
  @Test
  public void shouldReturnPaginatedActivitiesForGivenTask() {
    Set<TaskActivityType> types = new HashSet<>();
    types.addAll(Arrays.asList(TaskActivityType.values()));
    QueryParams queryParams = new QueryParams((Order) null);
    when(taskActivitiesDataService.byTaskAndActivityTypes(TASK_ID, types, queryParams))
        .thenReturn(activities);

    List<TaskActivity> actual = activityService.getTaskActivities(TASK_ID, types, queryParams);

    assertNotNull(actual);
    assertEquals(activities, actual);
  }
  @Test
  public void shouldAddTaskWarningActivityWithGivenKeyAndField() {
    String messageKey = "warning.manipulation";

    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);

    activityService.addWarning(task, messageKey, ERROR_FIELD.get(0));

    verify(taskActivitiesDataService).create(captor.capture());

    assertActivity(
        messageKey, ERROR_FIELD, TASK_ID, TaskActivityType.WARNING, null, null, captor.getValue());
  }
Ejemplo n.º 5
0
  /**
   * 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
   */
  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());
    }
  }
  @Test
  public void shouldAddTaskSuccessActivity() {
    String messageKey = "task.success.ok";

    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);

    activityService.addSuccess(task);

    verify(taskActivitiesDataService).create(captor.capture());

    assertActivity(
        messageKey,
        Collections.<String>emptyList(),
        TASK_ID,
        TaskActivityType.SUCCESS,
        null,
        null,
        captor.getValue());
  }
  @Test
  public void shouldAddTaskWarningActivity() {
    String messageKey = "task.warning.taskDisabled";

    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);

    activityService.addWarning(task);

    verify(taskActivitiesDataService).create(captor.capture());

    assertActivity(
        messageKey,
        Collections.<String>emptyList(),
        TASK_ID,
        TaskActivityType.WARNING,
        null,
        null,
        captor.getValue());
  }
  @Test
  public void shouldAddTaskWarningActivityWithGivenException() {
    TaskHandlerException exception =
        new TaskHandlerException(
            TRIGGER, "trigger.exception", new TaskHandlerException(TRIGGER, "task.exception"));
    String messageKey = "warning.manipulation";

    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);

    activityService.addWarning(task, messageKey, ERROR_FIELD.get(0), exception);

    verify(taskActivitiesDataService).create(captor.capture());

    assertActivity(
        messageKey,
        ERROR_FIELD,
        TASK_ID,
        TaskActivityType.WARNING,
        getStackTrace(exception.getCause()),
        null,
        captor.getValue());
  }
  @Test
  public void shouldAddErrorActivityWithTaskException() {
    String messageKey = "error.notFoundTrigger";
    TaskHandlerException exception =
        new TaskHandlerException(TRIGGER, messageKey, ERROR_FIELD.get(0));
    Map<String, Object> errorParameters = new HashMap<>();
    errorParameters.put("errorKey", "errorValue");

    ArgumentCaptor<TaskActivity> captor = ArgumentCaptor.forClass(TaskActivity.class);

    activityService.addError(task, exception, errorParameters);

    verify(taskActivitiesDataService).create(captor.capture());

    assertActivity(
        messageKey,
        ERROR_FIELD,
        TASK_ID,
        TaskActivityType.ERROR,
        getStackTrace(exception),
        errorParameters,
        captor.getValue());
  }