Search in sources :

Example 11 with TaskError

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

the class TaskServiceImpl method validateActions.

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

Example 12 with TaskError

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

the class TriggerEventServiceImpl method validateTrigger.

@Override
@Transactional
public Set<TaskError> validateTrigger(TaskTriggerInformation trigger) {
    Set<TaskError> errors = new HashSet<>();
    String subject = trigger.getSubject();
    String moduleName = trigger.getModuleName();
    boolean channelExists = false;
    boolean validTrigger = false;
    if (channelsDataService.countFindByModuleName(moduleName) != 0) {
        channelExists = true;
        validTrigger = triggerEventsDataService.countByChannelModuleNameAndSubject(moduleName, subject) > 0;
    }
    if (!validTrigger && dynamicChannelLoader.channelExists(moduleName)) {
        channelExists = true;
        validTrigger = dynamicChannelLoader.validateTrigger(moduleName, subject);
    }
    if (channelExists) {
        if (!validTrigger) {
            errors.add(new TaskError("task.validation.error.triggerNotExist", trigger.getDisplayName()));
        }
    } else {
        errors.add(new TaskError("task.validation.error.triggerChannelNotRegistered"));
    }
    return errors;
}
Also used : TaskError(org.motechproject.tasks.domain.mds.task.TaskError) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with TaskError

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

the class TaskDataProviderServiceImpl method registerProvider.

@Override
@Transactional
public void registerProvider(final InputStream stream) {
    final Type type = new TypeToken<TaskDataProvider>() {
    }.getType();
    final TaskDataProvider provider = (TaskDataProvider) motechJsonReader.readFromStream(stream, type);
    if (TaskDataProviderValidator.validateIsNotEmpty(provider)) {
        LOGGER.info("Registering a task data provider with name: {}", provider.getName());
        Set<TaskError> errors = TaskDataProviderValidator.validate(provider);
        if (!isEmpty(errors)) {
            throw new ValidationException(TaskDataProviderValidator.TASK_DATA_PROVIDER, TaskError.toDtos(errors));
        }
        addProvider(provider);
    } else {
        LOGGER.info("Registering an empty task data provider with name: {} is not possible", provider.getName());
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Type(java.lang.reflect.Type) ValidationException(org.motechproject.tasks.exception.ValidationException) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with TaskError

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

the class TaskServiceImplTest method shouldRemoveValidationErrorAndUpdateTaskOnlyIfItHasAnyValidationErrors.

@Test
public void shouldRemoveValidationErrorAndUpdateTaskOnlyIfItHasAnyValidationErrors() {
    Task task = new Task("name", trigger, asList(action), new TaskConfig(), true, false);
    Set<TaskError> existingErrors = new HashSet<>();
    existingErrors.add(new TaskError("task.validation.error.triggerNotExist"));
    task.addValidationErrors(existingErrors);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(triggerEventService.triggerExists(task.getTrigger())).thenReturn(true);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(trigger));
    Task actualTask = verifyCreateAndCaptureTask();
    assertFalse(actualTask.hasValidationErrors());
}
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) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) QueryExecution(org.motechproject.mds.query.QueryExecution) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with TaskError

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

the class TaskServiceImplTest method shouldValidateTasksOfDependentModulesAfterChannelUpdateForInvalidTriggers.

@Test
public void shouldValidateTasksOfDependentModulesAfterChannelUpdateForInvalidTriggers() {
    Task task = new Task("name", trigger, new ArrayList<>(asList(action)), new TaskConfig(), true, false);
    task.setId(6l);
    when(tasksDataService.executeQuery(any(QueryExecution.class))).thenReturn(asList(task));
    when(tasksDataService.findById(6l)).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);
    Set<TaskError> triggerValidationErrors = new HashSet<>();
    triggerValidationErrors.add(new TaskError("task.validation.error.triggerNotExist", trigger.getDisplayName()));
    when(triggerEventService.validateTrigger(eq(trigger))).thenReturn(triggerValidationErrors);
    taskService.validateTasksAfterChannelUpdate(getChannelUpdateEvent(trigger));
    Task actualTask = verifyUpdateAndCaptureTask(times(2));
    assertFalse(actualTask.isEnabled());
    List<Object> errors = new ArrayList<Object>(actualTask.getValidationErrors());
    assertEquals(1, errors.size());
    assertThat(errors, hasItem(hasProperty("message", equalTo("task.validation.error.triggerNotExist"))));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) QueryExecution(org.motechproject.mds.query.QueryExecution) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TaskError (org.motechproject.tasks.domain.mds.task.TaskError)36 HashSet (java.util.HashSet)25 Test (org.junit.Test)10 Channel (org.motechproject.tasks.domain.mds.channel.Channel)8 Task (org.motechproject.tasks.domain.mds.task.Task)8 Transactional (org.springframework.transaction.annotation.Transactional)8 HashMap (java.util.HashMap)7 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)7 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)7 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)6 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)6 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)5 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)5 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)4 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)4 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)4 Map (java.util.Map)3 QueryExecution (org.motechproject.mds.query.QueryExecution)3 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)3 ValidationException (org.motechproject.tasks.exception.ValidationException)3