Search in sources :

Example 1 with ValidationException

use of org.motechproject.tasks.exception.ValidationException 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 2 with ValidationException

use of org.motechproject.tasks.exception.ValidationException in project motech by motech.

the class TaskServiceImplTest method shouldFailValidationIfTriggerChannelIsNotRegistered.

@Test
public void shouldFailValidationIfTriggerChannelIsNotRegistered() {
    TaskTriggerInformation trigger = new TaskTriggerInformation("triggerDisplay", "triggerChannel", "triggerModule", "1.0", "subject", "triggerListenerSubject");
    TaskActionInformation action = new TaskActionInformation("actionDisplay", "actionChannel", "actionModule", "1.0", "subject");
    Task fooTask = new TaskBuilder().withName("foo").withTrigger(trigger).withTaskConfig(new TaskConfig()).addAction(action).build();
    fooTask.setEnabled(true);
    when(channelService.getChannel("foo-module")).thenReturn(null);
    when(channelService.getChannel("actionModule")).thenReturn(null);
    Set<TaskError> errors = new HashSet<>();
    errors.add(new TaskError("task.validation.error.triggerChannelNotRegistered"));
    when(triggerEventService.validateTrigger(trigger)).thenReturn(errors);
    expectedException.expect(ValidationException.class);
    expectedException.expect(new TypeSafeMatcher<ValidationException>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matchesSafely(ValidationException actualException) {
            final TaskErrorDto triggerChannelError = new TaskErrorDto("task.validation.error.triggerChannelNotRegistered");
            final TaskErrorDto actionChannelError = new TaskErrorDto("task.validation.error.actionChannelNotRegistered");
            Set<TaskErrorDto> taskErrors = actualException.getTaskErrors();
            return taskErrors.contains(triggerChannelError) && taskErrors.contains(actionChannelError);
        }
    });
    taskService.save(fooTask);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) ValidationException(org.motechproject.tasks.exception.ValidationException) Description(org.hamcrest.Description) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) Set(java.util.Set) HashSet(java.util.HashSet) TaskErrorDto(org.motechproject.tasks.dto.TaskErrorDto) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ValidationException

use of org.motechproject.tasks.exception.ValidationException in project motech by motech.

the class TasksBlueprintApplicationContextTracker method addingService.

@Override
public Object addingService(ServiceReference reference) {
    Bundle module = reference.getBundle();
    ApplicationContext applicationContext = (ApplicationContext) super.addingService(reference);
    if (isMDS(module.getSymbolicName())) {
        return applicationContext;
    }
    LOGGER.debug("Staring to process {}", applicationContext.getDisplayName());
    synchronized (getLock()) {
        if (contextInvalidOrProcessed(reference, applicationContext)) {
            return applicationContext;
        }
        markAsProcessed(applicationContext);
    }
    try {
        Resource resource = applicationContext.getResource("classpath:task-channel.json");
        if (resource.exists()) {
            try (InputStream stream = resource.getInputStream()) {
                channelService.registerChannel(stream, module.getSymbolicName(), module.getVersion().toString());
            }
            LOGGER.info("Registered channel for {} successfully.", module.getSymbolicName());
        }
    } catch (IOException | ValidationException e) {
        LOGGER.error(e.getMessage(), e);
    }
    try {
        taskAnnotationBeanPostProcessor.processAnnotations(applicationContext);
    } catch (ValidationException e) {
        LOGGER.error(e.getMessage(), e);
    }
    LOGGER.debug("Processed {}", applicationContext.getDisplayName());
    return applicationContext;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ValidationException(org.motechproject.tasks.exception.ValidationException) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 4 with ValidationException

use of org.motechproject.tasks.exception.ValidationException 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)

Aggregations

ValidationException (org.motechproject.tasks.exception.ValidationException)4 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)3 Transactional (org.springframework.transaction.annotation.Transactional)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Description (org.hamcrest.Description)1 Test (org.junit.Test)1 Channel (org.motechproject.tasks.domain.mds.channel.Channel)1 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)1 Task (org.motechproject.tasks.domain.mds.task.Task)1 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)1 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)1 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)1 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)1 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)1 TaskErrorDto (org.motechproject.tasks.dto.TaskErrorDto)1 Bundle (org.osgi.framework.Bundle)1