Search in sources :

Example 21 with TaskError

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

the class TriggerEventServiceImplTest method shouldReturnChannelNotFoundErrorIfTriggerDoesNotExist.

@Test
public void shouldReturnChannelNotFoundErrorIfTriggerDoesNotExist() throws Exception {
    TaskTriggerInformation triggerInformation = prepareTaskTriggerInformation(prepareTrigger());
    when(channelsDataService.countFindByModuleName(MODULE_NAME)).thenReturn((long) 0);
    when(dynamicChannelLoader.channelExists(MODULE_NAME)).thenReturn(true);
    when(dynamicChannelLoader.validateTrigger(MODULE_NAME, triggerInformation.getSubject())).thenReturn(false);
    Set<TaskError> errors = triggerEventService.validateTrigger(triggerInformation);
    verify(channelsDataService, times(1)).countFindByModuleName(MODULE_NAME);
    verify(triggerEventsDataService, never()).countByChannelModuleNameAndSubject(MODULE_NAME, triggerInformation.getSubject());
    verify(dynamicChannelLoader, times(1)).channelExists(MODULE_NAME);
    verify(dynamicChannelLoader, times(1)).validateTrigger(MODULE_NAME, triggerInformation.getSubject());
    assertEquals(1, errors.size());
    assertEquals("task.validation.error.triggerNotExist", errors.iterator().next().getMessage());
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Test(org.junit.Test)

Example 22 with TaskError

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

the class TriggerEventServiceImplTest method shouldValidateStaticTrigger.

@Test
public void shouldValidateStaticTrigger() throws Exception {
    TaskTriggerInformation triggerInformation = prepareTaskTriggerInformation(prepareTrigger());
    when(channelsDataService.countFindByModuleName(MODULE_NAME)).thenReturn((long) 1992);
    when(triggerEventsDataService.countByChannelModuleNameAndSubject(MODULE_NAME, triggerInformation.getSubject())).thenReturn((long) 1);
    Set<TaskError> errors = triggerEventService.validateTrigger(triggerInformation);
    verify(channelsDataService, times(1)).countFindByModuleName(MODULE_NAME);
    verify(triggerEventsDataService, times(1)).countByChannelModuleNameAndSubject(MODULE_NAME, triggerInformation.getSubject());
    verify(dynamicChannelLoader, never()).channelExists(anyString());
    verify(dynamicChannelLoader, never()).getTrigger(any(TaskTriggerInformation.class));
    assertEquals(0, errors.size());
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Test(org.junit.Test)

Example 23 with TaskError

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

the class TriggerEventServiceImplTest method shouldReturnChannelNotFoundErrorIfChannelDoesNotExist.

@Test
public void shouldReturnChannelNotFoundErrorIfChannelDoesNotExist() throws Exception {
    TaskTriggerInformation triggerInformation = prepareTaskTriggerInformation(prepareTrigger());
    when(channelsDataService.countFindByModuleName(MODULE_NAME)).thenReturn((long) 0);
    when(dynamicChannelLoader.channelExists(MODULE_NAME)).thenReturn(false);
    Set<TaskError> errors = triggerEventService.validateTrigger(triggerInformation);
    verify(channelsDataService, times(1)).countFindByModuleName(MODULE_NAME);
    verify(triggerEventsDataService, never()).countByChannelModuleNameAndSubject(MODULE_NAME, triggerInformation.getTriggerListenerSubject());
    verify(dynamicChannelLoader, times(1)).channelExists(MODULE_NAME);
    verify(dynamicChannelLoader, never()).validateTrigger(MODULE_NAME, triggerInformation.getTriggerListenerSubject());
    assertEquals(1, errors.size());
    assertEquals("task.validation.error.triggerChannelNotRegistered", errors.iterator().next().getMessage());
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Test(org.junit.Test)

Example 24 with TaskError

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

the class ChannelServiceImpl method addOrUpdate.

@Override
@Transactional
public synchronized void addOrUpdate(final Channel channel) {
    Set<TaskError> errors = ChannelValidator.validate(channel);
    if (!isEmpty(errors)) {
        throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors));
    }
    // MOTECH-3049 There is a bug in datanucleus and sometimes ActionParameter is not found in TaskBundleClassLoader,
    // so we temporary change contextClassLoader to TaskBundleClassLoader while updating channel
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader());
        final Channel existingChannel = getChannel(channel.getModuleName());
        if (existingChannel != null && !existingChannel.equals(channel)) {
            LOGGER.debug("Updating channel {}", channel.getDisplayName());
            existingChannel.setActionTaskEvents(channel.getActionTaskEvents());
            existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents());
            existingChannel.setDescription(channel.getDescription());
            existingChannel.setDisplayName(channel.getDisplayName());
            existingChannel.setModuleName(channel.getModuleName());
            existingChannel.setModuleVersion(channel.getModuleVersion());
            channelsDataService.update(existingChannel);
            sendChannelUpdatedEvent(channel);
        } else if (existingChannel == null) {
            LOGGER.debug("Creating channel {}", channel.getDisplayName());
            channelsDataService.create(channel);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName()));
}
Also used : ValidationException(org.motechproject.tasks.exception.ValidationException) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 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) {
    LOGGER.debug("Validating all actions in task: {} with ID: {}", task.getName(), task.getId());
    Set<TaskError> errors = new HashSet<>();
    for (TaskActionInformation action : task.getActions()) {
        Channel channel = channelService.getChannel(action.getModuleName());
        errors.addAll(validateAction(task, channel, action));
    }
    logResultOfValidation("actions", task.getName(), errors);
    return errors;
}
Also used : Channel(org.motechproject.tasks.domain.mds.channel.Channel) HandlerPredicates.tasksWithRegisteredChannel(org.motechproject.tasks.service.util.HandlerPredicates.tasksWithRegisteredChannel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) HashSet(java.util.HashSet)

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